I have a collider and it sometimes collides with another colliders. I would like to know the force the other collider chrashed with mine (to calculate damage). I’m not interested in torque and friction and anything separately, only the resultant force which my collider gets.
I hope I was clear enough. Is there any way to know this?
I’m not sure how you would isolate force strictly from collision information, but you could calculate the impulse on the object relatively easily.
Impulse = m∆v
Assuming we have an inelastic collision.
m1v1 + m2v2 = (m1 + m2)vf
vf (velocity final) = (v1m1 + v2m2) / (m1 + m2)
then....
∆v = vf - v1
so the impulse = m∆v
Now, collision information provides a relative velocity. If you set the velocity of the second object to the relative velocity and then treat the velocity of your current object as zero, you can simplify the math.
v1 = 0.0
v2 = collision.relativeVelocity
vf = v2m2 / (m1 + m2)
∆v = vf
In code terms:
function OnCollisionEnter (col : Collision) {
var vFinal = col.rigidbody.mass * col.relativeVelocity / (rigidbody.mass + col.rigidbody.mass);
var impulse = vFinal * rigidbody.mass;
}
Now, elastic collisions. If there is no energy loss (bounciness >= 1) then the ∆v is the velocity of the 2nd object minus the velocity of the first object:
var impulse = rigidbody.mass * ( col.rigidbody.velocity - rigid.body.velocity);
To my understanding you can’t actually access collision force, per se. You’ll probably want to look at collision normals, the velocity, and object masses. I’m sure you can find an equation pretty quickly. There was a pretty long forum discussion on the matter here: http://forum.unity3d.com/threads/23746-getting-impact-FORCE-not-just-velocity Might help you out a bit! Good luck!
You can’t know the reaction force directly, but it can be calculated: you can get the rigidbody.velocity variation due to the collision, calculate the acceleration and multiply by the rigidbody.mass to get the force. Unfortunately, the velocity changes in the first FixedUpdate after the collision. A good way to work around this is to create a fake OnCollisionEnter - let’s call it OnAfterCollision - and call it in the first FixedUpdate after the collision:
private var fCollided: boolean = false;
private var fLastVel: Vector3;
private var fCollision: Collision;
function OnCollisionEnter(coll: Collision){
fCollision = coll; // save collision data
fCollided = true; // signal that a collision happened
}
function FixedUpdate(){
if (fCollided){ // if collision happened...
fCollided = false; // reset flag
// calculate acceleration due to collision
var acc = (rigidbody.velocity - fLastVel)/Time.fixedDeltaTime;
// convert to force:
var force = rigidbody.mass * acc;
// call OnAfterCollision passing the Collision
// info and the reaction force:
OnAfterCollision(fCollision, force);
}
fLastVel = rigidbody.velocity; // update last velocity
}
// place your damage code in OnAfterCollision: you will have all the
// original Collision data and the reaction force:
function OnAfterCollision(coll: Collision, force: Vector3){
print("Force="+force.magnitude);
}
use collision.impulse
you can get Vector3 value of force.
OnCollisionEnter(Collision col)
{
Vector3 impactForce = col.impulse / Time.deltaTime;
//Handle ahead yourself!! Cheers
}