3D rotation matrix around vector

I am using matrix for performing 3D rotations. I know that in 3D space the matrix product order is important - changing the order of the matrices can effect the rotate result.

So I am interesting about how can I create a rotate matrix that perform rotation (clockwise) around some vector, say $(1, 0, 1)$.

I thought that $R_{(1,0,1)}(\theta) = R_X(\theta) \cdot R_Z(\theta)$ but it seems wrong because if I tries to rotate the $(1, 0, 1)$ with $45^{\circ}$ it moves out from $(1, 0, 1)$ but it should stay in place.

So how can I build such a rotation matrix as product of 3D trasformation matrices?

2 Answers

There is a direct formula for the rotation matrix for an arbitrary axis and angle. Given a unit vector $a = (a_x, a_y, a_z)$ and angle $\theta$, the matrix can be constructed as follows (derivation from Wikipedia):

First build a matrix $C$ from the components of $a$ according to the following formula:

$$C = \begin{bmatrix} 0 & -a_z & a_y \\ a_z & 0 & -a_x \\ -a_y & a_x & 0 \end{bmatrix}$$

Then you can construct the rotation matrix from $C$ as follows:

$$R_a(\theta) = I + C \sin \theta + C^2 (1 - \cos \theta)$$

where $I$ is the 3×3 identity matrix. This assumes you're using a column-vector convention; if you're using row vectors instead, transpose $C$.

By the way, this formula has a neat geometric interpretation. The matrix $C$ has the effect of calculating cross products with $a$. In other words, for any vector $v$, $Cv = a \times v$. If you imagine rotating some vector $v$ about $a$, the tip of $v$ traces out a circle perpendicular to $a$; $Cv$ is a vector tangent to that circle, and $C^2 v$ (which equals $a \times (a \times v)$) is a vector normal to the circle. So with those two vectors, you have a basis for the plane of the circle; then you can apply a variation of the usual sine/cosine formula for points on a circle.

• And the neat geometric interpretation is what I used. Personally I prefer to build the matrixes with a few number of atomic operations because then I only need to remember 3 rules of construction. May 7, 2016 at 6:05

Build a matrix with one of the vectors a=(1,0,1) now you need another vector to define the matrix vector say b=(1, 0, 0) take the cross product of a and b lets call this c. Then take the cross product of a and c for a rectified b'. Normalize a, b' and c.

Your matrix T is now [a b' c] plus any affine part you deem neccesery. Then do T-1 * Rx(Angle) * T. where Rx(a) is a function that returns a rotation matrix around the x axis with a angle of a.

• This is a quick answer over the phone, spelling corrections and tex formulas welcome. May 6, 2016 at 20:15