Search in sources :

Example 6 with Contact

use of org.jbox2d.dynamics.contacts.Contact 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 7 with Contact

use of org.jbox2d.dynamics.contacts.Contact in project libgdx by libgdx.

the class WorldRayCastWrapper method popContact.

public 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) {
        if (reg.primary) {
            Contact c = reg.creator.pop();
            c.init(fixtureA, indexA, fixtureB, indexB);
            return c;
        } else {
            Contact c = reg.creator.pop();
            c.init(fixtureB, indexB, fixtureA, indexA);
            return c;
        }
    } else {
        return null;
    }
}
Also used : ShapeType(org.jbox2d.collision.shapes.ShapeType) ContactRegister(org.jbox2d.dynamics.contacts.ContactRegister) ParticleContact(org.jbox2d.particle.ParticleContact) Contact(org.jbox2d.dynamics.contacts.Contact) ParticleBodyContact(org.jbox2d.particle.ParticleBodyContact)

Example 8 with Contact

use of org.jbox2d.dynamics.contacts.Contact in project libgdx by libgdx.

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.
   */
public void collide() {
    // Update awake contacts.
    Contact c = m_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) == false) {
                Contact cNuke = c;
                c = cNuke.getNext();
                destroy(cNuke);
                continue;
            }
            // Check user filtering.
            if (m_contactFilter != null && m_contactFilter.shouldCollide(fixtureA, fixtureB) == false) {
                Contact cNuke = c;
                c = cNuke.getNext();
                destroy(cNuke);
                continue;
            }
            // Clear the filtering flag.
            c.m_flags &= ~Contact.FILTER_FLAG;
        }
        boolean activeA = bodyA.isAwake() && bodyA.m_type != BodyType.STATIC;
        boolean activeB = bodyB.isAwake() && bodyB.m_type != BodyType.STATIC;
        // At least one body must be awake and it must be dynamic or kinematic.
        if (activeA == false && activeB == false) {
            c = c.getNext();
            continue;
        }
        int proxyIdA = fixtureA.m_proxies[indexA].proxyId;
        int proxyIdB = fixtureB.m_proxies[indexB].proxyId;
        boolean overlap = m_broadPhase.testOverlap(proxyIdA, proxyIdB);
        // Here we destroy contacts that cease to overlap in the broad-phase.
        if (overlap == false) {
            Contact cNuke = c;
            c = cNuke.getNext();
            destroy(cNuke);
            continue;
        }
        // The contact persists.
        c.update(m_contactListener);
        c = c.getNext();
    }
}
Also used : Contact(org.jbox2d.dynamics.contacts.Contact)

Example 9 with Contact

use of org.jbox2d.dynamics.contacts.Contact in project libgdx by libgdx.

the class ContactManager method addPair.

/**
   * Broad-phase callback.
   * 
   * @param proxyUserDataA
   * @param proxyUserDataB
   */
public void addPair(Object proxyUserDataA, Object proxyUserDataB) {
    FixtureProxy proxyA = (FixtureProxy) proxyUserDataA;
    FixtureProxy proxyB = (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) == false) {
        return;
    }
    // Check user filtering.
    if (m_contactFilter != null && m_contactFilter.shouldCollide(fixtureA, fixtureB) == false) {
        return;
    }
    // Call the factory.
    Contact c = pool.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 = m_contactList;
    if (m_contactList != null) {
        m_contactList.m_prev = c;
    }
    m_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);
    }
    ++m_contactCount;
}
Also used : ContactEdge(org.jbox2d.dynamics.contacts.ContactEdge) Contact(org.jbox2d.dynamics.contacts.Contact)

Example 10 with Contact

use of org.jbox2d.dynamics.contacts.Contact in project libgdx by libgdx.

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.
   * 
   * @param fixture the fixture to be removed.
   * @warning This function is locked during callbacks.
   */
public final void destroyFixture(Fixture fixture) {
    assert (m_world.isLocked() == false);
    if (m_world.isLocked() == true) {
        return;
    }
    assert (fixture.m_body == this);
    // Remove the fixture from this body's singly linked list.
    assert (m_fixtureCount > 0);
    Fixture node = m_fixtureList;
    // java change
    Fixture last = null;
    boolean found = false;
    while (node != null) {
        if (node == fixture) {
            node = fixture.m_next;
            found = true;
            break;
        }
        last = node;
        node = node.m_next;
    }
    // You tried to remove a shape that is not attached to this body.
    assert (found);
    // java change, remove it from the list
    if (last == null) {
        m_fixtureList = fixture.m_next;
    } else {
        last.m_next = fixture.m_next;
    }
    // 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.
            m_world.m_contactManager.destroy(c);
        }
    }
    if ((m_flags & e_activeFlag) == e_activeFlag) {
        BroadPhase broadPhase = m_world.m_contactManager.m_broadPhase;
        fixture.destroyProxies(broadPhase);
    }
    fixture.destroy();
    fixture.m_body = null;
    fixture.m_next = null;
    fixture = null;
    --m_fixtureCount;
    // Reset the mass data.
    resetMassData();
}
Also used : BroadPhase(org.jbox2d.collision.broadphase.BroadPhase) ContactEdge(org.jbox2d.dynamics.contacts.ContactEdge) Contact(org.jbox2d.dynamics.contacts.Contact)

Aggregations

Contact (org.jbox2d.dynamics.contacts.Contact)11 ContactEdge (org.jbox2d.dynamics.contacts.ContactEdge)5 ParticleBodyContact (org.jbox2d.particle.ParticleBodyContact)5 ParticleContact (org.jbox2d.particle.ParticleContact)5 Joint (org.jbox2d.dynamics.joints.Joint)4 PulleyJoint (org.jbox2d.dynamics.joints.PulleyJoint)3 BroadPhase (org.jbox2d.collision.broadphase.BroadPhase)2 ShapeType (org.jbox2d.collision.shapes.ShapeType)2 AABB (org.jbox2d.collision.AABB)1 TOIInput (org.jbox2d.collision.TimeOfImpact.TOIInput)1 Vec2 (org.jbox2d.common.Vec2)1 CircleContact (org.jbox2d.dynamics.contacts.CircleContact)1 ContactRegister (org.jbox2d.dynamics.contacts.ContactRegister)1 ContactVelocityConstraint (org.jbox2d.dynamics.contacts.ContactVelocityConstraint)1 JointEdge (org.jbox2d.dynamics.joints.JointEdge)1 DefaultWorldPool (org.jbox2d.pooling.normal.DefaultWorldPool)1 Test (org.junit.Test)1