Collision between 2 objects

b0gd4n
Posts: 14
Joined: Thu Nov 07, 2013 2:38 am

Collision between 2 objects

Post by b0gd4n »

Hey guys,

I have 2 objects, a sphere and a box that, when one collides with the other, will do some action (i.e. destroy the box).

I have tried several ways:

- checkCollideWith always returns true;
- contactPairTest - this I do not understand how to use.
It takes 3 arguments, the 2 objects and a callback. I thought that the callback can be any function in my code, but it doesn't work like that.

Please someone enlighten me!
inverse42
Posts: 3
Joined: Mon Dec 24, 2012 5:47 am

Re: Collision between 2 objects

Post by inverse42 »

There is a global function pointer "gContactProcessedCallback" - set this to a function with this prototype:

Code: Select all

bool ContactProcessed(btManifoldPoint& cp, void *body0, void *body1)
{
        //Do your collision handling / action here

        return false;
}
body0 and body1 are btCollisionObject's (what btRigidBody inherits from).

It will probably be helpful to add some data to each rigid body with get/setUserPointer() to help identify the object during the callback.
b0gd4n
Posts: 14
Joined: Thu Nov 07, 2013 2:38 am

Re: Collision between 2 objects

Post by b0gd4n »

inverse42 wrote:There is a global function pointer "gContactProcessedCallback" - set this to a function with this prototype:

Code: Select all

bool ContactProcessed(btManifoldPoint& cp, void *body0, void *body1)
{
        //Do your collision handling / action here

        return false;
}
body0 and body1 are btCollisionObject's (what btRigidBody inherits from).

It will probably be helpful to add some data to each rigid body with get/setUserPointer() to help identify the object during the callback.
Thanks for the reply.

I am still blurred as I am new to c++ and, of course, Bullet.

What exactly do I do with this gContactProcessedCallback?

Sorry for these questions, I assume the answer is rather simple...
b0gd4n
Posts: 14
Joined: Thu Nov 07, 2013 2:38 am

Re: Collision between 2 objects

Post by b0gd4n »

Hey guys,

I am still struggling with this and I know this must be easy for some.

Could someone please give an example of how to call a method, for example CollissionResult(), when 2 btRigidBodies collide (i.e. bodyA and bodyB)?

I really can'y figure this one out!

Thanks in advance!
STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: Collision between 2 objects

Post by STTrife »

you make a function

bool ContactProcessed(btManifoldPoint& cp, void *body0, void *body1)
{
//Do your collision handling / action here

return false;
}


Then in your startup procudure you set gContactProcessedCallback = ContactProcessed

Now bullet will call this function for every 2 objects that collide, each frame. Each body can contain userData, which if I remember correctly is a void pointer or something similar, so you can place any data in there that you like. For example a number, a string etc. but you do need to c++ knowledge, specifically about pointers and casting pointers. I would suggest learning more C++ before you use a framework like bullet.
bwelch
Posts: 48
Joined: Thu Dec 12, 2013 4:04 pm

Re: Collision between 2 objects

Post by bwelch »

I know this is a month-old post, but in case others are wondering how to get collisions up and running somewhat quickly, this video tutorial helped me immensely:

http://www.youtube.com/watch?v=YweNArzAHs4

This particular video deals with collisions, but starting with part 0, it walks you through Bullet setup all the way through soft bodies. I wish there were more video tutorials like this for even more functions in Bullet!