Public Group

#### Archived

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

# Direct3D rotation problem

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

## 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 on other sites
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 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

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633678
• Total Posts
3013290
×