Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by fernancp444 · Feb 09, 2020 at 11:15 AM · physicsgravity

On integrating a physical trajectory before simulation

Hi!

I have super simple project with only two game objects, both of them spheres with rigidbodies. One of them is fixed and the other can move, it starts at a certain distance from the fixed one and with an initial velocity, and in the update it has a force towards the other object (at this point I might just say, think of two planets orbiting).

The force update is as follows

 Vector3 gravVect = sun.GetComponent<Rigidbody>().position-rigidBody.position;
 float distance = gravVect.magnitude;
 float mass = rigidBody.mass;
 gravVect.Normalize();
 rigidBody.AddForce(mass*gravVect/(distance*distance));

This is in the smaller planet script.

Now this works fine, and if a put an Effects/Trail as a child to this last object, it properly displays a fixed ellipse.

The problem is that I want the code to calculate the trajectory and display it as a line before the object moves. There is an Effects/Line as a child of this game object, and this same script does this calculation with the following code, which is just a Euler step integration of the equation of motion:

 void DrawLine()
     {
         Vector3 currentSpeed = rigidBody.velocity;
         Vector3 intSpeed = currentSpeed;
         Vector3 currentPosit = rigidBody.position;
         Vector3 intPosit = currentPosit;
         Vector3 gravVect = sun.GetComponent<Rigidbody>().position-currentSpeed;
         float distance = gravVect.magnitude;
         float dT = Time.fixedDeltaTime;
         for (int i = 0; i < pointsInteg; i++)
         {
             intSpeed = currentSpeed + dT*gravVect/(distance*distance);
             intPosit = currentPosit + dT*currentSpeed;
             polozhenya[i] = intPosit-rigidBody.position;
             currentSpeed = intSpeed;
             currentPosit = intPosit;
             gravVect = sun.GetComponent<Rigidbody>().position-currentPosit;
             distance = gravVect.magnitude;
         }
         lineTraj.SetPositions(polozhenya);
     }

The code is pretty much self contained, the for loop just calculates the next N positions. So polozhenya is the array of points, and it calculates a hundred or so and then puts them as points of the line object lineTraj

My main question is if I´m using the right timestep. According to the documentation the fixedDeltaTime is the time the physics engine uses for updates, so I thought it was right. I´m getting something like this:

The blue line is the proper trajectory the smaller object is following and the red one is the integrated one, which is closer to the real one in smaller orbits

The blue line is the proper trajectory the smaller object is following and the red one is the integrated one, which is closer to the real one in smaller orbits

So what do you guys think is going on? I thought by artificially making the timestep much smaller and got something like in the following picture, so I don't think that's the problem

alt text

sin-titulo.png (104.9 kB)
sin-titulo.png (105.3 kB)
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

276 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 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 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 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 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 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 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 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 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 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 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 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

How to make GameObject fall with the same gravity while using AddForce() 1 Answer

Charcter physics and groundedness 0 Answers

Confused about how AddForce works 1 Answer

left/right movement is slow when below ground level 0 Answers

How to make a player instantly reach to a change in gravity? 2 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