# 2D Planet Gravity

I'm trying to make a simple game where a spaceship is launched and then its path is effected by the gravity of planets.

I wish to know how to replicate the effect the planets have on the spaceship in this game so a spaceship can 'loop' around a planet to change direction.

I have managed to achieve some bogus results where the spaceship loops in a huge ellipse around the planet or is only slightly affected by the gravity of a planet using Vectors.

p.s I have plenty of coding experience just none to do with game dev.

-

In your case there is an spaceship, which moves affected by the gravity of all the planets. Each planet applies force equal to 'm1*m2/ r^2' in which m1 and m2 represent masses of spaceship and planet, and r is equal to euclidean distance of planet and space ship. you just have to calculate sum of all the forces applied to ship. from the rules of newton we know 'f = m*a' where f is total force applied to an object, m is it's mass and a is it's acceleration. from this point you can read this page to learn how to use acceleration to compute velocity and position over time.

-
+1 Nice resource you linked, explaining the 4th order Runge-Kutta integration method and everything. – David Gouveia Dec 14 '11 at 15:05
My problem at the moment is the path the spaceship takes. I need it to be more 'game-like'. I've made a simple picture to show what I mean. i44.tinypic.com/2w2kdo4.jpg A is what I have now, B is what I want. – baked Dec 14 '11 at 17:08
@baked: and increasing the planet's mass wouldn't work? – Jesse Emond Dec 14 '11 at 18:02
In addition to the Gaffer on Games article you absolutely need to read this Codeflow article! Runge-Kutta does not look like your best bet here. – sam hocevar Dec 14 '11 at 20:19
If you have A and want B, how about reducing the initial velocity of your spaceship? – Ben May 4 '12 at 15:05

Each planet should have gravitational field that pulls any object in its vicinity towards its center. In other words, you should apply a force from the spaceship's position to the planet's center each step of the simulation.

This force depends on the mass of both objects, but in this case, since the mass of the spaceship is probably insignificant compared to the planet's, you can just try out different constant depending on the size of the planet until you get the feel right.

Most importantly, this force grows weaker the larger the distance between both objects. You will do this by dividing by the squared distance between both. After a certain distance you might want to "manually" cutoff and stop applying the force.

Just translate this force into an acceleration, and the basic physic equations should take care of the rest.

In pseudocode

``````force=0
for each planet
if distance < constant
force += mass * (planet.xy - ship.xy) / distance^2
apply force to ship
``````
-
This is actually quite similar to how I have been doing it currently. Maybe it's just a matter of playing with the constants? – baked Dec 14 '11 at 15:29
Not sure really but I'd say it's probably just tweaking now. And I think it also depends on the initial force you apply to the spaceship in order to get it into orbit. – David Gouveia Dec 14 '11 at 20:21