diff --git a/core/math/linalg/general.odin b/core/math/linalg/general.odin index f82d75bff..8c4f2954a 100644 --- a/core/math/linalg/general.odin +++ b/core/math/linalg/general.odin @@ -167,6 +167,18 @@ vector_triple_product :: proc "contextless" (a, b, c: $T/[$N]$E) -> T where IS_N length :: proc{vector_length, quaternion_length} length2 :: proc{vector_length2, quaternion_length2} + +@(require_results) +clamp_length :: proc "contextless" (v: $T/[$N]$E, a: E) -> T where IS_FLOAT(E) { + if a <= 0 { + return 0 + } + + m2 := length2(v) + return v if (m2 <= a*a) else (v / sqrt(m2) * a) // returns original when m2 is 0 +} + + @(require_results) projection :: proc "contextless" (x, normal: $T/[$N]$E) -> T where IS_NUMERIC(E) { return dot(x, normal) / dot(normal, normal) * normal