# Get Rotation angles and position from D3DXMATRIX

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

## Recommended Posts

Hi I would like to retieve from a D3DXMATRIX two 3D vectors (xyz). One for position and the other for rotation (in euler angles) I checked MSDN and couldnt find any matrix function that allows me to query for position and rotation. Weird.... Could somebody let me know how to do that?

##### Share on other sites
Try searching this forum for the keyworks 'extract matrix'. You will find *a lot* of hits from questions just like yours [smile].

Also, check out this DXSDK page and it's subpages. It tells you exactly where to find the individual transforms in a D3DXMATRIX.

Specifically, translation is found in the _41, _42, _43 positions.

##### Share on other sites
Thanks circlesoft.
I checked MSDN before I posted this question and could not find any specific function allowing me to extract from D3DMATRIX position or rotation which is so silly as this operation is very often needed. There is one to transform coordinates by a matrix but still I think there should be a function clearly named GetRotation or GetPosition. Come on if you are writing a matrix or any other library you should avoid users to access its internal structure directly. ok I know that _41 _42 _43 etc.. are public and they represent translation but still you dont wanna spend all your day on things like this if you're concentrating on a higher level games structure. Even though its simple but takes time to refresh etc.. If you're writing a library for public use then you should simplify it to a single function call rather than allowing people to access internal parts. Thats the whole idea of C++. Anyway I dont wanna stretch this post. All I needed is to get rotation and position from Microsoft's matrix library but I guess they didnt have enough imagination to write one so I guess I need to look in my math book and implement it my own one wasting another 30min of my dev time. Useless as all MS is thats why I always prefer OGL stuff but unfortunately sometimes I dont have a choice :)

##### Share on other sites
Or...you could just do things the easy way and keep a copy of the position and rotation [wink]

##### Share on other sites
I agree with circlesoft. the problem that you may encounter is that the exact rotation that you put into a matrix may differ from what comes back. that's because what comes back isn't necessary relative to the model. so what i normally do is keep my own copy of the rotation as well.

here's some code if you still want to pull the rots:

		//Returns the Yaw, Pitch, and Roll components of this matrix. This		//function only works with pure rotation matrices.		void GetRotation(float *v) const		{			//yaw=v[0], pitch=v[1], roll=v[2]			//Note, we use the cosf function rather than sinf just in case the			//angles are greater than [-1,+1]			v[1]  = -asinf(_32); //pitch			float cp = cosf(v[1]); 			//_22 = cr * cp;			float cr = _22 / cp;			v[2] = acosf(cr);			//_33 = cy * cp;			float cy = _33 / cp;			v[0] = acosf(cy);		}		//creates a rotation matrix based on euler angles Y * P * R		//in the same order as DirectX.		void Rotate(const float *v)		{			//yaw=v[0], pitch=v[1], roll=v[2]			float cy = cosf(v[0]); 			float cp = cosf(v[1]);			float cr = cosf(v[2]);			float sp = sinf(v[1]);			float sr = sinf(v[2]);			float sy = sinf(v[0]);						_11  = cy * cr+ sr * sp * sy;			_12 = sr * cp;			_13 = cr * -sy + sr * sp * cy;			_21 = -sr * cy + cr * sp * sy;			_22 = cr * cp;			_23 = -sr * -sy + cr * sp * cy;			_31 = cp * sy;			_32 = -sp;			_33 = cy * cp;		}

Just so you can see how I unwind the rot matrix i've included the code to build it. Btw, the code only works for pure rotation matrices that have uniform scale- I haven;t gotten round to writing a func to unwind a non-uniform matrix. Goggle for 'Matrix Decompositions'

##### Share on other sites
btw, the code above is from my custom matrix class. it is setup similar to dx9 (row major order, _11 -> _44). it can be adapted for other things. btw, i thought directx incldued support for this already though?

##### Share on other sites
Would the following approach not work ?

(assuming 0, 0, 0, 1) in the last column,

row 4 = position vector; extract it and store separately

make position 0. matrix is now rotation only! And this now represetns an axis-angle matrix.

convert it to a quternion using D3DXQuaternionRotationMatrix()

from Quternion, extract the axis and angle!

-- oops .. wrong answer -- enver mind!!! (I did not read the complete question1)

##### Share on other sites
btw, there's another thread just like this one: clicky that shows how to decompose a matrix using D3DX.

• 13
• 18
• 29
• 11
• 20