rotation without a matrix?

Started by
7 comments, last by jho 23 years, 6 months ago
Is there a way to multiply a vector (x,y,z) to represent a rotation of A degrees about X-axis, B degrees about Y-axis, C degrees about Z axis WITHOUT using a matrix? right now I construct my own 3 "rotation matrices" for each X, Y, and Z, multiply them together, and then multiply it with my vector.. I was wondering if there''s a better way.
Advertisement
Well, if you know how to build the rotation matrices for each axis and you know how matrices work, you can skip some math by figuring out where the unneccesary multiplication exists (ie. x*0, rotation about x doesn''t change x), then build the final matrix using a lot of algebra. I would bet in a standard rotaion, ther is a lot of unused data, for example, you aren''t even translating (you could get by with a 3x3 matrix).

Or quaternions.

-R
it''s very simple,
just use sin and cos functions!!!
--------Dave[ Math Studio ] A Computer Algebra System
Flipcode has got some good matrix FAQ''s, one of them revealed this optimal method of constructing a rotational matrix.

It was written in matrix[16] format, so here it is for a D3DMATRIX:


float A, B, C, D, E, F, AD, BD;
D3DMATRIX r;

A = (float)cos(xangle);
B = (float)sin(xangle);
C = (float)cos(yangle);
D = (float)sin(yangle);
E = (float)cos(zangle);
F = (float)sin(zangle);

AD = A * D;
BD = B * D;

r(0,0) = C * E;
r(0,1) = -C * F;
r(0,2) = -D;
r(1,0) = -BD * E + A * F;
r(1,1) = BD * F + A * E;
r(1,2) = -B * C;
r(2,0) = AD * E + B * F;
r(2,1) = -AD * F + B * E;
r(2,2) = A * C;

r(0,3) = r(1,3) = r(2,3) = 0.0f;
r(3,0) = r(3,1) = r(3,2) = 0.0f; // could plug x,y,z''s..
r(3,3) = 1.0f;


Cheers

Matt



Hi.. what''s the URL to this FAQ?
I tried your code and it''s not working, my vertices disappeared
Try here

Remember, where I commented that you could plug the x,y,z's in (to create a translated rotational matrix) you can only do that if you don't intend to do any more matrix multiplications with it (when the origin should be 0,0,0).

Also, are you sure you were multiplying your 3 rotation matrices correctly before? (What order were you using - see the faq).

That code should work, it's copied and pasted from my project....

Cheers

Matt





Edited by - 3dmodelman on October 19, 2000 6:24:37 PM
I got it working, I make a typo oops..
but the rotation doesn''t look 100% right comparing to my original slow method. It rotates erraticly.

Does the order of X Y Z rotation matter? I thought it''s a commutative operation, X*Y*Z = Z*X*Y etc. My program has no problem if I swap the order I multiply the rotation.

But this code from the FAQ, is there some special assumption or something? Sorry I''m not very good in math maybe I missed something.

jho, I''m stuck for suggestions.

I made a routine to replace the three matrix mults (that is pasted above) and it made no difference whatsoever, except for the speed optimisation.

Double check for typo''s again - copy and paste straight from this page if you can.

Are you doing EXACTLY the same rotations with each method? You may be experiencing Gimbal Lock, but both methods should be affected in just the same way, with the same rotations.

Finally - Are any of your rotation matrices translated BEFORE their next multiplication (it doesn''t sound like they are if you can re-arrange their order). This method constructs a rot matrix where all rotations are applied about the world origin (0,0,0). If you want it to cause rotations about another origin you''ll need to multiply it with a translation matrix. (Are you trying to plug in x,y,z''s ? - I might be wrong with my assumptions there so try multiplying it with a translation matrix instead).

Cheers

Matt



Oh I think I know what the problem might be,
my app''s originally construct the the rotation matrix in this order, R = Identity * Z * X * Y, if I try to go Identity * X * Y * Z it''ll behave kind of weird. Maybe it''s gimbal locking.

I think the formulation from the FAQ is based on the order X * Y * Z. I also checked if my matrix system is transpose of yours, and flipped the [j] to [j].. no luck. I think I might need to formulate another one for my specific order of Z * X * Y.<br> </i>

This topic is closed to new replies.

Advertisement