Trying to work out the impact force of two colliders - Please help!

Hi all, I am making a driving game and I need to penalise the player when he/she hits another object.

I have a formula that charges cash for the speed or impact of the collision, but the problem is I have no idea how to get the variable that is the impact force of the two colliders colliding.

I thought that I could take the velocity of the rigidbody at impact, but when you call oncollision, its already happened at that point at the velocity is already reduced.

Is there a way that anyone can think of to get a reliable float figure back that is based on the rigidbody speed at time of impact?

1 Like

There was something in the beta release notes about the collision impuse being exposed in 5.2. Right now there’s no way to get it so you’ll have to approximate it yourself.

Collision impulses aren’t really that reliable for this type of thing anyway. I wrote the physics engine and did all the vehicle dynamics work for VRC Pro (an R/C car racing simulator) and tried to do something like this using collision impulses for implementing car damage and incident points (a number that would be determined much the same way as your money effect).

The approach doesn’t really work very well as it turns out. It’s very difficult to tune how you want it, and there are always situations where what looks like a light impact is actually very heavy just because of the way the cars hit each other. What looks like a glancing blow that would do nothing to the car often involves a very large impulse, and what looks like a hard hit is sometimes just a series of very small impulses. So I wouldn’t even bother trying to go down that road even after 5.2 is released unless you’re curious.

You’re probably best off using the velocities of the cars during impact directly. If I were to do it, when a collision is detected I’d start by just checking the speeds of the cars relative to each other. Maybe create a vector connecting their positions and dot the velocity along that vector. Something like that. It won’t be perfect but I’d bet it’s a lot better than using the impulses.

Keep in mind that a typical impact usually involves lots of collision happening very quickly over several frames, so it’s tough to pin down a number for this type of thing. You might find yourself registering 20 hits instead of just 1, while another time in a crash that looks even worse you only get 3 hits. It’s tricky stuff.

1 Like

that’s really great information and thank you for taking the time to write that!

I will try and write a much simpler system then that looks at the players speed and calculates it from that. Perhaps store the player speed every 10th of second into a variable, and then at the moment of impact, check the last entry and calculate from that… probably a bit dirty as you say, but likely to have results that may be consistent…

Sure, that’d work. I’d probably just have a public variable somewhere that keeps track of the previous velocity vector. But recording every 1/10th second would probably work just as well considering the cars probably don’t change speed much in that time.

hi i used - works good

   void OnCollisionEnter(Collision collision)
   {
         float forceOfImpact= collision.relativeVelocity.magnitude;
   
         if   (forceOfImpact > 20 && speedKMh-30 > Vehicle.getKMHSpeed())
          {
               if   (forceOfImpact>42)  {Big_Impact_Effect(); }
               else if  (forceOfImpact > 30) {Small_Impact_Effect(); }
               else   {Tiny_Impact_Effect(); }
          };
    }

it compares previous speed and (curent - after hit) speed, if it smaller than previous for 30 kmh then it’s not bump or any thing else but real impact.