Quaternion interpolation isssue

Started by
4 comments, last by cmdkeen 20 years, 4 months ago
Hello, when using quaternions for rotations I faced a problem: when I interpolate between two quaternions with different axes the path of the rotated object is buckled. Example: when I have a rotation with axis = (0, 0, 1) and 90 degree and rotate to (1, 0, 0) and 180 degree I run through the inbetween states (1/3, 0, 2/3), 120 and (2/3, 0, 1/3), 150. Thus the path of the rotation is not straight.. I interpolate between the two quaternions by interpolating the angle and the axis independently and then creating a new quaternion from the result. I thought about the problem and came to the conclusion that this follows directly from the involved math. It seems to be a consequence of the interpolation between both axes. Has anyone an idea how to fix this? [Edited by - cmdkeen on May 19, 2008 8:32:08 PM]
Advertisement
OK, I only really know enough about quats to get me by, but heres some ideas:

you say you interpolate the axis and angle independantly - thats probably where the problem is. Just interpolate the quat directly (not that I can tell you how to that! but check out the code in the docs at http://math3d.sf.net for ideas). As far as I know the fact the the quat is 4d is what lets it represent orderless rotations, and be interpolated smoothly.

Oh yeah, might want to look at spherical interpolation rather than linear. Again I know enough to use use them, but couldn''t exaplin how to implement them.
[size="1"]
Actually, you want to use Spherical Linear Interpolation (SLERP), which is (conveniently) exactly what interpolating the quaternion directly does.

There''s a great quaternion article on Gamasutra:

http://www.gamasutra.com/features/19980703/quaternions_01.htm

Good luck
Something like this:
inline void EQuat::SLerp(EQuat *out, EQuat *q2, float ratio){  float  omega, cosom, sinom, scale0, scale1;  // calc cosine  cosom = x * q2->x + y * q2->y + z * q2->z + w * q2->w;  // adjust signs (if necessary)  if ( cosom < 0.0 )  {    cosom = -cosom;    scale1 = -1.0f;  }  else  {    scale1 = 1.0f;  }  if ( (1.0 - cosom) > 0.0001f/*DELTA*/ )  {    // standard case (slerp)    omega = acosf(cosom);    sinom = sinf(omega);    scale0 = sinf((1.0f - ratio) * omega) / sinom;    scale1 *= sinf(ratio * omega) / sinom;  }  else  {            // "from" and "to" quaternions are very close     //  ... so we can do a linear interpolation    scale0 = 1.0f - ratio;    scale1 *= ratio;  }  // calculate final values  out->x = (EFloat) (scale0 * x + scale1 * q2->x);  out->y = (EFloat) (scale0 * y + scale1 * q2->y);  out->z = (EFloat) (scale0 * z + scale1 * q2->z);  out->w = (EFloat) (scale0 * w + scale1 * q2->w);}

Thanks for your replies, guys. I''ll try it this evening

[Edited by - cmdkeen on May 19, 2008 8:56:39 PM]
And if you use it only for animation you can try the linear interpolation (it is approximately like SLERP), it is a lot faster.

inline xxquaternion lerp_unit(const xxquaternion& q1, const xxquaternion& q2, float t ){
xxquaternion r;
float inner = q1.x*q2.x + q1.y*q2.y + q1.z*q2.z +q1.w*q2.w;
if(inner < 0 ){
r = q1 - (q2+q1)*t;
}else{
r = q1 + (q2-q1)*t;
}
r.normalize();
return r;
}

This topic is closed to new replies.

Advertisement