Question About Detection Collision !

Youne
Posts: 40
Joined: Sun Jan 27, 2008 2:55 pm

Question About Detection Collision !

Post by Youne »

Hi to All,

I have a model which consists of several linked and non-linked objects. What I want is for the physics engine to inform me as soon as it detects a collision between two specific objects. I also want the physicis engine to delete the two objects and replace them by a single object which will model the two joint objects. I know that I have to use :

gContactAddedCallback
gContactProcessedCallback
gContactDestroyedCallback

but can someone explain to me how I have to implement this in my model ?

thank you for you help
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: Question About Detection Collision !

Post by chunky »

This was discussed just recently:
http://www.bulletphysics.com/Bullet/php ... f=9&t=1943

Saliently, grep the Demos/ dir for gContactAddedCallback. The code can be as simple as:

Code: Select all

extern ContactAddedCallback gContactAddedCallback;

static bool TWContactAddedCallback(btManifoldPoint& cp,
								   const btCollisionObject* colObj0,int partId0,int index0,
								   const btCollisionObject* colObj1,int partId1,int index1) {
  printf("There was a collision of some sort\n");
}

and then, somewhere in your initialisation code...

Code: Select all

  gContactAddedCallback = TWContactAddedCallback;
And for each rigid body you create that you want callbacks on...

Code: Select all

  rigidBody->setCollisionFlags(staticBody->getCollisionFlags()  | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
The "g" prefix is short for "global". Just make it point to your function of choice.

Hope that helps,
Gary (-;
Youne
Posts: 40
Joined: Sun Jan 27, 2008 2:55 pm

Re: Question About Detection Collision !

Post by Youne »

Hi to all,

Thanks you for your quick replay chunky,

I was able to implement the part of code that allows me to know when the collision was happened. Now my model informs me as soon as it detects a collision between two objects. What I want now is : how I have to do for that at each contact, I can have the identity of objects that collide.

Thank You very much

Youne
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: Question About Detection Collision !

Post by chunky »

const btCollisionObject* colObj0 and const btCollisionObject* colObj1 are the two objects that collided.

You'll want to look up the btCollisionObject::{set,get}UserPointer functions.

Gary (-;
Youne
Posts: 40
Joined: Sun Jan 27, 2008 2:55 pm

Re: Question About Detection Collision !

Post by Youne »

Hi to all

My model informs me as soon as it detects a collision between two objects. What I want now is : how I have to do for that at each contact, I can have the identity of objects that collide. I looked but at btCollisionObjet but I don't understand how can I go from btCollisionObjet to btRigidbody.

Thank You very much

Youne
ola
Posts: 169
Joined: Sun Jan 14, 2007 7:56 pm
Location: Norway

Re: Question About Detection Collision !

Post by ola »

You can do this to check if it's a rigidbody, and if so, get a proper pointer to it:

Code: Select all

btRigidBody* rigidbody = dynamic_cast<btRigidBody*>(collision_object);
if(rigidbody)
   {
   // It's a rigid body!!
   rigidbody->doSomething();
   }
Cheers,
Ola