Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Planet's gravity

Tags:

flash

gravity

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/

like image 462
Steven Carlborg Avatar asked Aug 12 '11 17:08

Steven Carlborg


People also ask

What determines a planet's gravity?

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.

Do planets have their own gravity?

Each planet, moon and asteroid have their own gravitational pull defined by their density, size, mass, and proximity to other celestial bodies.


1 Answers

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

like image 119
Jim Lewis Avatar answered Oct 21 '22 18:10

Jim Lewis