# Simulating Gravity in 3D Game?

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?

-
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. –  Bernhard Jan 27 '12 at 13:37
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. –  dmckee Jan 27 '12 at 18:13
Can't you use ready-made Physics Engines? –  Sachin Shekhar Mar 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.

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

or

$$-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.

-
My brain might have just exploded, but that makes sense. Ugh, I'm a mathemtician, not a physicist. –  Joseph Roberts Jan 27 '12 at 16:14
Basically, ai is the acceleration towards i, a is the acceleration towards everything? G is gravitational field strength? –  Joseph Roberts Jan 27 '12 at 16:15
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? –  Joseph Roberts Jan 27 '12 at 16:17
As a fun note, delta t is 3 seconds. My engine is really not designed for space simulation. –  Joseph Roberts Jan 27 '12 at 16:18
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. –  queueoverflow Jan 27 '12 at 18:08