2D orbital physics

2019-03-12 13:42发布

问题:

I'm working on a 2D physics engine for a game. I have gravity and masses working, using a simple iterative approach (that I know I'll have to upgrade eventually); I can push the masses around manually and watch them move and it all works as I'd expect.

Right now I'm trying to set up the game world in advance with a satellite in a simple circular orbit around a planet. To do this I need to calculate the initial velocity vector of the satellite given the mass of the planet and the desired distance out; this should be trivial, but I cannot for the life of me get it working right.

Standard physics textbooks tell me that the orbital velocity of an object in circular orbit around a mass M is:

v = sqrt( G * M / r )

However, after applying the appropriate vector the satellite isn't going anything like fast enough and falls in in a sharply elliptical orbit. Random tinkering shows that it's off by about a factor of 3 in one case.

My gravity simulation code is using the traditional:

F = G M m / r^2

G is set to 1 in my universe.

Can someone confirm to me that these equations do still hold in 2D space? I can't see any reason why not, but at this point I really want to know whether the problem is in my code or my assumptions...


Update: My physics engine works as follows:

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.

(Using vectors where appropriate, of course.)

Right now I'm doing one physics tick every frame, which is happening about 500-700 times per second. I'm aware that this will accumulate errors very quickly, but it should at least get me started.

(BTW, I was unable to find an off-the-shelf physics engine that handles orbital mechanics --- most 2D physics engines like Chipmunk and Box2D are more focused on rigid structures instead. Can anyone suggest one I could look at?)

回答1:

You need to make sure that your delta t iterative time value is small enough. You will definitely have to tinker with the constants in order to get the behaviour you expect. Iterative simulation in your case and most cases is a form of integration where errors build up fast and unpredictably.



回答2:

Yes, these equations hold in 2D space, because your 2D space is just a 2D representation of a 3D world. (A "real" 2D universe would have different equations, but that's not relevant here.)

A long shot: Are you perhaps using distance to the surface of the planet as r?

If that isn't it, try cutting your time step in half; if that makes a big difference, keep reducing it until the behavior stops changing.

If that makes no difference, try setting the initial velocity to zero, then watching it fall for a few iterations and measuring its acceleration to see if it's GM/r2. If the answer still isn't clear, post the results and we'll try to figure it out.