summaryrefslogtreecommitdiffstats
path: root/core/math/basis.cpp
diff options
context:
space:
mode:
authorlawnjelly <lawnjelly@gmail.com>2023-05-09 13:49:26 +0100
committerlawnjelly <lawnjelly@gmail.com>2023-05-11 08:34:34 +0100
commit50c5ed4876250f785be54b8f6124e7663afa38dc (patch)
tree33b998adda461dd8d62e11f8bde2463f396de795 /core/math/basis.cpp
parent769d8a7bbe6f59a8a7cae0194b65bf078c9bb2b4 (diff)
downloadredot-engine-50c5ed4876250f785be54b8f6124e7663afa38dc.tar.gz
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.
Diffstat (limited to 'core/math/basis.cpp')
-rw-r--r--core/math/basis.cpp4
1 files changed, 2 insertions, 2 deletions
diff --git a/core/math/basis.cpp b/core/math/basis.cpp
index 95a4187062..bfd902c7e2 100644
--- a/core/math/basis.cpp
+++ b/core/math/basis.cpp
@@ -807,8 +807,8 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
z = (rows[1][0] - rows[0][1]) / s;
r_axis = Vector3(x, y, z);
- // CLAMP to avoid NaN if the value passed to acos is not in [0,1].
- r_angle = Math::acos(CLAMP((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2, (real_t)0.0, (real_t)1.0));
+ // acos does clamping.
+ r_angle = Math::acos((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2);
}
void Basis::set_quaternion(const Quaternion &p_quaternion) {