Radial velocity I need to calculate the radial velocity ($v_r$) of an object to another. For this I have the cartesian coordinates ($X_n$,$Y_n$,$Z_n$) and cartesian velocities, ($\dot{X_n}$,$\dot{Y_n}$,$\dot{Z_n}$) (with n = (1,2) depending on which object it is). The final result should be in three dimensions, but first I am trying to get this working in one and two dimensions:
For 1D it is simple: $v_r$=$\dot{X_1}-\dot{X_2}$. There is no angle between the two velocity vectors, and the velocity can be negative too.
For 2D it is somewhat more difficult, due to the angle between the objects. This angle $\theta$ is $\arctan{\frac{Y_1-Y_2}{X_1-Y_2}}$. One should also keep track in which quadrant it is, as $\arctan()$ only returns values between $-\pi$ and $\pi$ and the angle is obviously between $0$ and $2\pi$. I think the radial velocity is now best calculated in two steps: calculate the x and y components, and then add them by using Pythagoras. 
$$
{v_r}_X = (\dot{X_1}-\dot{X_2})\cos{\theta} \\
{v_r}_Y = (\dot{Y_1}-\dot{Y_2})\sin{\theta} 
$$
Is it correct so far? In the obvious cases ($\theta = \frac{k}{2}\pi$, with $k$ a positive integer) this is correct, but I am unsure about the non-obvious cases.
Then comes the 3D part. This part bothers me the most (unless I am wrong in the 1D or 2D part, then I just fail :P). Now there is another angle, $\phi$. $\theta$ is calculated in the same way as before because it haves no $Z$ component, and $\phi$ is calculated like this:
$$
\phi = \arcsin{(\frac{(Z_1-Z_2)}{\sqrt{(X_1-X_2)^2+(Y_1-Y_2)^2+(Z_1-Z_2)^2}})}
$$
This angle is always between $-\frac{1}{2}\pi$ and $\frac{1}{2}\pi$. Therefor one should look at $(Z_1-Z_2)$. If that is positive or negative, $\phi$ is positive or negative, respectively.
Very naively I am trying something like:
$$
{v_r}_X = (\dot{X_1}-\dot{X_2})\cos{\theta}\cos{\phi} \\
{v_r}_Y = (\dot{Y_1}-\dot{Y_2})\sin{\theta}\cos{\phi} \\
{v_r}_Z = (\dot{Z_1}-\dot{Z_2})\sin{\phi}
$$
After this it is adding with Pythagoras again. Is this somewhere in the neighborhood of correct? I tried again some obvious cases when the sine or cosine become 0 or 1, and then it seems correct. Help is very much appreciated!
 A: You are making it complicated. If the objects are at positions $\vec{r}_1$ and $\vec{r}_2$, and velocities $\vec{v}_1$ and $\vec{v}_2$ their relative velocity is $\vec{v}_2-\vec{v}_1$ and to get the radial velocity (if I understand correctly, the velocity in the direction of their connecting line), you just use dot product to project to this direction:
$$v_{radial}=(\vec{v}_2-\vec{v}_1)\cdot\frac{\vec{r}_2-\vec{r}_1}{|\vec{r}_2-\vec{r}_1|}$$
If you want the transverse velocity (tangential or whatever you call it), you can now get its magnitude from $|\vec{v}_2-\vec{v}_1|^2=v_{radial}^2+v_{tangential}^2$.

The dot product is of course $(x,y,z)\cdot(a,b,c)=ax+by+cz$. The absolute value introduces some square roots into the equation.

Edit: this is the magnitude of the velocity. If you need a vector, just multiply by the direction again:
$$\vec{v}_{radial}=\left((\vec{v}_2-\vec{v}_1)\cdot\frac{\vec{r}_2-\vec{r}_1}{|\vec{r}_2-\vec{r}_1|}\right)\frac{\vec{r}_2-\vec{r}_1}{|\vec{r}_2-\vec{r}_1|}$$
$$=\left((\vec{v}_2-\vec{v}_1)\cdot(\vec{r}_2-\vec{r}_1)\right)\frac{\vec{r}_2-\vec{r}_1}{|\vec{r}_2-\vec{r}_1|^2}$$
This also gets rid of the square root.
