-1

So I'm trying to write a 2D space game in Java, and I'm trying to work out gravity for planets. What would the steps be in pseudo-code for this to happen?

Kyranstar
  • 1,650
  • 2
  • 14
  • 35
  • I'm no pro at this, but easiest would be to use a library that already does this for you, and I *think* that they do exist, although I'm not sure as I've never done this sort of thing. – Hovercraft Full Of Eels Jan 06 '14 at 04:25
  • can you please explain: what are those objects? - gravity only affects EACH OTHER when both masses are similar (an apple doesn't force the earth to fall towards it, when falling from a tree) – Martin Frank Jan 06 '14 at 05:29
  • 4
    sorry - the apple DOES force the earth to fall towards the apple - but it's nearly not measurable... please forgive – Martin Frank Jan 06 '14 at 05:30

1 Answers1

4

Relevant characteristics of each object: location, speed and direction of travel (tracking this as x-speed and y-speed may be easier), mass. It may of course have other attributes such as orientation which don't affect this. Or even attributes which do, such as its own rocket thrust, but let's start with the simple case and it should be obvious how to expand upon it.

At each animation step, 

  For each object,
    Calculate the gravitational force of every other object, based on our mass,
      their mass, and direction from us to them. 
    Sum those forces together to get the total x and y forces on this object
    Using F=ma, determine the acceleration applied to this object by this force
      (how much its speed changes in the x and y directions).

  For each object
    Use its x and y speeds to update its location.

Lather. Rinse. Repeat.

The smaller the steps you can take per pass, the more accurate this simulation will be... but of course it will run more slowly. And note that the computational cost goes as N-squared.

Note the process of calculating the gravitational force involves a bit of trig to figure out how much of the force is applied on the X axis and how much on the Y axis. The original Space War game ran on a PDP-1 at MIT. That machine didn't have enough power to calculate the trig functions in realtime even for its simple 2-body system (planet and ship). So instead what they did was precalculate two 2D lookup tables, one for each of the axes, which let them simply lookup the gravitational force on the ship at any point in the playing field.

Also, as Space War pointed out, it may be massive overkill to try to account for all the gravitational forces. The effect of one planet on another in our solar system is pretty minimal compared to the sun's gravity unless you're looking at millennial time scales. The effect of ships on each other, or on planets, is also tiny. You can account for it all, but it may be much more efficient to use simpler modelling for everything but ship navigation -- and for that, to consider only the nearest planet/moon and maybe the sun.

keshlam
  • 7,931
  • 2
  • 19
  • 33
  • Your solution will fail given that calculations will be done in discrete time increments while true physics operates continuously. Better to use numeric methods to get quick calculation estimates that use higher order terms of the Taylor series. – Hovercraft Full Of Eels Jan 06 '14 at 05:04
  • The request was for "easiest" and "2d game". I submit that my suggestion has been repeatedly demonstrated to be more than adequate for that purpose. But hey, if you can provide a better alternative that's great; post pseudocode for your suggestion as a competing answer. – keshlam Jan 06 '14 at 05:23
  • 1
    NOTE: See also the "Related" questions at right. It looks like this has been discussed previously – keshlam Jan 06 '14 at 05:48