# Matrix and dirction ,position

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

## Recommended Posts

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
Hi,

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

kp

##### Share on other sites
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
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.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×