Planet's gravity
Asked Answered
Y

1

7

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/

Yellowbird answered 12/8, 2011 at 17:2 Comment(2)
Are you looking for the ship to be able to orbit the planets?Avowal
Yes, but also be affected by the mass of the planet.Yellowbird
V
6

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 + vx delta_t + ax delta_t^2 / 2

See: Equations of motion

Valles answered 12/8, 2011 at 17:11 Comment(3)
Actually, wait, couldn't we in addition to your solution, use F = mv²/r? Plugging in ma for F and solving for v. This is of course assuming your solution doesn't account for orbit.Avowal
F = mv^2 / r is only for uniform circular motion. For arbitrary motion in a gravitational field, it doesn't make any sense.Durative
@MGZero: With the proper initial conditions, units, choice of k, and sufficiently small time step, the above equations are all you need to simulate orbits.Valles

© 2022 - 2024 — McMap. All rights reserved.