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


Camera vectors from vector+quaternion

Recommended Posts

Reposted in DX as no joy from the maths forum 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
>>Why do you want to use LookAt instead of generating the matrix from the quaternion/position?

Good question..... this is confusing me now (I don''t get much time to look at this, and everytime I do I forget where I was up to

I have the world matrix for the entity, which all functions correctly. If I use it ''as is'' in SetTransform(...) then it is reversed in all axis (as though I''m looking out the back of the camera), if I use Matrix.Inverse() then it is reversed in 2 axis.

So... if I have a vector+quaternion how can I generate the correct view matrix from it? And while I''m on the subject, how can I point one vector+quat at another vector.

/sigh .... head hurts....

Share this post

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

Maths and Physics Forum Trhead

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

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