collision damage

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Peter Prins
Posts: 54
Joined: Fri Mar 11, 2011 11:44 pm

collision damage

Post by Peter Prins »

Hello Norbo,

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));
            }
        }
I've created the following data by smacking some objects together using my own version of the grabber:

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
.
.
.
As you can see, collisions hardly ever have a non-zero normal impulse. Do you know what I'm doing wrong?

Yours,
Peter
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: collision damage

Post by Norbo »

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?
It does try, yes. It might not always be able to do so. Nonzero collision softness (CollisionResponseSettings.Softness) can allow objects to 'squish' a little bit, but it's fairly insignificant.
As you can see, collisions hardly ever have a non-zero normal impulse. Do you know what I'm doing wrong?
I'm not sure- I don't see anything in the code which would be at fault. 0 impulse would be expected if it's a currently unused speculative contact, but the fact that those contacts have significant positive penetration depths means that they should be active.

I can't seem to replicate that behavior in the demos. Could you give it a shot with the latest demos so I could take a look?
Post Reply