# Kepler orbit : get position on the orbit over time

I'm developing a space-simulation related game, and I am having some trouble implementing the movement of binary stars, like this:

The two stars orbit their centroid, and their trajectories are ellipses.

I basically know how to determine the angular velocity at any position, but not the angular velocity over time. So, for a given angle, I can very easily compute the stars position (cf. http://en.wikipedia.org/wiki/Orbit_equation).

I'd want to get the stars position over time. The parametric equations of the ellipse works but doesn't give the correct speed : { X(t) = a×cos(t) ; Y(t) = b×sin(t) }.

Is it possible, and how can it be done?

• It could be done by simulating gravity which is fairly simple (add a force dependent on the mass * sqrt( distance ) and do simple physics)
– Elva
Commented Apr 14, 2011 at 13:18
• No don't do simple physics! Orbits are numerically unstable and everything will implode or explode. Commented Apr 14, 2011 at 14:46

## 3 Answers

Following a few links from the Wikipedia page you reference leads to Position as a function of time.

• Thanks. Looks like I'll have to use the Newton method to solve the second equation. Commented Apr 15, 2011 at 6:52

You seem to have had enough data to produce the handy animation above. Your simulation may need more accuracy than provided by my solution:

For each frame of your animation above, record the pixel positions of the centers of each star. Enter these values into two arrays in your program. For a given time t, find the corresponding four consecutive entries in each array and do a bicubic filter on them to produce the position of each star.

• Actually, the animation is from Wikipedia. Commented Apr 15, 2011 at 6:42

I've found numerical integration to be the easiest way. The inverse square law (F=GM/r^2) works pretty well. And Runge Kutta order four often called RK4 is easy to implment and works quiet well. You start out by writing a routien which takes first order time derivative, for instance for a single object in 2D space, you have X and Y coordinates, and X and Y velocities. The output is the time derivative, the time derivative of of position is simply velocity, so half of it is just copying values, then the acceleration is just the gravational attraction. Then you follow the Runge Kutta prescription. The error of a single time step is proportional to the time step to the fifth power. You adjust the time step to make the result accurate enough. One advantage on numerical integration is if you want to play with the system to make it more interesting, add extra orbiting bodies, or add friction, or whatever, you alreday have the machinery.

• Well, yes, this is a solution, but my program is not a physics simulation in real time. It is discrete, and I cannot update everything everywhere. This is why the position as a function of time is nice : doesn't need computation, always exact no matter how frequently the positions are updated. Commented Apr 15, 2011 at 6:52