Jump to content
  • Advertisement
Sign in to follow this  
slicer4ever

quaternion slerp, wrong direction?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, i seem to be having a problem with SLERP, in that it's not moving to the expected location by the shortest distance, instead it seems to be going around in the longest possible direction.

here's my SLERP code(SPEVector4 is simply: typedef float SPEVector4[4];, note in order: xyzw)


void SPEQuatSLERP(SPEVector4 a, SPEVector4 b, float Time, SPEVector4 Out){
float cTheta = a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
float Theta = (float)acosf(cTheta);
float sTheta = (float)sinf(Theta);
float w1=0.0f, w2=0.0f;
if(sTheta>0.001f){
w1 = (float)(sinf((1.0f-Time)*Theta)/sTheta);
w2 = (float)(sinf(Time*Theta)/sTheta);
}else{
w1 = 1.0f-Time;
w2 = Time;
}
for(unsigned int i=0;i<4;i++) Out=a*w1+b*w2;
return;
}

the a quat is:
0.0, 0.0, 0.0, 1.0
and the b quat is:
0.0, -.707101, 0.0, -.707101 (in other words 90 degrees around the y axis)
at 0.25 time i get:
0.0, -0.555, 0.0, 0.831
at 0.5 time i get:
0.0, -0.923, 0.0, 0.382
and at 0.75 i get:
0.0, -0.980, 0.0, -0.195

which basically means it's spinning around the y axis in the wrong direction, or at least taking the longest route to quat b rather than the shortest, is their a reason for this, my understanding was that SLERP generated the closest path to second quaternion.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by slicer4ever
my understanding was that SLERP generated the closest path to second quaternion.
Yes, but that's not necessarily the fastest angular path because 50% of quaternions map to the same rotation (if you negate a quaternion, it's the same rotation but a different quat). You must handle this specifically in your code. You can check whether the dot product is less than zero and negate the quaternion if it is to ensure you're interpolating around the shortest arc.

Share this post


Link to post
Share on other sites
ah, thanks very much, that tip solved the problem perfectly, thanks for the help, i didn't think about the fact that the negative quaternion would be the problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!