Search in sources :

Example 6 with Contact

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

the class Body method destroyFixture.

/**
 * Destroy a fixture.
 * This removes the fixture from the broad-phase and destroys all contacts
 * associated with this fixture.
 * This will automatically adjust the mass of the body if the body
 * is dynamic and the fixture has positive density.
 * All fixtures attached to a body are implicitly destroyed when the body is destroyed.
 * Note: this function is locked during callbacks.
 *
 * @param fixture the fixture to be removed
 */
public void destroyFixture(Fixture fixture) {
    world.assertNotLocked();
    assert (fixture.getBody() == this);
    assert (fixtures.size() > 0);
    assert fixtures.contains(fixture);
    fixtures.remove(fixture);
    // Destroy any contacts associated with the fixture.
    ContactEdge edge = m_contactList;
    while (edge != null) {
        Contact c = edge.contact;
        edge = edge.next;
        Fixture fixtureA = c.getFixtureA();
        Fixture fixtureB = c.getFixtureB();
        if (fixture == fixtureA || fixture == fixtureB) {
            // This destroys the contact and removes it from this body's contact list.
            world.getContactManager().destroy(c);
        }
    }
    if ((m_flags & e_activeFlag) == e_activeFlag) {
        BroadPhase broadPhase = world.getContactManager().broadPhase;
        fixture.destroyProxies(broadPhase);
    }
    fixture.destroy();
    resetMassData();
}
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 7 with Contact

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

the class ContactManager method popContact.

private Contact popContact(Fixture fixtureA, int indexA, Fixture fixtureB, int indexB) {
    final ShapeType type1 = fixtureA.getType();
    final ShapeType type2 = fixtureB.getType();
    final ContactRegister reg = contactStacks[type1.ordinal()][type2.ordinal()];
    if (reg == null)
        return null;
    Contact c = reg.creator.pop();
    if (reg.primary) {
        c.init(fixtureA, indexA, fixtureB, indexB);
    } else {
        c.init(fixtureB, indexB, fixtureA, indexA);
    }
    return c;
}
Also used : ShapeType(com.almasb.fxgl.physics.box2d.collision.shapes.ShapeType) ContactRegister(com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactRegister) Contact(com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact)

Example 8 with Contact

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

the class ContactManager method addPair.

/**
 * Broad-phase callback.
 *
 * @param proxyUserDataA proxy user data A
 * @param proxyUserDataB proxy user data B
 */
@Override
public void addPair(Object proxyUserDataA, Object proxyUserDataB) {
    Fixture.FixtureProxy proxyA = (Fixture.FixtureProxy) proxyUserDataA;
    Fixture.FixtureProxy proxyB = (Fixture.FixtureProxy) proxyUserDataB;
    Fixture fixtureA = proxyA.fixture;
    Fixture fixtureB = proxyB.fixture;
    int indexA = proxyA.childIndex;
    int indexB = proxyB.childIndex;
    Body bodyA = fixtureA.getBody();
    Body bodyB = fixtureB.getBody();
    // Are the fixtures on the same body?
    if (bodyA == bodyB) {
        return;
    }
    // TODO_ERIN use a hash table to remove a potential bottleneck when both
    // bodies have a lot of contacts.
    // Does a contact already exist?
    ContactEdge edge = bodyB.getContactList();
    while (edge != null) {
        if (edge.other == bodyA) {
            Fixture fA = edge.contact.getFixtureA();
            Fixture fB = edge.contact.getFixtureB();
            int iA = edge.contact.getChildIndexA();
            int iB = edge.contact.getChildIndexB();
            if (fA == fixtureA && iA == indexA && fB == fixtureB && iB == indexB) {
                // A contact already exists.
                return;
            }
            if (fA == fixtureB && iA == indexB && fB == fixtureA && iB == indexA) {
                // A contact already exists.
                return;
            }
        }
        edge = edge.next;
    }
    // Does a joint override collision? is at least one body dynamic?
    if (!bodyB.shouldCollide(bodyA)) {
        return;
    }
    // Check user filtering.
    if (contactFilter != null && !contactFilter.shouldCollide(fixtureA, fixtureB)) {
        return;
    }
    // Call the factory.
    Contact c = popContact(fixtureA, indexA, fixtureB, indexB);
    if (c == null) {
        return;
    }
    // Contact creation may swap fixtures.
    fixtureA = c.getFixtureA();
    fixtureB = c.getFixtureB();
    indexA = c.getChildIndexA();
    indexB = c.getChildIndexB();
    bodyA = fixtureA.getBody();
    bodyB = fixtureB.getBody();
    // Insert into the world.
    c.m_prev = null;
    c.m_next = contactList;
    if (contactList != null) {
        contactList.m_prev = c;
    }
    contactList = c;
    // Connect to island graph.
    // Connect to body A
    c.m_nodeA.contact = c;
    c.m_nodeA.other = bodyB;
    c.m_nodeA.prev = null;
    c.m_nodeA.next = bodyA.m_contactList;
    if (bodyA.m_contactList != null) {
        bodyA.m_contactList.prev = c.m_nodeA;
    }
    bodyA.m_contactList = c.m_nodeA;
    // Connect to body B
    c.m_nodeB.contact = c;
    c.m_nodeB.other = bodyA;
    c.m_nodeB.prev = null;
    c.m_nodeB.next = bodyB.m_contactList;
    if (bodyB.m_contactList != null) {
        bodyB.m_contactList.prev = c.m_nodeB;
    }
    bodyB.m_contactList = c.m_nodeB;
    // wake up the bodies
    if (!fixtureA.isSensor() && !fixtureB.isSensor()) {
        bodyA.setAwake(true);
        bodyB.setAwake(true);
    }
    ++contactCount;
}
Also used : ContactEdge(com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactEdge) Contact(com.almasb.fxgl.physics.box2d.dynamics.contacts.Contact)

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