Search code examples
openglquaternionsglm-math

About glm quaternion rotation


I want to make some rotation by quaternion.

The glm library was done this very well.

The following was my codes:

vec3 v(0.0f, 0.0f, 1.0f);
float deg = 45.0f * 0.5f;
quat q(glm::cos(glm::radians(deg)), 0, glm::sin(glm::radians(deg)), 0);
vec3 newv = q*v;
printf("v %f %f %f \n", newv[0], newv[1], newv[2]);

My question is which in many articles the formula of the rotation by quaternion was

rotated_v = q*v*q_conj

It's weird. In glm the vector "v" just multiply by the quaternion "q" can do the rotation.

It confused me.


Solution

  • After doing some research. I found the definition of the operation "*" in glm quaternion and what is going on in there.

    This implementation is based on those sites.

    Quaternion vector rotation optimisation,

    A faster quaternion-vector multiplication,

    Here's two version of the rotation by quaternion.

    //rotate vector 
    vec3 qrot(vec4 q, vec3 v) 
    { 
        return v + 2.0*cross(q.xyz, cross(q.xyz,v) + q.w*v);
    } 
    

    //rotate vector (alternative) 
    vec3 qrot_2(vec4 q, vec3 v)
    { 
        return v*(q.w*q.w - dot(q.xyz,q.xyz)) + 2.0*q.xyz*dot(q.xyz,v) +    
              2.0*q.w*cross(q.xyz,v);
    } 
    

    If someone can proof that. I would really appreciate it.