Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by purplelilgirl · Jan 09, 2012 at 04:20 AM · rigidbodypositionaddforce

Getting final position after rigidbody.addForce

Hi, Can I get / predict the final position of a rigidbody after addForce has been applied? Thank you.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image purplelilgirl · Jan 09, 2012 at 06:54 AM 0
Share

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)

5 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image purplelilgirl · Jan 09, 2012 at 08:14 AM 0
Share

that's what I thought. I'll wait for the wiki site to be back up then. thank you :)

avatar image purplelilgirl · Jan 18, 2012 at 03:14 AM 0
Share

the wiki seems to be back up now :) may I know the link to the script?

avatar image Bunny83 · Jan 18, 2012 at 03:40 AM 1
Share

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.

avatar image purplelilgirl · Jan 18, 2012 at 03:55 AM 0
Share

gotcha, thanks :)

avatar image
-1

Answer by adrenak · Jan 09, 2012 at 04:48 AM

You can use rigidbody.position

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spacepilot · Jan 09, 2012 at 08:22 AM 0
Share

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

avatar image purplelilgirl · Jan 09, 2012 at 08:25 AM 0
Share

I used to be a lurker :))

avatar image spacepilot · Jan 09, 2012 at 08:40 AM 0
Share

Syclamoth was faster :-(. But I was the first who gave you karma nevertheless :-D.

avatar image purplelilgirl · Jan 09, 2012 at 08:54 AM 0
Share

yes, you were :) thank you!

avatar image
-1

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image purplelilgirl · Jan 09, 2012 at 08:38 AM 0
Share

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.

avatar image spacepilot · Jan 09, 2012 at 08:52 AM 2
Share

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.

avatar image purplelilgirl · Jan 09, 2012 at 09:13 AM 0
Share

got it :) thanks :) oh and the writing style is not unusual, how else are you supposed to write a raised number

avatar image spacepilot · Jan 09, 2012 at 11:19 AM 0
Share

...like this:

t² = t * t

but it doesn't work in scripts.
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges