# How to build rotation matrix around an axis

This topic is 4914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I want to know how to implement a function similar with D3DXMatrixRotationAxis(). I had searched on google and couldn't find anything. The DirectX documentation only provides documentation for how to make rotation matrix around x, y, and z axis. Thanks a lot for the attention.

##### Share on other sites
Using quaternions. The following code builds a 3x3 rotation matrix in column major order (for OpenGL). If you want a 4x4 matrix and/or use it in D3D, then you need to adjust the indices accordingly.

// w = angle (in radians)// A = axis// M = matrixfloat s, t, c, sx, sy, sz, tx, ty;c = cos(w); s = sin(w); t = 1.0f-c;sx = s*A.x; sy = s*A.y; sz = s*A.z;tx = t*A.x; ty = t*A.y;m[0] = tx*A.x+c;m[1] = tx*A.y-sz;m[2] = tx*A.z+sy;m[3] = tx*A.y+sz;m[4] = ty*A.y+c;m[5] = ty*A.z-sx;m[6] = tx*A.z-sy;m[7] = ty*A.z+sx;m[8] = t*A.z*A.z+c;

##### Share on other sites
I don't htink that's what he exactly what he wants, it's close tho. if I'm not mistaken you want the matrix that converts a vector and an angle into a rotation matrix right? you can use above but if you don't know quartinions you won't know how to do the converstion b/w a vecor, angle to quartinion. or maybe it's just vector form, in where you have the angle encoded into the magnitude? if you want it just reply and I'll fish out a book for you. or if you want a derivation deposit 2 dollars lol jk. it's derived by simplifying the rotation by realising that the component of the point that is in the direction of the rotation vector doesn't change only the component that is not in the direction of the rotation vecor changes, and it does so in a siple way.

tim

##### Share on other sites
Quote:
 Original post by timwI don't htink that's what he exactly what he wants, it's close tho. if I'm not mistaken you want the matrix that converts a vector and an angle into a rotation matrix right?

Uhm... That's exactly what I posted.

##### Share on other sites
sorry I thought you posted something that took a quaternion and made a matrix out of it. you'd have to extract the information out of a quaternion vector. didn't realize you were taking axis angle form. my mistake

tim

##### Share on other sites
Thanks for the replies.

I had tried the code that Yann gave, but I couldn't make it work in D3D. What I did was putting m[0], m[1], m[2] into the first row of my matrix, i.e. m[0][0], m[0][1], m[0][2], and m[0][3] is 0. I did like this until the third row. And into the fourth row I put 0, 0, 0, 1.

Is that correct?

Do I have to normalize the axis vector first?

##### Share on other sites
Alternatively, the formula for rotation of X about a normalised vector N is:

X' = N(X.N) + (X - N(X.N))*cos(theta) + X^N*sin(theta)

(^ is cross product). That can be converted into a matrix. Whether it's clockwise or anticlockwise will depend on whether you use an LHS or RHS coordinate system. The 1st term is the component parallel to the axis vector and the 2nd and 3rd term are like a 2D rotation. So in answer to your question yes, I think it does have to be normalised, although I haven't tried to work the whole thing through.

##### Share on other sites
If the axis is not already unit-length, you'll need to normalize it.

##### Share on other sites
Solved it.
Thanks for all the answers guys.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 41
• ### Forum Statistics

• Total Topics
634131
• Total Posts
3015709
×