Archived

This topic is now archived and is closed to further replies.

Metal Typhoon

The math behind rotation ?

Recommended Posts

i know it''s supposed to be a 4x4 matrix but what will be the outcome when i''m done with my rotation ? and i can find the vector. How does it goes on my matrix ? | 1 0 0 0| | 0 1 0 0| | 0 0 1 0| | 0 0 0 -1| what does each number mean ??

Share this post


Link to post
Share on other sites
The columns of the upper 3x3 portion of that matrix represent the 3 axes of your rotated and scaled coordinate system, measured in a base system. These axis vectors are called the "basis" vectors of the rotated coordinate system.

For example, that matrix could represent the position and orientation (and scaling) of an automobile, represented relative to a map of the world. A matrix with rotation will have different vectors in the columns of the upper 3x3 portion. For example, if you rotate the car by 45 degrees about the z axis, you would have:


|0.707 -0.707 0 0|
|0.707 0.707 0 0|
| 0 0 1 0|
| 0 0 0 -1|


Looking at the columns, the rotated x axis is the first 3 #''s in the 1st column (0.707, 0.707, 0), the rotated y axis is the first 3 #''s in the 2nd column (-0.707, 0.707, 0), and the rotated z axis is the 1st 3 #''s of the 3rd column (0,0,1). Of course, since we rotated about z, the z axis doesn''t change.

Also, you should note that different 3D graphics API''s treat the matrix differently. The above is the OpenGL approach, while Direct3D (I believe) uses the transpose of this matrix, e.g., the basis vectors are in the rows of the upper 3x3 portion rather than the columns.


Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
Ok, here's the deal with 2d rotation (learn it first, and it will be easy to derive 3d rotation as well). Let's derive 2d rotation. I'm not sure if you are familiar with polar coordinate systems or not, but let's take a look at them. In rectangular coordinate systems (the one your probably most familiar with), you represent a point as [x, y]. With polar coordinate systems, you represent that same point as [r, theta]. Now, r = distance from origin, and theta = number of degrees rotated from positive x-axis. An important thing to remember about polar coordinate systsems, is they are not unique, as are rectangular coordinate systems. There are many ways to represent the same point (for example, 2pi and 4pi will plot the same point as far as plotting the point goes). Ok, but anyways, if you want to move r from the theta you are currently at, and move it by alpha degrees more then you get


x' = r cos(theta + alpha)
y' = r sin(theta + alpha)
(note that ' just means rotated or whatever)


there's a trig identity that tells us this simplifies to


x' = r (cos(alpha)cos(theta) - sin(alpha)sin(theta))
y' = r (cos(alpha)sin(theta) + sin(alpha)cos(theta))


now, we can multiply in r and simplify to


x' = r*cos(alpha)cos(theta) - r*sin(alpha)sin(theta)
y' = r*cos(alpha)sin(theta) + r*sin(alpha)cos(theta)


which we can simplify as r*cos(alpha) = x and r*sin(alpha) = y
so we end up with


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


which if you want to put x and y into a vector, and the cosines and sines in a matrix, you end up with the matrix/vector multiplication (remember, and vector is also a matrix)


|cos(theta) -sin(theta) | |x|
|sin(theta) cos(theta) | |y|


and this describes a rotation around the z-axis (a 2d rotation). Now to do the same in three dimension, just imagine an x rotation being on the plane that is defined with the y and z axis, the y rotation being on the plane that is defined with the x and z axis, and z rotation(think 2d) on the x and y axis. That means you plug in this same little formula, but leave whatever axis you are rotating AROUND out of the matrix. Here is the matrix where you can tell what goes with what. Look at this and if you are rotating about x, leave anything with x out of the equation, and plug the right y and z values in. You have to be careful with the y rotations though, as they start in the bottom row.


|xx xy xz|
|yx yy yz|
|zx zy zz|


the colums are more important but the rows also define the relative coordinates. Now, if you are using opengl, this is actually a 4x4 matrix (for homogeneous coordinate systems), and you need to add 0's and 1's as necissary to keep this a column matrix. Hope that helps.


[edited by - xg0blin on January 20, 2003 12:36:01 AM]

Share this post


Link to post
Share on other sites
Okay, my question was sort of similar, and I have a question about the equation (it does work for me, but why? is another story )

You wrote:

"x'' = r cos(theta + alpha)"

I like the r*cos(theta) part; it makes sense to me. Could you elaborate on the alpha part though? I don''t quite grasp what alpha is for. Why would I want to take theta (presumably the angle I want to rotate to) and add alpha to it? (not arguing, because again, I know it works... but why?" Furthermore, where did alpha come from?

"x'' = r (cos(alpha)cos(theta) - sin(alpha)sin(theta))
y'' = r (cos(alpha)sin(theta) + sin(alpha)cos(theta))
"

I don''t really understand this, though I vaguely remember doing something like this in trig. Is this the double angle formula? My trig is a little hazy; its been awhile

Anyway, good informative post :D

Share this post


Link to post
Share on other sites
quote:
Original post by Peon
You wrote:

"x'' = r cos(theta + alpha)"

I like the r*cos(theta) part; it makes sense to me. Could you elaborate on the alpha part though? I don''t quite grasp what alpha is for. Why would I want to take theta (presumably the angle I want to rotate to) and add alpha to it? (not arguing, because again, I know it works... but why?" Furthermore, where did alpha come from?

"x'' = r (cos(alpha)cos(theta) - sin(alpha)sin(theta))
y'' = r (cos(alpha)sin(theta) + sin(alpha)cos(theta))
"



there are trigonometric sum theorems stating:

cos(alpha+theta) = cos(alpha)cos(theta) - sin(alpha)sin(theta)
and
sin(alpha+theta) = cos(alpha)sin(theta) + sin(alpha)cos(theta)

you can prove them using a transformation to complex numbers.

Share this post


Link to post
Share on other sites
I think the misunderstanding is that alpha is the existing rotation relative to the x axis. (this is the basis of the polar coordinate system)

Polar coordinates can be transformed back to cartesian coordinates using the two equations x = r*cos(alpha) and y = r*sin(alpha).


Share this post


Link to post
Share on other sites
alpha is also something called a "phase shift", which represents the amount by which two waves are out-of-sync. The sine and cosine functions are out-of-phase by PI/2 radians or 90 degrees. The phase shift stuff shows up in automatic control theory, various stability theories (e.g., numerical methods for doing real-time physics, fluid dynamics, material fracture, etc.).

Imagine a pure sine wave oriented left-to-right on a graph (the horizontal axis is theta, the vertical axis is the amplitude of the wave). By adjusting the value of alpha, you can shift the wave to the left or right along the horizontal axis. By doing this, the alpha value could be used to represent the motion of the wave over time.

An interesting result of the trigonometric identities presented by ga is that you can convert sine into cosine and vice versa by setting alpha = 90 degrees or 2*PI. Basically, the phase shift can be used to match two waves that are identical in shape but not in phase.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
alpha is what you want to rotate by. Your point is represented with theta, and you want to rotate it alpha degrees from where it is at. so from the x-axis it is (alpha + theta), which is reducible through the trig theory presented above.

Share this post


Link to post
Share on other sites