BulletPhysics Question

Started by
1 comment, last by Krohm 11 years, 4 months ago
Hello everyone,
Recently I've started my new small game project. During the desing I've decided to use BulletPhysics as the game's physical engine. But now I'm in a big truoble. I want to do the followings:
When two objects collide the specified sound has to be played. This depends on the material applied on the colloding trinagles of the objects. The sound sould be played (so the collision should be reported) only once (when the real collision happens, not on continous contact), depending on the impact speed and angle.
Alternatively if the above method is not possible it is also ok just to report if two "kind" of objects collide without reporting the material.
Any idea how to do this? I googled my problem and turned out that there are many methods to report object-object collision via callbacks but I can't find any correct sample code on the internet. It looks that the BulletPhysics is relly fast, but there are only a very few good tutorial. At least I was not able to find one I need to do this. If anyone knows how to do that or has a link to a good tutorial please help.
Thanks in advance!
Advertisement
Hi!

When using Bullet Physics in your game you implement some BulletInternalTickCallback( btDynamicsWorld * const world, btScalar const timeStep ) method bullet uses as internal callback.

dynamicsWorld->setInternalTickCallback( BulletInternalTickCallback );

dynamicsWorld declared as btDynamicsWorld*
In this method you want to make loop like that:


// look at all contacts
btDispatcher * const pDispatcher = world->getDispatcher();
for ( int manifoldInd=0; manifoldIdx<pDispatcher->getNumManifolds(); manifoldInd++ )
{
//get manifold
btPersistentManifold const * const pManifold = pDispatcher->getManifoldByIndexInternal( manifoldInd );
assert( manifold );

// get colliding bodies
btRigidBody const * const pBody0 = static_cast<btRigidBody const *>(pManifold->getBody0());
btRigidBody const * const pBody1 = static_cast<btRigidBody const *>(pManifold->getBody1());

...



So you acquired pointers to your colliding bodies. The next thing you want to do is: make an external list of colliding pairs of objects and update it every frame. So you can compare if the pair of colliding objects is already in the list, so you have continuous contact, otherwise they are just collided. Other data about collision you can get from btPersistentManifold object. For keeping material data of the object you can create external wrapper for every physics body like:


struct PhysicsBody
{
btRigidBody const * const pBody;
enum Material m_Mat;
};
Those are probably the most over-engineered solutions ever. I'm not sure BulletInternalTickCallback is supposed to be overriden for this reason. While it offers extra precision WRT an explicit check at the end of the tick I have doubts it's going to produce a different result as long as frame latency is low enough.
Manifold checking occurs in two ways: either check the manifolds manually or use a Ghost Object, with the latter being the recommended solution.

So you can compare if the pair of colliding objects is already in the list, so you have continuous contact, otherwise they are just collided
This is a bit of an over-approximation, as pairs are generated by broadphase, manifold distance is key here (as well as "direction"). Two objects will collide only if manifold distance is <=0 assuming pair (objA, objB) in correct order.

Previously "Krohm"

This topic is closed to new replies.

Advertisement