5

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?

noone
  • 19,520
  • 5
  • 61
  • 76

2 Answers2

6

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:

  • only postSolve will have valid values for the impulse that was applied
  • many preSolve/postSolve calls can occur per collision
  • the normal impulse points from body A to body B
  • there may only be one impulse (that NaN was from the second point I'm guessing?)

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).

iforce2d
  • 8,194
  • 3
  • 29
  • 40
1

"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.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • -1E15 to -1E30 is low, not small as compared to 1E-15 to 1E-30. I also have very small values, but they may indeed be valid. I'm looking at the collision between a dynamic and a static body. May that be the cause for the weird numbers? – noone Oct 13 '13 at 08:04
  • Well, depending on the direction, the number is either positive or negative. – Tarik Oct 13 '13 at 08:16