Rotating a 3d Model Direct X 9

Started by
1 comment, last by jpventoso 15 years, 9 months ago
I've been following Direct 3d 9 tutorials at DirectXtutorial.com. I like to experiment with my programs alot. My goal is to be able to rotate the model both horizontally and vertically. Well actually it is to make the illusion that the model is being rotated (doesnt really matter) But anyways here is the block of code i am trying to modify -------------------------------------------------------------------- static float index = 0.0f; static float indexvertical = 0.0f; if (KEY_DOWN(VK_RIGHT)) index+=0.03f; if (KEY_DOWN(VK_LEFT)) index-=0.03f; if (KEY_DOWN(VK_UP)) indexvertical+=0.03f; if (KEY_DOWN(VK_DOWN)) indexvertical-=0.03f; D3DXMATRIX matRotateY; D3DXMATRIX matRotateX; D3DXMatrixRotationY(&matRotateY, index); d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY)); D3DXMatrixRotationX(&matRotateX, indexvertical); d3ddev->SetTransform(D3DTS_WORLD, &(matRotateX)); ------------------------------------------------------------------------ I would think that it would let me do both vertical and horizontal at the press of a key, but it doesn't. It will only do it vertically. If i change the Block Back a little like this ------------------------------------------------------------------------- static float index = 0.0f; if (KEY_DOWN(VK_RIGHT)) index+=0.03f; if (KEY_DOWN(VK_LEFT)) index-=0.03f; D3DXMATRIX matRotateY; D3DXMatrixRotationY(&matRotateY, index); d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY)); -------------------------------------------------------------------------- I does work Horizontally. But there is no code for it to go vertically. I hope that is all that is necessary, If you need the whole source code i can upload it somewhere. Thanks in Advanced
Advertisement
When you call 'SetTransform', it overwrites any previously set transformation with the new one - this is why you're only seeing the last rotation you set.

The solution is to combine the two rotations using a matrix 'Multiply' operation, then you 'SetTransform' the result. I don't have the DirectX help handy so you'll have to find a function to do that yourself. (:

A subtlety you may run in to is that the order in which you multiply matrices affects the result. In fact the same is true of 3D rotations in real life. If you turn left and then up you end up looking up - whereas if you turn up then left you finish lying on your side.
The Trouble With Robots - www.digitalchestnut.com/trouble
Do what Geoffrey says, and to do the multiplication just use the D3DXMatrixMultiply function by passing the OUT result matrix parameter, and the two rotation matrices as IN parameters.

This topic is closed to new replies.

Advertisement