• Create Account

## Issues interpolating quaternions from Bullet

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1colinhect  Members

197
Like
0Likes
Like

Posted 14 June 2011 - 03:23 PM

My game has a fixed time-step and interpolates translations and rotations between updates. My quaternion slerp function works fine for my own rotations, but when converting Bullet's quaternions to my own quaternion structure I get strange artifacts occasionally when slerping. The body will appear to slerp the wrong direction for a split second. Here is a video demonstrating it (the jerking in the beginning is due to the video capture process).

Here is my code for getting the rigid body's rotation and for slerping:


const Quaternion<> RigidBody::getOrientation() const {
return Physics::fromQuaternion(rigidBody->getWorldTransform().getRotation());
}

const Quaternion<> Physics::fromQuaternion(const btQuaternion& q) {
return Quaternion<double>(-q.w(), Vector<3, double>(q.x(), q.y(), q.z()));
}

template <class T>
const Quaternion<T> Quaternion<T>::slerp(const Quaternion<T>& a, const Quaternion<T>& b, T delta) {
T w1, w2;

T cosTheta = dot(a, b);
T theta    = (T)acos(cosTheta);
T sinTheta = (T)sin(theta);

if (sinTheta > 0.001) {
w1 = (T)(sin((1.0 - delta) * theta) / sinTheta);
w2 = (T)(sin(delta * theta) / sinTheta);
} else {
w1 = 1.0 - delta;
w2 = delta;
}

Quaternion<T> result(a * w1 + b * w2);
result.normalize();

return result;
}

template const Quaternion<double> Quaternion<double>::slerp(const Quaternion<double>&, const Quaternion<double>&, double);


My slerp code works fine for my own rotations. This only seems to happen on quaternions converted from Bullet.

Any ideas?

### #2scgames  Members

2082
Like
1Likes
Like

Posted 14 June 2011 - 09:49 PM

Since q and -q represent the same orientation, a typical implementation of quaternion SLERP will first check the dot product of the input quaternions and negate one of them if the dot product is negative (this ensures that the interpolation takes place over the shortest arc).

Although I can't say for sure that that's what's causing the problems you're seeing, it seems likely that it is, so that's probably the first thing I'd try.

### #3colinhect  Members

197
Like
0Likes
Like

Posted 15 June 2011 - 10:11 AM

Since q and -q represent the same orientation, a typical implementation of quaternion SLERP will first check the dot product of the input quaternions and negate one of them if the dot product is negative (this ensures that the interpolation takes place over the shortest arc).

Although I can't say for sure that that's what's causing the problems you're seeing, it seems likely that it is, so that's probably the first thing I'd try.

Thank you very much for the response. Here is my updated code:


template <class T>
const Quaternion<T> Quaternion<T>::slerp(const Quaternion<T>& a, const Quaternion<T>& b, T delta) {
T w1, w2;

T cosTheta = dot(a, b);
T theta    = (T)acos(cosTheta);
T sinTheta = (T)sin(theta);

if (sinTheta > 0.001) {
w1 = (T)(sin((1.0 - delta) * theta) / sinTheta);
w2 = (T)(sin(delta * theta) / sinTheta);
} else {
w1 = 1.0 - delta;
w2 = delta;
}

Quaternion<T> b1;

if (cosTheta < 0.0) {
b1 = -b;
} else {
b1 = b;
}

Quaternion<T> result(a * w1 + b1 * w2);
result.normalize();

return result;
}


This gives me much better results. However there are still moments where the rotation will stutter (it looks way better than rotating the opposite direction though). Here is a quick video demonstrating it (the framerate is constant here, the stuttering is only on the rotating rigid bodies).

However, I tried just using linear interpolation and I don't get this stuttering effect (and honestly I cannot observe any difference between slerp and lerp). I am just going to use lerp for now since it has no visual artifacts. But if you have any ideas as to why my slerp is stuttering I'd love to hear them.

I really appreciate the help. I'm pretty sure you have answered the majority of my (everyone's?) math-related questions on this board.

### #4scgames  Members

2082
Like
1Likes
Like

Posted 15 June 2011 - 10:41 AM

The main difference between SLERP and NLERP is that the latter doesn't interpolate with constant angular velocity (assuming the interpolation parameter changes at a constant rate, the speed of the rotation will increase and then decrease over the course of the interpolation). In many cases this effect is not noticeable though. Furthermore, NLERP has the advantage of being less expensive than SLERP.

Regarding the artifacts you're seeing, try checking the dot product and performing the negation (if necessary) as the first step in the function, rather than towards the end. (As is, you're computing some of the initial values using the 'uncorrected' input quaternions, which I imagine could cause some problems.)

### #5colinhect  Members

197
Like
0Likes
Like

Posted 15 June 2011 - 12:04 PM

The main difference between SLERP and NLERP is that the latter doesn't interpolate with constant angular velocity (assuming the interpolation parameters changes at a constant rate, the speed of the rotation will increase and then decrease over the course of the interpolation). In many cases this effect is not noticeable though. Furthermore, NLERP has the advantage of being less expensive than SLERP.

Regarding the artifacts you're seeing, try checking the dot product and performing the negation (if necessary) as the first step in the function, rather than towards the end. (As is, you're computing some of the initial values using the 'uncorrected' input quaternions, which I imagine could cause some problems.)

Ahh that was a silly mistake. Thanks once again for the help. It works great now.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.