• 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
0
Question by popuppirate · Mar 24, 2015 at 09:16 AM · rigidbodylinerenderertrajectory

3d Rigidbody Trajectory Prediction

Hello all,

I am attempting to produce a linerender model of a rigidbody's trajectory using an initial force applied to a dragless rigidbody and it's mass. However, the simulation wildly overestimates the distance travelled.

I begin by applying a force to a gameobject, 'ball' with a 'forward force' relative to me as follows:

  ball.rigidbody.AddForceAtPosition (kick_direction * forward_force, ball.transform.position);

To simulate the trajectory, I consider the following:

Velocity after one second of movement:

F=ma

a= F/m

therefore v1 = (F/m)*t = F/m

Distance travelled due to deceleration (see SUVAT equations):

s=u*t+0.5at^2

which results in the following code:

 void Update(){
         LineRenderer line = GetComponent<LineRenderer> ();
         line.SetVertexCount (20);
         distance_to_ball = Vector3.Distance (ball.position, transform.position);
     //    Mathf.Clamp (forward_force, 100f, 700f);
     //    Mathf.Clamp (angle, 0f, 90f);
         if (Input.GetMouseButton (0)) {
             forward_force += 300f * Time.deltaTime;
             float ball_mass = ball.rigidbody.mass;
             float ball_drag = ball.rigidbody.drag;
             float u_x =(kick_direction.x*forward_force)/ball_mass;
             float u_y =(kick_direction.y*forward_force)/ball_mass;
             float u_z =(kick_direction.z*forward_force)/ball_mass;
                 for(int i=0; i<20; i++){
                 float x_pos = u_x*(i+1)- 0.5f*(ball_drag*u_x)*Mathf.Pow ((i+1),2);
                 float y_pos =u_y*(i+1)+0.5f*(Physics.gravity.y-ball_drag*u_y)*Mathf.Pow ((i+1),2);
                 float z_pos = u_z*(i+1)-0.5f*(ball_drag*u_z)*Mathf.Pow ((i+1),2);    
 
                 line.SetPosition(i, new Vector3(x_pos, y_pos, z_pos));
                 }
 
             }
 
     }


I have included what I believe to be the correct deceleration due to drag, but this is irrelevant as the object i am considering has 0 drag.

Any help would be much appreciated!

Cheers,

Jack

Comment
Add comment · Show 3
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 siaran · Mar 24, 2015 at 03:11 PM 0
Share

is your kick_direction vector normalized?

avatar image popuppirate · Mar 24, 2015 at 04:26 PM 0
Share

Hi mate,

Yeah, the force is applied to a normalized vector. To clarify, the force on the ball works perfectly.

avatar image popuppirate · Mar 24, 2015 at 04:43 PM 0
Share

I just tried the solution here- exactly the same overshooting problem, which would suggest that I'm doing something wrong with the force...

http://answers.unity3d.com/questions/589797/simulating-a-trajectory-that-takes-collisions-into.html

1 Reply

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

Answer by Ericool · Mar 24, 2015 at 05:01 PM

when working with physic use vector instead of float there is already plenty of method and operator overload for you . Then you can simply * two vectors or call normalize() or simply calculate the sum of forces with + .

You should end up with something like that

  for( .. i ...)
 {
     Vector3 f_initial
     Vector3 f_current = CalculateInstantanateForce(f_initial , time);
     AddForce (f_current , i);
 
 Vector3 CalculateInstantanateForce(Vector3 initial , float time)
 {
    return (/*to do some calculus*/)
 }
 void AddForce(Vector3 force , int index)
 {
    vertexArray.Insert(index , force);
     
 
 }
 
 void Render()
 {
    foreach(.. vertexArray.Count ...)
    {
        //to do
     }
 }


This is simply a mock-up ..

Comment
Add comment · Show 1 · 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 popuppirate · Mar 25, 2015 at 09:04 AM 0
Share

It was much easier to apply a velocity to the ball rather than a force! Formulas now work perfectly. Thank you!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

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

Related Questions

Predicting A Trajectory 1 Answer

Can you pls help me fix this rigidbody collision problem 0 Answers

Ball Isn't following Trajectory line : Unity3d 1 Answer

How to predict orbit based on time? 1 Answer

Projected Orbital Path of 1 Object Orbiting Another.(Physics) 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