Browse Source

Clamp dot in `angle_between` to avoid precision errors.

Fixes #3978
Jeroen van Rijn 1 year ago
parent
commit
24e6f16f4a
1 changed files with 2 additions and 1 deletions
  1. 2 1
      core/math/linalg/general.odin

+ 2 - 1
core/math/linalg/general.odin

@@ -275,7 +275,8 @@ to_ptr :: proc{vector_to_ptr, matrix_to_ptr}
 vector_angle_between :: proc "contextless" (a, b: $V/[$N]$E) -> E {
 	a0 := normalize0(a)
 	b0 := normalize0(b)
-	return math.acos(dot(a0, b0))
+	d  := clamp(dot(a0, b0), -1, +1)
+	return math.acos(d)
 }
 quaternion64_angle_between :: proc "contextless" (a, b: $Q/quaternion64) -> f16 {
 	c := normalize0(conj(a) * b)