Archived

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

twan

Camera 3 axis rotate problem

Recommended Posts

twan    128
What i''m trying to do is to make a camera that moves around the three axis. But when i rolled 90 deg my left/right is still the y axe (so it looks like up/down at time position). How can i change it ? Here is my code :
void CCamera::RotateCamera()
{
  D3DXVECTOR3 vDirection, vRotAxis;
  D3DXMATRIX matRotAxis, matRotY, matRotZ;

  D3DXVec3Normalize(&vDirection, &(lookat_vector - eye_vector));
  D3DXVec3Cross(&vRotAxis, &vDirection, &up_vector);
  D3DXVec3Normalize(&vRotAxis, &vRotAxis);

  D3DXMatrixRotationAxis(&matRotAxis, &vRotAxis, xrot / -360);
  D3DXMatrixRotationY(&matRotY, yrot / -360);
  D3DXMatrixRotationZ(&matRotZ, zrot / -360);

  D3DXVec3TransformCoord(&vDirection, &vDirection, &(matRotAxis * matRotY * matRotZ));
  D3DXVec3TransformCoord(&up_vector, &up_vector, &(matRotAxis * matRotY * matRotZ));
  lookat_vector = vDirection + eye_vector;
}
 
Hopefully somebody can help me out.

Share this post


Link to post
Share on other sites
Ghwerig    122
Try this:
  
void CCamera::RotateCamera()
{
D3DXVECTOR3 vDirection, vUp, vRight;
D3DXMATRIX mat,matPitch, matYaw, matRoll;

vDirection=lookat_vector - eye_vector;
D3DXVec3Cross(&vRight, &vDirection, &up_vector);
D3DXVec3Cross(&vUp, &vRight, &vDirection);

D3DXMatrixRotationAxis(&matPitch, &vRight, -xrot*D3DX_PI/180);
D3DXMatrixRotationAxis(&matYaw, &vUp, -yrot*D3DX_PI/180);
D3DXMatrixRotationAxis(&matRoll, &vDirection, -zrot*D3DX_PI/180);

D3DXMatrixMultiply(&mat,&matYaw,&matPitch);
D3DXMatrixMultiply(&mat,&mat,&matRoll);

D3DXVec3TransformCoord(&vDirection, &vDirection, &mat);
D3DXVec3TransformCoord(&up_vector, &up_vector, &mat);
lookat_vector = vDirection + eye_vector;
}

Hope i got it right

Remember:
1. Angles are in radians
2. Matrix multiplications are expensive-store them for reuse
3. Normalisation is expensive. Don't use it if you don't need to

[edited by - Ghwerig on January 15, 2004 4:29:12 PM]

Share this post


Link to post
Share on other sites
twan    128
Thanks for you''re reply. As soon as i''m home i will try it out, i must work right now .

Are quaternions faster then matrix multiplications and normalizing ?

Share this post


Link to post
Share on other sites
Ghwerig    122
I''m not quite sure if quaternions are faster than matrices,
i think D3DX uses SSE for matrix calculations.

If you are going to use quaternions, normalize vDirection, up_vector and vRight and store them in your class. Just check that they are still perpendicular and normalized from time to time.

In my code we are wasting cycles because D3DXMatrixRotationAxis
needs normalized vectors internally and i don''t know if they check if the vector is normalized or just go and normalize it
(to heck with microsoft not giving us D3DX Library source!)

I think i would change to quats just for SLERP.

Share this post


Link to post
Share on other sites
twan    128
Thanks you''re code works fine. I''m also trying to make a quaternion version but i it doesn''t work even not a tiny bit . Do you know some very good tutorials ?

Share this post


Link to post
Share on other sites