<p>I'm coding a game, and a fundamental mechanic is celestial bodies moving in elliptical, Keplerian orbits around a single gravitational point in a planetary system (not our solar system).  The bodies in this system all have their orbital elements and masses defined, and those orbital elements do not ever change throughout the game (except true anomaly, of course).</p>
<p>I'm trying to solve a problem, and I don't know how to approach it in a way that will work.</p>
<p>The problem is:</p>
<p>GIVEN (as function inputs)<br />
Orbiting mass &quot;m&quot; (kg, a planet)<br />
Orbited mass &quot;M&quot; (kg, a star)<br />
Semimajor axis (in AU)<br />
Eccentricity (0 &lt; e &lt; 1)<br />
Inclination (rads or degrees)<br />
Longitude of ascending node (rads or degrees)<br />
Argument of periapsis (rads or degrees)<br />
True anomaly (current position of planet in orbit)<br />
Delta Time (seconds, minutes, hours, days, months, etc)</p>
<p>OUTPUT: Position (in xyz coords with gravitational point at origin 0,0,0)
How can I find the position of the planet after time t?</p>
<p>THE DIFFICULTY IS...<br />
I already can calculate the distance vector and instantaneous velocity of the planet (thanks to <a href="http://orbitsimulator.com/formulas" rel="nofollow noreferrer">http://orbitsimulator.com/formulas</a> !!!), but that only tells me where the planet will be after 1 second and...<br />
delta time needs to be any value from 1 second to 100+ days!</p>
<p>The only solution I can think of involves running that &quot;update body position each second&quot; a bazillion times (ie if you want to see where it will be in 1 minute, run the &quot;find velocity of orbiting body&quot; function 60 times, updating the position each time), and then showing the final result, which isn't practical when you want to see where it will be in a month or more (and not practical when updating the positions of different bodies after different time deltas). Considering the velocity of the body changes in an elliptical orbit from moment to moment, how can this problem be solved efficiently for arbitrarily long time deltas?</p>
<p>I know Elite: Frontier got something like this working way back when, but I have no idea how...I've been reading up as much as I can find for the past month! ...mostly from <em>Orbital Mechanics for Engineering Students</em> by Howard Curtis, and I struggle considerably with understanding calculus equations.</p>
<p>Much appreciation for all you celestial mechanics savants and any insight or solutions you may have to share!</p>

CC BY-SA 4.0
2023-05-08T14:32:10.837Z
At the risk of stating the (hopefully) obvious: have you tried solving using Kepler's equation?  If so, why did that not work?
Greg Miller
CC BY-SA 4.0
2023-05-08T15:26:04.980Z
You're describing a software problem, not an orbital mechanics problem, and you seem to be overestimating the complexity. Your "bazillion" is less than 9 million which is a small number these days. Worst case you could precalculate millions of positions versus time and store them then just do a lookup. Can't imagine a game where time increments from seconds to hundreds of days would matter.
stretch
CC BY-SA 4.0
2023-05-08T18:04:49.993Z
You can easily invert Kepler's equation using Newton's method. It converges quickly, unless the eccentricity is very close to 1. Here's a short demo in Python.
PM 2Ring
CC BY-SA 4.0
2023-05-08T18:05:02.133Z
https://sagecell.sagemath.org/?z=eJxlkVFLwzAUhd8L_Q-H7cHWdXWtTNiw4EsFkQ7xxRdBsvZ2i1uTmmYTGfvvJpbSDu9DuJwkX-45GeNJHElpPFO9J3XVgL4OTHMpcOQMK_rWUhi1Ir2VheuM8aa41iSw_sFLhviViw3iWRyFs3kYR65TKlmhYnoLXtXSkHPZBGi4CFBz13Gdgkrs_l7zKA-Q-UvXgakUCTJMQDmu7Xkv803jRZ1kOEby28OlVPgEF1BMbMiLAiwWHchWYWlehqnBDpCp7-PGMqc9M-2Y7RCTxFzuhVpxob1yZPtTugyjeXnGqTDdLZ1Hg5u8BFs3XpH6uEdE08VgGltrRWzXSor0QQmkNo0HgyfFct0Go6nRJpZkFt4FWCXxLAA7aPlxqAumKXlk-4Y6nzYD3mewMkajYQY2gtiYrLlZuPG96vcys5d2KbTJ_Pf8Lk58GZ-XA-PZheeLb_wFALuS2g==&lang=python
PM 2Ring
CC BY-SA 4.0
2023-05-08T20:49:36.907Z
When you say that you know the "true anomaly" Does this mean you know the anomaly at a given (and fixed) time $t_0$ and you want help to find the anomaly at some other time $t$ ?  This is the classic "solve kepler's equation" problem.
James K
CC BY-SA 4.0
2023-05-09T08:41:23.033Z
@Greg Miller @ James K - Yes, it looks like Keplers equation should help with this :| I was glancingly concerned that it would require too many iterations and I have to run the final game on a mobile phone, but I think it might work out...I'm obviously not familiar with this subject deeply (and I get spun around easily) but I very much appreciate the direction you guys pointed me in!  I'll bang away at that and see if I can get it to work :)
w94n9
CC BY-SA 4.0
2023-05-09T08:41:27.410Z
@PM 2Ring thank you for the python code demo!  I think it'll really help accelerate implementing this core mechanic :)
w94n9
CC BY-SA 4.0
2023-05-09T09:34:11.940Z
See also "[How to get true anomaly from time?](https://space.stackexchange.com/q/23988)"
Sten
<positional-astronomy><orbital-elements><speed><modeling><eccentric-orbit>
Find position of orbiting body along orbit efficiently
w94n9
# Find position of orbiting body along orbit efficiently

