Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by YellowyCake · Oct 02, 2014 at 10:22 AM · rounding-errors

Object movement- rounding error?

I made a simple 2d game that makes two planets orbit each other. To each of the planets, I attached the following script:

 #pragma strict
 var velocity = Vector2(0,0);
 var target : Transform;
 var weight = 4000;
 private var acc = Vector2(0,0);
 private var join = Vector2(0,0);
 function Start() {
 }
 
 function FixedUpdate () {
     var pos = transform.position;
     join = target.transform.position - pos;
     acc = 10 / join.sqrMagnitude * join.normalized;
     velocity = velocity + acc;
     pos = transform.position + velocity;
     transform.position = pos;
 }

The two planets are each set to have an opposite initial velocity(0.4 and -0.4), along the vertical axis, and are otherwise identical. This should make a cyclic orbit; however, when I set them to trace their paths, I get the following result: asymmetric, colliding orbital paths of two planets

The planets eventually meet in the middle and fling off into space. Do to the asymmetry, I believe this is an error of some sort, and not simply incorrectly coded orbital mechanics. Does anyone know a method to solve this problem or a workaround?

roundingerr.png (28.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

1 Reply

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

Answer by NoseKills · Oct 02, 2014 at 04:25 PM

You have 2 instances of that script running in 2 different objects.

The FixedUpdate gets called on one of the planets first and that planet moves. Then it gets called on the other one. At this point, since the other planet has already moved, the join vector gets a different value for the second planet which leads to different acceleration, which leads to different speeds.

To fix this, you need to calculate the new position for both planets before moving either one (assuming the math is otherwise correct).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Having an action that occurs after a Quaternion slerp reaches goal 2 Answers

Mathf.CeilToInt I do not understand it ? 2 Answers

Unity/C# Division problem 2 Answers

Float output being rounded when unintended 1 Answer

How can I detect when a float value crosses an integer threshold? 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