# 3D Planetary gravity

so i am making a thing in javascript and i want to have planetary gravity in my "universe".

But when i use an equation to calculate the acceleration by gravity i get one number. That is how fast the object accelerates in the direction of the other object, but how do i convert a speed in one direction to a speed in the X, Y and Z axis?

• Do you used Newton formula F=G*(M+m) / R^2 ? How do you calculated R ? Jan 2 '16 at 11:02
• I used Newton's law of universal gravity (F=GmM/r^2) and converted the newtons you get from the equation to distance. See how i calculated the distance here: mathsisfun.com/algebra/distance-2-points.html Jan 2 '16 at 11:08
• you need some vector math : mathsisfun.com/algebra/vectors.html . The direction from A to B is B-A (vector difference) normalized. So (B-A)/r where r is distance. Jan 2 '16 at 11:31

A vector simply stores the x, y, and z components independently. A unit vector is a vector of length one - you make a unit vector by taking your vector and dividing each component by it's length, the length is length = sqrt(x*x+y*y+z*z). You add vectors by adding the components independently. You multiply a vector by a constant like gravity by multiplying it's components independently.