# Rotation matrix along a custom axis

For a certain software that I'm developing, I need to create a rotation matrix for a custom axis, and being almost completely self-taught in math, I am trying to wrap my mind around it, yet failing horribly. This is the simple rotation matrix for rotation along Z axis: $$\begin{pmatrix} \cos\theta& -\sin\theta& 0\\ \sin\theta& \cos\theta& 0\\ 0& 0& 1 \end{pmatrix}$$ Can someone explain how to I modify this matrix for use with a custom axis, which is offset from the Z axis by n degrees (and on the YZ plane)? I went at this problem for 2 days now, and I still can't solve it, no matter how many times I read the wiki article about rotation matrices... Attaching a picture to help you visualize the problem. Thank you in advance.

• en.wikipedia.org/wiki/Rodrigues%27_rotation_formula – Giuseppe Negro Sep 22 '18 at 17:01
• Thank you for that link, Giuseppe, but that stuff is way over my understanding. Wouldn't it be too much for me to ask how do I apply it to my described issue? – Justinas Rubinovas Sep 22 '18 at 17:41
• There's an explicit formula in the second section, just apply that, forget about the Lie group and matrix exponential stuff – Giuseppe Negro Sep 22 '18 at 17:42
• In the "Statement" section – Giuseppe Negro Sep 22 '18 at 17:43

In general, the result of rotating a vector $$\mathbf v$$ about an axis $$\mathbf k$$ (expressed as a unit vector) by an angle $$\theta$$ is given by the Rodrigues rotation formula: $$\mathbf v' = \mathbf v \cos\theta + (\mathbf k \times \mathbf v)\sin \theta + \mathbf k(\mathbf k \cdot \mathbf v)(1-\cos \theta).$$ In your case you have $$\mathbf k = (0, \sin n, \cos n)$$. To compute the rotation matrix, we can use the above formula to compute $$\mathbf v'$$ in the case of $$\mathbf v$$ being each of the standard basis vectors $$\mathbf e_1=(1,0,0)$$, $$\mathbf e_2=(0,1,0)$$, and $$\mathbf e_3 =(0,0,1)$$. Stacking these results side-by-side as column vectors will gives us the $$3 \times 3$$ rotation matrix. Explicitly,
$$\begin{bmatrix}\cos \theta &-\cos n\sin \theta &\sin n\sin \theta \\ \cos n\sin \theta & (1-\cos \theta)(\sin n)^2 + \cos\theta & (1-\cos \theta)\cos n\sin n \\ -\sin n\sin\theta & (1- \cos\theta)\cos n\sin n & (1-\cos\theta)(\cos n)^2 + \cos\theta \end{bmatrix}$$