- Home /
How to calculate force from explosion on a rigidbody
Lets say I have a ball the explodes on contact with anything using the Rigidbody.AddExplosion function. It explodes it front of 10 cubes which are in a line and whom are rigid bodies. How would I go about calculate how hard each rigid body was hit with the explosion? The closer the cube is to the explosion, the harder it should be hit. I've been searching for this answer for the past couple days and cannot find it or even come up with one. Any help is appreciated. Thanks.
Rhithik not quite sure what you want, do you want to know the exact force? or do you want to apply less force the higher the distance? $$anonymous$$aybe show us some code you have to understand better what you are trying to do.
I don't really believe you've been looking for this for days.
The first thing I found on google is the Rigidbody.AddExplosionForce in the Unity Script Reference and this page is as clear as it can get. It even says that "The explosion force will fall off linearly with distance to the rigidbody.". It's all in the function, just give it a certain force and a range and it calculates the distance falloff for you. I don't really get what you want more?
Second hit on google: http://unity3d.com/support/resources/unity-extensions/explosion-framework
It's really all in there, download it and give it a look, it does exactly what you describe.
Answer by Bunny83 · Jul 12, 2012 at 03:33 PM
Rigidbody.AddExplosionForce already do the calculation for you. You just have to specify the center of the explosion and call that function on the RB.
The explosion framework, which has been mentioned by Yoerick above, does probably use this function and is a good example how to use it.
Generally You just need to find all objects that are in "range" and call AddExplosionForce on all of them with exact the same parameters:
// C#
float range = 10.0f;
float force = 100.0f;
Collider[] objs = Physics.OverlapSphere(transform.position, range);
foreach(Collider C in objs)
{
var R = C.rigidbody;
if (R == null)
continue;
R.AddExplosionForce(force, transform.position, range);
}
edit
Since your question isn't very detailed what you actually want, most people wait for a clarification what you want to do and for what purpose.
If you need to know some kind of damaging value for each object, well You can't see how AddExplosionForce calculates the distance. It could be linear, it could be quadratic or logarithmic. If you really want to know how it calculates the force, you might do some tests to figure it out.
The other possibility is that you just calculate a damage-value yourself, the way you want.
You usually start by determine the distance of each object to the explosion source and linearize the distance within the range:
// C#
float CalculateExplosionValue(Vector3 aSource, Vector3 aTarget, float aRange)
{
float dist = (aTarget - aSource).magnitude;
if (dist > aRange)
return 0.0f;
return 1.0f - dist / aRange;
}
This function will return 1.0f if the object "sits" on the explosion and 0.0 when it's at max range or further away. Any position within the radius will be linearly between 1.0 and 0.0.
This can now be made a quadratic falloff or any other function graph you like.
@fafase: Could be, but i guess it would be
F = Power*3 / 4*Pi*r^3
;) The pressure reduces linearly with the expanding volume. But in the end it's quite irrelevant since it's just a simulation.
Your answer
Follow this Question
Related Questions
Calculate Position of Rigidbody after applying Force 0 Answers
Decrease ExplosionForce and damage continously by distance 2 Answers
Exploide GameObjects Into BrokenParts : Unity 1 Answer
AddExplosionForce 0 Answers
calculate the estimated distance travel based on the Force applied to Rigid Body 1 Answer