diff --git a/quaternion.cpp b/quaternion.cpp index b778036f..7b0313a3 100644 --- a/quaternion.cpp +++ b/quaternion.cpp @@ -118,17 +118,19 @@ cruft::slerp (quaternion a, quaternion b, T t) b = -b; } - // Calculate coefficients. - // - // We previously used a linear interpolation when cosine was almost 1, but - // it tended to trigger assertions for normalised quaternions; feel free to - // change this back, but at this point performance isn't the most prominent - // concern. - T const omega = std::acos (cosine); // extract theta from dot product's cos theta - T const sine = std::sin (omega); + // Calculate coefficients + T p, q; + if (T(0.999999999f) > cosine) { + T omega = std::acos (cosine); // extract theta from dot product's cos theta + T sine = std::sin (omega); - T const p = std::sin ((1 - t) * omega) / sine; - T const q = std::sin ( t * omega) / sine; + p = std::sin ((1 - t) * omega) / sine; + q = std::sin ( t * omega) / sine; + } else { + // Very close, do linear interp (because it's faster) + p = 1 - t; + q = t; + } return a * p + b * q; }