Page 1 of 1

How to know if a body is colliding

Posted: Sun May 01, 2022 9:16 pm
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?

Re: How to know if a body is colliding

Posted: Thu May 05, 2022 3:13 pm
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.