# How to calculate acceleration due to gravity in a 3D $N$-Body system?

How do you calculate acceleration due to gravity for objects in 3D space?

My current understanding for the force due to gravity on object $$i$$ from object $$j$$ is

$$\mathbf{F}_g=(\mathbf{r}_j-\mathbf{r}_i)m_jm_iG/|\mathbf{r}_j-\mathbf{r}_i|^3$$

where $$\mathbf{r}_i$$ and $$\mathbf{r}_j$$ are the 3D position vectors of object $$i$$ and object $$j$$.

Is this right? If not, what is?

Also, should $$|\mathbf{r}_j-\mathbf{r}_i|^3$$ be the same as $$[(x_j-x_i)^2+(y_j-y_i)^2+(z_j-z_i)^2]^{3/2}$$ or the vector $$[|x_j-x_i|,|y_j-y_i|, |z_j-z_i|]$$ cubed?

The net force on an object $$i$$ is equal to: $$\mathbf{F}_i=\sum_jGm_im_j\frac{\mathbf{r}_j-\mathbf{r}_i}{|\mathbf{r}_j-\mathbf{r}_i|^3}$$ That is, it is the sum of each individual gravitational force that the object experiences. This can be generalized to continuous mass distributions by an appropriate integral.
Here, the term in the denominator refers to the magnitude of the vector $$\mathbf{r}_j-\mathbf{r}_i$$, which corresponds to the first quantity you listed. It's there because gravity is an inverse-square force.