# How to correctly create planetary orbits from kepler elements

I would like to build a gravity model (with 3D graphics) that would build the orbit of the planets based on orbital elements input and the gravity calculation for each element.

My initial code is live here (Click the 0.005 to turn it into 0.05 and then click on restart time to run the orbits).

The example above has all planets on Solar System (up to Neptune), the Sun and the asteroid Apophis. For clarity, the Sun and planets other than earth have been hidden, but are still considered for gravity calculations.

However, the orbits do not match the correct estimate intersection date. In this article, it says that Earth-Apophis close-encounters will happen on 13 April (meaning that the earth should be in the orbit's intersection point every 13 April, or very close to it).

The data from the program is taken from Nasa JPL.

Start date is 2454441.5, as given in JPL Apophis data. Planets orbital elements have been corrected to this date, with the calculations in the above PDF.

Why the orbits intersection date is wrong? Should I be doing something else? Am I doing something wrong?

The above Apophis link also cites a "Standard Dynamical Model". If anyone knows how to correctly create one, I believe this would answer my question as well.

TLDR: How can I correctly create planetary orbits from kepler elements?

Thank you in advance if anyone can help me!

• Answer to a deleted comment: I'm converting keplerian orbital elements into cartesian coordinates and then solving it numerically (n^2 total gravity calculations, n = number of bodies). What I meant by the date is that Earth should be at the intersect position on April 13 every year. Apophis position only matters on 2029 and 2036 like you said. – hawaii Aug 15 '16 at 15:43
• I don't know how to do that, but for all 8 planets and Apophis, the calculation is enormously complicated. Once you have 3 bodies, simple equations no longer work and you need to run constant corrections. There's no longer a simple formula. (look up 3 body problem). I don't think what you ask can be programmed without a PHD in perturbation theory and pages and pages of code and perhaps a supercomputer. – userLTK Aug 15 '16 at 18:41
• @userLTK No, straight (numerical) integration of the system should be accurate enough Eg Verlet integration. I'd guess there is some mistake in the initialisation of the system. – James K Aug 15 '16 at 19:32
• Thanks @JamesK I didn't know that. I remember the 3 body problem being unsolvable when I studied math, but I'm guessing there's work-arounds with a stable system. – userLTK Aug 16 '16 at 11:02

• The approximate elements for the planets are approximately correct. Earth may have wildly different $\Omega$ and $\omega$ due to near-zero $I$, but the longitude of perihelion should be about the same. Good luck! – Mike G Aug 17 '16 at 13:45