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 Generalfil · May 24, 2018 at 12:36 PM · unity 5scripting problemmath

Need help rendering Orbits, with Orbital Elements

Hi there!

Currently I'm working a space-related project, which includes the solar system. I have run into a bit of halt. I want to show the orbits of the major bodies in our solar system, but I do not need to simulate velocity or gravity or anything like that. I just want the path rendered out in the scene (im using Kerbal space programs "static" body orbits as inspiration).


I do have some ellipse rendering code here, but i really need some help or tips how to use orbital elements to show the orbits. Instead of x,y,z,angle, i want to use the 6 keplarian elements.

Any help is appreciated! :)


 public class OrbitHandler : MonoBehaviour {
 
     public Vector3 radius = new Vector3(1f, 1f, 1f);
     public float rotationAngle = 45;
     public int resolution = 500;
 
     private Vector3[] positions;
     private LineRenderer lr;
 
 
     void OnValidate()
     {
         UpdateEllipse();
     }
 
     public void UpdateEllipse()
     {
         if (lr == null)
             lr = GetComponent<LineRenderer>();
 
         lr.positionCount = resolution + 3;
 
         AddPointToLineRenderer(0f, 0);
         for (int i = 1; i <= resolution + 1; i++)
         {
             AddPointToLineRenderer((float)i / (float)(resolution) * 2.0f * Mathf.PI, i);
         }
         AddPointToLineRenderer(0f, resolution + 2);
     }
 
     void AddPointToLineRenderer(float angle, int index)
     {
         Quaternion pointQuaternion = Quaternion.AngleAxis(rotationAngle, Vector3.forward);
         Vector3 pointPosition;
 
         pointPosition = new Vector3(radius.x * Mathf.Cos(angle), radius.z * Mathf.Sin(angle), radius.y * Mathf.Sin(angle));
         pointPosition = pointQuaternion * pointPosition;
 
         lr.SetPosition(index, pointPosition);
     }
 }



Comment
Add comment · Show 2
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 elenzil · May 24, 2018 at 07:45 PM 2
Share

there's a moderately detailed discussion of converting keplerian orbital elements to unity cartesian coordinates here.

avatar image Generalfil elenzil · May 25, 2018 at 06:42 AM 0
Share

Ohh that looks good, thank you ill check it out!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Generalfil · Jun 01, 2018 at 02:12 PM

Hi there i have been able to get a code going but i cant figure out the correct true anomaly, which seems to be the main problem. If anyone can help i would be very thankful! Attaching my code

  public void UpdateEllipse()
     {
         lr.positionCount = resolution + 2;
 
         lr.SetPosition(0,AddPointToLineRenderer( 0));
         for (int i = 1; i <= resolution + 1; i++)
         {
             lr.SetPosition(i, AddPointToLineRenderer(i*Mathf.Deg2Rad));
         }
     }
 
     Vector3 AddPointToLineRenderer(float index)
     {
         // pointQuaternion = Quaternion.AngleAxis(rotationAngle, Vector3.forward);
         Vector3 pointPosition;
 
         pointPosition = KeplerToCarthesian(index * Mathf.Rad2Deg, semiMajorAxis,eccentrity,argumentofP,longOfAccNode, inclination);
 
         return pointPosition;        
     }
 
     /// <summary>
     /// Convert keplarian elements into vector3, needs (Mean Anomaly, Semi Majoris Axis, Eccentricity, Argument of Periapsis, Longitude of Ascending node, Inclination) 
     /// </summary>
     /// <returns></returns>
     Vector3 KeplerToCarthesian(double meanAnomaly , float a, float e, float w, float lan, float inc)
     {
         //Fix so inlication calculates properly
         inc *= Mathf.Deg2Rad;
 
         //Gets E and True Anomaly
         double E = GetEccentricAnomaly(meanAnomaly, e);
         double T = GetTrueAnomaly(e, E);
 
         double r = a * (1 - e * Math.Cos(E));
 
         double X = r * (Math.Cos(lan) * Math.Cos(w + T) - Math.Sin(lan) * Math.Sin(w + T) * Math.Cos(inc));
         double Y = r * (Math.Sin(lan) * Math.Cos(w + T) + Math.Cos(lan) * Math.Sin(w + T) * Math.Cos(inc));
         double Z = r * (Math.Sin(inc) * Math.Sin(w + T));
 
         return new Vector3((float)X *10, (float) Z*10, (float) Y*10);
     }
 
     private double GetTrueAnomaly(float e, double E)
     {
         int dp = 6;
         double S = Math.Sin(E);
 
         double C = Math.Cos(E);
 
         double fak = Math.Sqrt(1.0 - e * e);
 
         double phi = Math.Atan2(fak * S, C - e) / Mathf.Deg2Rad;
 
         return Math.Round(phi * Math.Pow(10, dp)) / Math.Pow(10, dp);
     }
 
     private double GetEccentricAnomaly(double meanAnomaly, float e)
     {
         //Solve kepler equation to get Ecentric anomaly
         int tolerance = 6;
         int maxIter = 30, i = 0;
         double delta = Math.Pow(10, -tolerance);
         double E, F;
 
         meanAnomaly /= 360.0f;
 
         meanAnomaly = 2.0 * Math.PI * (meanAnomaly - Math.Floor(meanAnomaly));
 
         if (e < 0.8)
             E = meanAnomaly;
         else
             E = Math.PI;
 
         F = E - e * Math.Sin(meanAnomaly) - meanAnomaly;
 
         while ((Math.Abs(F) > delta) && (i < maxIter))
         {
 
             E = E - F / (1.0 - e * Math.Cos(E));
             F = E - e * Math.Sin(E) - meanAnomaly;
             i++;
         }
 
         E /= (Math.PI / 180.0);
 
         return Math.Round(E * Math.Pow(10, tolerance)) / Math.Pow(10, tolerance);
     }
 }
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

227 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

Related Questions

How to build a car script that allows for car to turn in full circles 1 Answer

Clamp a Vector above minimum angle from another Vector? 0 Answers

"StateMachineBehaviour" Not working for sub sates ? 0 Answers

How can I change ApiCompatibilityLevel before Unity start? 0 Answers

How to add feature to Standard.Shader? 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