# Problem calculating rotation matrix around arbitrary axis

I'm trying to calculate the rotating matrix around the Z axis in a counter clockwise direction.

In-order to do that I've decided on a vector A [1 0 0] and I wish to rotate it around the z axis so it'll end up as At [0 1 0] (90 degrees rotation around the z axis)

Let's consider our matrix R. Now, what I've done is set At coefficients to the product of the matrix R by the vector A as follows:

At.x = A.x * R00 + A.y * R10 + A.z * R20
At.y = A.x * R01 + A.y * R11 + A.z * R21
At.z = A.x * R02 + A.y * R12 + A.z * R22


Now, replacing the A and At coefficients we get:

0 = 1 * R00 + 0 * R10 + 0 * R20
1 = 1 * R01 + 0 * R11 + 0 * R21
0 = 1 * R02 + 0 * R12 + 0 * R22


Next, to make these equations right I've deducted the following:

R00 must be 0
R01 must be 1
R02 must be 0
others R coefficients are irrelevant to me


Creating the matrix so far:

0     1     0
0     0     0
0     0     0


Since we rotated around the z axis by 90 degrees we can say the following about the earlier deduction of the matrix coefficients:

R00 = cos(θ)
R01 = sin(θ)
R02 = cos(θ)


And finally, applying this to the matrix:

cos(θ)     sin(θ)     cos(θ)
0          0          0
0          0          0


After building my matrix, I've tested it here: Matrix Calculation Online Website

With the following same vector A [1 0 0] and matrix R I ended up with And got the desired Result of At [0 1 0]

So, summing it all the process I've done, my question is why for every resource I looked for the answer the rotating around the z axis matrix should look like this: Z axis rotation matrix

What have I done wrong / missed calculating to get the same matrix ?

• You should test some different cases (maybe you have). It is very easy to get the right result accidentally. – Carser Jul 30 '16 at 16:06
• @Carser I suppose you're right, but that would only make me realize that the way I found this matrix is wrong I wish to know how to be able to compute it – Jorayen Jul 30 '16 at 16:12
• Well it seems you've designed your matrix specifically for the case of $90^\circ$, whereas the rotation matrix you link to is the more general case for any angle $\theta$. Basically, you're case is the specific instance for the general matrix where $\theta = 90^\circ$. – Carser Jul 30 '16 at 16:13
• @Carser I see, could you tell me on how could I design it for any value of θ – Jorayen Jul 30 '16 at 16:17
• As a quick sanity check of your solution, remember that the columns of a transformation matrix are the images of the basis vectors. The matrix you’ve ended up with collapses everything onto the $x$-axis! This can’t possibly be right, since, among other things, the $z$-axis shouldn’t be affected at all by this rotation. – amd Jul 30 '16 at 21:02

It seems like you've designed a set of equations specifically for the case of $\theta=90^\circ$. If you want to derive the more general rotation matrix about axis $z$, then you have to allow for any rotation $\theta$.
Consider rotating some vector like your vector $\begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix}$ or slightly more generally $\begin{bmatrix} r \\ 0 \\ 0 \end{bmatrix}$ around the $z$ axis by an arbitrary angle $\theta$.
The position of the head of the new vector can be calculated using trigonometry. It's really the same mathematics as polar coordinates where here you're rotating a radius of $r$.
So the new $x$ and $y$ coordinates of the vector head position can be found with $$x = r \cos(\theta)$$ $$y = r \sin(\theta)$$ Writing this with matrices gives $$\begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} \cos(\theta) & 0 \\ 0 & \sin(\theta) \end{bmatrix} \begin{bmatrix} r \\ 0 \end{bmatrix}$$ This gives a slightly more general formula but requires the vector start parallel to the $x$ axis. We can generalize further by considering rotating any vector $\begin{bmatrix} v_x \\ v_y \\ v_z \end{bmatrix}$ in 3D about the $z$ axis. Using the same trig we can deduce that the new $x$ position of the vector head is $$x = v_x \cos(\theta) - v_y\sin(\theta)$$ the new $y$ position is $$y = v_x \sin(\theta) + v_y\cos(\theta)$$ and since we're rotating around $z$ the $v_z$ value does not change $$z = v_z$$ which writing with matrices gives you the rotation matrix you'll recognize $$\begin{bmatrix} x \\ y \\ z \end{bmatrix} = \begin{bmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} v_x \\ v_y \\ v_z \end{bmatrix}$$