Search in sources :

Example 1 with Joint

use of org.jbox2d.dynamics.joints.Joint in project opennars by opennars.

the class DrawPhy2D method draw.

public void draw(World w) {
    graphics = panel.getDBGraphics();
    for (LayerDraw l : layers) l.drawGround(this, w);
    int flags = getFlags();
    if ((flags & DebugDraw.e_shapeBit) != 0) {
        for (Body b = w.getBodyList(); b != null; b = b.getNext()) {
            drawBody(b);
        }
    // drawParticleSystem(m_particleSystem);
    }
    if ((flags & DebugDraw.e_jointBit) != 0) {
        for (Joint j = w.getJointList(); j != null; j = j.getNext()) {
            drawJoint(j);
        }
    }
    for (LayerDraw l : layers) l.drawSky(this, w);
    flush();
}
Also used : Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint) Body(org.jbox2d.dynamics.Body) Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint)

Example 2 with Joint

use of org.jbox2d.dynamics.joints.Joint in project libgdx by libgdx.

the class WorldRayCastWrapper method createJoint.

/**
   * create a joint to constrain bodies together. No reference to the definition is retained. This
   * may cause the connected bodies to cease colliding.
   * 
   * @warning This function is locked during callbacks.
   * @param def
   * @return
   */
public Joint createJoint(JointDef def) {
    assert (isLocked() == false);
    if (isLocked()) {
        return null;
    }
    Joint j = Joint.create(this, def);
    // Connect to the world list.
    j.m_prev = null;
    j.m_next = m_jointList;
    if (m_jointList != null) {
        m_jointList.m_prev = j;
    }
    m_jointList = j;
    ++m_jointCount;
    // Connect to the bodies' doubly linked lists.
    j.m_edgeA.joint = j;
    j.m_edgeA.other = j.getBodyB();
    j.m_edgeA.prev = null;
    j.m_edgeA.next = j.getBodyA().m_jointList;
    if (j.getBodyA().m_jointList != null) {
        j.getBodyA().m_jointList.prev = j.m_edgeA;
    }
    j.getBodyA().m_jointList = j.m_edgeA;
    j.m_edgeB.joint = j;
    j.m_edgeB.other = j.getBodyA();
    j.m_edgeB.prev = null;
    j.m_edgeB.next = j.getBodyB().m_jointList;
    if (j.getBodyB().m_jointList != null) {
        j.getBodyB().m_jointList.prev = j.m_edgeB;
    }
    j.getBodyB().m_jointList = j.m_edgeB;
    Body bodyA = def.bodyA;
    Body bodyB = def.bodyB;
    // If the joint prevents collisions, then flag any contacts for filtering.
    if (def.collideConnected == false) {
        ContactEdge edge = bodyB.getContactList();
        while (edge != null) {
            if (edge.other == bodyA) {
                // Flag the contact for filtering at the next time step (where either
                // body is awake).
                edge.contact.flagForFiltering();
            }
            edge = edge.next;
        }
    }
    return j;
}
Also used : Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint) ContactEdge(org.jbox2d.dynamics.contacts.ContactEdge)

Example 3 with Joint

use of org.jbox2d.dynamics.joints.Joint in project libgdx by libgdx.

the class WorldRayCastWrapper method solve.

