# Matrix and dirction ,position

## Recommended Posts

derek7    100
If a object in 3d space rotate it and move it somewhere now given a matrix that do the same thing,can I work out the rotating angle and translation? If impossible, can I woke out the 3 direction of object?

##### Share on other sites
kovacsp    306
Hi,

use D3DXMatrixDecompose() to get the rotation, scaling and translation from a matrix.

kp

##### Share on other sites
derek7    100
Quote:
 Original post by kovacspHi,use D3DXMatrixDecompose() to get the rotation, scaling and translation from a matrix.kp

I WANT to get 3 direction of a object . that just get a rotation quaternion.

##### Share on other sites
Drakex    273
I think you want .. Euler Angles. I.e. the "3 direction" of the object.

Getting the Euler angles out of a matrix is a little tricky, and it depends on the rotation order that was used to create the matrix. If you used YXZ order (for example, a matrix created with D3DXMatrixRotationYawPitchRoll), you can use the following code to extract the three angles:

void matToEulerAngles(D3DXMATRIX& mat, float& x, float& y, float& z){	if(mat.m[2][1] > 1)		mat.m[2][1] = 1;	if(mat.m[2][1] < -1)		mat.m[2][1] = -1;	x = asin(mat.m[2][1]);	const double epsilon = 0.01;	if(x + epsilon < (3.1415926535 / 2))	{		if(x - epsilon > (-3.1415926535 / 2))		{			z = atan2(-mat.m[0][1], mat.m[1][1]);			y = atan2(-mat.m[2][0], mat.m[2][2]);		}		else		{			z = -atan2(mat.m[0][2], mat.m[0][0]);			y = 0;		}	}	else	{		z = atan2(mat.m[0][2], mat.m[0][0]);		y = 0;	}}

If you call the function like so:

float xang, yang, zang;matToEulerAngles(someMatrix, xang, yang, zang);

xang, yang, and zang will contain the Euler angles in radians.

If you're not using YXZ rotation order, this will give you bogus answers. The formula needs to be changed if you use a different order.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account