Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Box2D collision impact calculation

Tags:

libgdx

box2d

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?

like image 876
noone Avatar asked Oct 13 '13 07:10

noone


2 Answers

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

like image 170
iforce2d Avatar answered Sep 30 '22 00:09

iforce2d


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

like image 31
Tarik Avatar answered Sep 29 '22 23:09

Tarik