M.Zhao
M.Zhao

Reputation: 53

How to use quaternions to describe a rotation angle which is more than 360 degrees?

I'm trying to use quaternions to do rotation animation. My algorithm creates Quaternions, and slerps every frame. Here is my code to construct a quaternion by the axis and the rotation angle.

template <typename U>
Quaternion(Vector3<U> vec, const float& angle)
{
    vec.normalize();
    float cosa = cos(angle/2);
    float sina = sin(angle/2);
    w = cosa;
    x = sina * vec.x;
    y = sina * vec.y;
    z = sina * vec.z;
}

Then I found that when I tried to rotate 4π radians, the animation does not work because the quaternion I created is equivalent to 0 degrees. I wonder if quaternions can represent rotations over 360 degrees? Or is my animation algorithm in need of improvement?

Upvotes: 5

Views: 4292

Answers (3)

ALX23z
ALX23z

Reputation: 4713

Think of quaternions as instant rotations - rotating by 4π radians instantly is the same as doing nothing.

This is not what you want when you animate rotation of 4π radians over 20 seconds. You can solve it by creating an Euler Vector (a 3D vector whose direction represents the axis of rotation, same as in quaternion, while its length represents the speed/angle of the rotation), see https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation. Later, multiply it by time passed and convert it into quaternion or 3D matrix depending on what your graphics wants.

Upvotes: 1

geza
geza

Reputation: 29970

I wonder if quaternions can represent rotations over 360 degrees?

No, it can not.

Quaternions between the range [360;720] will treated as rotations at the other direction: [-360;0].

And quaternions between the range [720*k; 720*(k+1)] will be treated as rotations [0;720].

If you use slerp for this kind of animation, quaternions are not good for them.

Quaternions can only slerp between angles which are smaller than 360.

If you still want to do this, use a different representation, like axis-angle.

Upvotes: 6

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122955

Rotating be 360 degrees is the same as rotating by 0 degrees. To rotate by an angle alpha bigger than 360 simply rotate by alpha-360 or more general by alpha % 360.

(360 used as synonym for 2pi, you need to take care about degree vs radians of course. And not sure if thats a typo, but 360 degree is 2pi not 4pi)

PS: Actually I think there is nothing wrong with your code, and maybe you dont have to change anything. It's just your expectations that were wrong: You should get the same for a rotation by 4pi as for a rotation by 0.

Upvotes: 1

Related Questions