Archived

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

Jerimiah

Converting from Quaternion to DX LookAtLH

Recommended Posts

Partly D3D - but as this more of a maths problem thought better to post here. I have my world objects and my camera stored in the same base class. So they each share lots of properties. Notably a world position and a quaternion specifying their rotation. For normal objects this is great, the position vector + the rotation quaternion let me create a world Transform matrix. The bit I''m getting confused by is the camera part. It needs to construct a view matrix. What I have at the moment (for hack purposes) is: Matrix.LookAtLH(new Vector3(_xPos,_yPos,_zPos), _lookAt, new Vector3(0f,1f,0f)); Where _xPos etc specify the world position, but I need a way to generate the _lookAt and Up vectors from the position+rotation I have stored. Any suggestions?

Share this post


Link to post
Share on other sites
For a regular object, you get the orientation matrix by converting the quaternion
q = q_yaw * q_pitch * q_roll
to a matrix. To do the same thing with a camera, you have to do the inverse transformation, which can be done either by finding the inverse of that matrix, or by calculating the matrix of
q = q_(-roll) * q_(-pitch) * q_(-yaw)

In case you don''t know, the matrix associated with the quaternion q = (n, x, y, z) is


n^2 + x^2 - y^2 - z^2 2xy - 2zn 2xz + 2yn
2xy + 2zn n^2 - x^2 + y^2 - z^2 2yz - 2xn
2zx - 2yn 2zy + 2xn n^2 - x^2 - y^2 + z^2

Share this post


Link to post
Share on other sites
D3DMATRIX camTransform;
D3DMATRIX camViewTransform;

// Obtain camera transform from a quaternion.

D3DXMatrixRotationQuaternion( &camTransform, &camOrienationQuat );

// Build camera "view" transform.

D3DVECTOR* camTransformYAxis = ( D3DVECTOR* )( &camTransform.m[1][0] );
D3DVECTOR* camTransformZAxis = ( D3DVECTOR* )( &camTransform.m[2][0] );

D3DXMatrixLookAtLH( &camViewTransform, &camPos, &( camPos + camTransformZAxis ), &camTransformYAxis );

Share this post


Link to post
Share on other sites
Darn - nearly there (thanks both of you) ..... just one catch - I''m doing this in C#, and there is no damned documentation for DX....

>>D3DVECTOR* camTransformYAxis = ( D3DVECTOR* )( &camTransform.m[1][0] );
>>D3DVECTOR* camTransformZAxis = ( D3DVECTOR* )( &camTransform.m[2][0] );

As we don''t have pointers into the Matrix structure in C#, which elements of the matrix are needed.... (M21, M22, M23 and M31, M32, M33 I''m guessing?)

Share this post


Link to post
Share on other sites
Thanks all - for future reference... here''s the C# solution....

Firstly, Meduzza''s code was damned near there, but the order of the camTransformYAxis/camTransformZAxis generation was different to the order in LookATLH - so my first mistake was getting these reversed (DOH!)... yes.. M21, M22, M23 and M31, M32, M33 are the correct elements.

Secondly, Meduzza''s solution provided a rear view camera - so I had to reverse them - prviously this wasn''t easy (which was where my problem was comming from), but now the vectors had been obtained it was simple. Thanks Aprosenf for the -(x) suggestion.


  
// Horribly unoptimised....

// _matrixRotation is a rotation matrix

// _quatRotation is a stored rotation quaternion

// _xPos, _yPos, zPos - the position of the camera

_matrixRotation.RotateQuaternion(_quatRotation);
Vector3 camPos = new Vector3(_xPos,_yPos,_zPos);
Vector3 camTY = new Vector3(_matrixRotation.M21,_matrixRotation.M22,_matrixRotation.M23);
Vector3 camTZ = new Vector3(-(_matrixRotation.M31),-(_matrixRotation.M32),-(_matrixRotation.M33));
return Matrix.LookAtLH(camPos, camPos + camTZ, camTY);

Share this post


Link to post
Share on other sites