# how get Local XYZ axis from world matrix?

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

## Recommended Posts

hi every one, I am trying to get the coordinates of the Local XYZ axis of my objects through their world matrix, (each object has one world matrix). so this is what I used: Local X = (world._11, world._21, world._31) ; Local Y = (world._12, world._22, world._32) ; Local Z = (world._13, world._23, world._33) ; but it seems that they need some kind of formatting, because they don't really behave like the Local Axis. if you want to know why do I want to get those local axis, it's because I want to make an abjoct rotate around one of its local axis (like an airplane would do for instance). thank for you help.

##### Share on other sites
I'm not sure exactly what you mean, but maybe this will help:

DirectX uses 4x4 matrices to represent a 3-dimensional transform. They're like a different set of bases on the same vector space. If you want to transform a vector into some other matrix's space, you just multiply it by the matrix. Likewise, if you want to take a vector that's inside some matrix's space and get its equivalent outside, multiply your vector by the matrix's inverse.

Hope that helps,
Max

##### Share on other sites
The way I do it is whenever I update an object's world matrix (i.e. moving it on the x, y, z axes) I also store it's position in a 3D vector (i.e. if I decide to move the object 10 units on the x axis, besides creating its world matrix I add the 10 units to the x position in the vector)
And when I want to rotate the object around itself I create a world matrix using the negative values of x,y,z in the position vector. this takes the object to the origin. I create a rotation matrix around the desired axis.
I create a world matrix using the position vector x, y, z (their original values) and then I multiply the matrices like this :
originMat * rotationMat * worldMat (Make sure the multiplication happens in that order). the last matrix returns your object to its original position after its been rotated around the desired axis.

##### Share on other sites
thanks you guys, it seems that the idea of keeping track of the local axis using verctors is a nice idea.
however I am still wondering if there is a way to get the local axis directly from the world matrix. if any one has further infomation on this subject please tell me about it.
thanks DesignerX, _Flecko

##### Share on other sites
Quote:
 Original post by Dark Metamorphosisthanks you guys, it seems that the idea of keeping track of the local axis using verctors is a nice idea. however I am still wondering if there is a way to get the local axis directly from the world matrix. if any one has further infomation on this subject please tell me about it.thanks DesignerX, _Flecko

A | 1 0 0 0 |B | 0 1 0 0 |C | 0 0 1 0 |D | 0 0 0 1 |    x y z w

Here, the first three values in row A are your local X-axis = (1,0,0)
first three values in row B are your local Y-axis = (0,1,0)
and first three values in row C are your local z-axis = (0,0,1).

The same applies for any rotation matrix.
(Unless someone corrects me)

Note that for OpenGL the matrix would be transposed, so first column would be x-axis, etc...

##### Share on other sites
dotproduct, does this apply to directX, because this is what I've been doing to get the local X,Y,Z axis. but it seems that the values stored in these rows are actually the coordinates of the origin XYZ according to the local XYZ, so you will have to extract the local XYZ using a system of 3 equation and 3 unknows, which is a lot of calculation. I might be wrong about this, so please tell me what you up to.
thanks every one

##### Share on other sites
Just an idea, not sure if it'll work... sling your matrix into a D3DXMatrixDecompose() call, and then get the resultant rotation quaternion and feed it into a D3DXQuaternionToAxisAngle()...

Using the information gained from one (or both) of those calls might well allow you to retrieve the information you want.

hth
Jack

##### Share on other sites
Let me see if I understand. The object has a complete matrix state, including translation and rotation, and you want to rotate that state even more, but locally? Here's one way to do that:
VECTOR temp( world._41, world._42, world._43 );world._41 = world._42 = world._43 = 0.0f;MATRIX another_matrix;another_matrix.Rotate( new_rotations );world = world * another_matrix;world._41 = temp.x;world._42 = temp.y;world._43 = temp.z;

It's could be much nicer if you impliment your own matrix-rotation code that ignores the translation. Here's an example of one of my functions:
VOID Matrix::AddRotateX(FLOAT angle){	// Angles	FLOAT cos_angle = cosf( angle );	FLOAT sin_angle = sinf( angle );	// Rotate Y Vector	FLOAT ty = _22;	_22 = ty * cos_angle + _23 * sin_angle;	_23 = _23 * cos_angle - ty * sin_angle;	// Rotate X Vector	ty = _12;	_12 = ty * cos_angle + _13 * sin_angle;	_13 = _13 * cos_angle - ty * sin_angle;	// Rotate Z Vector	ty = _32;	_32 = ty * cos_angle + _33 * sin_angle;	_33 = _33 * cos_angle - ty * sin_angle;/*...this needs to be ignored..	// Rotate T Vector	ty = _42;	_42 = ty * cos_angle + _43 * sin_angle;	_43 = _43 * cos_angle - ty * sin_angle;*/}

With my example, it would be as simple as calling World.AddRotateX( PI ); and the object would do a 180 in local space. No need to multiply other matrices together or such things.

edit: Errm, I think I did misunderstand. You're meaning you want to be able to rotate an object the same way no matter which orientation it is currently in? I use quaternions for such things, but you can also use D3DXMatrixRotationAxis on your matrix.

To rotate around the x axis, pass the _11,_12,_13 vector to that function.
For the y axis, it's _21, _22, _23,
and for z axis, _31, _32, _33.
Make sure they're normalized if you do any scaling.

• 10
• 9
• 13
• 41
• 15