Skip to main content
gamedev.net
🔒 Locked

How do Rotation Matrices Work?

Started by Silvo Jan 11, 2007 at 12:41 AM 12 replies 7.5k views
Original Post
Silvo
Silvo
I have learned from various internet sources what the rotation matrices are and how to use them, but I have no idea why they are the way they are and why they work. Could you please tell me what the relevance of the trigonometric functions in the rotation matrices is, or could you link to an article that can? So far I have not found any explanation on why they are the way they are. Thank you.
Bulma
Bulma
I believe there is a ton of info on the net, like:
http://en.wikipedia.org/wiki/Coordinate_rotation

It is 2D, but all 3 basic rotational matrices (by X, by Y and by Z axis) are just that, rotation in 2D plane (X rotation is rotation in YZ plane, etc...)
Bulma
Silvo
Silvo
Thank you for the link; however, I am afraid that the information is a little above me at the moment. It also does not explain why the matrix is

[cos x, -sin x]
[sin x, cos x ]

and not something else, say
[sin x, -cos x]
[cos x, sin x ]

or any other combination (used x's as I am not bothered to find a theta). The trouble I am having is that I have no idea why those values are those values, nor do I have any idea about why they are in those positions.

Could someone please enlighten me?
Bulma
Bulma
Ah yes, it is not really explained there...
[cos x, -sin x]
[sin x, cos x ]

Every point can be described with something like:
P=x*i + y*j

i and j are vectors that make original coordinate system. So, point [2,3] in fact means that you have 2 units in direction of vector i, and 3 units in direction of vector j. Now, what rotation matrix do is make new pair of i and j vectors, that are rotated by some amount, lets call them i' and j'.
So, new point will be:
P'=x*i' + y*j'

Let say that original system had vector i defined as:
i = [1 0]*[i j]
and j is defined as:
j = [0 1]*[i j]

you actually need to change those vectors for some angle, lets call it 'fi'
Image Hosted by ImageShack.us

So, new rotated vectors i' and j' will be:
i' = [cos(fi) sin(fi)]*[i j]
j' = [-sin(fi) cos(fi)]*[i j]

and your transformed point will be:
P'=x*[cos(fi) sin(fi)]*[i j] + y*[-sin(fi) cos(fi)]*[i j]

and from that line you can extract your rotation matrix:
x' = x*cos(fi) - y*sin(fi)
y' = x*sin(fi) + Y*cos(fi)

R= [cos(fi) -sin(fi)]
[sin(fi) cos(fi)]

[Edited by - Bulma on January 11, 2007 5:06:18 AM]
Bulma
TheAdmiral
TheAdmiral
Points for originality with 'Fi' [rolleyes].
Did you mean Phi?

There are dozens of ways to envision a rotation transformation, but none of them are entirely straightforward.

Before you try to determine the significance of the trig functions in a rotation matrix, first be sure you understand how matrix multiplication works. A matrix can represent a linear transformation between two coordinate systems. It simply transforms each coordinate to a linear combination of the original coordinates. In this case, the factors are trigonometric functions of a parameter, θ.

Now sin(θ), cos(θ) are the magnitudes of a horizontal unit line, after rotation by θ, when projected onto the x and y axes (note that x² + y² = 1 = sin(θ)² + cos(θ)² for any θ). This is clear from the paradigm examples of elementary trigonometry with unit-hypotenuse right-angled triangles.

Another way of visualising the same thing is to imagine an object on the edge of a turntable. Looking directly from the top, this object traces out a circle as the table rotates. Now if you move your viewpoint so you look at the turntable directly from the side, the object will appear to move from side to side (but not back to front), tracing out a sine curve in time. If somebody else was looking at it from the side in the same way, but at an angle of 90° (in the plane of rotation) from you, they would see it tracing out a cosine curve in simultaneity with your sine curve.
Here, you and your friend's lines of sight are the x and y axes, the object is a point on a unit circle, and the angle θ of rotation is increasing linearly with time. In other words, cos(θ) and sin(θ) are the projected coordinates of a point θ radians around the unit circle. i.e. (cos(θ), sin(θ)) is the position of (1, 0) when rotated about (0, 0) by an angle θ.

Once the unit circle situation is out of the way, the rest is easy. Scaling (i.e. non unit-circle) corresponds to multiplication and translation (i.e. non origin-centric) corresponds to addition.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Silvo
Silvo
Bulma:
Quote:
Original post by Bulma
Ah yes, it is not really explained there...
[cos x, -sin x]
[sin x, cos x ]

Every point can be described with something like:
P=x*i + y*j


So, if I had a point (2,3), would x=2 and y=3 and i would be the x-axis and j the y-axis?

If so, then i=[1 0] and j=[0 1]. then
P = 2*[1 0] + 3*[0 1]

and if I am correct:
P = (2*1 + 2*0) + (3*0 + 3*1)
P = 2 + 3

out of curiosity, does that mean that P is a vector and:
P = [2 3]
???


Quote:

i and j are vectors that make original coordinate system. So, point [2,3] in fact means that you have 2 units in direction of vector i, and 3 units in direction of vector j. Now, what rotation matrix do is make new pair of i and j vectors, that are rotated by some amount, lets call them i' and j'.
So, new point will be:
P'=x*i' + y*j'


That makes sense.

Quote:

Let say that original system had vector i defined as:
i = [1 0]*[i j]
and j is defined as:
j = [0 1]*[i j]


Hold it right there... i = [1 0]*[i j]... doesn't that mean that
i = [1 0]*[ ( [1 0]*[i j] ) j]
which means that
i = [1 0]*[ ( [1 0]*[ ( [1 0]*[i j] ) j] ) j]

and so on?

Just realised something:
i = [1 0]*[i j]
i = 1*i + 0*j
i = i + 0
or maybe
i = [i 0]

which doesn't help.

So at this point I start to lose you. What is i and what is j?

Quote:

you actually need to change those vectors for some angle, lets call it 'fi'
Image Hosted by ImageShack.us


The red dots are the original i and j vectors on the axes, and the yellow dots are the rotated vectors. The green lines join to the yellow dots and are the new axes. They also form right-angled triangles with the original axes.

You then proceed to say that the two sides are cos(fi) * i and sin(fi) * j. How do you know that? Of the triangle, you have all three angles, but you know no side lengths. cos(fi) * i would imply that i is the hypotenuse. Of cause! i is the length!!! That means that you have the two sides of the triangle, which are the coordinates of the yellow dot, which is the vector on the new axis! Huzzah!

so, that explains that the coordinates of the new vector for the x-axis are [cos(fi)*i sin(fi)*j], where i and j are the lengths of the vectors (1).

and the y-axis' new vector is [-sin(fi)*i cos(fi)*j].

Horray!

Quote:

So, new rotated vectors i' and j' will be:
i' = [cos(fi) sin(fi)]*[i j]
j' = [-sin(fi) cos(fi)]*[i j]


right, so i and j are the positions of the old vector, and i' and j' are the positions of the new vector.

So, if you started with a normal set of axes: [1 0] and [0 1], then
i' = [cos(fi) sin(fi)]*[1 0]

so is it:
i' = 1*cos(fi) + 0*sin(fi)
i' = cos(fi)
or:
i' = [1*cos(fi) 0*sin(fi)]
i' = [cos(fi) 0]
?

Something is wrong. I have been treating i and j as the coordinates of the previous axis' vector; however, there are two vectors.

So, again I ask, what are i and j?


Quote:

and your transformed point will be:
P'=x*[cos(fi) sin(fi)]*[i j] + y*[-sin(fi) cos(fi)]*[i j]


I am afraid that I do not understand this. This stems from P = x*i + y*j, doesn't it? SO what is the result of that? is it a scalar or a vector? What are i and j?


Quote:

and from that line you can extract your rotation matrix:
x' = x*cos(fi) - y*sin(fi)
y' = x*sin(fi) + Y*cos(fi)

R= [cos(fi) -sin(fi)]
[sin(fi) cos(fi)]


I actually understand that bit, but there are still some things I don't understand. Thank you very much for your explanation, Bulma. I now understand why the trig function are what they are and where they are, but that has unleashed some more problems, namely, what are i and j, and how do I work out those multiplications later on? I think that I am stuffing up the vector maths here, so if you could clear up those few things, that would be excellent.

TheAdmiral:

Well, Fi or Phi, does it really matter? I would have been content with theta...

Quote:
Before you try to determine the significance of the trig functions in a rotation matrix, first be sure you understand how matrix multiplication works. A matrix can represent a linear transformation between two coordinate systems. It simply transforms each coordinate to a linear combination of the original coordinates. In this case, the factors are trigonometric functions of a parameter, θ.


Right, I understand that bit.

Quote:
Now sin(θ), cos(θ) are the magnitudes of a horizontal unit line, after rotation by θ, when projected onto the x and y axes (note that x² + y² = 1 = sin(θ)² + cos(θ)² for any θ). This is clear from the paradigm examples of elementary trigonometry with unit-hypotenuse right-angled triangles.


so, what you are saying is that after rotation by θ (you found the symbol!), sin(θ) and cos(θ) are used to find the magnitude (that means length, right?) of the new line. Or perhaps you mean the rotated position. I'm not entirely sure...

Quote:
Another way of visualising the same thing is to imagine an object on the edge of a turntable. Looking directly from the top, this object traces out a circle as the table rotates. Now if you move your viewpoint so you look at the turntable directly from the side, the object will appear to move from side to side (but not back to front), tracing out a sine curve in time. If somebody else was looking at it from the side in the same way, but at an angle of 90° (in the plane of rotation) from you, they would see it tracing out a cosine curve in simultaneity with your sine curve.
Here, you and your friend's lines of sight are the x and y axes, the object is a point on a unit circle, and the angle θ of rotation is increasing linearly with time. In other words, cos(θ) and sin(θ) are the projected coordinates of a point θ radians around the unit circle. i.e. (cos(θ), sin(θ)) is the position of (1, 0) when rotated about (0, 0) by an angle θ.


A clever way of thinking about those waves... I never thought of it like that. Your final statement solves a lot of problems, but it doesn't explain why that is so. No I have worked that out, and it makes sense.

Quote:
Once the unit circle situation is out of the way, the rest is easy. Scaling (i.e. non unit-circle) corresponds to multiplication and translation (i.e. non origin-centric) corresponds to addition.


Those are the easy ones!

Anonymous Poster:
Quote:
Realize that a Matrix is nothing more than a shorthand notation system.
There isn't a specific 'intuition' to why the rows and colums have whatever numbers placed in them, you really have to write out the longhand notation - the actual equations that the Matrix represents, to see Why they did it that way and why it makes a rotation...


Unfortunately for me it is not that easy. Looking at it written out makes it worse, if possible.

Thank you all for you help.
Palidine
Palidine
The short and easy answer at this point is "learn Linear Algebra". It's a whole field of mathematics and it would seem that you just haven't studied it yet. Trying to compress the whole of the field into an internet forum is going to be very difficult.

The best bet might be to go to a library or a bookstore (depending on your financial state) and get a Linear Algebra textbook and start working your way through it.

-me
Silvo
Silvo
Jack:

Thanks for the link. It shows a nice way to visualise matrices, and the shrink/stretch thing at the end is neat too. Unfortunately, it doesn't really explain how those values fit in with the big picture.

Palidine:

It's true that I have not learned linear algebra (can you tell?) but I was hoping I wouldn't have to get a book on it. I'm starting grade 11 maths this year, and I think some time next year we get into it. THe thing is, I don't believe that I really need a superior knowledge of linear algebra to the kind of things I want to do (rotations), so I was hoping I could get a bit of help here to understand it. I am going to read into it more now.
Bulma
Bulma
Silvo:

Well, I am sure that my explaining isn`t perfect, and my pictures are even worse.. [embarrass], so let me try to fix that a bit...
i and j are indeed vectors. I run into one problem (and I think that is source of your confusion) that in order to express vectors i and j, I used the same vectors. It would be better if I entered one new set of vectors, like for example A and B, and said:
i = [1 0][A B] = 1*A + 0*B
j = [0 1][A B] = 0*A + 1*B

and later:
i' = [cos(fi) sin(fi)]*[A B] = cos(fi)*A + sin(fi)*B
j' = [-sin(fi) cos(fi)]*[A B]= -sin(fi)*A + cos(fi)*B

Both i and j, and i' and j', and A and B are unit vectors (lenght of one), that is why those sin/cos functions are valid. cos(fi)*i is a vector, lenght is relly just a cos(fi). There was a circle of unit lenght, but it got lost in the picture export somewhere. Both yellow and red dots are on those imaginary circle.

Does that clears thing a bit?

TheAdmiral:
Yeah, it should be Phi, but, 'fi' is shorter[grin], and as you can see, english is not my native language anyway.
Bulma
Silvo
Silvo
Well, it doesn't matter if English is your native language or not, as Phi is Greek!

Thank you so much, that clears things up a lot!!!

i and j are the vectors of the old axes, i' and j' are the vectors of the new axes after rotation, and a and b are the real x and y axes??

Have I got it correct??
TheAdmiral
TheAdmiral
Off topic:
Quote:
Original post by Bulma
Yeah, it should be Phi, but, 'fi' is shorter[grin], and as you can see, english is not my native language anyway.

Of course, and that's just fine. I didn't mean any offense - please don't take my attempt at humour personally [smile].

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.