#### Archived

This topic is now archived and is closed to further replies.

# Quaternion interpolation isssue

This topic is 5448 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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);}

##### Share on other sites
Thanks for your replies, guys. I''ll try it this evening

[Edited by - cmdkeen on May 19, 2008 8:56:39 PM]

##### Share on other sites
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;
}

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013443
×