Jump to content
  • Advertisement
Sign in to follow this  
israel

Camera Rotation using Quaternions and DirectX and C#

This topic is 4906 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 all, I need to implement camera rotation. I think I need to get a quaternion of the current view, calculate a quaternion for the rotation and set the new view. My problem is that I don't know how to implement that. Does anybody know a link to an example or the code? Thanx [Edited by - israel on February 8, 2005 4:33:13 PM]

Share this post


Link to post
Share on other sites
Advertisement


oid Camera::Update(D3DXVECTOR3 vecPos, D3DXVECTOR3 vecRot)
{
vecCameraSource.x = vecPos.x; //x
vecCameraSource.y = vecPos.y; //y
vecCameraSource.z = vecPos.z; //z

D3DXQUATERNION QUAT;
D3DXMATRIX matAT;
D3DXMATRIX matRot;
D3DXMATRIX matView;
D3DXQuaternionRotationYawPitchRoll(&QUAT, DegreesToRadians(vecRot.z), DegreesToRadians(vecRot.y),DegreesToRadians(vecRot.x));
D3DXMatrixAffineTransformation(&matAT, 1.00f, NULL, &QUAT, &vecCameraSource);

//D3DXMatrixRotationZ(&matRot, DegreesToRadians(vecRot.x));
D3DXMatrixInverse(&matView, NULL, &matAT);
//matView = matView * matRot;
matCameraView = matView;
pd3dDevice->SetTransform(D3DTS_VIEW, &matCameraView);

frFrustum.Update( matCameraView, matProj);
}




You can do per rotation or just use D3DXQuaternionRotationYawPitchRoll()... it works quite well in most cases.

Share this post


Link to post
Share on other sites
Sorry, my brain is not working. I forgot to mention 'm working in c# and I want to accumulate the rotations.

Share this post


Link to post
Share on other sites
thats in c++, its not to rip straight from the post, just look at it and check msdn.microsoft.com for what each function does... its pretty straight forward... spend some time at it...

Share this post


Link to post
Share on other sites
your position is where you want your camera to be located, and the rotation is like in half life, quake, or doom, when you look up or to the side.

Share this post


Link to post
Share on other sites
Thanx all, I found the answer. It was much easier than I thought, here is the code in case somebody ever needs it:

public void rotateUp()
{
Matrix mat = device.Transform.World; // my current view
Matrix nMat = Matrix.Identity;
nMat.RotateX((float)(angleInDeg * Math.PI / 180.0)); // a matrix rotated up

Matrix final = Matrix.Multiply(mat, nMat); // adding my old view with the new rotation
device.Transform.World = final; // setting the new view
}

Share this post


Link to post
Share on other sites
with that you hafta worry about gimbal locks, i would defnitly switch to quaternions now before it becomes a pain later...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!