Planetary gravity

How would you simulate the influence of planets of different masses on a ship?

I am writing an asteroid-like flash game that has a small ship floating across a field of planets. The planets will not affect each other, but only on the ship.

Something like this Java simulator, but with big planets:

http://dan-ball.jp/en/javagame/planet/

+7
source share
1 answer

For a planet with mass m , at a distance r from the ship, the ship will experience acceleration:

a = k m / r ^ 2,

where k is some constant that depends on the units you use. Acceleration will be directed toward the planet. It would be convenient to split the acceleration into its components along the x and y axes (provided that you are working in two dimensions). If the planet is at an angle theta in the xy plane relative to the ship,

a x = a cos ( theta strong>)

a y = a sin ( theta strong>)

For multiple planets, you can simply add acceleration components by component.

If the ship has an initial speed v x at time t , then the speed at the next step t + delta_t :

v x + a x * delta_t

If this vessel is in the initial position p x at time t, then the position at t + delta_t will be:

p x + v x delta_t + a x delta_t ^ 2/2

See: Equations of motion

+6
source

All Articles