Search in sources :

Example 1 with Contact

use of com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact in project FXGL by AlmasB.

the class Fixture method refilter.

/**
 * Call this if you want to establish collision that was previously disabled by
 * ContactFilter::ShouldCollide.
 */
public void refilter() {
    // Flag associated contacts for filtering.
    ContactEdge edge = body.getContactList();
    while (edge != null) {
        Contact contact = edge.contact;
        Fixture fixtureA = contact.getFixtureA();
        Fixture fixtureB = contact.getFixtureB();
        if (fixtureA == this || fixtureB == this) {
            contact.flagForFiltering();
        }
        edge = edge.next;
    }
    World world = body.getWorld();
    if (world == null) {
        return;
    }
    // Touch each proxy so that new pairs may be created
    BroadPhase broadPhase = world.getContactManager().broadPhase;
    for (int i = 0; i < proxyCount; ++i) {
        broadPhase.touchProxy(proxies[i].proxyId);
    }
}
Also used : BroadPhase(com.almasb.fxgl.physics.box2d.collision.broadphase.BroadPhase) ContactEdge(com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactEdge) Contact(com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact)

Example 2 with Contact

use of com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact 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 3 with Contact

use of com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact in project FXGL by AlmasB.

the class ContactManager method pushContact.

private void pushContact(Contact contact) {
    Fixture fixtureA = contact.getFixtureA();
    Fixture fixtureB = contact.getFixtureB();
    if (contact.m_manifold.pointCount > 0 && !fixtureA.isSensor() && !fixtureB.isSensor()) {
        fixtureA.getBody().setAwake(true);
        fixtureB.getBody().setAwake(true);
    }
    ShapeType type1 = fixtureA.getType();
    ShapeType type2 = fixtureB.getType();
    IDynamicStack<Contact> creator = contactStacks[type1.ordinal()][type2.ordinal()].creator;
    creator.push(contact);
}
Also used : ShapeType(com.almasb.fxgl.physics.box2d.collision.shapes.ShapeType) Contact(com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact)

Example 4 with Contact

use of com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact in project FXGL by AlmasB.

the class ContactManager method collide.

/**
 * This is the top level collision call for the time step. Here all the narrow phase collision is
 * processed for the world contact list.
 */
void collide() {
    // Update awake contacts.
    Contact c = contactList;
    while (c != null) {
        Fixture fixtureA = c.getFixtureA();
        Fixture fixtureB = c.getFixtureB();
        int indexA = c.getChildIndexA();
        int indexB = c.getChildIndexB();
        Body bodyA = fixtureA.getBody();
        Body bodyB = fixtureB.getBody();
        // is this contact flagged for filtering?
        if ((c.m_flags & Contact.FILTER_FLAG) == Contact.FILTER_FLAG) {
            // Should these bodies collide?
            if (!bodyB.shouldCollide(bodyA)) {
                Contact cNuke = c;
                c = cNuke.getNext();
                destroy(cNuke);
                continue;
            }
            // Check user filtering.
            if (contactFilter != null && !contactFilter.shouldCollide(fixtureA, fixtureB)) {
                Contact cNuke = c;
                c = cNuke.getNext();
                destroy(cNuke);
                continue;
            }
            // Clear the filtering flag.
            c.m_flags &= ~Contact.FILTER_FLAG;
        }
        boolean activeA = bodyA.isAwake() && bodyA.getType() != BodyType.STATIC;
        boolean activeB = bodyB.isAwake() && bodyB.getType() != BodyType.STATIC;
        // At least one body must be awake and it must be dynamic or kinematic.
        if (!activeA && !activeB) {
            c = c.getNext();
            continue;
        }
        int proxyIdA = fixtureA.getProxyId(indexA);
        int proxyIdB = fixtureB.getProxyId(indexB);
        boolean overlap = broadPhase.testOverlap(proxyIdA, proxyIdB);
        // Here we destroy contacts that cease to overlap in the broad-phase.
        if (!overlap) {
            Contact cNuke = c;
            c = cNuke.getNext();
            destroy(cNuke);
            continue;
        }
        // The contact persists.
        c.update(contactListener);
        c = c.getNext();
    }
}
Also used : Contact(com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact)

Example 5 with Contact

use of com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact in project FXGL by AlmasB.

the class World method solveTOI.

