Quantcast
Viewing all articles
Browse latest Browse all 146

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][1] [1]: E:\Backjup+19+Nov\Pictures+(E)\Image1.jpg void DrawEllipse() { //The Line that will draw the orbit LineRenderer orbitLine = GetComponent (); 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 allPoints = new List (); 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.

Viewing all articles
Browse latest Browse all 146

Trending Articles