sousou03 / glmatrix

Automatically exported from code.google.com/p/glmatrix
0 stars 0 forks source link

quat4.slerp is broken #54

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This is totally my fault, I didn't do enough verification before I checked in 
the code. In any case, the quaterninon slerp code doesn't do anything like what 
it should right now.

I've got corrected code already, I just need to clean it up before committing. 
The code is below if anyone needs it in the meantime:

quat4.slerp = function(quat, quat2, lerp, dest) {
    if(!dest) { dest = quat; }

    var cosHalfTheta = quat[3] * quat2[3] + quat[0] * quat2[0] + quat[1] * quat2[1] + quat[2] * quat2[2];

    if (Math.abs(cosHalfTheta) >= 1.0){
        if(dest != quat) {
            dest[3] = quat[3];
            dest[0] = quat[0];
            dest[1] = quat[1];
            dest[2] = quat[2];
        }
        return dest;
    }

    var halfTheta = Math.acos(cosHalfTheta);
    var sinHalfTheta = Math.sqrt(1.0 - cosHalfTheta*cosHalfTheta);

    if (Math.abs(sinHalfTheta) < 0.001){
        dest[3] = (quat[3] * 0.5 + quat2[3] * 0.5);
        dest[0] = (quat[0] * 0.5 + quat2[0] * 0.5);
        dest[1] = (quat[1] * 0.5 + quat2[1] * 0.5);
        dest[2] = (quat[2] * 0.5 + quat2[2] * 0.5);
        return dest;
    }
    var ratioA = Math.sin((1 - lerp) * halfTheta) / sinHalfTheta;
    var ratioB = Math.sin(lerp * halfTheta) / sinHalfTheta; 

    dest[3] = (quat[3] * ratioA + quat2[3] * ratioB);
    dest[0] = (quat[0] * ratioA + quat2[0] * ratioB);
    dest[1] = (quat[1] * ratioA + quat2[1] * ratioB);
    dest[2] = (quat[2] * ratioA + quat2[2] * ratioB);

    return dest;
}

Original issue reported on code.google.com by Tojiro@gmail.com on 24 Apr 2011 at 4:22

GoogleCodeExporter commented 8 years ago

Original comment by Tojiro@gmail.com on 7 Oct 2011 at 6:53