Sign in to follow this  
israel

Camera Rotation using Quaternions and DirectX and C#

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


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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this