From ed224298e5ae951093b42a004afacbd275e54494 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 14 Apr 2024 16:59:12 +0200 Subject: [PATCH] Also add is_zero_approx() to Vector4. --- core/math/vector4.cpp | 4 ++++ core/math/vector4.h | 1 + core/variant/variant_call.cpp | 2 ++ doc/classes/Vector4.xml | 7 +++++++ 4 files changed, 14 insertions(+) diff --git a/core/math/vector4.cpp b/core/math/vector4.cpp index db870e80cd..1ac1399811 100644 --- a/core/math/vector4.cpp +++ b/core/math/vector4.cpp @@ -72,6 +72,10 @@ bool Vector4::is_equal_approx(const Vector4 &p_vec4) const { return Math::is_equal_approx(x, p_vec4.x) && Math::is_equal_approx(y, p_vec4.y) && Math::is_equal_approx(z, p_vec4.z) && Math::is_equal_approx(w, p_vec4.w); } +bool Vector4::is_zero_approx() const { + return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z) && Math::is_zero_approx(w); +} + real_t Vector4::length() const { return Math::sqrt(length_squared()); } diff --git a/core/math/vector4.h b/core/math/vector4.h index 88646b8eb0..595552429c 100644 --- a/core/math/vector4.h +++ b/core/math/vector4.h @@ -73,6 +73,7 @@ struct _NO_DISCARD_CLASS_ Vector4 { _FORCE_INLINE_ real_t length_squared() const; bool is_equal_approx(const Vector4 &p_vec4) const; + bool is_zero_approx() const; real_t length() const; void normalize(); Vector4 normalized() const; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index ba6a9f04eb..79f625c36c 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -706,6 +706,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector4, max_axis); VCALL_LOCALMEM0R(Vector4, length_squared); VCALL_LOCALMEM1R(Vector4, is_equal_approx); + VCALL_LOCALMEM0R(Vector4, is_zero_approx); VCALL_LOCALMEM0R(Vector4, length); VCALL_LOCALMEM0(Vector4, normalize); VCALL_LOCALMEM0R(Vector4, normalized); @@ -2807,6 +2808,7 @@ void register_variant_methods() { ADDFUNC0R(VECTOR4, INT, Vector4, max_axis, varray()); ADDFUNC0R(VECTOR4, REAL, Vector4, length_squared, varray()); ADDFUNC1R(VECTOR4, BOOL, Vector4, is_equal_approx, VECTOR4, "v", varray()); + ADDFUNC0R(VECTOR4, BOOL, Vector4, is_zero_approx, varray()); ADDFUNC0R(VECTOR4, REAL, Vector4, length, varray()); ADDFUNC0(VECTOR4, NIL, Vector4, normalize, varray()); ADDFUNC0R(VECTOR4, VECTOR4, Vector4, normalized, varray()); diff --git a/doc/classes/Vector4.xml b/doc/classes/Vector4.xml index 05fb84f555..e5938d4757 100644 --- a/doc/classes/Vector4.xml +++ b/doc/classes/Vector4.xml @@ -94,6 +94,13 @@ + + + + Returns [code]true[/code] if this vector's values are approximately zero, by running [method @GDScript.is_zero_approx] on each component. + This method is faster than using [method is_equal_approx] with one value as a zero vector. + +