# matrix rotation using d3dxmatrix

## Recommended Posts

I am trying to rotate a model on its own axis. I am doing the following i.e. code but its not working currently. Its rotating but its also sort of translating. I think I need to get the translation out of the invWorldMatrix. Could anyone tell me what I am doing wrong
D3DXMATRIX matTransform, invMatTransform;
device->GetTransform(D3DTS_WORLD, &matTransform);
D3DXMatrixInverse(&invMatTransform, NULL, &matTransform);
D3DXMATRIX NewRotation = m_Rotation * invMatTransform;

m_Transform = NewRotation * m_Translation;

device->SetTransform(D3DTS_WORLD, &m_Transform);


Thanks

##### Share on other sites
sorry if ive missed the point here, just a quick copy / paste from my code

D3DXMATRIX Total;	D3DXMATRIX RotX;	D3DXMATRIX RotY;	D3DXMATRIX RotZ;	D3DXMATRIX Pos;		D3DXMatrixIdentity(&Total);	D3DXMATRIX SC;	D3DXMatrixScaling(&SC, _cScale.x, _cScale.y, _cScale.z);	D3DXMatrixRotationX(&RotX,  _cRot.x);	D3DXMatrixRotationY(&RotY,  _cRot.y);	D3DXMatrixRotationZ(&RotZ,  _cRot.z);  	D3DXMatrixTranslation(&Pos,_cpos.x,_cpos.y,_cpos.z);	D3DXMatrixMultiply(&Total,&Total,&SC);	D3DXMatrixMultiply(&Total,&Total,&RotX);	D3DXMatrixMultiply(&Total,&Total,&RotY);	D3DXMatrixMultiply(&Total,&Total,&RotZ);	D3DXMatrixMultiply(&entityWorld,&Total,&Pos);

hope it is of some help

##### Share on other sites
Quote:
 Original post by CRACK123I am trying to rotate a model on its own axis. I am doing the following i.e. code but its not working currently. Its rotating but its also sort of translating. I think I need to get the translation out of the invWorldMatrix. Could anyone tell me what I am doing wrong*** Source Snippet Removed ***Thanks

This is the wrong way to go about it. It seems as though you're storing the position of your object as a world matrix, and then getting it back from DirectX when you need to modify it.

This is a bad idea. Not only is it slow, you're going to run into the problems you're experiencing now. The problem is that matrix multiplication is not commutative - that is: (a * b) != (b * a). That means that a translation, then a rotation, is not the same as a rotation then a translation. Rotation should be calculated before translation - and your current method isn't going to work since the matrix you get from GetTransform() is going to be translated.

The effect is that you're going to see some odd movements of your object, for example you're probably going to see it orbiting a point, or something like that.

The solution is to store your object position and rotation separately. In your object class somewhere, store a D3DXVECTOR3 for position, and a D3DXVECTOR3 for rotation (I'll use euler angles for now, since they're simplest).

D3DXMATRIX rotationMatrix;D3DXMATRIX translationMatrix;D3DXMatrixRotationYawPitchRoll(&rotationMatrix, object.m_rot.y, object.m_rot.x, object.m_rot.z); // x is pitch, y is yaw, z is rollD3DXMatrixTranslation(&translationMatrix, object.m_pos.x, object.m_pos.y, object.m_pos.z);D3DXMATRIX worldMatrix = rotationMatrix * translationMatrix;device->SetTransform(D3DTS_WORLD, &worldMatrix);
Hope this helps.

##### Share on other sites
just the same as mine but with a much much better explanation ;P

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627744
• Total Posts
2978902

• 10
• 10
• 21
• 14
• 14