ID:1914279
 
(See the best response by Lummox JR.)
I'm working on a raycasting engine, and I have a question about rotation matrices.

I guess this is more of a math question, but can someone explain to me a bit about why/how rotation matrices work? I understand that if you take a vector,

[2]
[2]

and multiply it by the matrix :

[cos(a) sin(a)]
[-sin(a) cos(a)]

...the vector should rotate clockwise by angle a.

What I'm not sure of is how that works exactly. Maybe it has to do with my poor understanding of Sin, Cos, and Tan. I thought those were just ratios of the triangle?

I guess i'm just not sure how (VectorX * cos(a) + VectorY * sin(a)) would give me my new X position. I've looked at a couple generic matrix tutorials and none of them seem to directly explain this. I'm kind of looking for a layman's explanation of rotation because i'm not much of a math guy.
It's shown on the first page of this (I googled "2D rotation matrix derivation"):
https://engineering.purdue.edu/~bethel/rot2.pdf

"Inspection" refers to looking at the geometry of the figure. There are a lot of triangles involved when you rotate axes.
Best response
In Cartesian coordinates (y gets higher as you go up), the rotation formula--leaving matrices out of it for now--for clockwise rotation is this:

x' = x * cos(a) + y * sin(a)
y' = -x * sin(a) + y * cos(a)

In DM, the matrix version looks like this:

        a d 0
x y 1 × b e 0 = x' y' 1
c f 1

The corresponding matrix in DM would be matrix(cos(a),sin(a),0,-sin(a),cos(a),0).

If you want to understand how rotation works, the best answer is that it's based on the angle addition identities:

sin(A + B) = sin(A) * cos(B) + cos(A) * sin(B)
cos(A + B) = cos(A) * cos(B) - sin(A) * sin(B)

So imagine for a second that x and y coordinates are as follows:

x = r * cos(A)
y = r * sin(A)

And now say you want to rotate clockwise by B. By Cartesian conventions, angles actually go counter-clockwise, so you'd actually want to subtract B from the angle instead of adding it:

x' = r * cos(A - B)
y' = r * sin(A - B)

So far so good. Now substitute the angle addition identities.

x' = r * (cos(A) * cos(-B) - sin(A) * sin(-B))
= r * (cos(A) * cos(B) + sin(A) * sin(B))
= r * cos(A) * cos(B) + r * sin(A) * sin(B)
= x * cos(B) + y * sin(B)

y' = r * (sin(A) * cos(-B) + cos(A) * sin(-B))
= r * (sin(A) * cos(B) - cos(A) * sin(B))
= r * sin(A) * cos(B) - r * cos(A) * sin(B)
= y * cos(B) - x * sin(B)

The last stage is to back-substitute x and y in place of r*cos(A) and r*sin(A). All that's left are x, y, and the sine and cosine of B (the rotation angle).
Thanks, I think I understand it better.

So multiplying your current vector(x,y) by the ratios(sin and cos) of the angle you want to transform by is what drives the rotation.

I kind of drew up an example of how I see it working. Just by changing the ratio sin of the vector (2,2) I can see it moving along the graph, albeit in a straight line.


In your example though I was a bit confused as to what r represents when it comes to a simple vector. Is that magnitude? Or would the X value of a vector just be equal to cos(A)?
It's the magnitude (distance from the origin of the plane) of the point (x,y) when represented in polar coordinates. Basically any coordinate (x,y) can be represented as:

x = rcos(t)
y = rsin(t)

Where t is the angle (x,y)(0,0)(1,0), and r is the distance of (x,y) from (0,0).
In my example the r value was the radius, or distance from the center. Basically I put x and y into polar coordinates (expressed as radius and angle instead of x and y).

So for instance, position (4,3) has a radius of 5, and the angle is roughly 37°.

Your graph isn't really accurate because the coordinate change isn't linear. If you start with position (2,2), the radius is 2*sqrt(2) and the angle is 45°.

x = 2*sqrt(2) * cos(45)
y = 2*sqrt(2) * sin(45)

The sine and cosine of 45° are both 1/sqrt(2). Now consider rotating 45° clockwise.

x' = x * cos(a) + y * sin(a)
y' = -x * sin(a) + y * cos(a)

x' = 2 * cos(45) + 2 * sin(45)
= 2 * (1/sqrt(2)) + 2 * (1/sqrt(2))
= 4 / sqrt(2)
= 2 * sqrt(2)

y' = -2 * sin(45) + 2 * cos(45)
= -2 * (1/sqrt(2)) + 2 * (1/sqrt(2))
= 0