- #1

- 75

- 0

## Homework Statement

I am writing a program for a computer science class in which I am doing an n-body simulation in 3-dimensional space. Currently, I have figured out the gravitational force along the hypotenuse between two bodies. Now I have to split these up into the x, y, and z components, and this is where I am having trouble.

## Homework Equations

Force between two objects (force_t)=G*m1*m2/r^2

Typically, force_x=force_t*cos(angle between x-axis and hypotenuse)

force_y=force_t*sin(angle between x-axis and hypotenuse)

## The Attempt at a Solution

So, I can figure out my triangle. I know that my two bodies are located at (x1, y1, z1) and (x2, y2, z2), so I have my triangle. And I am fairly certain that the above equations will hold for x and y, even though I'm in three dimensions, but I'm just not sure what the corresponding equation for the z component will be. My thought was that the combination of the three forces has to equal the total force, so perhaps I solve for the z component algebraically using the pythagorean theorem, but I have no way to check whether or not this is true.

Anyway, any help would be appreciated. Thanks!