I'm using a standard Box2D ContactListener
to listen on collision events. What I want is to calculate the strength of the impact of the collision between Bodies.
I've read many different descriptions of how people calculate it. Some use the preSolve
callback, others use postSolve
. Some use the Manifold
, others the ContactImpulse
. Some take only the first point's normalImpulse+tangentImpulse, others take the sum of all points and others, again, take the maximum. Some people ignore tangentImpulses completely...
I cannot get my head around this problem. Sometimes I get only impulses in postSolve
and the impulses in preSolve
are 0 in total. Sometimes it's the other way around. Sometimes I get ridiculously high values (say 1E15 to 1E30) and sometimes they are ridiculously low (say -1E15 to -1E30). I even had the case that one of the impulses was NaN
(Not a Number).
Is there anyone who can explain to me how to solve this problem and maybe explain how to interpret those impulses? Or maybe point me to any kind of open source game which uses Box2D and also needs to calculate the impact for any kind of damage system?
I think you need to define what you consider to be "the strength of the impact" before anyone can tell you how you should calculate it. As you've said there are many ways to approach the problem.
Here are a few tips I might mention:
This page has more details: http://www.iforce2d.net/b2dtut/collision-anatomy
It's common to just look at the impulse values in the first postSolve after the contact begins, since that is usually the largest and represents the initial hit.
Another approach might be to keep track of the total impulse values over say, the last 0.5 seconds, and if that exceeds some threshold then the object should break. This would let you handle the case where two objects are already touching, and a third hits them (for example if two objects are stacked and a third object is dropped from above, common sense tells us that the one on the bottom should not escape being damaged just because it was not directly touched).
"Sometimes I get ridiculously high values (say 1E15 to 1E30) and sometimes they are ridiculously low (say -1E15 to -1E30)."
Depending on the direction, the force will be either positive or negative. For extremely rigid bodies, collisions at even moderate speeds could result in extremely high forces/accelerations (in absolute values). Think for example of two spheres made of steel colliding at 200 km/h.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With