Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

NightMonkey

Direct3D rotation problem

This topic is 5563 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

Hi, I am writing a space flight game and are having a few problems with the rotation of the space craft. The game is written using Direct3D and are using the following code to rotate the ship: D3DXMATRIX matWorld; D3DXMatrixTranslation(&matWorld,m_Position.fXPos ,m_Position.fYPos, m_Position.fZPos); D3DXMATRIX matTemp, matRotateX, matRotateY, matRotateZ; D3DXMatrixRotationY( &matRotateY, -m_Position.fYRot ); D3DXMatrixRotationX( &matRotateX, -m_Position.fXRot ); D3DXMatrixRotationZ( &matRotateZ, -m_Position.fZRot ); D3DXMatrixMultiply( &matTemp, &matRotateX,&matRotateY ); D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp ); D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld ); m_LocalMatrix = matWorld; The problem im having is when the ship is rotated at say 90 degrees on the Z axis, when I change the pitch the ship is rotated along the world X axis instead of its new axis which would be the world Y axis. Does anyone know how I rotate the ship on its new local axis instead of the world axis?

Share this post


Link to post
Share on other sites
Advertisement
Assuming that the object you want to rotate has a centre at (0,0,0) in its vertex buffer, you must always translate AFTER ROTATION AND SCALING. Visit www.andypike.com for some great DirectX tutorials. I had similar problems once with OpenGL. (hint to NeHe, emphasize this)

You can correct the problem with:

D3DXMATRIX matWorld, matTranslate;

D3DXMatrixIdentity(&matWorld);

D3DXMatrixTranslation(&matTranslate,m_Position.fXPos
,m_Position.fYPos, m_Position.fZPos);

D3DXMATRIX matTemp, matRotateX, matRotateY, matRotateZ;
D3DXMatrixRotationY( &matRotateY, -m_Position.fYRot );
D3DXMatrixRotationX( &matRotateX, -m_Position.fXRot );
D3DXMatrixRotationZ( &matRotateZ, -m_Position.fZRot );

D3DXMatrixMultiply( &matTemp, &matRotateX,&matRotateY );
D3DXMatrixMultiply( &matTemp, &matRotateZ, &matTemp );
D3DXMatrixMultiply( &matWorld, &matTemp, &matWorld );
D3DXMatrixMultiply( &matWorld, &matTranslation, &matWorld);

m_LocalMatrix = matWorld;


I didn't check that code but you get my angle. When transforming always:

- Translate the object to (0,0,0) (unless it is already there)
- Scale and rotate the object (I don't think that order matters)
- Translate the object to its final position in 3D space.

This is the order in which you should multiply your matrices.

>>>>>>>>>>>>>>>>>
Ilthigore
<<<<<<<<<<<<<<<<<


[edited by - ilthigore on March 31, 2003 3:45:23 PM]

Share this post


Link to post
Share on other sites
You should NOT use 3 separate matrices (one for each rotation), this is good for games like quake and other "running" games.

Flight and space games should only use one matrix for rotations.

You should do one rotation at a time, and apply it directly to the actual ship matrix, because the present ship matrix is being changed by each rotation... I have samples illustrating this, but not with me at this moment...

www.cppnow.com

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!