Artifacts when interpolating my own quaternions

colinhect
Posts: 3
Joined: Mon Jan 18, 2010 6:33 pm

Artifacts when interpolating my own quaternions

Post by colinhect »

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

http://www.youtube.com/watch?v=aHueytww10k

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

Code: Select all

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?