How to know if there has been a collision

Bilal
Posts: 8
Joined: Tue Nov 13, 2007 2:30 pm

How to know if there has been a collision

Post by Bilal »

Hi...
I am new to bullet and working on a vehicle simulator using bullet


I want to play a certain sound as the vehicle collides with some other body..
But how would my main program know about the collision???
I dont know how to do it....

Please tell if you know

Thanx in advance
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: How to know if there has been a collision

Post by Erwin Coumans »

A collision usually results in creation, processing and (when objects separate) destruction of contacts points.

All those 3 events have a callback. There is no elegant interface yet, but the user can override those callbacks using those global internals:

gContactAddedCallback
gContactProcessedCallback
gContactDestroyedCallback

For each rigidbody that you want a callback, you need to set the CF_CUSTOM_MATERIAL_CALLBACK flag:

Code: Select all

rigidBody->setCollisionFlags(staticBody->getCollisionFlags()  | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
See ConcaveDemo for an example.

Another method is to simply iterate over all contact points, see CollisionInterfaceDemo how to do this.

We need to provide some more demos and docs how to use those callbacks.
Hope this helps,
Erwin
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: How to know if there has been a collision

Post by chunky »

I'm coming up against this problem soon, and I'm not entirely clear on the workflow, or how to do this:

I only want to know when the collisions happen - not actually modify the default behaviour of bullet when the collisions happen [eg, spaceship hits a barrier that bounces and additionally does damage, unlike a normal wall from which it just bounces].

How would I override the callback so that the original callbacks are still called, but my function that does damage to the ship is also called?

Thank-you very much,
Gary (-;
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: How to know if there has been a collision

Post by Erwin Coumans »

You can safely use the callbacks (gContactAddedCallback, gContactProcessedCallback) to add some game logic events.

It is an additional user callback, even if you leave the implementation empty, the default rigid body dynamics response is still executed.

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

Re: How to know if there has been a collision

Post by chunky »

Fantastic!

Thank-you

Gary (-;
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: How to know if there has been a collision

Post by chunky »

Heya,

So I got the callbacks working by hooking gContactAddedCallback. Only problem is that for every collision between two bodies [as it appears to me], gContactAddedCallback is called a bunch of times. Typically it's something like 2-4 times for each collision.

Is there an easy way to get a callback that's only called once for each actual collision, instead of multiple times for what are presumably multiple contact points?

Thank-you very much,
Gary (-;
Murphy
Posts: 32
Joined: Fri Aug 31, 2007 6:36 am

Re: How to know if there has been a collision

Post by Murphy »

I tried using the callback method but was never receiving a callback for when a point is removed so now I am currently iterating over all contact points and this seems to be working.

chunky, I maintain a list of active collisions and only add a new collision if the two bodies aren't already in the list together. I would imagine this would work for you too.

As a side note, I wanted to know if there is a way to extract some kind of impact amount from a contact point. We want to play a sound only when a strong collision occurs. Is this what the m_distance1 variable is for in the btManifoldPoint?

Also, what is m_normalWorldOnB useful for? My understanding is that it is the normal from m_positionWorldOnA to m_positionWorldOnB (which are the same point I assume). So that normal would be 0, 0, 0, but it isn't.

Thanks

***Edit***
I decided to upgrade to the latest version of Bullet and now I see m_appliedImpulse in btManifoldPoint. This seems like exactly what I need. I would still like to know what the normal and distance are for however.
chunky
Posts: 145
Joined: Tue Oct 30, 2007 9:23 pm

Re: How to know if there has been a collision

Post by chunky »

chunky, I maintain a list of active collisions and only add a new collision if the two bodies aren't already in the list together. I would imagine this would work for you too
Yeah, that's the direction I was going to take. It was also, though, somewhat related to this: http://www.bulletphysics.com/Bullet/php ... f=9&t=1991
Currently I don't see a way to granularise it so that the callbacks gets called once per physics tick instead of once per {some other} loop. If there's a callback somewhere in bt{Continuous,Discrete}DynamicsWorld::internalSingleStepSimulation that I'm not seeing, then that would be the correct place to do this stuff [ie, that would be the place to clear out the list you're generating in the collision callback].

If you don't clear out this list between internal physics ticks, then your gameplay won't be framerate-independant...

Gary (-;