Junyu Ke
Junyu Ke

Reputation: 135

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.

Upvotes: 7

Views: 9868

Answers (2)

Junyu Ke
Junyu Ke

Reputation: 135

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.

Upvotes: 6

Paltoquet
Paltoquet

Reputation: 1234

It works when the imaginary part of your quaternion is perpendicular with your vector.

It's your case vec3(0,sin(angle),0) is perpendicular with vec3(0,0,1);

You will see that you need to multiply by the conjugate when it's not right.

q quaternion, v vector.

when you do q * v normally you will obtain a 4D vector, another quaternion. We just don't care about the first component and assume it's 0, a pure quaternion. when you do q * v * q' you are sure to obtain a pure quaternion which translate to a good 3D vector

You can test with non perpendicular vector/quaternion and you will see that your rotation is not right

https://www.3dgep.com/understanding-quaternions/

Upvotes: 1

Related Questions