From e26f0906f229f237ad22d5b1986c1669b354b791 Mon Sep 17 00:00:00 2001 From: VolTer Date: Sat, 24 Sep 2022 14:20:06 +0200 Subject: [PATCH] Implement snappedi, snappedf, and Vector[2/3/4]i.snapped --- core/math/vector2i.cpp | 6 +++ core/math/vector2i.h | 1 + core/math/vector3i.cpp | 7 +++ core/math/vector3i.h | 1 + core/math/vector4i.cpp | 8 +++ core/math/vector4i.h | 1 + core/variant/variant_call.cpp | 3 ++ core/variant/variant_utility.cpp | 87 ++++++++++++++++++++++++++++++-- doc/classes/@GlobalScope.xml | 59 +++++++++++++++++----- doc/classes/Vector2.xml | 16 +++--- doc/classes/Vector2i.xml | 9 +++- doc/classes/Vector3.xml | 12 ++--- doc/classes/Vector3i.xml | 9 +++- doc/classes/Vector4.xml | 10 ++-- doc/classes/Vector4i.xml | 9 +++- tests/core/math/test_vector2i.h | 8 ++- tests/core/math/test_vector3i.h | 8 +++ tests/core/math/test_vector4i.h | 8 +++ 18 files changed, 222 insertions(+), 40 deletions(-) diff --git a/core/math/vector2i.cpp b/core/math/vector2i.cpp index dfed42e4d6b..ff8693ee5bc 100644 --- a/core/math/vector2i.cpp +++ b/core/math/vector2i.cpp @@ -39,6 +39,12 @@ Vector2i Vector2i::clamp(const Vector2i &p_min, const Vector2i &p_max) const { CLAMP(y, p_min.y, p_max.y)); } +Vector2i Vector2i::snapped(const Vector2i &p_step) const { + return Vector2i( + Math::snapped(x, p_step.x), + Math::snapped(y, p_step.y)); +} + int64_t Vector2i::length_squared() const { return x * (int64_t)x + y * (int64_t)y; } diff --git a/core/math/vector2i.h b/core/math/vector2i.h index e131bdea944..927be110301 100644 --- a/core/math/vector2i.h +++ b/core/math/vector2i.h @@ -119,6 +119,7 @@ struct _NO_DISCARD_ Vector2i { Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); } Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); } Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const; + Vector2i snapped(const Vector2i &p_step) const; operator String() const; operator Vector2() const; diff --git a/core/math/vector3i.cpp b/core/math/vector3i.cpp index b248f35035b..901f2b5a640 100644 --- a/core/math/vector3i.cpp +++ b/core/math/vector3i.cpp @@ -48,6 +48,13 @@ Vector3i Vector3i::clamp(const Vector3i &p_min, const Vector3i &p_max) const { CLAMP(z, p_min.z, p_max.z)); } +Vector3i Vector3i::snapped(const Vector3i &p_step) const { + return Vector3i( + Math::snapped(x, p_step.x), + Math::snapped(y, p_step.y), + Math::snapped(z, p_step.z)); +} + Vector3i::operator String() const { return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ")"; } diff --git a/core/math/vector3i.h b/core/math/vector3i.h index 710fd963765..36bac3d8ae6 100644 --- a/core/math/vector3i.h +++ b/core/math/vector3i.h @@ -77,6 +77,7 @@ struct _NO_DISCARD_ Vector3i { _FORCE_INLINE_ Vector3i abs() const; _FORCE_INLINE_ Vector3i sign() const; Vector3i clamp(const Vector3i &p_min, const Vector3i &p_max) const; + Vector3i snapped(const Vector3i &p_step) const; /* Operators */ diff --git a/core/math/vector4i.cpp b/core/math/vector4i.cpp index 77f6fbd5b71..e906ab45ad5 100644 --- a/core/math/vector4i.cpp +++ b/core/math/vector4i.cpp @@ -65,6 +65,14 @@ Vector4i Vector4i::clamp(const Vector4i &p_min, const Vector4i &p_max) const { CLAMP(w, p_min.w, p_max.w)); } +Vector4i Vector4i::snapped(const Vector4i &p_step) const { + return Vector4i( + Math::snapped(x, p_step.x), + Math::snapped(y, p_step.y), + Math::snapped(z, p_step.z), + Math::snapped(w, p_step.w)); +} + Vector4i::operator String() const { return "(" + itos(x) + ", " + itos(y) + ", " + itos(z) + ", " + itos(w) + ")"; } diff --git a/core/math/vector4i.h b/core/math/vector4i.h index a32414bb188..cb5a48daf9e 100644 --- a/core/math/vector4i.h +++ b/core/math/vector4i.h @@ -79,6 +79,7 @@ struct _NO_DISCARD_ Vector4i { _FORCE_INLINE_ Vector4i abs() const; _FORCE_INLINE_ Vector4i sign() const; Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const; + Vector4i snapped(const Vector4i &p_step) const; /* Operators */ diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 91af2bab856..721cf71808c 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1646,6 +1646,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector2i, sign, sarray(), varray()); bind_method(Vector2i, abs, sarray(), varray()); bind_method(Vector2i, clamp, sarray("min", "max"), varray()); + bind_method(Vector2i, snapped, sarray("step"), varray()); /* Rect2 */ @@ -1734,6 +1735,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector3i, sign, sarray(), varray()); bind_method(Vector3i, abs, sarray(), varray()); bind_method(Vector3i, clamp, sarray("min", "max"), varray()); + bind_method(Vector3i, snapped, sarray("step"), varray()); /* Vector4 */ @@ -1773,6 +1775,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector4i, sign, sarray(), varray()); bind_method(Vector4i, abs, sarray(), varray()); bind_method(Vector4i, clamp, sarray("min", "max"), varray()); + bind_method(Vector4i, snapped, sarray("step"), varray()); /* Plane */ diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index 3843c32bcc3..e8883f96aa7 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -322,8 +322,52 @@ struct VariantUtilityFunctions { return Math::step_decimals(step); } - static inline double snapped(double value, double step) { - return Math::snapped(value, step); + static inline Variant snapped(const Variant &x, const Variant &step, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + if (x.get_type() != step.get_type() && !((x.get_type() == Variant::INT && step.get_type() == Variant::FLOAT) || (x.get_type() == Variant::FLOAT && step.get_type() == Variant::INT))) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 1; + return Variant(); + } + + switch (step.get_type()) { + case Variant::INT: { + return snappedi(x, VariantInternalAccessor::get(&step)); + } break; + case Variant::FLOAT: { + return snappedf(x, VariantInternalAccessor::get(&step)); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor::get(&x).snapped(VariantInternalAccessor::get(&step)); + } break; + case Variant::VECTOR2I: { + return VariantInternalAccessor::get(&x).snapped(VariantInternalAccessor::get(&step)); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor::get(&x).snapped(VariantInternalAccessor::get(&step)); + } break; + case Variant::VECTOR3I: { + return VariantInternalAccessor::get(&x).snapped(VariantInternalAccessor::get(&step)); + } break; + case Variant::VECTOR4: { + return VariantInternalAccessor::get(&x).snapped(VariantInternalAccessor::get(&step)); + } break; + case Variant::VECTOR4I: { + return VariantInternalAccessor::get(&x).snapped(VariantInternalAccessor::get(&step)); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double snappedf(double x, double step) { + return Math::snapped(x, step); + } + + static inline int64_t snappedi(double x, int64_t step) { + return Math::snapped(x, step); } static inline Variant lerp(const Variant &from, const Variant &to, double weight, Callable::CallError &r_error) { @@ -1132,6 +1176,40 @@ static _FORCE_INLINE_ Variant::Type get_ret_type_helper(void (*p_func)(P...)) { }; \ register_utility_function(#m_func, m_args) +#define FUNCBINDVR2(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], r_error); \ + } \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError ce; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], ce); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Callable::CallError ce; \ + Variant r; \ + r = VariantUtilityFunctions::m_func(PtrToArg::convert(p_args[0]), PtrToArg::convert(p_args[1]), ce); \ + PtrToArg::encode(r, ret); \ + } \ + static int get_argument_count() { \ + return 2; \ + } \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { return false; } \ + static Variant::UtilityFunctionType get_type() { return m_category; } \ + }; \ + register_utility_function(#m_func, m_args) + #define FUNCBINDVR3(m_func, m_args, m_category) \ class Func_##m_func { \ public: \ @@ -1415,6 +1493,10 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(signf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(signi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDVR2(snapped, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(snappedf, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(snappedi, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(pow, sarray("base", "exp"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(log, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(exp, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); @@ -1428,7 +1510,6 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(ease, sarray("x", "curve"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(snapped, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDVR3(lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(lerpf, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index ed404ea5042..60ed7fac8ef 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -16,7 +16,7 @@ - Returns the absolute value of a [Variant] parameter [param x] (i.e. non-negative value). Variant types [int], [float], [Vector2], [Vector2i], [Vector3] and [Vector3i] are supported. + Returns the absolute value of a [Variant] parameter [param x] (i.e. non-negative value). Supported types: [int], [float], [Vector2], [Vector2i], [Vector3], [Vector3i], [Vector4], [Vector4i]. [codeblock] var a = abs(-1) # a is 1 @@ -36,6 +36,7 @@ var f = abs(Vector3i(-7, -8, -9)) # f is (7, 8, 9) [/codeblock] + [b]Note:[/b] For better type safety, use [method absf], [method absi], [method Vector2.abs], [method Vector2i.abs], [method Vector3.abs], [method Vector3i.abs], [method Vector4.abs], or [method Vector4i.abs]. @@ -143,7 +144,7 @@ i = ceil(1.001) # i is 2.0 [/codeblock] See also [method floor], [method round], and [method snapped]. - [b]Note:[/b] For better type safety, see [method ceilf], [method ceili], [method Vector2.ceil], [method Vector3.ceil] and [method Vector4.ceil]. + [b]Note:[/b] For better type safety, use [method ceilf], [method ceili], [method Vector2.ceil], [method Vector3.ceil], or [method Vector4.ceil]. @@ -168,7 +169,7 @@ - Clamps the [param value], returning a [Variant] not less than [param min] and not more than [param max]. Variant types [int], [float], [Vector2], [Vector2i], [Vector3] and [Vector3i] are supported. + Clamps the [param value], returning a [Variant] not less than [param min] and not more than [param max]. Supported types: [int], [float], [Vector2], [Vector2i], [Vector3], [Vector3i], [Vector4], [Vector4i]. [codeblock] var a = clamp(-10, -1, 5) # a is -1 @@ -188,6 +189,7 @@ var f = clamp(Vector3i(-7, -8, -9), Vector3i(-1, 2, 3), Vector3i(-4, -5, -6)) # f is (-4, -5, -6) [/codeblock] + [b]Note:[/b] For better type safety, use [method clampf], [method clampi], [method Vector2.clamp], [method Vector2i.clamp], [method Vector3.clamp], [method Vector3i.clamp], [method Vector4.clamp], or [method Vector4i.clamp]. @@ -367,7 +369,7 @@ a = floor(-2.99) # a is -3.0 [/codeblock] See also [method ceil], [method round], and [method snapped]. - [b]Note:[/b] For better type safety, see [method floorf], [method floori], [method Vector2.floor], [method Vector3.floor] and [method Vector4.floor]. + [b]Note:[/b] For better type safety, use [method floorf], [method floori], [method Vector2.floor], [method Vector3.floor], or [method Vector4.floor]. @@ -950,7 +952,7 @@ round(2.6) # Returns 3 [/codeblock] See also [method floor], [method ceil], and [method snapped]. - [b]Note:[/b] For better type safety, use [method roundf], [method roundi], [method Vector2.round], [method Vector3.round] or [method Vector4.round], instead. + [b]Note:[/b] For better type safety, use [method roundf], [method roundi], [method Vector2.round], [method Vector3.round], or [method Vector4.round]. @@ -987,21 +989,22 @@ - Returns the sign of [param x] as same type of [Variant] as [param x] with each component being -1, 0 and 1 for each negative, zero and positive values respectively. Variant types [int], [float], [Vector2], [Vector2i], [Vector3] and [Vector3i] are supported. + Returns the same type of [Variant] as [param x], with [code]-1[/code] for negative values, [code]1[/code] for positive values, and [code]0[/code] for zeros. Supported types: [int], [float], [Vector2], [Vector2i], [Vector3], [Vector3i], [Vector4], [Vector4i]. [codeblock] sign(-6.0) # Returns -1 sign(0.0) # Returns 0 sign(6.0) # Returns 1 - sign(Vector3(-6.0, 0.0, 6.0) # Returns (-1, 0, 1) + sign(Vector3(-6.0, 0.0, 6.0)) # Returns (-1, 0, 1) [/codeblock] + [b]Note:[/b] For better type safety, use [method signf], [method signi], [method Vector2.sign], [method Vector2i.sign], [method Vector3.sign], [method Vector3i.sign], [method Vector4.sign], or [method Vector4i.sign]. - Returns the sign of [param x] as a [float]: -1.0 or 1.0. Returns 0.0 if [param x] is 0.0. + Returns [code]-1.0[/code] if [param x] is negative, [code]1.0[/code] if [param x] is positive, and [code]0.0[/code] if if [param x] is zero. [codeblock] sign(-6.5) # Returns -1.0 sign(0.0) # Returns 0.0 @@ -1013,7 +1016,7 @@ - Returns the sign of [param x] as an [int]: -1 or 1. Returns 0 if [param x] is 0. + Returns [code]-1[/code] if [param x] is negative, [code]1[/code] if [param x] is positive, and [code]0[/code] if if [param x] is zero. [codeblock] sign(-6) # Returns -1 sign(0) # Returns 0 @@ -1063,16 +1066,46 @@ + + + + + Returns the multiple of [param step] that is the closest to [param x]. This can also be used to round a floating point number to an arbitrary number of decimals. + The returned value is the same type of [Variant] as [param step]. Supported types: [int], [float], [Vector2], [Vector2i], [Vector3], [Vector3i], [Vector4], [Vector4i]. + [codeblock] + snapped(100, 32) # Returns 96 + snapped(3.14159, 0.01) # Returns 3.14 + + snapped(Vector2(34, 70), Vector2(8, 8)) # Returns (32, 72) + [/codeblock] + See also [method ceil], [method floor], and [method round]. + [b]Note:[/b] For better type safety, use [method snappedf], [method snappedi], [method Vector2.snapped], [method Vector2i.snapped], [method Vector3.snapped], [method Vector3i.snapped], [method Vector4.snapped], or [method Vector4i.snapped]. + + + - Snaps the float value [param x] to a given [param step]. This can also be used to round a floating point number to an arbitrary number of decimals. + Returns the multiple of [param step] that is the closest to [param x]. This can also be used to round a floating point number to an arbitrary number of decimals. + A type-safe version of [method snapped], returning a [float]. [codeblock] - snapped(100, 32) # Returns 96 - snapped(3.14159, 0.01) # Returns 3.14 + snapped(32.0, 2.5) # Returns 32.5 + snapped(3.14159, 0.01) # Returns 3.14 + [/codeblock] + + + + + + + + Returns the multiple of [param step] that is the closest to [param x]. + A type-safe version of [method snapped], returning an [int]. + [codeblock] + snapped(53, 16) # Returns 48 + snapped(4096, 100) # Returns 4100 [/codeblock] - See also [method ceil], [method floor], and [method round]. diff --git a/doc/classes/Vector2.xml b/doc/classes/Vector2.xml index 5590f823365..2f253eda78d 100644 --- a/doc/classes/Vector2.xml +++ b/doc/classes/Vector2.xml @@ -99,7 +99,7 @@ - Returns the vector "bounced off" from a plane defined by the given normal. + Returns a new vector "bounced off" from a plane defined by the given normal. @@ -276,7 +276,7 @@ - Returns the vector scaled to unit length. Equivalent to [code]v / v.length()[/code]. + Returns a new vector scaled to unit length. Equivalent to [code]v / v.length()[/code]. @@ -303,21 +303,21 @@ - Returns this vector projected onto the vector [code]b[/code]. + Returns the result of projecting the vector onto the given vector [param b]. - Returns the vector reflected (i.e. mirrored, or symmetric) over a line defined by the given direction vector [param n]. + Returns the result of reflecting the vector from a line defined by the given direction vector [param n]. - Returns the vector rotated by [param angle] (in radians). See also [method @GlobalScope.deg_to_rad]. + Returns the result of rotating this vector by [param angle] (in radians). See also [method @GlobalScope.deg_to_rad]. @@ -329,7 +329,7 @@ - Returns a new vector with each component set to one or negative one, depending on the signs of the components, or zero if the component is zero, by calling [method @GlobalScope.sign] on each component. + Returns a new vector with each component set to [code]1.0[/code] if it's positive, [code]-1.0[/code] if it's negative, and [code]0.0[/code] if it's zero. The result is identical to calling [method @GlobalScope.sign] on each component. @@ -345,14 +345,14 @@ - Returns this vector slid along a plane defined by the given normal. + Returns the result of sliding the vector along a plane defined by the given normal. - Returns this vector with each component snapped to the nearest multiple of [param step]. This can also be used to round to an arbitrary number of decimals. + Returns a new vector with each component snapped to the nearest multiple of the corresponding component in [param step]. This can also be used to round the components to an arbitrary number of decimals. diff --git a/doc/classes/Vector2i.xml b/doc/classes/Vector2i.xml index eab880e57f4..cd729d57fba 100644 --- a/doc/classes/Vector2i.xml +++ b/doc/classes/Vector2i.xml @@ -92,7 +92,14 @@ - Returns a new vector with each component set to one or negative one, depending on the signs of the components, or zero if the component is zero, by calling [method @GlobalScope.sign] on each component. + Returns a new vector with each component set to [code]1[/code] if it's positive, [code]-1[/code] if it's negative, and [code]0[/code] if it's zero. The result is identical to calling [method @GlobalScope.sign] on each component. + + + + + + + Returns a new vector with each component snapped to the closest multiple of the corresponding component in [param step]. diff --git a/doc/classes/Vector3.xml b/doc/classes/Vector3.xml index 1a2cdfe10ee..054ca7e22cd 100644 --- a/doc/classes/Vector3.xml +++ b/doc/classes/Vector3.xml @@ -287,14 +287,14 @@ - Returns this vector projected onto the vector [param b]. + Returns the result of projecting the vector onto the given vector [param b]. - Returns this vector reflected from a plane defined by the given normal. + Returns the result of reflecting the vector from a plane defined by the given normal [param n]. @@ -302,7 +302,7 @@ - Rotates this vector around a given axis by [param angle] (in radians). The axis must be a normalized vector. + Returns the result of rotating this vector around a given axis by [param angle] (in radians). The axis must be a normalized vector. See also [method @GlobalScope.deg_to_rad]. @@ -314,7 +314,7 @@ - Returns a new vector with each component set to one or negative one, depending on the signs of the components, or zero if the component is zero, by calling [method @GlobalScope.sign] on each component. + Returns a new vector with each component set to [code]1.0[/code] if it's positive, [code]-1.0[/code] if it's negative, and [code]0.0[/code] if it's zero. The result is identical to calling [method @GlobalScope.sign] on each component. @@ -338,14 +338,14 @@ - Returns this vector slid along a plane defined by the given normal. + Returns a new vector slid along a plane defined by the given normal. - Returns this vector with each component snapped to the nearest multiple of [param step]. This can also be used to round to an arbitrary number of decimals. + Returns a new vector with each component snapped to the nearest multiple of the corresponding component in [param step]. This can also be used to round the components to an arbitrary number of decimals. diff --git a/doc/classes/Vector3i.xml b/doc/classes/Vector3i.xml index 1c2a033f7a4..8b5c5321f28 100644 --- a/doc/classes/Vector3i.xml +++ b/doc/classes/Vector3i.xml @@ -87,7 +87,14 @@ - Returns the vector with each component set to one or negative one, depending on the signs of the components. + Returns a new vector with each component set to [code]1[/code] if it's positive, [code]-1[/code] if it's negative, and [code]0[/code] if it's zero. The result is identical to calling [method @GlobalScope.sign] on each component. + + + + + + + Returns a new vector with each component snapped to the closest multiple of the corresponding component in [param step]. diff --git a/doc/classes/Vector4.xml b/doc/classes/Vector4.xml index 662d0bce3ae..b9df1ffb464 100644 --- a/doc/classes/Vector4.xml +++ b/doc/classes/Vector4.xml @@ -189,21 +189,21 @@ - Returns the vector scaled to unit length. Equivalent to [code]v / v.length()[/code]. + Returns the result of scaling the vector to unit length. Equivalent to [code]v / v.length()[/code]. - Returns a vector composed of the [method @GlobalScope.fposmod] of this vector's components and [param mod]. + Returns a new vector composed of the [method @GlobalScope.fposmod] of this vector's components and [param mod]. - Returns a vector composed of the [method @GlobalScope.fposmod] of this vector's components and [param modv]'s components. + Returns a new vector composed of the [method @GlobalScope.fposmod] of this vector's components and [param modv]'s components. @@ -215,14 +215,14 @@ - Returns a new vector with each component set to one or negative one, depending on the signs of the components, or zero if the component is zero, by calling [method @GlobalScope.sign] on each component. + Returns a new vector with each component set to [code]1.0[/code] if it's positive, [code]-1.0[/code] if it's negative, and [code]0.0[/code] if it's zero. The result is identical to calling [method @GlobalScope.sign] on each component. - Returns this vector with each component snapped to the nearest multiple of [param step]. This can also be used to round to an arbitrary number of decimals. + Returns a new vector with each component snapped to the nearest multiple of the corresponding component in [param step]. This can also be used to round the components to an arbitrary number of decimals. diff --git a/doc/classes/Vector4i.xml b/doc/classes/Vector4i.xml index e9ac5b9475f..834d59db66e 100644 --- a/doc/classes/Vector4i.xml +++ b/doc/classes/Vector4i.xml @@ -83,7 +83,14 @@ - Returns a new vector with each component set to one or negative one, depending on the signs of the components, or zero if the component is zero, by calling [method @GlobalScope.sign] on each component. + Returns a new vector with each component set to [code]1[/code] if it's positive, [code]-1[/code] if it's negative, and [code]0[/code] if it's zero. The result is identical to calling [method @GlobalScope.sign] on each component. + + + + + + + Returns a new vector with each component snapped to the closest multiple of the corresponding component in [param step]. diff --git a/tests/core/math/test_vector2i.h b/tests/core/math/test_vector2i.h index c7a0dccdcce..9ee844ffa83 100644 --- a/tests/core/math/test_vector2i.h +++ b/tests/core/math/test_vector2i.h @@ -131,12 +131,16 @@ TEST_CASE("[Vector2i] Other methods") { "Vector2i aspect should work as expected."); CHECK_MESSAGE( - Vector2i(1, 2) == vector.min(Vector2i(3, 2)), + vector.min(Vector2i(3, 2)) == Vector2i(1, 2), "Vector2i min should return expected value."); CHECK_MESSAGE( - Vector2i(5, 3) == vector.max(Vector2i(5, 2)), + vector.max(Vector2i(5, 2)) == Vector2i(5, 3), "Vector2i max should return expected value."); + + CHECK_MESSAGE( + vector.snapped(Vector2i(4, 2)) == Vector2i(0, 4), + "Vector2i snapped should work as expected."); } TEST_CASE("[Vector2i] Abs and sign methods") { diff --git a/tests/core/math/test_vector3i.h b/tests/core/math/test_vector3i.h index 56578f99eb1..45240bd2ffd 100644 --- a/tests/core/math/test_vector3i.h +++ b/tests/core/math/test_vector3i.h @@ -127,6 +127,14 @@ TEST_CASE("[Vector3i] Operators") { "Vector3i constructed from Vector3 should work as expected."); } +TEST_CASE("[Vector3i] Other methods") { + const Vector3i vector = Vector3i(1, 3, -7); + + CHECK_MESSAGE( + vector.snapped(Vector3i(4, 2, 5)) == Vector3i(0, 4, -5), + "Vector3i snapped should work as expected."); +} + TEST_CASE("[Vector3i] Abs and sign methods") { const Vector3i vector1 = Vector3i(1, 3, 5); const Vector3i vector2 = Vector3i(1, -3, -5); diff --git a/tests/core/math/test_vector4i.h b/tests/core/math/test_vector4i.h index 30d38607ddb..8a9522f9cc0 100644 --- a/tests/core/math/test_vector4i.h +++ b/tests/core/math/test_vector4i.h @@ -130,6 +130,14 @@ TEST_CASE("[Vector4i] Operators") { "Vector4i constructed from Vector4 should work as expected."); } +TEST_CASE("[Vector3i] Other methods") { + const Vector4i vector = Vector4i(1, 3, -7, 13); + + CHECK_MESSAGE( + vector.snapped(Vector4i(4, 2, 5, 8)) == Vector4i(0, 4, -5, 16), + "Vector4i snapped should work as expected."); +} + TEST_CASE("[Vector4i] Abs and sign methods") { const Vector4i vector1 = Vector4i(1, 3, 5, 7); const Vector4i vector2 = Vector4i(1, -3, -5, 7);