How would you simulate the effect of planets of different mass on a ship?
I'm writing a Flash game similar to asteroids that has a small ship navigating through a field of planets. The planets won't exert force on each other but only on the ship.
Something like this Java simulation but with bigger planets:
http://dan-ball.jp/en/javagame/planet/
A planet's size and mass determines its gravitational pull. A planet's mass and size determines how strong its gravitational pull is. Models can help us experiment with the motions of objects in space, which are determined by the gravitational pull between them.
Each planet, moon and asteroid have their own gravitational pull defined by their density, size, mass, and proximity to other celestial bodies.
For a planet of mass m, at a distance r from the ship, the ship will experience an acceleration:
a = k m / r^2,
where k is some constant that depends on the units you're using. The acceleration will be directed toward the planet. It might be convenient to break down the acceleration into its components along the x and y axes (assuming you're working in 2 dimensions). If the planet is at an angle theta in the x-y plane, relative to the ship,
ax = a cos(theta)
ay = a sin(theta)
For multiple planets, you can just add the accelerations component-wise.
If the ship has an initial velocity vx at time t, then the velocity at the next time step t + delta_t would be:
vx + ax * delta_t
If this ship is at initial position px at time t, then the position at t + delta_t would be:
px + vxdelta_t + axdelta_t^2 / 2
See: Equations of motion
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With