private void solveTOI(final TimeStep step) {
    final Island island = toiIsland;
    island.init(2 * JBoxSettings.maxTOIContacts, JBoxSettings.maxTOIContacts, 0, contactManager.getContactListener());
    if (stepComplete) {
        for (Body b : bodies) {
            b.m_flags &= ~Body.e_islandFlag;
            b.m_sweep.alpha0 = 0.0f;
        }
        for (Contact c = contactManager.contactList; c != null; c = c.m_next) {
            // Invalidate TOI
            c.m_flags &= ~(Contact.TOI_FLAG | Contact.ISLAND_FLAG);
            c.m_toiCount = 0;
            c.m_toi = 1.0f;
        }
    }
    // Find TOI events and solve them.
    for (; ; ) {
        // Find the first TOI.
        Contact minContact = null;
        float minAlpha = 1.0f;
        for (Contact c = contactManager.contactList; c != null; c = c.m_next) {
            // Is this contact disabled?
            if (!c.isEnabled()) {
                continue;
            }
            // Prevent excessive sub-stepping.
            if (c.m_toiCount > JBoxSettings.maxSubSteps) {
                continue;
            }
            float alpha = 1.0f;
            if ((c.m_flags & Contact.TOI_FLAG) != 0) {
                // This contact has a valid cached TOI.
                alpha = c.m_toi;
            } else {
                Fixture fA = c.getFixtureA();
                Fixture fB = c.getFixtureB();
                // Is there a sensor?
                if (fA.isSensor() || fB.isSensor()) {
                    continue;
                }
                Body bA = fA.getBody();
                Body bB = fB.getBody();
                BodyType typeA = bA.getType();
                BodyType typeB = bB.getType();
                assert (typeA == BodyType.DYNAMIC || typeB == BodyType.DYNAMIC);
                boolean activeA = bA.isAwake() && typeA != BodyType.STATIC;
                boolean activeB = bB.isAwake() && typeB != BodyType.STATIC;
                // Is at least one body active (awake and dynamic or kinematic)?
                if (!activeA && !activeB) {
                    continue;
                }
                boolean collideA = bA.isBullet() || typeA != BodyType.DYNAMIC;
                boolean collideB = bB.isBullet() || typeB != BodyType.DYNAMIC;
                // Are these two non-bullet dynamic bodies?
                if (!collideA && !collideB) {
                    continue;
                }
                // Compute the TOI for this contact.
                // Put the sweeps onto the same time interval.
                float alpha0 = bA.m_sweep.alpha0;
                if (bA.m_sweep.alpha0 < bB.m_sweep.alpha0) {
                    alpha0 = bB.m_sweep.alpha0;
                    bA.m_sweep.advance(alpha0);
                } else if (bB.m_sweep.alpha0 < bA.m_sweep.alpha0) {
                    alpha0 = bA.m_sweep.alpha0;
                    bB.m_sweep.advance(alpha0);
                }
                assert (alpha0 < 1.0f);
                int indexA = c.getChildIndexA();
                int indexB = c.getChildIndexB();
                // Compute the time of impact in interval [0, minTOI]
                final TOIInput input = toiInput;
                input.proxyA.set(fA.getShape(), indexA);
                input.proxyB.set(fB.getShape(), indexB);
                input.sweepA.set(bA.m_sweep);
                input.sweepB.set(bB.m_sweep);
                input.tMax = 1.0f;
                pool.getTimeOfImpact().timeOfImpact(toiOutput, input);
                // Beta is the fraction of the remaining portion of the .
                float beta = toiOutput.t;
                if (toiOutput.state == TOIOutputState.TOUCHING) {
                    alpha = JBoxUtils.min(alpha0 + (1.0f - alpha0) * beta, 1.0f);
                } else {
                    alpha = 1.0f;
                }
                c.m_toi = alpha;
                c.m_flags |= Contact.TOI_FLAG;
            }
            if (alpha < minAlpha) {
                // This is the minimum TOI found so far.
                minContact = c;
                minAlpha = alpha;
            }
        }
        if (minContact == null || 1.0f - 10.0f * JBoxSettings.EPSILON < minAlpha) {
            // No more TOI events. Done!
            stepComplete = true;
            break;
        }
        // Advance the bodies to the TOI.
        Fixture fA = minContact.getFixtureA();
        Fixture fB = minContact.getFixtureB();
        Body bA = fA.getBody();
        Body bB = fB.getBody();
        backup1.set(bA.m_sweep);
        backup2.set(bB.m_sweep);
        bA.advance(minAlpha);
        bB.advance(minAlpha);
        // The TOI contact likely has some new contact points.
        minContact.update(contactManager.getContactListener());
        minContact.m_flags &= ~Contact.TOI_FLAG;
        ++minContact.m_toiCount;
        // Is the contact solid?
        if (!minContact.isEnabled() || !minContact.isTouching()) {
            // Restore the sweeps.
            minContact.setEnabled(false);
            bA.m_sweep.set(backup1);
            bB.m_sweep.set(backup2);
            bA.synchronizeTransform();
            bB.synchronizeTransform();
            continue;
        }
        bA.setAwake(true);
        bB.setAwake(true);
        // Build the island
        island.clear();
        island.add(bA);
        island.add(bB);
        island.add(minContact);
        bA.m_flags |= Body.e_islandFlag;
        bB.m_flags |= Body.e_islandFlag;
        minContact.m_flags |= Contact.ISLAND_FLAG;
        // Get contacts on bodyA and bodyB.
        tempBodies[0] = bA;
        tempBodies[1] = bB;
        for (int i = 0; i < 2; ++i) {
            Body body = tempBodies[i];
            if (body.getType() == BodyType.DYNAMIC) {
                for (ContactEdge ce = body.m_contactList; ce != null; ce = ce.next) {
                    if (island.m_bodyCount == island.m_bodyCapacity) {
                        break;
                    }
                    if (island.m_contactCount == island.m_contactCapacity) {
                        break;
                    }
                    Contact contact = ce.contact;
                    // Has this contact already been added to the island?
                    if ((contact.m_flags & Contact.ISLAND_FLAG) != 0) {
                        continue;
                    }
                    // Only add static, kinematic, or bullet bodies.
                    Body other = ce.other;
                    if (other.getType() == BodyType.DYNAMIC && !body.isBullet() && !other.isBullet()) {
                        continue;
                    }
                    // Skip sensors.
                    boolean sensorA = contact.m_fixtureA.isSensor();
                    boolean sensorB = contact.m_fixtureB.isSensor();
                    if (sensorA || sensorB) {
                        continue;
                    }
                    // Tentatively advance the body to the TOI.
                    backup1.set(other.m_sweep);
                    if ((other.m_flags & Body.e_islandFlag) == 0) {
                        other.advance(minAlpha);
                    }
                    // Update the contact points
                    contact.update(contactManager.getContactListener());
                    // Was the contact disabled by the user?
                    if (!contact.isEnabled()) {
                        other.m_sweep.set(backup1);
                        other.synchronizeTransform();
                        continue;
                    }
                    // Are there contact points?
                    if (!contact.isTouching()) {
                        other.m_sweep.set(backup1);
                        other.synchronizeTransform();
                        continue;
                    }
                    // Add the contact to the island
                    contact.m_flags |= Contact.ISLAND_FLAG;
                    island.add(contact);
                    // Has the other body already been added to the island?
                    if ((other.m_flags & Body.e_islandFlag) != 0) {
                        continue;
                    }
                    // Add the other body to the island.
                    other.m_flags |= Body.e_islandFlag;
                    if (other.getType() != BodyType.STATIC) {
                        other.setAwake(true);
                    }
                    island.add(other);
                }
            }
        }
        subStep.dt = (1.0f - minAlpha) * step.dt;
        subStep.inv_dt = 1.0f / subStep.dt;
        subStep.dtRatio = 1.0f;
        subStep.positionIterations = 20;
        subStep.velocityIterations = step.velocityIterations;
        subStep.warmStarting = false;
        island.solveTOI(subStep, bA.m_islandIndex, bB.m_islandIndex);
        // Reset island flags and synchronize broad-phase proxies.
        for (int i = 0; i < island.m_bodyCount; ++i) {
            Body body = island.m_bodies[i];
            body.m_flags &= ~Body.e_islandFlag;
            if (body.getType() != BodyType.DYNAMIC) {
                continue;
            }
            body.synchronizeFixtures();
            // Invalidate all contact TOIs on this displaced body.
            for (ContactEdge ce = body.m_contactList; ce != null; ce = ce.next) {
                ce.contact.m_flags &= ~(Contact.TOI_FLAG | Contact.ISLAND_FLAG);
            }
        }
        // Commit fixture proxy movements to the broad-phase so that new contacts are created.
        // Also, some contacts can be destroyed.
        contactManager.findNewContacts();
        if (subStepping) {
            stepComplete = false;
            break;
        }
    }
}
Also used : TOIInput(com.almasb.fxgl.physics.box2d.collision.TimeOfImpact.TOIInput) 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)

Aggregations

Contact (com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact)8 ContactEdge (com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactEdge)5 BroadPhase (com.almasb.fxgl.physics.box2d.collision.broadphase.BroadPhase)2 ShapeType (com.almasb.fxgl.physics.box2d.collision.shapes.ShapeType)2 Joint (com.almasb.fxgl.physics.box2d.dynamics.joints.Joint)2 TOIInput (com.almasb.fxgl.physics.box2d.collision.TimeOfImpact.TOIInput)1 ContactRegister (com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactRegister)1 JointEdge (com.almasb.fxgl.physics.box2d.dynamics.joints.JointEdge)1