# How does orbital math work?

I'm writing a 2d space game, and I want the players to be able to 'orbit' objects and other players (if they're fast enough, of course), but I'm having trouble coming up with the right way to think about this physically (I never took physics, so this part is hard for me).

Note that this is not a 'realistic' space sim -- more like boats, really, so I don't want to do this by having the objects actually have enough gravity to force the orbit normally, because that'll make players that didn't press the 'orbit' button get sucked in and not be fun.

Also, do note that the objects players are trying to orbit may themselves be trying to orbit another object or even be mutual orbits.

I'm using the RK4 integrator right now, though if that needs to change, I'm totally up for it.

• Check my answer. Updated with specific solution. – Ramsay Smith Aug 28 '15 at 2:36

When the user presses the "orbit" button, store the vector between the ship and the planet. On every update, change the vector's angle leaving magnitude the same and then update the ships position by adding the planets position and the vector pointing at the ships new position. This will create the effect of the ship moving in a uniform circle around the object being orbited.

Also, it would be a nice touch if you programmed the ship to slowly turn until it was pointed 90 degrees to the planet before starting the uniform orbit, otherwise the ship could be going straight toward it then sharply turn into orbit when the user presses the button.

## More Detailed Physics

If you want more complicated things like true elliptical orbits and correct orbital speeds, you will need to do some research into real physics.

This question has already been asked: https://stackoverflow.com/questions/4038554/2d-orbital-physics; But for those that find this page, I will summarize the results and try to apply it to your situation and somewhat explain the physics.

His engine uses the following equation to calculate the force applied on both objects, where F is the force, G is the gravitational constant, m and M are the masses of the two objects, and finally r is the radius.

F = G M m / r^2


Remember that Newton's Third Law says that "all forces between two objects exist in equal magnitude and opposite direction", meaning that this equation is evaluated once and represents both forces present.

for each time step of length t:
reset cumulative forces on each object to 0.
for each unique pair of objects:
calculate force between them due to gravity.
accumulate force to the two objects.
for each object:
calculate velocity change dV for this timestep using Ft / m.
v = v + dV.
calculate position change dS using v * t.
s = s + dS.


Also, he discovered that he needed to normalize the direction vector before he applied it to the force.

• This is not actually an answer to my question as stated -- I'm trying to avoid complicating the simulation with gravity. – Silas Snider Aug 25 '15 at 17:12
• It's probably best to just implement the actual physics or you are going to find that your faked code is more complicated and less efficient than the real physics. You don't have to apply gravity throughout your whole game, just in the equation for orbits. – Fuzzy Logic Aug 25 '15 at 17:18
• +1 I find Ramsay answer coherent with the question "How does orbital math work?" – dnk drone.vs.drones Aug 28 '15 at 6:51