Sign in to follow this  
starfox5194

Rotating a 3d Model Direct X 9

Recommended Posts

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this