From fc97e72bf42b837e49360199d5243ddcd07ea381 Mon Sep 17 00:00:00 2001 From: Imbris Date: Fri, 15 Oct 2021 13:17:06 -0400 Subject: [PATCH] Fix NaNs --- common/src/comp/ori.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/common/src/comp/ori.rs b/common/src/comp/ori.rs index 72331809fb..025b759b22 100644 --- a/common/src/comp/ori.rs +++ b/common/src/comp/ori.rs @@ -204,7 +204,7 @@ impl Ori { // NOTE: acos is very sensitive to errors at small angles // - https://www.researchgate.net/post/How_do_I_calculate_the_smallest_angle_between_two_quaternions // - see angle_between unit test epislons - let angle = 2.0 * between.w.min(1.0).acos(); + let angle = 2.0 * between.w.min(1.0).max(-1.0).acos(); if angle < PI { angle } else { TAU - angle } } @@ -300,6 +300,10 @@ fn rotation_2d(Vec2 { x, y }: Vec2, axis: Vec3) -> Quaternion { // vector = vec3(0, 0, 1) * +/- sqrt((1 - cos(a)) / 2) // // cos(a) = x / |xy| => x (when normalized) + + // Prevent NaNs from negative sqrt (float errors can put this slightly over 1.0) + let x = x.min(1.0).max(-1.0); + let scalar = ((1.0 + x) / 2.0).sqrt() * y.signum(); let vector = axis * ((1.0 - x) / 2.0).sqrt();