collision introspection

Official forum for the Chipmunk2D Physics Library.
Post Reply
sanotehu
Posts: 19
Joined: Sun Feb 27, 2011 1:11 am
Contact:

collision introspection

Post by sanotehu »

Hi!

Let's say I wanted to implement some damage-logic as a collision callback. It would use the impulse of the collision and the velocities before and after to calculate some stuff and maybe modify the collision force/velocities. So I need to know a few things: has the collision impulse been applied at the time of the callback? Can it be modified or should I apply extra forces to do this? What's the best way to get before and after velocities (for calculating energy changes)?

In case anyone has done this before, the goal is to limit restitution force to a maximum 'strength' of an object beyond which damage is applied based on the energy loss of the collision. It's maybe a bit more rigorous and correct than it has to be, feel free to tell me I'm being crazy.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: collision introspection

Post by slembcke »

You can get the total applied impulse in a post-solve or post-step callback using cpArbiterTotalImpulse():
http://files.slembcke.net/chipmunk/rele ... #cpArbiter

Applying another impulse after the solver has finish will sort of work, but the contacts/constraints are solved as complete system. Undoing one of the collisions doesn't undo it's indirect effect on other objects. You can try it, but it might just cause as many artifacts as leaving it alone. It might also wreck havok with the overlap solver.

Consider a stack of boxes falling together. When they hit the ground, the contacts between the boxes will stop all of them at once. If you undo the collision impulse of only the bottom box, the solver has already made all of the other boxes stop. Undoing the collision impulse won't make the others move again.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
sanotehu
Posts: 19
Joined: Sun Feb 27, 2011 1:11 am
Contact:

Re: collision introspection

Post by sanotehu »

Good point about the solver. I'll have to think about that to see if it causes trouble.

I think I just figured out how to calculate damage without measuring energy... I'll experiment a bit.

Thanks for all your help!
sanotehu
Posts: 19
Joined: Sun Feb 27, 2011 1:11 am
Contact:

Re: collision introspection

Post by sanotehu »

How do I find the rotational impulse of a collision? Linear has API functions, but there can be multiple contact points so I can't just do a cross product.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: collision introspection

Post by slembcke »

You are going to have to dig into Chipmunk's private API a bit to get that. I'm also not so sure it's a well defined quantity either. While the linear velocities of both objects have the same change in momentum, the change to the angular velocity is dependent on the offset of the contact point for each body. I'm not quite sure if there is a sensible way to calculate it.

What do you want it for? If you just want the energy of the collision, that can be derived from just the linear impulse as it's calculated using the apparent mass of the collision which includes rotational parts. I'm not 100% sure (the units match at least), but I think the kinetic energy could be calculated like the following. You should include chipmunk_private.h, the cpContact structure will be hidden in 6.0.

Code: Select all

cpFloat kineticEnergy = 0.0f;
for(int i=0; i<arbiter->numContacts; i++){
  cpContact *con = arbiter->contacts[i];
  kineticEnergy += con->jnAcc*con->jnAcc/con->nMass;
}
For applying damage I generally just use

Code: Select all

magicCoefficient*cpvlength(cpArbiterTotalImpulse())
as the damage amount. People don't really have an intuitive sense for this sort of thing anyway, so you can really just make it up whatever you want as long as it doesn't feel unfair.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
sanotehu
Posts: 19
Joined: Sun Feb 27, 2011 1:11 am
Contact:

Re: collision introspection

Post by sanotehu »

>People don't really have an intuitive sense for this sort of thing anyway, so you can really just make it up whatever you want as long as it doesn't feel unfair.

But I'll know! Damage is energy, not force, and especially not momentum! The gods of thermodynamics will haunt me in my sleep: "Not only did you violate the first law, your abomination doesn't even have the right units! You are sentenced to live the rest of your life in hellish guilt."

I'll just have to close my eyes and repeat "It's an abstract fiction, it doesn't have to be consistent" until my soul rots enough so that I don't care anymore.

Thanks for the pointer on the kinetic energy stuff. What exactly is jnAcc? I'll see what I can do with that. Maybe I can cook something up that will save my soul from eternal damnation in physics hell. :)

Thanks again!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: collision introspection

Post by slembcke »

Oh, I meant to explain that actually. cpContact.jnAcc is the accumulated final impulse applied to the contact in the direction of the surface normal. cpContact.nMass is the "apparent mass" of the collision at the contact point in the direction of the surface normal. I don't know if that's really what the value is called, but I've heard it called that before. Basically the apparent mass gets lower the more an impulse would make something spin. There is also jtAcc (and tMass), the tangent impulse used for friction, but I don't really think that would change the energy amount much. You could always just add the two terms together if you wanted it to be super accurate.

Squaring the impulse and dividing by the apparent mass makes sense to me and ends up in units of energy, but I didn't actually check it. I suppose you could find out easily enough by making a simple scene and calculating the total kinetic energy of two colliding bodies in both a pre-solve and post solve collision handler.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
sanotehu
Posts: 19
Joined: Sun Feb 27, 2011 1:11 am
Contact:

Re: collision introspection

Post by sanotehu »

Awesome! That is pretty much exactly what I need. I'm sure I can figure something out from those numbers.

Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests