In my app, when two bodies collide, I have to decide whether to damage a body or not depending on their collision velocities and their collision tolerances. The bodies can collide from any direction. My understanding with vectors manipulation is very little. I am not getting how to set tolerance and then comparing their velocities and tolerances. Can anyone give an idea about setting tolerance and comparing it?
1 Answers
The simplest way would be to take the difference between their velocities, eg.
b2Vec2 velDiff = body1->GetLinearVelocity() - body2->GetLinearVelocity();
float collisionSpeed = velDiff.Length();
You might also want to take into account the mass of the bodies involved, as a collision between heavier bodies will have more energy.
That would usually be sufficient for simple shapes like a circle or a box, especially if they collide directly. For longer thin shapes, or where the bodies collide at a point further from their center of mass, or where they are significantly rotating, you might find it to be inaccurate.
A more accurate way would be to check the size of the collision reaction impulse that Box2D calculated to push the bodies apart. You can find that by implementing the PostSolve function in your collision callback, and looking at the size of the normalImpulse component.