(Asked by: username_0 on 2023-05-08)

I'm coding a game, and a fundamental mechanic is celestial bodies moving in elliptical, Keplerian orbits around a single gravitational point in a planetary system (not our solar system). The bodies in this system all have their orbital elements and masses defined, and those orbital elements do not ever change throughout the game (except true anomaly, of course).

I'm trying to solve a problem, and I don't know how to approach it in a way that will work.

The problem is:

GIVEN (as function inputs)  
Orbiting mass "m" (kg, a planet)  
Orbited mass "M" (kg, a star)  
Semimajor axis (in AU)  
Eccentricity (0 < e < 1)  
Inclination (rads or degrees)  
Longitude of ascending node (rads or degrees)  
Argument of periapsis (rads or degrees)  
True anomaly (current position of planet in orbit)  
Delta Time (seconds, minutes, hours, days, months, etc)

OUTPUT: Position (in xyz coords with gravitational point at origin 0,0,0)
How can I find the position of the planet after time t?

THE DIFFICULTY IS...  
I already can calculate the distance vector and instantaneous velocity of the planet (thanks to <http://orbitsimulator.com/formulas> !!!), but that only tells me where the planet will be after 1 second and...  
delta time needs to be any value from 1 second to 100+ days!

The only solution I can think of involves running that "update body position each second" a bazillion times (ie if you want to see where it will be in 1 minute, run the "find velocity of orbiting body" function 60 times, updating the position each time), and then showing the final result, which isn't practical when you want to see where it will be in a month or more (and not practical when updating the positions of different bodies after different time deltas). Considering the velocity of the body changes in an elliptical orbit from moment to moment, how can this problem be solved efficiently for arbitrarily long time deltas?

I know Elite: Frontier got something like this working way back when, but I have no idea how...I've been reading up as much as I can find for the past month! ...mostly from *Orbital Mechanics for Engineering Students* by Howard Curtis, and I struggle considerably with understanding calculus equations.

Much appreciation for all you celestial mechanics savants and any insight or solutions you may have to share!

**Tags:** positional-astronomy, orbital-elements, speed, modeling, eccentric-orbit
> **Comments:**
> **username_1**: At the risk of stating the (hopefully) obvious: have you tried solving using Kepler's equation?  If so, why did that not work?  
> **username_2**: You're describing a software problem, not an orbital mechanics problem, and you seem to be overestimating the complexity. Your "bazillion" is less than 9 million which is a small number these days. Worst case you could precalculate millions of positions versus time and store them then just do a lookup. Can't imagine a game where time increments from seconds to hundreds of days would matter.  
> **username_3**: You can easily invert Kepler's equation using Newton's method. It converges quickly, unless the eccentricity is very close to 1. Here's a short demo in Python.  
> **username_3**: https://sagecell.sagemath.org/?z=eJxlkVFLwzAUhd8L_Q-H7cHWdXWtTNiw4EsFkQ7xxRdBsvZ2i1uTmmYTGfvvJpbSDu9DuJwkX-45GeNJHElpPFO9J3XVgL4OTHMpcOQMK_rWUhi1Ir2VheuM8aa41iSw_sFLhviViw3iWRyFs3kYR65TKlmhYnoLXtXSkHPZBGi4CFBz13Gdgkrs_l7zKA-Q-UvXgakUCTJMQDmu7Xkv803jRZ1kOEby28OlVPgEF1BMbMiLAiwWHchWYWlehqnBDpCp7-PGMqc9M-2Y7RCTxFzuhVpxob1yZPtTugyjeXnGqTDdLZ1Hg5u8BFs3XpH6uEdE08VgGltrRWzXSor0QQmkNo0HgyfFct0Go6nRJpZkFt4FWCXxLAA7aPlxqAumKXlk-4Y6nzYD3mewMkajYQY2gtiYrLlZuPG96vcys5d2KbTJ_Pf8Lk58GZ-XA-PZheeLb_wFALuS2g==&lang=python  
> **username_4**: When you say that you know the "true anomaly" Does this mean you know the anomaly at a given (and fixed) time $t_0$ and you want help to find the anomaly at some other time $t$ ?  This is the classic "solve kepler's equation" problem.  
> **username_0**: @Greg Miller @ James K - Yes, it looks like Keplers equation should help with this :| I was glancingly concerned that it would require too many iterations and I have to run the final game on a mobile phone, but I think it might work out...I'm obviously not familiar with this subject deeply (and I get spun around easily) but I very much appreciate the direction you guys pointed me in!  I'll bang away at that and see if I can get it to work :)  
> **username_0**: @PM 2Ring thank you for the python code demo!  I think it'll really help accelerate implementing this core mechanic :)  
> **username_5**: See also "[How to get true anomaly from time?](https://space.stackexchange.com/q/23988)"  

---

## 0 answers

