How to know if a body is colliding

Post Reply
maiklof
Posts: 24
Joined: Wed Nov 20, 2013 10:01 am

How to know if a body is colliding

Post by maiklof »

Hi,
I am looking for a way to know if a body with a collision shape is having a collision with another collision shape. Is there any method to get the answer as a boolean (True if it has one or several collisions), or a way to get the total number of collisions points?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: How to know if a body is colliding

Post by drleviathan »

One way to do it is to scan all of the contact manifolds after each step. So something like this:

Code: Select all

void updateContactMap(btCollisionDispatcher* collision_dispatcher) {
    int num_manifolds = collision_dispatcher->getNumManifolds();
    for (int i = 0; i < num_manifolds; ++i) {
        btPersistentManifold* contact_manifold =  collision_dispatcher->getManifoldByIndexInternal(i);
        // sometimes you get a manifold with zero contacts so filter for that case
        if (contact_manifold->getNumContacts() > 0) {
            const btCollisionObject* object_A = contact_manifold->getBody0();                           
            const btCollisionObject* object_B = contact_manifold->getBody1();                           
            // ... do something
        }
    }
}
Where the collision_dispatcher is the one you gave the btDiscreteDynamicsWorld when you created it.
Post Reply