- Home /
Getting final position after rigidbody.addForce
Hi, Can I get / predict the final position of a rigidbody after addForce has been applied? Thank you.
sorry for the confusion, I meant given the force that I will apply to the rigidbody will I be able to know where the rigidbody will land or where it's final position will be? I'm trying to make a projection/ trajectory arc you see (similar to golf games)
Answer by syclamoth · Jan 09, 2012 at 07:13 AM
You can't easily predict the future, under any circumstances. However, assuming that the rigidbody keeps a straight ballistic trajectory, it is possible to extrapolate its eventual resting place by stepping it out. There is a nice script on the Unify Wiki that can do this. I'd link you to it, but the entire website is down at the moment!
The script basically works by predicting (by raytracing) how an object would move in one frame (assuming constant gravity and no external forces) from its current velocity. To calculate the resultant velocity from an impulse, use this equation-
velocity = momentum / mass;
Which means that a change in velocity will be equal to the 'force' value in the AddForce function divided by the rigidbody's mass.
Every 'step', it moves the 'predicted position' by the velocity / physics timestep (Time.fixedDeltaTime), and uses Physics.Linecast to determine if it hits anything. To calculate the eventual landing point, you can simulate steps until the 'predicted' object strikes the ground.
that's what I thought. I'll wait for the wiki site to be back up then. thank you :)
the wiki seems to be back up now :) may I know the link to the script?
f you found out that the wiki is back up, why don't you actually visit it? You have the information that there is such a script. The scripts in the wiki are sorted pretty well. If you go to the Scripts/Physics category you will find "Trajectory Simulation" right at the bottom.
Answer by adrenak · Jan 09, 2012 at 04:48 AM
You can use rigidbody.position
A newbie! purplelilgirl, where are you from? You just registered to ask this question? I'll give you your first answer! Brings $$anonymous$$arma :-D
Syclamoth was faster :-(. But I was the first who gave you karma nevertheless :-D.
Answer by spacepilot · Jan 09, 2012 at 08:33 AM
The code to get the position of an object is:
var CurrentPosition : Vector3;
var Transformposition : Vector3;
function Start(){
CurrentPosition = rigidbody.position; //Gives position of objects rigidbody
Transformposition = transform.position; //Gives position of parent object
}
The full syntax is: Vector3() = [Objectname].transform.position;
for transform. If objectname is omitted, the object is used the current script is attached to. Also mind the usage of lowercase letters, while Transform also exists but has a different effect. Don't know the difference between rigidbody.position and transform.position since both are applied to the same object - but you asked the question.
There also is a local-version of positions. Search the documentation for this: Unity-documentation
thanks for the answer, but that was not what I was looking for :) I'm trying to predict the final position of the rigidbody is force is added.
It's a quiete special thing, look at the units:
speed: m/s
accelleration: m/s^2
acceleration is causing a parabola, speed a line, when drawing their curves. But you want the position?! Wait, wait...there are two methods of motion in physics...this here is a uniform accelerated motion (when idealizing it for calculation)...
a = deltaV / deltaT
when a was not changed within a timeframe (this is the idealization), deltaV is the change in velocity then and deltaT is the timeframe. It's also the tricky part because acceleration is raising or lowering an objects speed, not it's position (unlike velocity does). The formula and all calculations based on this are only valid when a doesn't get changed. (Basic problem in physics - the other method is assu$$anonymous$$g that a = 0, to calculate motion.)
Based on this,
deltaV = a * deltaT
...and finally the position (with looking it up at the right place) should be:
pos = a/2 * t^2 + V0*t + pos0
pos: position
a:acceleration
t:time
V0: speed the object had before (without the added acceleration)
pos0: position it had before the impuls
Just to make sure because writing-style may look unusual:
t^2 = t*t.
got it :) thanks :) oh and the writing style is not unusual, how else are you supposed to write a raised number
...like this:
t² = t * t
but it doesn't work in scripts.Answer by Ashkan_gc · Dec 10, 2012 at 09:29 AM
This script simulate it in a simple way but expanding on it requires a little Physics research trajectory simulation
Keep in mind that in many situations rolling your own simple physics stuff will make life much easier, it doesn't need to be super complicated to make a fun gameplay.
Answer by $$anonymous$$ · Feb 08, 2020 at 02:19 PM
To estimate the position after adding addforce (with rough accuracy) you can just play with the numbers until you find the right combination.
Vector3 force = dir; //whatever force your adding
rigidbody.AddForce(force);
//Starting point
var pos = transform.position;
pos += (guess * 0.02f); //This will depend on other factors in your physics but a starting point.
//Add this section below if you want to account for gravity (ie ball coming back down)
float ydif = pos.y - act_s.db_tras[0].position.y;
var extra_y = guess * 0.0037f * ydif; //Depends on your gravity
//This removes negatives (ie ball going straight to ground and bouncing back up, it won't be perfect but depends on your needs
if (ydif < 0)
extra_y = new Vector3(0, 0, 0);
pos += extra_y;
//If you want the prediction to be the height of your starting point (ie ground level)
pos.y = transform.position.y;
//final result is in pos
Those 2 numbers 0.02f for distance and 0.0037f for height/gravity are what you want to try different combinations with.
This won't be perfect, but it will be close for enough depending on the situation.
Your answer
Follow this Question
Related Questions
Pick and move physics object 1 Answer
Rigidbody.Addforce against position 0 Answers
Rigidbody.Addforce against position 1 Answer
Destroy object on collision or check position 2 Answers
Why is velocity checking intensive? 0 Answers