Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate an object in XYZ axes on DirectX?

I tried this code:

D3DXMatrixRotationX(&matRotate, rx);
D3DXMatrixRotationY(&matRotate, ry);
D3DXMatrixRotationZ(&matRotate, rz);
d3ddev->SetTransform(D3DTS_WORLD, &matRotate);

But it seems to always only rotate the last rotation (Z axis).

How do i rotate an object with all the XYZ axes at the same time? Im trying to find DirectX equivalent for OpenGL rotation:

glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glRotatef(rz, 0, 0, 1);


EDIT: Looks like i figured it out by myself:

D3DXMATRIX matRotateX;
D3DXMATRIX matRotateY;
D3DXMATRIX matRotateZ;
D3DXMatrixRotationX(&matRotateX, rx);
D3DXMatrixRotationY(&matRotateY, ry);
D3DXMatrixRotationZ(&matRotateZ, rz);
D3DXMATRIX matRotate = matRotateX*matRotateY*matRotateZ;
d3ddev->SetTransform(D3DTS_WORLD, &matRotate);

IF not, please comment. I cant post it as an answer until 8 hours has passed! (need +7 reputation to do it).

like image 244
Rookie Avatar asked Oct 09 '22 01:10

Rookie


1 Answers

D3DXMatrixRotationX doesn't rotate a matrix but creates a matrix that can be used to rotate something.

So it you could use the matrix right afterwards each step and rotate something else or you could use D3DXMatrixRotationYawPitchRoll so that you only need to create one once...

D3DXMatrixRotationYawPitchRoll(&matRotate, ry, rx, rz);

EDIT: Your edit works too...

like image 103
Cpt. Red Avatar answered Oct 13 '22 11:10

Cpt. Red