[Math] Vector Rotation in 3D

3d

Given:

  • Two points: ($x_1$, $y_1$, $z_1$), ($x_2$, $y_2$, $z_2$)
  • A vector that is parallel to the $x$-axis and points to ascending numbers (intuitively stated, the vector points 'East').

I am hoping for a formula that tells me how many degrees I need to rotate the given vector around:

1) the x-axis
2) the y-axis
3) the z-axis

… in order to have the vector become parallel with the line that goes through the given points.

Best Answer

Define $u := \frac{1}{d} (x_2 - x_1, y_2 - y_1, z_2 - z_1)^T$, in which $d$ is the distance between your two points. This means, $u$ is the unit vector in the direction of the line segment. You are looking for a rotation that maps $e_x = (1, 0, 0)$ to $u$.

The solution is not unique, as there are (infinitely) many rotations that achieve this, but you can single out the one that goes the most "direct" route - which is the one rotating about the axis perpendicular to both $u$ and $e_x$.

If you define $v := u \times e_x$ and $w := v \times u$ (using the cross product), the rotation matrix $R = (u, v, w)$ achieves exactly this (here, the vectors are the columns of $R$, which is a 3x3 matrix).

This matrix allows you to perform the desired rotation, and if all you need to do is rotate stuff, you should stick with it. However, if you really need to extract the rotation angles about the individual axes, it gets a bit ugly, bu see for example here for a way to do it.