cgmath icon indicating copy to clipboard operation
cgmath copied to clipboard

Quaternion slerp buggy

Open FredrikNoren opened this issue 9 years ago • 2 comments

Hey,

Been trying to use the quaternion slerp method but it seems to return bad results. Here's an algo that worked better for us:

let num2;
let num3;
let num = p;
let mut num4 = (((self.v.x * other.v.x) + (self.v.y * other.v.y)) + (self.v.z * other.v.z)) + (self.s * other.s);
let mut flag = false;
if num4 < 0.0 {
    flag = true;
    num4 = -num4;
}
if num4 > 0.999999 {
    num3 = 1.0 - num;
    num2 = if flag { -num } else { num };
} else {
    let num5 = num4.acos();
    let num6 = 1.0 / num5.sin();
    num3 = ((1.0 - num) * num5).sin() * num6;
    num2 = if flag {
        -(num * num5).sin() * num6
    } else {
        (num * num5).sin() * num6
    };
}
Quaternion::new(
    (num3 * self.s) + (num2 * other.s),
    (num3 * self.v.x) + (num2 * other.v.x),
    (num3 * self.v.y) + (num2 * other.v.y),
    (num3 * self.v.z) + (num2 * other.v.z),
)

Don't have any test cases but just from running this in a 3d application this one returns results that work and the existing one doesn't.

The algo comes from: https://github.com/mono/MonoGame/blob/0b25f56e861fb602db884c40267b99ea947bae15/MonoGame.Framework/Quaternion.cs#L731

FredrikNoren avatar Feb 21 '16 22:02 FredrikNoren

Nice! Also note that I also have some bugs with my other quaternion functions that might also be contributing. See #285 and #290.

brendanzab avatar Mar 25 '16 04:03 brendanzab

Slerp is begging for some tests

kvark avatar Jul 03 '16 03:07 kvark