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 /
avatar image
0
Question by Petshop_Snake · Sep 07, 2018 at 09:42 AM · gravityspaceorbitline rendererrealistic

How can I make an efficient, yet accurate planetary orbit using Apoapsis and Periapsis distance from an object I am orbiting

I am trying to simulate an accurate 2D sim of our solar system. I figured if I have the Apoapsis & Periapsis Distances from the object I'm orbiting, and (I think) I know that these points are 180 deg apart, that I could just Mathf.Lerp the distance & get the points using distance & angle But this is what turns out. See below for Code

 void DrawEllipse()
     {
         //The Line that will draw the orbit
         LineRenderer orbitLine = GetComponent<LineRenderer> ();
         orbitLine.useWorldSpace = true;
         //the "Half ellipse" from Apoapsis to Periapsis
         pointsToPeriapsis = new Vector3[halfOfLineSegments];
         //The other half
         pointsToApoapsis = new Vector3[halfOfLineSegments];
         //getting the lerping number evenly spaced
         lerpPoint = 1.0d / halfOfLineSegments;
         //Setting the first postions at opposite points because both sides must be equal + half the time
         pointsToPeriapsis [0] = GetPositionfromDistanceAndAngle (apoapsis, angleOfApoapsis); //Vector2.right is zero deg
         pointsToApoapsis [0] = GetPositionfromDistanceAndAngle (periapsis, angleOfApoapsis + 180); 
         //repeat for each point in half of the array (other half is being done simutaniously
         for (int i = 1; i < halfOfLineSegments; i++)
         {
             //Lerp the distance from apo-peri & vice-versa using "lerpPoint"
             float aTOpDistance = Mathf.Lerp (float.Parse (apoapsis.ToString ()), float.Parse (periapsis.ToString ()), float.Parse (lerpPoint.ToString ()) * i);
             float pTOaDistance = Mathf.Lerp (float.Parse (periapsis.ToString ()), float.Parse (apoapsis.ToString ()), float.Parse (lerpPoint.ToString ()) * i);
             //Lerp through the angles
             float aTOpLerpAngle = Mathf.LerpAngle (0, 180, float.Parse (lerpPoint.ToString ()) * i) + angleOfApoapsis;
             //If the angle is more than a full circle
             if (aTOpLerpAngle >= 360)
             {    //Subtract a full circle
                 aTOpLerpAngle -= 360;
             }
             //repeat for othe side
             float pTOaLerpAngle = Mathf.LerpAngle (180, 360, float.Parse (lerpPoint.ToString ()) * i) + angleOfApoapsis;
             if (pTOaLerpAngle >= 360)
             {
                 pTOaLerpAngle -= 360;
                 if (pTOaLerpAngle >= 360)
                 {
                     pTOaLerpAngle -= 360;
                 }
             }
             //Calculate the next points
             pointsToPeriapsis [i] = GetPositionfromDistanceAndAngle (aTOpDistance, aTOpLerpAngle);
             pointsToApoapsis [i] = GetPositionfromDistanceAndAngle (pTOaDistance, pTOaLerpAngle);
         }
         // Apply everything to the LineRenderer
         orbitLine.positionCount = pointsToPeriapsis.Length + pointsToApoapsis.Length;
         List <Vector3> allPoints = new List<Vector3> ();
         allPoints.AddRange (pointsToPeriapsis);
         allPoints.AddRange (pointsToApoapsis);
         orbitLine.SetPositions (allPoints.ToArray ());
     }
 
     Vector3 GetPositionfromDistanceAndAngle(double distance, float angle)
     {
         //The Internet donated this, since I didn't pay attention in Trigonometry class
         float rad = float.Parse (angle.ToString ()) * Mathf.Deg2Rad;
         float x = float.Parse (distance.ToString ()) * Mathf.Cos (rad);
         float y = float.Parse (distance.ToString ()) * Mathf.Sin (rad);
         return new Vector3 (x + orbiting.transform.position.x, y + orbiting.transform.position.y);
     }

If someone can help me fix this and/or if you have any tips to improve. I know I can bring the points in the LineRenderer down, I just added more to see if it helps with my problem. Also, I made the orbit in the picture highly eccentric because the problem is much more noticeable this way. Thanks guys.

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

Answer by misher · Sep 07, 2018 at 10:52 AM

The easiest way would be using animation curves.

EDIT: it is difficult to describe, i will add unity package with the prefab so you can see how it is done. Basically you create an animation for your object and set 3 important keyframes and 2 auxiliary keyframes to regulate one axix deviation along the path: 1. Apoapsis point (in my case 0 seconds) 2. First deviation point (0,5s) 3. Periapsis Point (1 second) 4. Second deviation point (1,5s) 5. Again Apoapsis point (going back to where it has started) (2 seconds)

Now with help of deviation points you can "shift" one of the movement axis to the sides of the dtraight path, creating the proper orbit path, you can play around it till you obtain desired orbit shape. Of course it will never be not mathematically correct orbital movement it is just for simple visual effect. If you want to go with mathematically "right" solution, you should code all the space body physics and sample your orbit every frame all via code...


orbit-with-animation-curves.zip (5.7 kB)
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 Petshop_Snake · Sep 08, 2018 at 03:21 PM 0
Share

I hope this helps, cause I have no idea wat animation curves are, i am going to research it now, but if you can, will you please provide an example or something?

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

92 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

Related Questions

Natural rotation of orbiting object 3 Answers

Point gravity implementation lacks precision 2 Answers

calculate the future position of an object in orbit 1 Answer

Realistic gravity much too strong 1 Answer

Is there a Unity Package for this? 1 Answer


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