I'm having a couple of problems while rotating an object every frame with GLM.
First problem
I'm trying to rotate the object with a small increase using quaternion multiplication.
glm::quat deltaQuat = glm::qua(0.001f, glm::vec3(0.0f, 1.0f, 0.0f));
object.SetWorldRotation(object.GetWorldRotation() * deltaQuat);
The result of this code is that the rotation switches back and forward with its inverse each frame, plus the small delta.
Full framerate video here, and the logs I get:
Object world rotation: quat(1.000000, {0.000000, 0.000000, 0.000000})
Object world rotation: quat(0.001000, {0.000000, 1.000000, 0.000000})
Object world rotation: quat(-0.999999, {0.000000, 0.002000, 0.000000})
Object world rotation: quat(-0.003000, {0.000000, -0.999997, 0.000000})
Object world rotation: quat(0.999994, {0.000000, -0.004000, 0.000000})
Object world rotation: quat(0.005000, {0.000000, 0.999990, 0.000000})
Object world rotation: quat(-0.999985, {0.000000, 0.006000, 0.000000})
Object world rotation: quat(-0.007000, {0.000000, -0.999979, 0.000000})
Object world rotation: quat(0.999972, {0.000000, -0.008000, 0.000000})
Object world rotation: quat(0.009000, {0.000000, 0.999964, 0.000000})
Object world rotation: quat(-0.999955, {0.000000, 0.010000, 0.000000})
Object world rotation: quat(-0.011000, {0.000000, -0.999945, 0.000000})
Object world rotation: quat(0.999934, {0.000000, -0.012000, 0.000000})
Object world rotation: quat(0.013000, {0.000000, 0.999922, 0.000000})
Object world rotation: quat(-0.999909, {0.000000, 0.014000, 0.000000})
Object world rotation: quat(-0.015000, {0.000000, -0.999895, 0.000000})
Object world rotation: quat(0.999880, {0.000000, -0.015999, 0.000000})
Object world rotation: quat(0.016999, {0.000000, 0.999864, 0.000000})
Object world rotation: quat(-0.999847, {0.000000, 0.017999, 0.000000})
Object world rotation: quat(-0.018999, {0.000000, -0.999829, 0.000000})
Object world rotation: quat(0.999810, {0.000000, -0.019999, 0.000000})
Second problem
Similarly to the first one, I'm trying to rotate the object with a small increase, this time using euler angles.
object.SetWorldEulerRotation(object.GetWorldEulerRotation() + glm::vec3(0.0f, 0.1f, 0.0f));
// where:
void SetWorldEulerRotation(glm::vec3 const& eulerRotation)
{
SetWorldRotation(glm::quat(eulerRotation));
}
// and
glm::vec3 GetWorldEulerRotation() const
{
return glm::eulerAngles(m_rotation); // m_rotation is a world quaternion
}
The result of this code is that the object's rotation does not go beyond 180º in either direction. It jitters between a little over that rotation and a little under it.
Video here, and the logs I get:
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Object world rotation: quat(0.707403, {0.000000, 0.706810, 0.000000})
Object world rotation: quat(0.703863, {-0.000000, 0.710336, -0.000000})
Is there something I'm misunderstanding about quaternions/Euler angles, rotations in general or the GLM library?
glm::qua(float s, glm::vec3 v)
interprets its arguments. It looks from your code like you're expectings
to be an angle andv
to be a unit vector representing a rotation axis. But from the logs, it looks like we're seeings
as the real (w) component of the quaternion andv
as the imaginary triplet. Do you get anything more sensible if you try something likeglm::qua(glm::cos(angle/2), glm::vec3(0, glm::sin(angle/2), 0))
? \$\endgroup\$glm::quat deltaQuat = glm::quat(glm::vec3(0.0f, 0.001f, 0.0f));
solves the first problem. \$\endgroup\$