- Home /
Can you use exchanged momentum in a collision script?
The physics engine clearly understands momentum, but I can't find any scripting component to call as a variable during a collision. I've got a kinematic rigidbody that I want to "damage" based of the strength of the collision, but I don't want to move it. I've tried using relative velocity, and that produces weird results when the collision skims the edge instead of hitting head on, so that's out.
The velocity of the hit is the only thing that comes to my $$anonymous$$d.
collision.relativeVelocity.sqr$$anonymous$$agnitude;
But it looks like you have already tried that. One thing that I could suggest, is try to find out when the collision is head on or just a skim. You could do this by finding where the contact points are in relation to the object orientations
Answer by Mendenbarr · Mar 13, 2014 at 03:33 PM
Turns out you can, with coll being the collision, coll.relativeVelocity returns the velocity the moment of impact, while coll.rigidbody.velocity returns the velocity the moment after impact, allowing you to find the change in velocity, which leads to the change in momentum, assuming the target you're calling the function from is kinematic.
momemtumY = (coll.relativeVelocity.y - Math.Abs(coll.rigidbody.velocity.y) * coll.rigidbody.mass);
momemtumX = (coll.relativeVelocity.x - Math.Abs(coll.rigidbody.velocity.x) * coll.rigidbody.mass);
momentum = (float)Math.Sqrt(Math.Pow(momemtumY,2) + Math.Pow(momemtumX,2));