• ### Announcements

#### Archived

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

# camera roll

## Recommended Posts

error    122
My camera seems to work ok apart from when I tried to add roll to it. The camera seems to be rotating it self around the Z axis instead of on the spot. I know it's supposed to rotate around the axis but I want it to stay in the same spot. I don't understand why it's doing this because the pitch and yaw rotations are fine. It's simliar to what happens when you translate an object and then rotate it rather than rotate it on its local axis then translate it, but I don't know how to fix it. The code is below. I've uploaded a simple program aswell here Arrow keys move, insert/delete are for pitch and home/end are for roll. Move back a bit when it first starts to see the objects. Hope someone can help.

void C3deCamera::CameraUpdate(LPDIRECT3DDEVICE8 pd3dDevice)
{
D3DXMATRIX matView;
D3DXMATRIX matTrans;
D3DXMATRIX matTemp;
D3DXMATRIX matTemp1;
D3DXMATRIX matXRot;
D3DXMATRIX matYRot;
D3DXMATRIX matZRot;

CameraInput();

camAngleX	= D3DX_PI * (angleX / 180.0f);
camAngleY	= D3DX_PI * (angleY / 180.0f);
camAngleZ	= D3DX_PI * (angleZ / 180.0f);

// Do the rotations, x, y, z

D3DXMatrixRotationZ(&matZRot,camAngleZ);
D3DXMatrixRotationY(&matYRot,camAngleY);
D3DXMatrixRotationX(&matXRot,camAngleX);

// First translate

D3DXMatrixTranslation(&matTrans,camPosX,camPosY,camPosZ);

// Multiply them together

matView = matTrans;

D3DXMatrixMultiply(&matTemp,&matTrans,&matZRot);
D3DXMatrixMultiply(&matTemp1,&matTemp,&matYRot);
D3DXMatrixMultiply(&matView,&matTemp1,&matXRot);

// Set the view

pd3dDevice->SetTransform(D3DTS_VIEW,&matView);
}

void C3deCamera::CameraInput()
{

// Yaw

if (KEY_DOWN(VK_RIGHT))
{
if( angleY == 0.0f ) angleY = 360.0f;

angleY -= 1.0f;
}
else if (KEY_DOWN(VK_LEFT))
{
if (angleY == 360.0f) angleY = 0.0f;

angleY += 1.0f;
}

// Pitch

if (KEY_DOWN(VK_DELETE))
{
angleX -= 1.0f;
if (angleX < -360.0f) angleX = 0.0f;
}
else if (KEY_DOWN(VK_INSERT))
{
angleX += 1.0f;
if (angleX > 360.0f) angleX = 0.0f;
}

// Roll

if (KEY_DOWN(VK_HOME))
{
angleZ -= 1.0f;
if (angleZ < -360.0f) angleZ = 0.0f;
}
else if (KEY_DOWN(VK_END))
{
angleZ += 1.0f;
if (angleZ > 360.0f) angleZ = 0.0f;
}

// Forward and backwards

if (KEY_DOWN(VK_UP))
{
camPosX -= 1.4f * -sinf( D3DXToRadian(angleY) );
camPosZ -= 1.4f *  cosf( D3DXToRadian(angleY) );
}
else if (KEY_DOWN(VK_DOWN))
{
camPosX += 1.4f * -sinf( D3DXToRadian(angleY) );
camPosZ += 1.4f *  cosf( D3DXToRadian(angleY) );
}

// Strafe

if (KEY_DOWN('Q'))
{

}
else if (KEY_DOWN('W'))
{

}
}

[edited by - error on May 29, 2003 2:31:02 PM]

##### Share on other sites
Sonicmage    122
Your problem has to do with the order in which you are multiplying your matrices. The order is yaw, pitch, roll so what you want is V = Y * X * Z ; Or, you can save yourself the hassle and use D3DXMatrixRotationYawPitchRoll.

##### Share on other sites
error    122
ok, i tried that and the roll works but if you roll at say 45 degrees then look up and down you look at that angle as well instead of straight up and down, which makes sense due to the order of rotations. However, I want it to look straight up/down regardless of the other angles

I was doing the rotations in Z,Y,X order because that is what the DX Docs say in the View Transformation section.

D3DXMatrixRotationYawPitchRoll didn''t quite work, all axis were rotating around the axis instead of on the spot, except Y possibly.

Anymore ideas?

error    122
anyone?