Advertisement

Axis of Rotation

Started by March 22, 2003 07:18 AM
4 comments, last by Metorical 21 years, 11 months ago
Hi guys, I''ve managed to code your basic matrix rotation and this works fine when I''m rotating about the x,y and z axis. Problem is now I have to rotate around another axis (defined by a vector, infact the cross product of an up vector and look direction vector). The axis is also defined as a vector. Could anyone help me with the maths to do this. I''m guessing it''s something like translate transformation I''m working with in to x,y,z space and apply it or translate the point to be rotated, transform it then translate it back by inverse. Any help would be appreciated. -Meto
Do you use D3DX? -> D3DXMatrixRotationAxis()


I am a signature virus. Please add me to your signature so that I may multiply.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Advertisement
Nope

I''m interested in the maths more than API specific methods of doing it.

I wrote my own little matrix transform thing but I will probably eventually be using OpenGLs matrix processing as it will be much faster than mine.

Please though any help with the maths.
it's not very difficult

this code converts the axis/angle into a quaternion, then convert the quaternion into a matrix.

axis of rotation is x, y, z.

void CreationRotationMatrix(float Mat[3][3], float x, float y, float z, float a){        //-------------------------------------------------------        // variables        //-------------------------------------------------------	float qx, qy, qz, qr; // quaternion	float D1,D2,D3,D4,D5,D6,D7,D8,D9; // precalcs to convert quat to matrix        float d; // length of axis vector         //-------------------------------------------------------        // normalise axis (maybe not required)        //-------------------------------------------------------        d = sqrt(x*x+y*y+z*z);            if (d < 0.0001f)            return false;                x /= d;        y /= d;        z /= d;                //-------------------------------------------------------        // calculate rotational quaternion        //-------------------------------------------------------	qx = x * sin(a * 0.5f);	qy = y * sin(a * 0.5f);	qz = z * sin(a * 0.5f);	qr = cos(a * 0.5f);        //-------------------------------------------------------        // Cponvert quaternion to matrix        //-------------------------------------------------------	D1 = (qx * qx) * 2.0f;	D2 = (qy * qy) * 2.0f;	D3 = (qz * qz) * 2.0f;	float r2 = qr * 2.0f;	D4 = qx * r2;	D5 = qy * r2;	D6 = qz * r2;	D7 = qx * qy * 2.0f;	D8 = qx * qz * 2.0f;	D9 = qy * qz * 2.0f;	Mat[0][0] = 1.0f  - D2 - D3;	Mat[0][1] = D7 - D6;	Mat[0][2] = D8 + D5;	Mat[1][0] = D7 + D6;	Mat[1][1] = 1.0f  - D1 - D3;	Mat[1][2] = D9 - D4;	Mat[2][0] = D8 - D5;	Mat[2][1] = D9 + D4;	Mat[2][2] = 1.0f  - D1 - D2;}   


you can optimise the routine a bit (sin/cos, normalisation).

you don't have to think of 'quaternions' as such if you don't want. Think of the qx, qy, qz, qr as temporary calculations.

Also, it depends if your matrix is row-major or column-major. You might have to change Mat[1][2] = ... into Mat[2][1] = ...., and so forth.

non!

[edited by - oliii on March 22, 2003 10:47:38 AM]

[edited by - oliii on March 22, 2003 10:48:42 AM]

[edited by - oliii on March 22, 2003 10:49:28 AM]

Everything is better with Metal.

Here is a formula. I use to be able to find a derivation on Mathworld for an Axis-Angle rotation matrix, but I don''t seem to be able to any more.
Keys to success: Ability, ambition and opportunity.
Cheers guys this is very helpful!

Thanks, Meto

This topic is closed to new replies.

Advertisement