private void solve(TimeStep step) {
    m_profile.solveInit.startAccum();
    m_profile.solveVelocity.startAccum();
    m_profile.solvePosition.startAccum();
    // update previous transforms
    for (Body b = m_bodyList; b != null; b = b.m_next) {
        b.m_xf0.set(b.m_xf);
    }
    // Size the island for the worst case.
    island.init(m_bodyCount, m_contactManager.m_contactCount, m_jointCount, m_contactManager.m_contactListener);
    // Clear all the island flags.
    for (Body b = m_bodyList; b != null; b = b.m_next) {
        b.m_flags &= ~Body.e_islandFlag;
    }
    for (Contact c = m_contactManager.m_contactList; c != null; c = c.m_next) {
        c.m_flags &= ~Contact.ISLAND_FLAG;
    }
    for (Joint j = m_jointList; j != null; j = j.m_next) {
        j.m_islandFlag = false;
    }
    // Build and simulate all awake islands.
    int stackSize = m_bodyCount;
    if (stack.length < stackSize) {
        stack = new Body[stackSize];
    }
    for (Body seed = m_bodyList; seed != null; seed = seed.m_next) {
        if ((seed.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
            continue;
        }
        if (seed.isAwake() == false || seed.isActive() == false) {
            continue;
        }
        // The seed can be dynamic or kinematic.
        if (seed.getType() == BodyType.STATIC) {
            continue;
        }
        // Reset island and stack.
        island.clear();
        int stackCount = 0;
        stack[stackCount++] = seed;
        seed.m_flags |= Body.e_islandFlag;
        // Perform a depth first search (DFS) on the constraint graph.
        while (stackCount > 0) {
            // Grab the next body off the stack and add it to the island.
            Body b = stack[--stackCount];
            assert (b.isActive() == true);
            island.add(b);
            // Make sure the body is awake.
            b.setAwake(true);
            // propagate islands across static bodies.
            if (b.getType() == BodyType.STATIC) {
                continue;
            }
            // Search all contacts connected to this body.
            for (ContactEdge ce = b.m_contactList; ce != null; ce = ce.next) {
                Contact contact = ce.contact;
                // Has this contact already been added to an island?
                if ((contact.m_flags & Contact.ISLAND_FLAG) == Contact.ISLAND_FLAG) {
                    continue;
                }
                // Is this contact solid and touching?
                if (contact.isEnabled() == false || contact.isTouching() == false) {
                    continue;
                }
                // Skip sensors.
                boolean sensorA = contact.m_fixtureA.m_isSensor;
                boolean sensorB = contact.m_fixtureB.m_isSensor;
                if (sensorA || sensorB) {
                    continue;
                }
                island.add(contact);
                contact.m_flags |= Contact.ISLAND_FLAG;
                Body other = ce.other;
                // Was the other body already added to this island?
                if ((other.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
                    continue;
                }
                assert (stackCount < stackSize);
                stack[stackCount++] = other;
                other.m_flags |= Body.e_islandFlag;
            }
            // Search all joints connect to this body.
            for (JointEdge je = b.m_jointList; je != null; je = je.next) {
                if (je.joint.m_islandFlag == true) {
                    continue;
                }
                Body other = je.other;
                // Don't simulate joints connected to inactive bodies.
                if (other.isActive() == false) {
                    continue;
                }
                island.add(je.joint);
                je.joint.m_islandFlag = true;
                if ((other.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
                    continue;
                }
                assert (stackCount < stackSize);
                stack[stackCount++] = other;
                other.m_flags |= Body.e_islandFlag;
            }
        }
        island.solve(m_profile, step, m_gravity, m_allowSleep);
        // Post solve cleanup.
        for (int i = 0; i < island.m_bodyCount; ++i) {
            // Allow static bodies to participate in other islands.
            Body b = island.m_bodies[i];
            if (b.getType() == BodyType.STATIC) {
                b.m_flags &= ~Body.e_islandFlag;
            }
        }
    }
    m_profile.solveInit.endAccum();
    m_profile.solveVelocity.endAccum();
    m_profile.solvePosition.endAccum();
    broadphaseTimer.reset();
    // Synchronize fixtures, check for out of range bodies.
    for (Body b = m_bodyList; b != null; b = b.getNext()) {
        // If a body was not in an island then it did not move.
        if ((b.m_flags & Body.e_islandFlag) == 0) {
            continue;
        }
        if (b.getType() == BodyType.STATIC) {
            continue;
        }
        // Update fixtures (for broad-phase).
        b.synchronizeFixtures();
    }
    // Look for new contacts.
    m_contactManager.findNewContacts();
    m_profile.broadphase.record(broadphaseTimer.getMilliseconds());
}
Also used : JointEdge(org.jbox2d.dynamics.joints.JointEdge) Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint) Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint) ParticleContact(org.jbox2d.particle.ParticleContact) Contact(org.jbox2d.dynamics.contacts.Contact) ParticleBodyContact(org.jbox2d.particle.ParticleBodyContact) ContactEdge(org.jbox2d.dynamics.contacts.ContactEdge)

Example 4 with Joint

use of org.jbox2d.dynamics.joints.Joint in project libgdx by libgdx.

the class WorldRayCastWrapper method drawDebugData.

/**
   * Call this to draw shapes and other debug draw data.
   */
public void drawDebugData() {
    if (m_debugDraw == null) {
        return;
    }
    int flags = m_debugDraw.getFlags();
    boolean wireframe = (flags & DebugDraw.e_wireframeDrawingBit) != 0;
    if ((flags & DebugDraw.e_shapeBit) != 0) {
        for (Body b = m_bodyList; b != null; b = b.getNext()) {
            xf.set(b.getTransform());
            for (Fixture f = b.getFixtureList(); f != null; f = f.getNext()) {
                if (b.isActive() == false) {
                    color.set(0.5f, 0.5f, 0.3f);
                    drawShape(f, xf, color, wireframe);
                } else if (b.getType() == BodyType.STATIC) {
                    color.set(0.5f, 0.9f, 0.3f);
                    drawShape(f, xf, color, wireframe);
                } else if (b.getType() == BodyType.KINEMATIC) {
                    color.set(0.5f, 0.5f, 0.9f);
                    drawShape(f, xf, color, wireframe);
                } else if (b.isAwake() == false) {
                    color.set(0.5f, 0.5f, 0.5f);
                    drawShape(f, xf, color, wireframe);
                } else {
                    color.set(0.9f, 0.7f, 0.7f);
                    drawShape(f, xf, color, wireframe);
                }
            }
        }
        drawParticleSystem(m_particleSystem);
    }
    if ((flags & DebugDraw.e_jointBit) != 0) {
        for (Joint j = m_jointList; j != null; j = j.getNext()) {
            drawJoint(j);
        }
    }
    if ((flags & DebugDraw.e_pairBit) != 0) {
        color.set(0.3f, 0.9f, 0.9f);
        for (Contact c = m_contactManager.m_contactList; c != null; c = c.getNext()) {
            Fixture fixtureA = c.getFixtureA();
            Fixture fixtureB = c.getFixtureB();
            fixtureA.getAABB(c.getChildIndexA()).getCenterToOut(cA);
            fixtureB.getAABB(c.getChildIndexB()).getCenterToOut(cB);
            m_debugDraw.drawSegment(cA, cB, color);
        }
    }
    if ((flags & DebugDraw.e_aabbBit) != 0) {
        color.set(0.9f, 0.3f, 0.9f);
        for (Body b = m_bodyList; b != null; b = b.getNext()) {
            if (b.isActive() == false) {
                continue;
            }
            for (Fixture f = b.getFixtureList(); f != null; f = f.getNext()) {
                for (int i = 0; i < f.m_proxyCount; ++i) {
                    FixtureProxy proxy = f.m_proxies[i];
                    AABB aabb = m_contactManager.m_broadPhase.getFatAABB(proxy.proxyId);
                    if (aabb != null) {
                        Vec2[] vs = avs.get(4);
                        vs[0].set(aabb.lowerBound.x, aabb.lowerBound.y);
                        vs[1].set(aabb.upperBound.x, aabb.lowerBound.y);
                        vs[2].set(aabb.upperBound.x, aabb.upperBound.y);
                        vs[3].set(aabb.lowerBound.x, aabb.upperBound.y);
                        m_debugDraw.drawPolygon(vs, 4, color);
                    }
                }
            }
        }
    }
    if ((flags & DebugDraw.e_centerOfMassBit) != 0) {
        for (Body b = m_bodyList; b != null; b = b.getNext()) {
            xf.set(b.getTransform());
            xf.p.set(b.getWorldCenter());
            m_debugDraw.drawTransform(xf);
        }
    }
    if ((flags & DebugDraw.e_dynamicTreeBit) != 0) {
        m_contactManager.m_broadPhase.drawTree(m_debugDraw);
    }
    m_debugDraw.flush();
}
Also used : Vec2(org.jbox2d.common.Vec2) Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint) Joint(org.jbox2d.dynamics.joints.Joint) PulleyJoint(org.jbox2d.dynamics.joints.PulleyJoint) AABB(org.jbox2d.collision.AABB) ParticleContact(org.jbox2d.particle.ParticleContact) Contact(org.jbox2d.dynamics.contacts.Contact) ParticleBodyContact(org.jbox2d.particle.ParticleBodyContact)

