# How to calculate the time to apoapsis & periapsis, given the orbital elements?

I have the 6 orbital elements (excluding Time of Periapsis Passage, obviously), 2 state vectors, and a lot of other parameters like orbital period. I am looking to use them to calculate the time to periapsis and time to apoapsis.

To give you a better idea of the values I already have, here is my code calculating all the data related to the orbit:

 void CalculateOrbitalData(Vector3 pos1, Vector3 vel1, float m1) {
Vector3 pos2 = planet.position;
Vector3 vel2 = planet.velocity;
float m2 = planet.mass;

//Relative Position Vector
Vector3 r = pos1 - pos2;

//Distance between bodies
float rmag = r.magnitude;

//Relative Velocity Vector
Vector3 v = vel1 - vel2;

//Specific Angular Momentum
Vector3 h = Vector3.Cross(r, v);

//Standard Gravitational Parameter
float µ = World.G * m2;

//Eccentricity Vector
Vector3 evec = (Vector3.Cross(v, h) / µ) - (r / Vector3.Magnitude(r));

//Eccentricity
float e = Vector3.Magnitude(evec);

//Vector to Ascending Node
Vector3 n = new Vector3(-h.x, h.z, 0);

//True Anomaly
float t = Mathf.Acos((Vector3.Dot(evec, r)) / (e * Vector3.Magnitude(r)));
if (Vector3.Dot(r, v) < 0)
t = (2 * Mathf.PI) - t;

//Longitude of Ascending Node (2D)
float Ω = 0;

//Inclination (2D)
float i = 0;

//Argument of Periapsis
float ω = Mathf.Atan2(evec.y, evec.x);
float ωdegrees = ω * (180 / Mathf.PI);
if (e == 0) {
ω = 0;
ωdegrees = 0;
}

//Eccentric Anomaly
float E = 2 * Mathf.Atan(Mathf.Tan(t / 2) / Mathf.Sqrt((1 + e) / (1 - e)));

//Mean Anomaly
float M = E - (e * Mathf.Sin(E));

//Semi-Major Axis
float a = 1 / ((2 / Vector3.Magnitude(r)) - (Mathf.Pow(Vector3.Magnitude(v), 2) / µ));

//Apoapsis
float ap = a * (1 + e);

//Periapsis
float pe = a * (1 - e);

//Orbital Period
float T = (2 * Mathf.PI) * Mathf.Sqrt(Mathf.Pow(a, 3) / µ) / 60;
//patch orbital period to stay in line with unity timestep
float fT = T * 1.205f;

//Mean motion
float m = (2 * Mathf.PI) / fT;

//Perifocal distance
float rp = (Vector3.Dot(h, h) / µ) / e + 1;
}

• Time to periapsis and time to apoapsis from when? A delta time needs something to delta from. Jun 29, 2016 at 19:13
• Can we do current time at current position? Jun 29, 2016 at 19:14
• May be more meaningful representation with Mean anomaly May 30, 2017 at 16:07

You seem to be looking for the time equation:

$$t=\sqrt{a^3\over\mu}\left(\tau-e\sin\tau\right)$$

where $t$ is time and $\tau$ is the eccentric anomaly. It looks like you have the eccentric anomaly at the "current" time. You can just plug that in to see the time relative to periapsis, which is at $t=0$ and $\tau=0$. For the time at apoapsis, plug in $\tau=\pi$. An orbit period is $\tau$ running over a range of $2\pi$, so the period is:

$$T=2\pi\sqrt{a^3\over\mu}$$

So your time since periapsis is $t$, and your time to the next periapsis is $T-t$ (assuming that $\tau$ is in $0$ to $2\pi$).

• This looks good however I'm guessing my $\tau$ is definitely not in $0$ to $2\pi$, or at least not calculated properly as I get a value of -0.109 when I first start the sim, as shown here Jun 29, 2016 at 21:14
• My E is from $-\pi$ to $\pi$, so I'm just adding $\pi$ to it, will touch back when I get it working Jun 29, 2016 at 21:36
• You'd need to add $2\pi$ to it if it is negative. Jun 30, 2016 at 15:32