cgmath
cgmath copied to clipboard
Quaternion slerp buggy
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
Nice! Also note that I also have some bugs with my other quaternion functions that might also be contributing. See #285 and #290.
Slerp is begging for some tests