Rotation is a length preserving operation and you will be unsurprised to hear that unit vectors and their dot products are the key.

Figure 1


In rotation, matrices are just handy containers for the axis which represent our coordinate system, indeed one of the definitions for 'Matrix' is 'container'. In two dimensions a matrix will contain two rows, the first row is simply the x axis and the second the y axis. In Figure 1 you can see the matrix which represents the world space transform; this is known as the identity matrix:


We cannot start talking about rotation without first discussing the concept of spaces. In 2d, space has two possible directions; rightwards and upwards (and the negative versions of each). These two directions form the identity matrix we've just seen above, and also represent what I'm going to call the world space transform. World space is a completely un-rotated and un-translated space in which all objects are in our world are positioned. Everything that we draw in a simulation has to be drawn in the same space for it to make sense when we see it, therefore we often look for rotations that go into world space and other rotations which go back again. An object in our 2d world will have a rotation matrix which describes how to rotate from object space into world space.

At this point an analogy is appropriate: your house can be most conveniently described in coordinates relative to the earth, you could say your house's coordinates were in world space. However, your hand is most conveniently described in coordinates relative to you, because you are constantly moving around during the day; its coordinates would be in object space, where you are the 'object' in question.

If you draw the matrix of an object as it rotates it is easier to see what the numbers actually represent.

In the demo you can see the rotation matrix axis as you move the mouse along with the values for each axis contained within a matrix. R is the rightwards direction and top row of the matrix and U is the upwards direction and second row. This would be equivalent to you turning on the spot (seen from above), to continue the analogy.

In 2d a rotation matrix M is defined by two orthogonal unit vectors which describe the upwards and rightwards directions in 2d space.

The identity matrix in 2d is:

This simply says that to move rightwards one metre, you step 1 unit in x and 0 units in y. Similarly to go up one metre you step 0 units in x and 1 unit in y. Note that I'm going to be talking about rotation in this section rather than transformation since we are talking about Vectors rather than Points and the matrices we're using are orthonormal.

Two common rotations which you might want to do to a vector are: rotating it from world space into object space and the opposite, rotating from object space into world space.

Rotating a vector from world space into object space

V starts out in world space and we want to find its coordinates in object space of the matrix M (shown in red below).

Figure 2

Figure 2 shows the situation, vector V is in world space, but we need it in object space. Object space exists at a rotation of -28o. If we just took the V straight without modifying it and rendered it in our object we would get V' (shown in green, on the right), which is clearly not at the same orientation as V, which is incorrect. What we need is to compute the true value of V in object space.

What we need to do is to call on the things we learned about projection. Projection can help us by telling us how far along each of our object space axis the real V is.

or in matrix parlance:

Here we are projecting V onto our right and upwards vectors in order to find the new x and y components of V in the space of M.

Notice how V·R and V·U are always along the axis vectors? This is the key to this operation; by doing this, we now know the coordinates of V in terms of our axis vector which describe the orientation of the object which is all we need! Drag the mouse in the demo to rotate object space.

If you rotate the object space so R is pointing directly right V·R and V·U represent what we might think of as the x and y coordinates of V in world space.

Rotating a vector from object space into world space

The opposite operation, rotating a vector into world space from object space is a little easier. We have our object space axis R and U as before, and we have our vector V in object space.

Figure 3

Figure 3 shows the situation this time; again object space is at -28o and we need vector V in world space. If we just took the value of V and used it directly, we'd end up with V' (on the right). Again this is not at the same orientation as V, so is incorrect. The correct V (in blue) is at the same orientation in both spaces.

To find V in world space we simply multiply R by the x component of our vector and U by the y component and add them together.

or in matrix parlance:

This is saying: move V.x units along our R axis vector and then V.y units along our U axis vector.

There is no demo to show this because you can see the result above - just imagine V·R and V·U are our object space vector coordinates V.x and V.y which combine to form vector world space V.

The two principles above also apply to matrices in a logical fashion; just by treating the matrix to rotate as a set of vectors and operate one at a time as above, remember the rows of the matrix are the axis vectors.

You should notice that in these two operations the order of matrix times vector is exchanged, this enables us to perfrom the operation without needing to explictly transpose one of the matrices in question.