0
votes

I have a quaternion derived from sensors that rotates the "camera" within an OpenGL ES scene. Also I apply the inverse of this quaternion to certain objects in the scene, so they are "facing" the "camera" - this works as expected. The issue is that I need to negate rotation on the Z axis for these objects. How do I come up with a quaternion which has no rotation within the Z component?

My tests: I have attempted to extract euler Angles, create a negating quaternion and build the rotation matrix for these objects from the multiplication of the two quaternions - results are incorrect.

glm::quat rMQ = cam->getCameraQuaternion();// retrieve camera quat
glm::vec3 a = glm::eulerAngles((rMQ))* 3.14159f / 180.f; // Euler angle set derived
glm::quat rMZ = glm::angleAxis(-a.z, vec3(0.0f, 0.0f, 1.0f)); // negating quaternion
glm::mat4 fM = glm::inverse(glm::mat4_cast(rMQ*rMZ)); //final mat4 for GL rotation
2

2 Answers

1
votes

There is no "rotation on the z axis" when you use quaternions, just an axis and an angle. You need to convert to Euler, flip the sign of one component, then convert back to quaternions.

For Euler angles it is up to you to define the order of rotations. Rotations are not commutative, so the order does matter, and that's why there is no generic decomposition of a rotation into components. Normally the order is xyz, but there is no reason why it has to be that way. In some APIs you get to choose the order.

0
votes

Try simply zeroing-out the 'z' component of the quaternion and then re-normalizing.

Quaternions, when used to represent rotation, can be thought of as an 'axis-angle' representation.