Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble converting matrix to quaternion and back

I am trying to store my rotation matrix as a quaternion and then when I want to use it for transformation, convert it back. I am using the glm library which provides mat4_cast and quat_cast for these. However, when I execute the following code:

glm::mat4 origTest = glm::lookAt(position, lookAtPt, up);
glm::quat quatTest = glm::quat_cast(origTest);
glm::mat4 mat4Test = glm::mat4_cast(quatTest);

I get different values for origTest and mat4Test. Am I missing something here? position, lookAtPt and up are glm::vec3.

like image 709
shaveenk Avatar asked Apr 28 '14 09:04

shaveenk


People also ask

Are quaternions faster than matrices?

Using quaternions in the GPU is actually faster than matrices in a modern GPU like the Apple's A8 chip.

How do you create a rotation matrix using quaternion?

Combine the quaternion rotations into a single representation, then apply the quaternion rotation to arbitrarily initialized Cartesian points. Combine the rotation matrices into a single representation, then apply the rotation matrix to the same initial Cartesian points.

Does GLM use quaternions?

The glm::mat4_cast function converts a quaternion into a 4x4 rotation matrix. This stands in place of the series of 3 rotations used in the last tutorial.

How do you convert a matrix to a quaternion?

the matrix is orthogonal. the matrix is special orthogonal which gives additional condition: det (matrix)= +1. Then the matrix can be converted to a quaternion using this basic form: qw= √ (1 + m00 + m11 + m22) /2.

Why is the matrix-to-quaternion-matrix conversion so bad?

This suggests that the matrix-to-quaternion conversion assumes that the matrix is orthonormal and that the quaternion-matrix conversion assumes that the quaternion is a unit quaternion; if these conditions don’t hold, the result is likely to be garbage. If you’re trying to represent arbitrary matrices, then a unit quaternion won’t suffice.

How to find the value of θ in a rotation matrix?

To fix the value of θ, we can use the relation Tr ( M) = 1 + 2 cos θ. Once θ and L is known, the quaternion corresponding to the rotation matrix M is then given by

What is the squared value of the quaternion of rotation matrix?

See equations and discussion in the paper above, p463-464. One of the quaternion elements is guaranteed to have a magnitude of greater than 0.5 and hence a squared value of 0.25. We can use this to determine the "best" set of parameters to use to calculate the quaternion from a rotation matrix


1 Answers

I was able to figure it out. A quaternion stores the rotation of an object in local space. Hence in order to derive the full view matrix that you can get from glm::lookAt(), you would first need to convert the quaternion in local space to a matrix. You would then translate an identity matrix by the desired position and you would perform an SRT multiplication to derive the final view matrix. In this case you need to invert the translation matrix since we are doing an inverse translation on the camera and also you would use an appropriate "lookAt" vector which is in the same direction as (lookAtPt - position)

/* Mat4 to Quat */
glm::vec3 lookAtPt = direction;
glm::mat4 rotMatrix = glm::lookAt(glm::vec3(0), lookAtPt, up);
glm::quat rotation = glm::quat_cast(rotMatrix);

/* Quat to Mat4 */    
glm::mat4 identityMat = glm::mat4(1.0f);
glm::mat4 rotMatrix = glm::mat4_cast(rotation);   //rotation is glm::quat
glm::mat4 transMatrix = glm::translate(identityMat, position); 
glm::mat4 viewMatrix = rotMatrix * glm::inverse(transMatrix);
like image 83
shaveenk Avatar answered Oct 19 '22 12:10

shaveenk