I'm trying to implement a way to have objects receive damage from collisions. I would like to have the damage depend on the impulse (in normal direction) applied during the collision. And to have a cut-off threshold depending on the relative velocity (in normal direction). This to prevent small collisions and objects sitting on top of each other to get damaged over time. My first question is, can I assume that the solver tries to eliminate all relative velocity at the point of impact in a single physics step?
Secondly I have trouble retrieving the data I need. I've created an IEndOfTimeStepUpdateable with the following code:
Code: Select all
void IEndOfTimeStepUpdateable.Update(float dt)
{
Console.WriteLine("Start damage:");
foreach (NarrowPhasePair npPair in _physicsManager.Space.NarrowPhase.Pairs)
{
CollidablePairHandler pair = npPair as CollidablePairHandler;
if (pair == null)
continue;
foreach (ContactInformation contact in pair.Contacts)
{
handle(contact, contact.Pair.CollidableA, contact.Pair.CollidableB);
handle(contact, contact.Pair.CollidableB, contact.Pair.CollidableA);
}
}
}
private void handle(ContactInformation contact, Collidable reciever, Collidable actor)
{
if (reciever.Tag == null) return;
ICollisionDamageTag tag = reciever.Tag as ICollisionDamageTag;
if (tag == null) return;
float normalVelocity;
Vector3.Dot(ref contact.RelativeVelocity, ref contact.Contact.Normal, out normalVelocity);
normalVelocity = Math.Abs(normalVelocity);
if (tag.Debug && normalVelocity > 5f)
{
Console.WriteLine("velocity " + MyToolbox.ToFloatingPointString(normalVelocity));
Console.WriteLine("impulse " + MyToolbox.ToFloatingPointString(contact.NormalImpulse));
Console.WriteLine("depth " + MyToolbox.ToFloatingPointString(contact.Contact.PenetrationDepth));
}
}
Code: Select all
.
.
.
Start damage:
velocity 12.5442972
impulse 0
depth 0.21103017
velocity 11.0694275
impulse 0
depth 0.124562651
Start damage:
velocity 12.4176083
impulse 0
depth 0.0009872663
.
.
.
Start damage:
velocity 13.399128
impulse 0
depth 0.0176501684
.
.
.
Start damage:
velocity 19.1623
impulse 0
depth 0.04034248
velocity 18.9324951
impulse 0
depth 0.0461097881
velocity 18.5614052
impulse 0
depth 0.0485692881
.
.
.
Start damage:
velocity 5.956197
impulse 0
depth 0.195842
velocity 6.117421
impulse 0
depth 0.195580125
.
.
.
Start damage:
velocity 50.782196
impulse 0
depth 0.07064493
.
.
.
Start damage:
velocity 6.05777
impulse 0
depth 0.006379716
.
.
.
Start damage:
velocity 17.5968971
impulse 0
depth 0.1143949
.
.
.
Start damage:
velocity 5.10093451
impulse 36.21381
depth 0.005889965
Start damage:
velocity 7.0085535
impulse 0
depth 0.128369331
velocity 6.873404
impulse 0
depth 0.117860727
.
.
.
Yours,
Peter