15
\$\begingroup\$

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

Similar to this game: http://sciencenetlinks.com/interactives/gravity.html

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.

Thanks in advance.

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

\$\endgroup\$

2 Answers 2

15
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ +1 Nice resource you linked, explaining the 4th order Runge-Kutta integration method and everything. \$\endgroup\$ Commented Dec 14, 2011 at 15:05
  • \$\begingroup\$ 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. \$\endgroup\$
    – baked
    Commented Dec 14, 2011 at 17:08
  • \$\begingroup\$ @baked: and increasing the planet's mass wouldn't work? \$\endgroup\$ Commented Dec 14, 2011 at 18:02
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ Commented Dec 14, 2011 at 20:19
  • \$\begingroup\$ If you have A and want B, how about reducing the initial velocity of your spaceship? \$\endgroup\$
    – Ben
    Commented May 4, 2012 at 15:05
4
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ This is actually quite similar to how I have been doing it currently. Maybe it's just a matter of playing with the constants? \$\endgroup\$
    – baked
    Commented Dec 14, 2011 at 15:29
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Commented Dec 14, 2011 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .