Sign in to follow this  
Ebola0001

need help combining cameras

Recommended Posts

Strange title, but it’s the best I could think of. What I have is a quaternion rotated camera... works well when its at 0,0,0 And I have the camera position... works well when it’s not rotated however when i combine them, it seems like its applying the transformation first THEN the rotation so its rotating the world around a point rather than rotating the camera. Here is what I’m doing
if (CamYaw != 0 | CamPitch != 0 | CamRoll != 0)
{
 D3DXQuaternionRotationMatrix(&matViewQuatIn, &matView);
 D3DXQuaternionRotationYawPitchRoll(&matViewQuat, CamYaw, CamPitch, CamRoll);
 D3DXQuaternionMultiply(&matViewQuatOut, &matViewQuatIn, &matViewQuat);
 D3DXQuaternionNormalize(&matViewQuatOut, &matViewQuatOut);
 D3DXMatrixRotationQuaternion(&matView, &matViewQuatOut);
}

matView._41 = CameraPosition.x;
matView._42 = CameraPosition.y;
matView._42 = CameraPosition.z;
    d3ddev->SetTransform(D3DTS_VIEW, &(matView));    // set the view transform to matView


obviously this is not the best solution (as it doesn't work) I have also tried forming a matrix using only the Camera position information and multiplying them
//matView._41 = CameraPosition.x;
//matView._42 = CameraPosition.y;
//matView._42 = CameraPosition.z;
D3DXMatrixTranslation(&matViewTranslation, &CameraPosition.x, &CameraPosition.y, &CameraPosition.z);
    d3ddev->SetTransform(D3DTS_VIEW, &(matView * matViewTranslation));    // set the view transform to matView


This yielded no camera movement whatsoever, it sat there, even though Camera Position was moving around. These code snippets were retyped, because I ripped out the code that didn't work out of frustration... so there may be some typos but that’s basically what I tried. So... what am I doing wrong?

Share this post


Link to post
Share on other sites
Two things I noticed. First, I'm not sure exactly what your intent with the quaternion multiplication is, but remember that (IIRC) in DirectX the quaternion product q1*q2 applies the rotations in the order q1->q2, counter to mathematical convention.

Also, it doesn't look like you're inverting the matrix before sending it to D3D; this is most likely part of the problem.

Share this post


Link to post
Share on other sites
OK... I got it to work now... thanks for the direction jyk :)

These were my thoughts on the quaternion math thing

These are the steps i go through to make my view matrix :)


1. Get the current View's rotation as a Quaternion
2. Make a Quaternion with the amount of rotational change this frame
3. Multiply the two together to get the new view rotation quat(with the current view quat from 1 as PQ1, if they are the other way around it will gimbal lock)
4. Normalize the new view quat to prevent generations of small math errors
5. Turn the new view quat into a matrix
6. make a new Identity Matrix to hold the translational info
7. move the camera position x,y,z to the matrix's fourth row
8. Multiply the Translation Matrix, and the Rotation Matrix, Order doesn't matter
9. Set the View Transform to the new combined translate/Rotate matrix
10. Sit back and smile

Here is the final code

D3DXQuaternionRotationMatrix(&matViewQuatIn,&matView); //Step 1
D3DXQuaternionRotationYawPitchRoll(&matViewQuat,CamYaw,CamPitch,CamRoll);//Step 2
D3DXQuaternionMultiply(&matViewQuatOut,&matViewQuatIn,&matViewQuat); //Step 3
D3DXQuaternionNormalize(&matViewQuatOut, &matViewQuatOut); //Step 4
D3DXMatrixRotationQuaternion(&matViewOut,&matViewQuatOut); //Step 5
D3DXMatrixIdentity(&matView); //Step 6
matView._41 = CameraPosition.x; //Step 7
matView._42 = CameraPosition.y;
matView._43 = CameraPosition.z;
matView = matView * matViewOut; //Step 8
d3ddev->SetTransform(D3DTS_VIEW, &(matView)); //Step 9





11 Lines of code for a 6 degree freedom camera... I'm proud of myself :)

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