- Home /
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 [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.
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...
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
Follow this Question
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