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?
 A: 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}$$
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.
