# Two body problem with given start positions and velocities [duplicate]

I am trying to program a $n$-body problem simulation. To calculate the position and velocity after a time-step I want to split it in multiple 2 body problems. Now I am stuck, trying to find the velocity and position of two bodies (ignoring all the others) after one time-step with given mass, start position and velocity.

I am able to calculate the force between the two, but I am unable to solve the differential equation $$\ddot{\vec{r_{12}}(t)} = G M \frac{\vec{r_{12}(t)}}{|\vec{r_{12}(t)}|^3} \\ M := m_1 + m_2 \\ \vec{r_{12}(t)} := \vec{r_1}(t) - \vec{r_2}(t)$$

to calculate the position relative to each other. What is $\vec{r}(t)$ and how can I calculate it? And is there another and maybe faster way to get the positions and velocities?

Edit: As @Sofia pointed out, this was not clear: $\vec{r_1}$ and $\vec{r_2}$ are the location vectors of the two masses.

-

## marked as duplicate by Kyle Kanos, ACuriousMind, Qmechanic♦Feb 12 at 16:33

@Leon why $M$ is equal to the sum of the two masses? The law of gravitational attraction is $F_{12}(t) = G M_1 M_2 \frac {\vec {r_{12}(t)}}{|\vec {r_{12}(t)}|^3}$ –  Sofia Feb 12 at 13:48
If your time step is small enough, you can loop over all bodies and calculate the total acceleration on each of them (component-wise). Multiply with the time step to get the change of velocity and from that you get the change of location. That way, you implicitly have covered all 2-body problems. –  Aziraphale Feb 12 at 13:48
@Sofia This is what I got after transforming $\ddot{\vec{r_1}} - \ddot{\vec{r_2}} = \frac{Gm_2\vec{r_{12}}}{|\vec{r_{12}}|} - \frac{Gm_1\vec{r_{21}}}{|\vec{r_{12}}|}$. –  Leon Feb 12 at 14:08
@Sofia both $\vec{r_1}$ and $\vec{r_2}$ are the location vectors of the masses. Thank you for pointing out, that this was not clear. –  Leon Feb 12 at 15:25

Now I am stuck, trying to find the velocity and position of two bodies (ignoring all the others) after one time-step with given mass, start position and velocity.

You get the final velocity at impact $v$ with

$$v=\sqrt{\int_{r_1}^{r_2} \left(\frac{2 G M}{r^2}+\frac{v_0^2}{r_1-r_2}\right) \, \text{d}r}$$

Where $r_2$ is the initial distance and $r_1$ the final (so if you yould have the moon falling on the earth $r_2$ was the distance from center to center and $r_1$ the radius of the earth plus the radius of the moon.

If you want the velocities at a specific distance in free fall just replace your $r_1$ with the distance you need.

The time $t$ until mass A hits mass B is

$$t=\int_{r_1}^{r_2} \frac{1}{\sqrt{2 G (M_1+M_2) \left(\frac{1}{r}-\frac{1}{r_2}\right)+v_0^2}} \, \text{d}r$$

Those are the analytical solutions. If you want to solve it differentially you need to go numeric. In the following example you have 2 point masses separated by a distance of 6371 km, one the mass of the earth and the other the mass of the moon. They will meet in 889 sec and the heavy mass will move x = 77 km in the right, and the light one R-x in the left direction. If you want to solve for velocity or acceleration at time t just replace x1[t] by x2'[t] or x1''[t]:

I am trying to program a n-body problem simulation

You can also check out my 4 Body simulations at http://yukterez.ist.org/3kp/3k3D.html, http://yukterez.ist.org/4kp2 and http://bit.ly/1aMbgGH - the code is more or less self explaining.

-