Tuesday 24 July 2012

Bullet Physics - Cocos3d - Collisions using contact-added callback and masks

Within my current game I want my truck to be able to come into contact with a power up.
When this happens I don't want it to influence the truck, just register the impact.

There is quite a straight forward way of dealing with collisions using bullet and this is using the contact added callback.

You will need to create one method that handles what happens when a collision occurs and then set a bullet variable to be equal to this method.

Then all you will have to do is register each object that uses the callback for collisions.

Create a function with the following signature


bool contact_callback( btManifoldPoint &btmanifoldpoint,
                            const btCollisionObject *btcollisionobject0,
                            int part_0, int index_0,
                            const btCollisionObject *btcollisionobject1,
                            int part_1, int index_1 ) 


Within this method you will be returned a pair of objects that have collided, it is up to you what you do with them.

I use the

rigidBody->setUserPointer(physicsObject);

method of the rigidBody to set a pointer to my own object.

This allows me to cast the pointer from the collisions to my own objects.

CC3PhysicsObject3D *phyObject0 = ( CC3PhysicsObject3D * )( ( btRigidBody * )btcollisionobject0 )->getUserPointer();
    

Note even if you remove an object that comes into contact immediatly from your physics world it will still call this call back a few times, I have an isBeingRemoved property on my own object which I set as a further check.

Then all you have to do is register your callback with the world

 gContactAddedCallback = contact_callback;

I do this at the bottom of my world initialisation code.

Finally I register any rigid bodies that you want to call this callback method when a collision has occurred.


pObject.rigidBody->setCollisionFlags(pObject.rigidBody->getCollisionFlags() |  btRigidBody::CF_NO_CONTACT_RESPONSE |btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK );


Here pObject is my physics. I use two flags here

btRigidBody::CF_NO_CONTACT_RESPONSE 

Which register the rigid body as not having an affect on the physics world

and

btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK 

Which registers the object to call the gContactAddedCallback related call back function.

I hope the above is of some help!

Duncan.



   


No comments:

Post a Comment