- Home /
what force did collider collide with?
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?
impulse is given to you ...
Contents/Documentation/en/ScriptReference/Collision-impulse.html
...the voice seemed to be saying as it emanated from all around like God himself was bellowing forth from the heavens with a gift for all mankind.
(Seriously, strange font-size theyve chosen for that kind of formatting, eh? :D )
best to emphasize for googling visitors since the answers below seem to be wildly out of date
Answer by Peter G · Jan 01, 2012 at 04:23 AM
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);
Answer by suyujin · Jan 01, 2012 at 12:10 AM
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!
collision forces are RIGHT THERE ...
Contents/Documentation/en/ScriptReference/Collision-impulse.html
Answer by aldonaletto · Jan 01, 2012 at 02:09 AM
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);
}
This works only if the body is able to move freely after the collision, not if it's stuck on the ground for instance.
Answer by TheGameLearner · Jun 04, 2018 at 05:18 AM
use collision.impulse you can get Vector3 value of force.
OnCollisionEnter(Collision col)
{
Vector3 impactForce = col.impulse / Time.deltaTime;
//Handle ahead yourself!! Cheers
}
Too bad that only works in 3D. I guess for 2D the code posted is the way to go.
Your answer
Follow this Question
Related Questions
Hitting Collider,Not Being Effected ? 1 Answer
How to Move Player in Particular Direction Without holding Down a Button? 0 Answers
Adding force to a bullet as it hits object? 2 Answers
pulling objects 1 Answer
collider not moving with the object 1 Answer