From efbb28d09a2a4aaa53875edd827e08137f9f19f4 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Tue, 9 May 2023 13:49:26 +0100 Subject: Make acos and asin safe A common bug with using acos and asin is that input outside -1 to 1 range will result in Nan output. This can occur due to floating point error in the input. The standard solution is to provide safe_acos function with clamped input. For Godot it may make more sense to make the standard functions safe. (cherry picked from commit 50c5ed4876250f785be54b8f6124e7663afa38dc) --- core/math/quaternion.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'core/math/quaternion.cpp') diff --git a/core/math/quaternion.cpp b/core/math/quaternion.cpp index 34e212a5b6..e4ad17c8ef 100644 --- a/core/math/quaternion.cpp +++ b/core/math/quaternion.cpp @@ -35,7 +35,8 @@ real_t Quaternion::angle_to(const Quaternion &p_to) const { real_t d = dot(p_to); - return Math::acos(CLAMP(d * d * 2 - 1, -1, 1)); + // acos does clamping. + return Math::acos(d * d * 2 - 1); } Vector3 Quaternion::get_euler(EulerOrder p_order) const { -- cgit v1.2.3