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

## 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 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 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 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 on other sites
I have one more question how do I calculate the vecPos and vecRot?

##### 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 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 on other sites
That doesn't really use quats, but hey [grin]

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

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631748
• Total Posts
3002030
×