# What is wrong with my orbit sim equations? How can I fix them?

Edit: Partial question: The object now moves in an ellipse like it's supposed to, but it orbits around a different point than I intend. How can I make it orbit around a particular object at a point?

New Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Orrery2 : MonoBehaviour
{
public Transform Center;
public Transform Other;
float x;
float y;
float z;
float Begining = 1;
float Elapsed;
float dE = 0;

// Update is called once per frame
void FixedUpdate()
{
Elapsed = Begining++;
PositionFromDate(100f, 0.8f, 1f, 2f, 2f, 2f);
Debug.Log(Elapsed);
Debug.Log("x:" + x + ", " + "y:" + y + ", " + "z:" + z);
Other.transform.position = new Vector3(x, y, z);
}
void PositionFromDate(float a, float e, float i, float W, float w, float M)
{
M = M * Elapsed;

//Newton's Method
float E = M;
while (Mathf.Abs(dE) < 1e-3)
{
dE = (E - e * Mathf.Sin(E) - M) / (1 - e * Mathf.Cos(E));
E -= dE;
Debug.Log("E:" + E + ", " + "e:" + e + ", " + "M:" + M + ", " + "dE:" + dE);
}

//Determine P and Q, 2d coordinate system in plane of orbit
float P = a * (Mathf.Cos(E) - e);
float Q = a * Mathf.Sin(E) * Mathf.Sqrt(Mathf.Abs(1 - Mathf.Pow(e, 2)));

Debug.Log("Line 46 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

// rotate by argument of periapsis
x = Mathf.Cos(w) * P - Mathf.Sin(w) * Q;
y = Mathf.Sin(w) * P + Mathf.Cos(w) * Q;

Debug.Log("Line 52 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

// rotate by inclination
z = Mathf.Sin(i) * x;
x = Mathf.Cos(i) * x;

Debug.Log("Line 58 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

// rotate by longitude of ascending node
float xTemp = x;
x = Mathf.Cos(W) * xTemp - Mathf.Sin(W) * y;
y = Mathf.Sin(W) * xTemp + Mathf.Cos(W) * y;

Debug.Log("Line 65 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
}
}


This is in c# in Unity. The intent of the script is to move a sprite around a 2-body orbit. Instead it pops around erratically and rapidly speeds off into the distance. I based most of this off of this other question, but I removed a lot of the conversions from the jpl database in the beginning of the script because I intend to use generated numbers on a larger scale. I don't know much about these maths, so I can't tell what is wrong.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Orrery2 : MonoBehaviour
{
public Transform Center;
public Transform Other;
float x;
float y;
float z;
float Begining = 1;
float Elapsed;
float dE = 0;
//a: Semi-Major Axis
//e: Eccentricity
//i: Inclination
//W: Longitude of the Ascending Node
//w: Argument of Periapsis
//M: Mean Anomaly
//E: Eccentric Anomaly

void FixedUpdate()
{
Elapsed = Begining++;
PositionFromDate(1f, 0.8f, 10f, 2f, 2f, 2f);
Debug.Log(Elapsed);
Debug.Log("x:" + x + ", " + "y:" + y + ", " + "z:" + z);
Other.transform.position = new Vector3(x, y, z);
}
void PositionFromDate(float a, float e, float i, float W, float w, float M)
{
a = a * Elapsed; //Semi-Major Axis
e = e * Elapsed; //Eccentricity
i = i * Elapsed; //Inclination
W = W * Elapsed; //Longitude of the Ascending Node

//Newton's Method
float E = M;
while (Mathf.Abs(dE) < 1e-3)
{
dE = (E - e * Mathf.Sin(E) - M) / (1 - e * Mathf.Cos(E));
E -= dE;
Debug.Log("E:" + E + ", " + "e:" + e + ", " + "M:" + M + ", " + "dE:" + dE);
}

//Determine P and Q, 2d coordinate system in plane of orbit
float P = a * (Mathf.Cos(E) - e);
float Q = a * Mathf.Sin(E) * Mathf.Sqrt(Mathf.Abs(1 - Mathf.Pow(e, 2)));

Debug.Log("Line 46 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

// rotate by argument of periapsis
x = Mathf.Cos(w) * P - Mathf.Sin(w) * Q;
y = Mathf.Sin(w) * P + Mathf.Cos(w) * Q;

Debug.Log("Line 52 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

// rotate by inclination
z = Mathf.Sin(i) * x;
x = Mathf.Cos(i) * x;

Debug.Log("Line 58 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);

// rotate by longitude of ascending node
float xTemp = x;
x = Mathf.Cos(W) * xTemp - Mathf.Sin(W) * y;
y = Mathf.Sin(W) * xTemp + Mathf.Cos(W) * y;

Debug.Log("Line 65 " + "a:" + a + ", " + "e:" + e + ", " + "i:" + i + "W:" + w + ", " + "w:" + w + ", " + "M:" + M + ", " + "E:" + E + ", " + "P:" + P + ", " + "Q:" + Q);
}
}

• One: you're specifying angles in degrees (inclination = 10, for example) but Mathf trig functions work in radians. Two: SMA, eccentricity, inclination, and LAN should be invariant, but you're multiplying them by elapsed time. – Russell Borogove Jan 20 at 19:13
• And Three: The one thing that should be changing with time, mean anomaly, is not. – David Hammen Jan 20 at 19:20
• I also recommend using real-world, SI units with appropriately large magnitudes throughout all your space!math and converting them to unity-convenient units only when setting the new transform. – Russell Borogove Jan 20 at 19:31
• Thank you, your first three points worked. I will work on the last one too. The object does move in an ellipse now, but the ellipse is not centered on the right place. How Should I make sure that it centers on the object it is orbiting? This is what it looks like now. – user29010 Jan 20 at 19:36
• making the time value any smaller than one makes the object not move at all. Instead I will make the distances bigger to smooth it out. – user29010 Jan 21 at 0:08