Matrix and dirction ,position

Started by
2 comments, last by Drakex 18 years, 4 months ago
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?
Advertisement
Hi,

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

kp
------------------------------------------------------------Neo, the Matrix should be 16-byte aligned for better performance!
Quote:Original post by kovacsp
Hi,

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.
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.
_______________________________________________________________________Hoo-rah.

This topic is closed to new replies.

Advertisement