Issues with gravity

I am currently working on a space simulation/game. Gravity is being calculated correctly and everything, I just have one problem…

As an object approaches the center of another object, the gravitational pull between the two approaches infinity (as it should). Since it is impossible to run a game at infinite fps, it is nearly impossible for two objects to meet at exactly at their centers due to their velocities. In other words, if an object is moving at 700km/s towards an object’s center, and it is only 7km away from its center, it passes the center by the next frame, and the gravitational pull has accelerated the object so much from the previous frame that the object shoots off into space.

I provided a .gif (http://www.2imgs.com/76b6d0fa29) to show you guys what is going on in case I did a poor job of describing it.
(The ship starts out as an orbital satellite, but I halt all of its movement to show you what goes on.)

I can’t think of any way I could possible fix this, does anyone have any ideas?

not sure what effect you want to have in the end but maybe a simple …

// when computing the force
distanceSquared = max( magsquared(ship - planet), planet_radius*planet_radius );

… could avoid massive acceleration.

Let the ship collide with the planet and explode? :o

Seriously, this kind of cornercases are inavoidable in simulations. For every kind of glitch that occurs due to limited accuracy, you need to include special cases in your calculations, that “hard-codes” a plausible outcome utilizing minimas, maximas and ranges covering the inaccuracies.

Basils answer is a good example. If it’s not what you assume to be a plausible outcome, implement something else in a similar fashion - or just let the ship explode :wink:

Have a Google for separating axis theorem and swept volumes.

http://www.metanetsoftware.com/technique/tutorialA.html

You can always “cheat” and simply place a limit on maximum speed. I believe practically all physics simulations do this to avoid problems like the one you mentioned.

If it is a rather simple 2d simulation, you could just move at certain steps:


float speed = 700000;
float step = 0;
while (step < speed) {
     move(1);
}