diff options
-rw-r--r-- | core/math/vector2.cpp | 4 | ||||
-rw-r--r-- | core/math/vector2.h | 3 | ||||
-rw-r--r-- | core/math/vector3.cpp | 11 | ||||
-rw-r--r-- | core/math/vector3.h | 1 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 3 | ||||
-rw-r--r-- | doc/classes/Vector2.xml | 18 | ||||
-rw-r--r-- | doc/classes/Vector3.xml | 9 | ||||
-rw-r--r-- | modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs | 38 | ||||
-rw-r--r-- | modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs | 19 | ||||
-rw-r--r-- | servers/physics_2d/joints_2d_sw.cpp | 4 |
10 files changed, 75 insertions, 35 deletions
diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index 46a08b53ab..ab114673d8 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -128,8 +128,8 @@ Vector2 Vector2::snapped(const Vector2 &p_step) const { Math::snapped(y, p_step.y)); } -Vector2 Vector2::clamped(real_t p_len) const { - real_t l = length(); +Vector2 Vector2::limit_length(const real_t p_len) const { + const real_t l = length(); Vector2 v = *this; if (l > 0 && p_len < l) { v /= l; diff --git a/core/math/vector2.h b/core/math/vector2.h index 6abe0f5ea9..2a540bd8aa 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -84,6 +84,7 @@ struct Vector2 { real_t length() const; real_t length_squared() const; + Vector2 limit_length(const real_t p_len = 1.0) const; Vector2 min(const Vector2 &p_vector2) const { return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y)); @@ -107,8 +108,6 @@ struct Vector2 { Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const; - Vector2 clamped(real_t p_len) const; - _FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, real_t p_weight) const; _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight) const; Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const; diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index d4317d506c..cc71b1dacf 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -64,6 +64,17 @@ Vector3 Vector3::snapped(Vector3 p_step) const { return v; } +Vector3 Vector3::limit_length(const real_t p_len) const { + const real_t l = length(); + Vector3 v = *this; + if (l > 0 && p_len < l) { + v /= l; + v *= p_len; + } + + return v; +} + Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const { Vector3 p0 = p_pre_a; Vector3 p1 = *this; diff --git a/core/math/vector3.h b/core/math/vector3.h index adfc52566f..8bab8fd847 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -86,6 +86,7 @@ struct Vector3 { _FORCE_INLINE_ Vector3 normalized() const; _FORCE_INLINE_ bool is_normalized() const; _FORCE_INLINE_ Vector3 inverse() const; + Vector3 limit_length(const real_t p_len = 1.0) const; _FORCE_INLINE_ void zero(); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 063611d415..90356435d9 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1419,6 +1419,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector2, distance_squared_to, sarray("to"), varray()); bind_method(Vector2, length, sarray(), varray()); bind_method(Vector2, length_squared, sarray(), varray()); + bind_method(Vector2, limit_length, sarray("length"), varray(1.0)); bind_method(Vector2, normalized, sarray(), varray()); bind_method(Vector2, is_normalized, sarray(), varray()); bind_method(Vector2, is_equal_approx, sarray("to"), varray()); @@ -1443,7 +1444,6 @@ static void _register_variant_builtin_methods() { bind_method(Vector2, abs, sarray(), varray()); bind_method(Vector2, sign, sarray(), varray()); bind_method(Vector2, snapped, sarray("step"), varray()); - bind_method(Vector2, clamped, sarray("length"), varray()); /* Vector2i */ @@ -1493,6 +1493,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector3, distance_squared_to, sarray("b"), varray()); bind_method(Vector3, length, sarray(), varray()); bind_method(Vector3, length_squared, sarray(), varray()); + bind_method(Vector3, limit_length, sarray("length"), varray(1.0)); bind_method(Vector3, normalized, sarray(), varray()); bind_method(Vector3, is_normalized, sarray(), varray()); bind_method(Vector3, is_equal_approx, sarray("to"), varray()); diff --git a/doc/classes/Vector2.xml b/doc/classes/Vector2.xml index 94d4b1a903..9e47ebfaea 100644 --- a/doc/classes/Vector2.xml +++ b/doc/classes/Vector2.xml @@ -110,15 +110,6 @@ Returns the vector with all components rounded up (towards positive infinity). </description> </method> - <method name="clamped" qualifiers="const"> - <return type="Vector2"> - </return> - <argument index="0" name="length" type="float"> - </argument> - <description> - Returns the vector with a maximum length by limiting its length to [code]length[/code]. - </description> - </method> <method name="cross" qualifiers="const"> <return type="float"> </return> @@ -232,6 +223,15 @@ Returns the result of the linear interpolation between this vector and [code]to[/code] by amount [code]weight[/code]. [code]weight[/code] is on the range of 0.0 to 1.0, representing the amount of interpolation. </description> </method> + <method name="limit_length" qualifiers="const"> + <return type="Vector2"> + </return> + <argument index="0" name="length" type="float" default="1.0"> + </argument> + <description> + Returns the vector with a maximum length by limiting its length to [code]length[/code]. + </description> + </method> <method name="move_toward" qualifiers="const"> <return type="Vector2"> </return> diff --git a/doc/classes/Vector3.xml b/doc/classes/Vector3.xml index 0a86369506..94ecce5e31 100644 --- a/doc/classes/Vector3.xml +++ b/doc/classes/Vector3.xml @@ -207,6 +207,15 @@ Returns the result of the linear interpolation between this vector and [code]to[/code] by amount [code]weight[/code]. [code]weight[/code] is on the range of 0.0 to 1.0, representing the amount of interpolation. </description> </method> + <method name="limit_length" qualifiers="const"> + <return type="Vector3"> + </return> + <argument index="0" name="length" type="float" default="1.0"> + </argument> + <description> + Returns the vector with a maximum length by limiting its length to [code]length[/code]. + </description> + </method> <method name="max_axis" qualifiers="const"> <return type="int"> </return> diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs index ebfe70aa82..c9bf415622 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs @@ -160,25 +160,6 @@ namespace Godot } /// <summary> - /// Returns the vector with a maximum length by limiting its length to `length`. - /// </summary> - /// <param name="length">The length to limit to.</param> - /// <returns>The vector with its length limited.</returns> - public Vector2 Clamped(real_t length) - { - var v = this; - real_t l = Length(); - - if (l > 0 && length < l) - { - v /= l; - v *= length; - } - - return v; - } - - /// <summary> /// Returns the cross product of this vector and `b`. /// </summary> /// <param name="b">The other vector.</param> @@ -335,6 +316,25 @@ namespace Godot } /// <summary> + /// Returns the vector with a maximum length by limiting its length to `length`. + /// </summary> + /// <param name="length">The length to limit to.</param> + /// <returns>The vector with its length limited.</returns> + public Vector2 LimitLength(real_t length = 1.0f) + { + Vector2 v = this; + real_t l = Length(); + + if (l > 0 && length < l) + { + v /= l; + v *= length; + } + + return v; + } + + /// <summary> /// Returns the axis of the vector's largest value. See <see cref="Axis"/>. /// If both components are equal, this method returns <see cref="Axis.X"/>. /// </summary> diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs index 3b895bbbf6..14aeac41d0 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs @@ -313,6 +313,25 @@ namespace Godot } /// <summary> + /// Returns the vector with a maximum length by limiting its length to `length`. + /// </summary> + /// <param name="length">The length to limit to.</param> + /// <returns>The vector with its length limited.</returns> + public Vector3 LimitLength(real_t length = 1.0f) + { + Vector3 v = this; + real_t l = Length(); + + if (l > 0 && length < l) + { + v /= l; + v *= length; + } + + return v; + } + + /// <summary> /// Returns the axis of the vector's largest value. See <see cref="Axis"/>. /// If all components are equal, this method returns <see cref="Axis.X"/>. /// </summary> diff --git a/servers/physics_2d/joints_2d_sw.cpp b/servers/physics_2d/joints_2d_sw.cpp index eaec582f9b..5a0a628fbc 100644 --- a/servers/physics_2d/joints_2d_sw.cpp +++ b/servers/physics_2d/joints_2d_sw.cpp @@ -322,7 +322,7 @@ bool GrooveJoint2DSW::setup(real_t p_step) { Vector2 delta = (B->get_transform().get_origin() + rB) - (A->get_transform().get_origin() + rA); real_t _b = get_bias(); - gbias = (delta * -(_b == 0 ? space->get_constraint_bias() : _b) * (1.0 / p_step)).clamped(get_max_bias()); + gbias = (delta * -(_b == 0 ? space->get_constraint_bias() : _b) * (1.0 / p_step)).limit_length(get_max_bias()); correct = true; return true; @@ -348,7 +348,7 @@ void GrooveJoint2DSW::solve(real_t p_step) { Vector2 jOld = jn_acc; j += jOld; - jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : j.project(xf_normal)).clamped(jn_max); + jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : j.project(xf_normal)).limit_length(jn_max); j = jn_acc - jOld; |