Skip to main content
GameDev.net gamedev.net
🔒 Locked

Rotation matrix

Started by DangerDave May 12, 2004 at 11:32 AM 4 replies 600+ views
Original Post
DangerDave
DangerDave
This has been bugging me for days now. All I need is a matrix which rotates a unit vector V = [Vx Vy Vz] to align with the Z-axis. Please help! I swear I''ve been trying to get this for days, whenever I try to derive it myself I keep getting a matrix which works for some V''s, but not others. I just need the matrix so I can see what I''ve been doing wrong. Thanks, Dave.
Dave.
mightyjebus
mightyjebus
ok, first you can get the axis of rotation using the cross product:

axis of rotation = V X Zaxis

and you can get the angle between the vectors using the dot product:

cos(angle) = (V dot Z) / |V|*|Z|

as for making the matrix, there was a site that explained this process (it seems to be down ATM)

http://www.makegames.com/3drotation

search the GD forums for posts by diane gruber (dgruber) i think she wrote the article
DangerDave
DangerDave
I know the general idea is to rotate into a plane and then perform a second rotation in that plane to align with the axis, but whenever I try and calculate the composite matrix it gives erroneous results when I test it. I just need a matrix that I can use to verify my results and see where I went wrong. Can anybody just post a matrix that I can use to see what I''m doing wrong?
Dave.
mightyjebus
mightyjebus
here's how to generate the matrix once you use my methods above to get the axis and angle:

// assume: (x y z) are the components of the rotation axis
float c = cos(angle);
float s = sin(angle);
float t = (1 - c);
| (t*x*x + c) (t*x*y - s*z) (t*z*x + s*y) |
Matrix M = | (t*x*y + s*z) (t*y*y + c) (t*z*y - s*x) |
| (t*x*z - s*y) (t*z*y + s*x) (t*z*z + c) |


[edited by - mightyjebus on May 12, 2004 2:21:55 PM]
jvsquare
jvsquare
A couple of other things:

Don''t forget to normalize your rotation axis (x,y,z) -- otherwise you''ll get odd results.

And that matrix will only work with column vectors. If you''re using row vectors (i.e. your multiplication order is vector*matrix) you''ll need to transpose it.

Topic Locked

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

Sign in to reply to this topic.