# 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

``````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  *= GetPositionfromDistanceAndAngle (aTOpDistance, aTOpLerpAngle);*
``````

_ pointsToApoapsis = GetPositionfromDistanceAndAngle (pTOaDistance, pTOaLerpAngle);_
* }*
* // Apply everything to the LineRenderer*
* orbitLine.positionCount = pointsToPeriapsis.Length + pointsToApoapsis.Length;*
* List allPoints = new List ();*
* orbitLine.SetPositions (allPoints.ToArray ());*
* }*

* Vector3 GetPositionfromDistanceAndAngle(double distance, float angle)*
* {*
* //The Internet donated this, since I didn’t pay attention in Trigonometry class*
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.

The easiest way would be using animation curves.

EDIT:
it is difficult to describe, i will add unity [124261-orbit-with-animation-curves.zip|124261] 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…