# 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.

## 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 on other sites
Quote:
 Original post by slicer4evermy 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 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.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

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

• Total Topics
633709
• Total Posts
3013478
×