Best strategy for collision callback

josemarin
Posts: 38
Joined: Fri Aug 12, 2005 5:37 pm

Best strategy for collision callback

Post by josemarin »

Hi!

What´s the best strategy for detecting collision among objects of a scene?

I need to detect when two objects are colliding (a rocket and a enemy, or the player and a land mine, or the player and a force field).

Some objects must react when colliding with certain other objects.

I need a callback function that would be called when a collision is performed, and then I would call the "OnCollide" function of each colliding object, passing which object collided with it.

Using the btCollisionDispatcher::setNearCallback would be a good start point?

Is there any sample on how to do this?

Thanks!

Jose
AlexSilverman
Posts: 141
Joined: Mon Jul 02, 2007 5:12 pm

Re: Best strategy for collision callback

Post by AlexSilverman »

Hi,

There's a callback called ContactAddedCallback, which is called once for each contact point. Details are here or you can search through ConcavePhysicsDemo.cpp for ContactAddedCallback to just see about the implementation. I'm using it and so far the only (admittedly small) difficulty has been that there can be multiple contact points for one collision, up to 4 if I'm not mistaken. Aside from that, this should do exactly what you need.

Hope this helps.

- Alex
hiker
Posts: 83
Joined: Tue Oct 24, 2006 11:52 pm
Location: Australia

Re: Best strategy for collision callback

Post by hiker »

Hi,

I had the same problem (collision of rockets with karts), see http://www.bulletphysics.com/Bullet/php ... f=9&t=1683. Besides using the callback there is also the option of deriving from btSequentialImpulseConstraintSolver, and overriding the solveGroup method. Not sure if there are any advantagesor disadvantages to these two approaches.

I think you might get a collision reported more than 4 times, if more than one substep per timestep is used (I think the limit is 4 per substep) . I am actually going to use a list to collect all collision pairs (in my case: karts hit by a rocket), and handle the actual collision (i.e. explosions) at the end of the timestep (i.e. when all substeps are finished). Not sure what would happen if you removed a rigidBody in a substep ... I think chaos would happen :)

Hope that help!
Joerg
andrewaah
Posts: 6
Joined: Tue Sep 25, 2007 1:56 pm

Re: Best strategy for collision callback

Post by andrewaah »

hiker wrote: I am actually going to use a list to collect all collision pairs (in my case: karts hit by a rocket), and handle the actual collision (i.e. explosions) at the end of the timestep (i.e. when all substeps are finished). Not sure what would happen if you removed a rigidBody in a substep ... I think chaos would happen :)
I'm also facing the problem of wanting to remove an object in the middle of a timestep. When I was writing my own physics I had no problem with this, but I'm still trying to figure out how bullet manages it all.

But if you want to remove an object mid timestep, you might be able to just change its collision group to a group that doesn't collide with anything.