开发者

Planet's 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/


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