Page 1 of 1

Rigid body callbacks

Posted: Fri Aug 12, 2011 5:46 am
by Kranatus
Hi, I currently have 3 rigid bodies: groundRigidBody, playerRigidBody, and enemyRigidBody. I want to run a function when playerRigidBody and enemyRigidBody collide. I
read several articles on this forum that suggest iterating through all the contact points like in the callback example on the wiki:

Code: Select all

int numManifolds = dynamicsWorld->getDispatcher()->getNumManifolds();
for (int i = 0; i < numManifolds; i++)
    {
        btPersistentManifold* contactManifold = dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
        btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
        btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());
        int numContacts = contactManifold->getNumContacts();
        
        for (int j = 0; j < numContacts; j++)
        {
            btManifoldPoint& pt = contactManifold->getContactPoint(j);
            if (pt.getDistance()<0.f)
            {
                const btVector3& ptA = pt.getPositionWorldOnA();
                const btVector3& ptB = pt.getPositionWorldOnB();
                const btVector3& normalOnB = pt.m_normalWorldOnB;
            }
        }
    }
This gives me all of the contact points and their locations, but how do I use this to find out if playerRigidBody and enemyRigidBody have collided? I guess what I want to say is something like, "if contact point was formed by playerRigidBody and enemyRigidBody then run function"

Re: Rigid body callbacks

Posted: Fri Aug 12, 2011 8:14 am
by majestik666
Check the contact added callback in here :

http://bulletphysics.com/Bullet/BulletF ... ource.html

gives you the pointers to the collisionObjects colliding, good way to find out
what's colliding with what

Re: Rigid body callbacks

Posted: Fri Aug 12, 2011 9:18 am
by Kranatus
Thx majestik666, that was exactly what I needed!