Jump to content
  • Advertisement
Sign in to follow this  
CRACK123

matrix rotation using d3dxmatrix

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by CRACK123
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

*** 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).

If you want to move/rotate your cube, just update your D3DXVECTOR3's in your object. Then, each frame, recalculate your world matrix:

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 roll
D3DXMatrixTranslation(&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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!