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

### Matrix

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:

### Spaces

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 shows the situation, vector *V* is in world space, but we need it in object space. Object space exists at a rotation of -28^{o}. 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* shows the situation this time; again object space is at -28^{o} 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.

Thanks very much for these useful tutorials.

Unless I’m completely misunderstanding something, your initial descriptions of mapping world->object space and object->world space are incorrect. They describe the green vector V’ as being incorrect, when in fact it is the blue vector V (on the right of both diagrams) that would be incorrect. V’ is the desired vector after performing the rotation.

Please confirm as I fear I may be missing something. Thanks again.

Hi jjc,

No problem

I think you are misunderstanding it slightly – the green V is the vector you would end up with if you just took the literal x and y values of the world-space V and used them in the object space coordinate system. This makes sense because the green V is exactly the original V rotated. What you want when doing this conversion is to end up with a V which is at a consistent orientation when rotated by the object space rotation matrix.

Hope that helps!

Cheers, Paul.

Ugggh, yes – got it now. Sorry, must have been too tired last night.

Thanks!

I’ve been dealing with dot products for a very long time now and

it’s so obvious that the matrix*vector operation is a bunch of dot products,

but I never though of thinking about it as just a simple projection until I read this article, truly awesome stuff!

I read quite a few books on 3d math and none of them mentioned that it is in fact this simple…well maybe they did but in that case it was buried under a lot of unnecessary math jargon.

Simple explanations with pictures is what math should be all about 😀

Keep it up!

Hi there,

Great! That’s just the kind of response I was hoping for When I first discovered rotation was all about projection I was surprised that I didn’t get taught it that way as well!

Cheers, Paul.

Something doesn’t seem clear to me.

In my code I have this:

vSep = rX * pointVX + rY * pointVY

vNorm = uX * pointVX + uY * pointVY

`newVX = vSep * rX + vNorm * uX`

newVY = vSep * rY + vNorm * uY

If your world-to-object and object-to-world formulas are correct, shouldn’t pointVX = newVX, and pointVY = newVY, since we’re just rotating it and then rotating it back?

For me, this isn’t the case (sometimes they nearly equal each other, sometimes they’re way off)

Hi Jared,

Using vector notation is often easier to express because there is less chance of typos:

Vobj.x = dot(Vworld, R);

Vobj.y = dot(Vworld, U);

Vworld = Vobj.x * R + Vobj.y * U;

If you expand that out, you should find the solution

Hope that helps!

Cheers, Paul.

Paul, thanks for the speedy reply!

Regardless of the notation, the formulas are the same, right? The math I put here in my original post was copy/pasted from my code.

So my question isn’t on the notation, but rather whether it’s correct or not. For some reason, when rotating pointV into R and U space, and changing it back into the world space, I’m getting different values from the original pointV.

R and U are unit vectors. R is perpendicular from U (so rX = uY and rY = uX). Is there something, aside from notation, wrong with my math?

Paul, hi again.

It seems the error was on my part. R and U wasn’t truly perpendicular: (uX SHOULD have been -rY, and uY = rX).

Thanks for the great article.

Hey, no problem – glad you got it sorted in the end

Yes! Thanks. I understand math + examples and simple explanations. It seems logical in my mind and I can reason about it.

Why is academic math all about memorizing formulas and not knowing what exactly is going on? Is it just me or does the academic system feel like a universal joke?

New technology affords a more intuitive explanation, especially with interactive demos. Academia is still very text book based which is probably why you get that impression

For getting V from object to world can’t we just project it to the world coordinates which is identity matrix?