Example 5 with Joint

use of org.jbox2d.dynamics.joints.Joint in project libgdx by libgdx.

the class Island method init.

public void init(int bodyCapacity, int contactCapacity, int jointCapacity, ContactListener listener) {
    // System.out.println("Initializing Island");
    m_bodyCapacity = bodyCapacity;
    m_contactCapacity = contactCapacity;
    m_jointCapacity = jointCapacity;
    m_bodyCount = 0;
    m_contactCount = 0;
    m_jointCount = 0;
    m_listener = listener;
    if (m_bodies == null || m_bodyCapacity > m_bodies.length) {
        m_bodies = new Body[m_bodyCapacity];
    }
    if (m_joints == null || m_jointCapacity > m_joints.length) {
        m_joints = new Joint[m_jointCapacity];
    }
    if (m_contacts == null || m_contactCapacity > m_contacts.length) {
        m_contacts = new Contact[m_contactCapacity];
    }
    // dynamic array
    if (m_velocities == null || m_bodyCapacity > m_velocities.length) {
        final Velocity[] old = m_velocities == null ? new Velocity[0] : m_velocities;
        m_velocities = new Velocity[m_bodyCapacity];
        System.arraycopy(old, 0, m_velocities, 0, old.length);
        for (int i = old.length; i < m_velocities.length; i++) {
            m_velocities[i] = new Velocity();
        }
    }
    // dynamic array
    if (m_positions == null || m_bodyCapacity > m_positions.length) {
        final Position[] old = m_positions == null ? new Position[0] : m_positions;
        m_positions = new Position[m_bodyCapacity];
        System.arraycopy(old, 0, m_positions, 0, old.length);
        for (int i = old.length; i < m_positions.length; i++) {
            m_positions[i] = new Position();
        }
    }
}
Also used : Position(org.jbox2d.dynamics.contacts.Position) Velocity(org.jbox2d.dynamics.contacts.Velocity) ContactVelocityConstraint(org.jbox2d.dynamics.contacts.ContactVelocityConstraint) Joint(org.jbox2d.dynamics.joints.Joint)

Aggregations

Joint (org.jbox2d.dynamics.joints.Joint)5 PulleyJoint (org.jbox2d.dynamics.joints.PulleyJoint)4 Contact (org.jbox2d.dynamics.contacts.Contact)2 ContactEdge (org.jbox2d.dynamics.contacts.ContactEdge)2 ParticleBodyContact (org.jbox2d.particle.ParticleBodyContact)2 ParticleContact (org.jbox2d.particle.ParticleContact)2 AABB (org.jbox2d.collision.AABB)1 Vec2 (org.jbox2d.common.Vec2)1 Body (org.jbox2d.dynamics.Body)1 ContactVelocityConstraint (org.jbox2d.dynamics.contacts.ContactVelocityConstraint)1 Position (org.jbox2d.dynamics.contacts.Position)1 Velocity (org.jbox2d.dynamics.contacts.Velocity)1 JointEdge (org.jbox2d.dynamics.joints.JointEdge)1