Rigid body callbacks

Post Reply
Kranatus
Posts: 2
Joined: Fri Aug 12, 2011 4:30 am

Rigid body callbacks

Post 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"
User avatar
majestik666
Posts: 66
Joined: Tue Mar 02, 2010 6:13 am

Re: Rigid body callbacks

Post 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
Kranatus
Posts: 2
Joined: Fri Aug 12, 2011 4:30 am

Re: Rigid body callbacks

Post by Kranatus »

Thx majestik666, that was exactly what I needed!
Post Reply