Search in sources :

Example 1 with Joint

use of com.almasb.fxgl.physics.box2d.dynamics.joints.Joint in project FXGL by AlmasB.

the class World method solve.

private void solve(TimeStep step) {
    // update previous transforms
    for (Body b : bodies) {
        b.m_xf0.set(b.m_xf);
    }
    // Size the island for the worst case.
    island.init(getBodyCount(), contactManager.contactCount, jointCount, contactManager.getContactListener());
    // Clear all the island flags.
    for (Body b : bodies) {
        b.m_flags &= ~Body.e_islandFlag;
    }
    for (Contact c = contactManager.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 = getBodyCount();
    if (stack.length < stackSize) {
        stack = new Body[stackSize];
    }
    for (Body seed : bodies) {
        if ((seed.m_flags & Body.e_islandFlag) == Body.e_islandFlag) {
            continue;
        }
        if (!seed.isAwake() || !seed.isActive()) {
            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());
            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() || !contact.isTouching()) {
                    continue;
                }
                // Skip sensors.
                boolean sensorA = contact.m_fixtureA.isSensor();
                boolean sensorB = contact.m_fixtureB.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) {
                    continue;
                }
                Body other = je.other;
                // Don't simulate joints connected to inactive bodies.
                if (!other.isActive()) {
                    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(step, gravity, 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;
            }
        }
    }
    // Synchronize fixtures, check for out of range bodies.
    for (Body b : bodies) {
        // 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.
    contactManager.findNewContacts();
}
Also used : JointEdge(com.almasb.fxgl.physics.box2d.dynamics.joints.JointEdge) Joint(com.almasb.fxgl.physics.box2d.dynamics.joints.Joint) Joint(com.almasb.fxgl.physics.box2d.dynamics.joints.Joint) Contact(com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact) ContactEdge(com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactEdge)

Example 2 with Joint

use of com.almasb.fxgl.physics.box2d.dynamics.joints.Joint in project FXGL by AlmasB.

the class Island method init.

public void init(int bodyCapacity, int contactCapacity, int jointCapacity, ContactListener listener) {
    m_bodyCapacity = bodyCapacity;
    m_contactCapacity = contactCapacity;
    m_jointCapacity = jointCapacity;
    m_bodyCount = 0;
    m_contactCount = 0;
    m_jointCount = 0;
    this.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 : Joint(com.almasb.fxgl.physics.box2d.dynamics.joints.Joint)

Example 3 with Joint

use of com.almasb.fxgl.physics.box2d.dynamics.joints.Joint in project FXGL by AlmasB.

the class World 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.
 * This function is locked during callbacks.
 * Note: creating a joint doesn't wake the bodies.
 *
 * @param def joint definition
 * @return joint
 */
public Joint createJoint(JointDef def) {
    assertNotLocked();
    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;
    ++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) {
        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(com.almasb.fxgl.physics.box2d.dynamics.joints.Joint) ContactEdge(com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactEdge)

Aggregations

Joint (com.almasb.fxgl.physics.box2d.dynamics.joints.Joint)3 ContactEdge (com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactEdge)2 Contact (com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact)1 JointEdge (com.almasb.fxgl.physics.box2d.dynamics.joints.JointEdge)1