Skip to content

Commit

Permalink
Also add is_zero_approx() to Vector4.
Browse files Browse the repository at this point in the history
  • Loading branch information
Relintai committed Apr 14, 2024
1 parent 3f2ed00 commit ed22429
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/math/vector4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
1 change: 1 addition & 0 deletions core/math/vector4.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/Vector4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@
<description>
</description>
</method>
<method name="is_zero_approx">
<return type="bool" />
<description>
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.
</description>
</method>
<method name="is_normalized">
<return type="bool" />
<description>
Expand Down

0 comments on commit ed22429

Please sign in to comment.