I IT developer, and I would code a small program that simulates the orbit of two bodies, the problem of two bodies eventually. I have no problem on the part programming but I have a little trouble with the calculations to be done to retrieve the position of the second body that is in orbit around the first. Could someone expliqer calculations me or give me a site with a concrete example, such calculations to do to have the orbit of the moon around the earth or the earth around the sun?

  • $\begingroup$ How about this? The only bit you really need is the one-line equation for (Newtonian) gravity between two objects, after that it's just modelling moving bodies. stackoverflow.com/questions/22068178/… $\endgroup$
    – Andy
    Mar 29, 2016 at 16:35
  • $\begingroup$ An easy way to implement this is the Leapfrog Integration. Have a look at the code in this answer. $\endgroup$
    – pela
    Mar 29, 2016 at 18:04

1 Answer 1


The equations of motion are just second order ordinary differential equations. They can be solved numerically by any of the usual methods, However, for two bodies an exact solution can be found, that solution was known to Kepler. To model the trajectory you need to know the orbital period and the eccentricty (e) of the orbit.

If you know the period of orbit of a body, then the "Mean anomaly (M)" is the angle time/period *2*pi radians (it increases uniformly from zero to 2pi in one orbital period.

The find the Eccentric anomaly ("E", the angle made by the body, the center of the elliptical orbit and the point of periapsis when the body is closest to the sun) you solve $M=E-e\sin(E)$ (it can be solved by newton's method quickly, though convergence is fastest for roughly circular orbits.

You then know the body is on a ellipse, with the sun at one focus and you have calculated the ray on which the body is found at a given time. This gives you the position of the body.

There is a rough implementation of this in a python gist


Not the answer you're looking for? Browse other questions tagged .