View Full Version : Matrix rotation?

Cactus Dan
03 March 2004, 05:24 PM

How do I calculate the rotation of a matrix around one of its local axes by a given angle?

For example:
I have a function that will return an object's matrix. Then I have an angle (theta) that I've calculated and want the matrix to rotate around its current local Y axis by theta.

I'm using my software's scripting language that seems to only have a function to set an object's rotation in HPB world coordinates.

Thanks in advance for any help.

Cactus Dan

03 March 2004, 07:55 PM
Let's say you have an object at (10,10,10) with normalized local axes:
[0 1 0]
[1 0 0]
[0 0 1]
which is equivalent to the object being rotated by 90 degrees (the
x and y axes were swapped).

Now let's say you want to rotate about the object x-axis, but it's
really rotatation about the world's y-axis.

Here's what ya do.
Transform your object to the origin by with a translation matrix T:
T = [1 0 0 -10]
[0 1 0 -10]
[0 0 1 -10]
[0 0 0 1].

Then map your axes. You want to map points along your object's x-axis
to points to the world's y-axis and your object's y-axis to the world's
x-axis. Just make an orthogonal matrix of your local axes, M.

M = [0 1 0 0]
[1 0 0 0]
[0 0 1 0]
[0 0 0 1]

What does it do? Well it maps points from the local x-axis to points
in the world's y-axis. OpenGL uses this same technique for it's function
gluLookAt to map your camera's coordinate system to OpenGL's camera coordinate
system (which is at (0,0,0) and looking down the -z axis).

[0 1 0 0] [0] [5] (multiplying M by (0,5,0) = (5,0,0)... it maps
[1 0 0 0] [5] = [0] (points in they y-axis to points in the x-axis.
[0 0 1 0] [0] [0] (also note M is a rotation matrix about the z-axis).
[0 0 0 1] [1] [1]

Now that you have your local system mapped to the world's sytem, apply
your rotation matrix, R. Then apply inverse transformations of M and T.

Concatenated Matrix = T*M*R*M*(-1)*T(-1)

Cactus Dan
03 March 2004, 04:39 AM

Thanks for the reply.

Although, I'm not sure I fully understand your answer.

It sounds like you move the object to the origin, rotate it, then move it back with the rotation?

Anyway, I did some research in the SDK and found some more matrix functions that set individual rotations around x, y and z axes. I also found one that returns the product of matrixA and matrixB. The result is matrixB expressed in matrixA's parent system.

So, my solution is to get the objects matrixA, create matrixB in memory (at the origin), rotate it around the Y axis, then express it in matrixA's parent system and set the new matrix to the object. It works.

It sounds like that might be what you were describing in your answer. I just got lucky and found those functions to make it easier for me to figure out.

Thanks again.

Cactus Dan

03 March 2004, 11:12 AM
Cactus Dan, im guessing this is coffee?

Have a look at this function, Matrix::SetRotAxis(axis,angle)

It allows you to rotate a Matrix on the axis by angle, heres an example

var angle=2;
var axis = vector(1,0,0);
var m = op->GetMg();

If this isn't for coffee, then totally ignore this reply :)

Cactus Dan
03 March 2004, 01:21 AM

Originally posted by Geespot
Cactus Dan, im guessing this is coffee?

Yep, it is COFFEE that I'm using.

Thanks for the tip. Your routine seems to work a little easier.

Cactus Dan

CGTalk Moderation
01 January 2006, 05:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.