Game Development Community

How to convert vector's direction

by ShaoWei · in Technical Issues · 12/01/2007 (10:07 pm) · 5 replies

I got a vector, and then want it to change its direction. For example the vector is "1 1 0", I want to konw how to convert 30 degree counterclockwise of the vector in xy plane? What should the result vector is ?

#1
12/02/2007 (4:53 pm)
To do this you multiply the vector and a 3x3 rotation matrix. (Three rows and columns)
The matrices for rotation about the principle axes (X,Y or Z) are well known.
What I did here was just to write out this multiplication in script.

// rotate input vector about the +Z axis
// usage: echo(rotateVectorAboutZ("1 1 0",30));
function rotateVectorAboutZ(%vec, %angleDeg) // angle (degrees) positive for counter clockwise rotation in X-Y plane about +Z
{
   %angleRad = mDegToRad(%angleDeg);
   %sinAngle = mSin(%angleRad);
   %cosAngle = mCos(%angleRad);
   %vx = getWord(%vec,0);
   %vy = getWord(%vec,1);
   %vz = getWord(%vec,2);
   return  (%vx * %cosAngle - %vy * %sinAngle) SPC (%vx * %sinAngle + %vy * %cosAngle) SPC %vz;
}

The matrices look like:
upload.wikimedia.org/math/6/0/c/60c337e16326d58a28d7c4f9d9e53390.pngupload.wikimedia.org/math/2/d/c/2dcbcfbb3e8744e4af1d3999808c136a.pngupload.wikimedia.org/math/c/a/5/ca5c3845617aec63f6a09ce173c8db71.pngfrom
en.wikipedia.org/wiki/Rotation_matrix
(I'm not sure this is very useful article for a beginner to rotations, but it does have pictures. The Qz() matrix was used here for a rotation about Z.)
#2
12/02/2007 (5:03 pm)
This is a much more useful reference:
mathworld.wolfram.com/RotationMatrix.html
#3
12/02/2007 (11:07 pm)
Thank you Matthew Jessick! I see that. I thought that the engine has this function, I look through out the reference, but has no answer! Thank you very much, I think this is the best place I hava ever found to shoot problems.
#4
12/05/2007 (3:20 pm)
The engine does have a built in function to create rotation matrices.

MatrixF RotateMat(EulerF(0,0,rotRadiansZ));
RotateMat.mulV(myVector);
#5
12/05/2007 (3:25 pm)
There's a really similar question answered here.