Alright, I am writing a space simulator for a 3D game and I would like to implement gravity of objects into it.

Is there a nice way to find a velocity vector which can be added to my engine output vector to create the effect of gravity.

In addition, how would I be able to find the velocities required to get object A into orbit around object B at a certain distance?

  • $\begingroup$ Are the objects in of the same size as your spaceship, or is your spaceship significantly smaller? In the second case, you can simplify a lot. $\endgroup$ – Bernhard Jan 27 '12 at 13:37
  • 2
    $\begingroup$ Either you want real physics, in which case I believe that your questions have been answered on the site already, or you want some kind of game physics in which case you might be better on GameDev.SE. $\endgroup$ – dmckee --- ex-moderator kitten Jan 27 '12 at 18:13
  • 1
    $\begingroup$ Can't you use ready-made Physics Engines? $\endgroup$ – Schrödinger's Cat Mar 14 '14 at 2:11

Well, you either need to solve differential equations (probably not the best way here) or simulate numerically.

There is no way to give the velocity for an arbitrary constellation without knowing its past. So you need to keep track of the velocity of everything as well.

Basically, you need these equations/assignments:

Sum all the gravitational forces of every other mass in your world.

$a_i$ is the acceleration of the mass in question towards the $i$-th mass. $G$ is Newton's gravitational constant, $\approx 6.67 \cdot 10^{-11}$.

$$\vec{a}_i = - G \frac{M_i}{r_i^2} \cdot \hat{\vec{r}}_i = - G \frac{M_i}{r_i^3} \cdot \vec{r}_i$$

The total acceleration on the object is summed acceleration onto the object. You have to add vectorially here. If, for example, you have two opposing forces, the mass would not be accelerated at all.

$$\vec{a} = \sum_i \vec{a}_i$$

This is the acceleration onto the mass $m$ when attracted by another mass $M$. You have to multiply it with a chosen $\Delta t$ in order to get the change in velocity. The smaller you choose it, the more accurate your simulation will be, but it will take longer. You need to try different values for it.

$$\Delta \vec{v} = \vec{a} \cdot \Delta t$$ $$\vec{v} := \vec{v} + \Delta \vec{v}$$

And use that updated velocity to calculate the change of position.

$$\Delta \vec{x} = \vec{v} \cdot \Delta t$$ $$\vec{x} := \vec{x} + \Delta \vec{v}$$

$\vec{a}$, $\vec{v}$ and $\vec{x}$ are vectors in 3D space.

The basic idea is $\int a \mathrm dt = v + v_0$, $\int v \mathrm dt = d + d_0$, where $v_0$ and $d_0$ are the $+ C$ integration constants. In this context, they are the speed and distance that the object already has or traveled, respectively.

Stationary Orbit

To get such an orbit, you have to set the centripetal force equal to the gravitational force. Or you say that centrifugal force and gravity cancel each other out.

So either start with

$$- m \frac{v^2}{r} = -G \frac{mM}{r^2}$$


$$-m \frac{v^2}{r} + G \frac{mM}{r^2} = 0$$

$m$ is the mass of the satellite, $M$ the mass of the big object, i. e. planet. $G$ is the gravitational constant and $r$ is the distance between the two objects.

Either way, you will end up with

$$ v = \sqrt{G \frac{M}{r}}$$

As the tangential velocity for an Orbit with radius $r$ around an object with mass $M$. This assumes that the other object is way heavier than the satellite.

  • $\begingroup$ My brain might have just exploded, but that makes sense. Ugh, I'm a mathemtician, not a physicist. $\endgroup$ – Joseph Roberts Jan 27 '12 at 16:14
  • $\begingroup$ Basically, ai is the acceleration towards i, a is the acceleration towards everything? G is gravitational field strength? $\endgroup$ – Joseph Roberts Jan 27 '12 at 16:15
  • $\begingroup$ Multiline comments would really help right now...In your orbit example, I assume v is the velocity required to counteract the acceleration to gravity and therefore remain in a somewhat stable orbit? $\endgroup$ – Joseph Roberts Jan 27 '12 at 16:17
  • $\begingroup$ As a fun note, delta t is 3 seconds. My engine is really not designed for space simulation. $\endgroup$ – Joseph Roberts Jan 27 '12 at 16:18
  • $\begingroup$ Your assumtions are correct. The velocity $v$ is the one that you need to fall around the planet, instead of onto the planet. You fall onto the planet, but you are so fast, that you moved around the planet, therefore not hitting it. $\endgroup$ – Martin Ueding Jan 27 '12 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.