How to rotate any vector by quaternion in glm?
I am experimenting with quaternion rotations and I can't figure out what is correct ... I am trying to rotate each vector quaternion (3 vectors = fwVec3, upVec3, rightVec3) but the axis does not rotate correctly - for example when I rotate 90 degrees around the rightVec3 object makes the object face down, meaning it now rotates 90 degrees, but when I rotate around the new upvector object, the object does not rotate around its own upvector, but instead, the world world advance ...
Please trust me when I say I have been looking for a while and I can't find anything that helps ...
My code:
Class members:
glm::vec3 m_DirectionWS = glm::vec3(1.0f, 0.0f, 0.0f); // FORWARD
glm::vec3 m_UpWS = glm::vec3(0.0f, 1.0f, 0.0f); // UP
glm::vec3 m_RightWS = glm::vec3(0.0f, 0.0f, 1.0f); // RIGHT
Rotation method:
void rotate(const float& angle, const glm::vec3& axis)
{
const float& aot = angle / 2.0f;
const float& cos_aot = sin(aot);
const float& sin_aot = cos(aot);
const glm::quat& quaternion = glm::quat(cos_aot, sin_aot * axis[0], sin_aot * axis[1], sin_aot * axis[2]);
// Multiply each local axis (vec3) with quaternion & normalize them
m_DirectionWS = glm::conjugate(quaternion) * m_DirectionWS * quaternion;
m_UpWS = glm::conjugate(quaternion) * m_UpWS * quaternion;
m_RightWS = glm::conjugate(quaternion) * m_RightWS * quaternion;
m_ModelMtx = glm::lookAt(m_Position, m_Position + m_DirectionWS, m_UpWS)
}
Calling the rotation function in the main loop:
Attempting to rotate an object around it own upward vector
entity->rotate(0.01f, entity->getUpWS());
What am I doing wrong? If I understand correctly, all there is is to multiply each axis by the rotation quaternion and then create a model matrix.
I am using glm :: lookAt (); to plot this matrix using the position of the object in world space, the direction of the worldpos + entity forward to get to where the object is, and its upvector.
I'm not sure if this is the order of the multiplication or how I am constructing the matrix, but something is not working here.
Anyone who understands 3D rotations with quaternions that can help me solve this example using 3 vectors for the object's axis fw, up, right and quaternion (angle, axis) to build a matrix model?
source to share
No one has answered this question yet
Check out similar questions: