Rotating across 0 in OpenGL
I'm using quaternions to rotate an object in OpenGL, but I'm having an issue where when I rotate from 5 degrees to -5 degrees (crossing a unit plane) I see my model spin "the long way." That is to say, it spins the full 350 degrees from 5 to -5 instead of the 10 degrees across the plane. This happens for rotations about each axis, anytime I "pass go" by rotating through 0 degrees, and I'm not sure how to fix it.
My code looks like this, where roll, pitch and yaw represent the Euler angles for my object's rotation:
quaternion[0] = cos(pitch/2)*cos(yaw/2)*cos(roll/2) + sin(pitch/2)*sin(yaw/2)*sin(roll/2);
quaternion[1] = sin(pitch/2)*cos(yaw/2)*cos(roll/2) - cos(pitch/2)*sin(yaw/2)*sin(roll/2);
quaternion[2] = cos(pitch/2)*sin(yaw/2)*cos(roll/2) + sin(pitch/2)*cos(yaw/2)*sin(roll/2);
quaternion[3] = cos(pitch/2)*cos(yaw/2)*sin(roll/2) - sin(pitch/2)*sin(yaw/2)*cos(roll/2);
glLoadIdentity();
glPushMatrix();
glRotatef((float) (2.0f * acos(quaternion[0]) * 180.0f / PI), quaternion[1], quaternion[2], quaternion[3]);
Any suggestions?
The problem are not your quaternions or the glRotate(), it is where you calculate the step distance for a rotation.
Possibly you are using a normalization of some kind, where a negative angle like -5
gets "corrected" to a value in the range 0..359, so it will become 355 and your rotation is then performed from 5 to 355.
上一篇: >欧拉
下一篇: 在OpenGL中旋转0