diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp
index 46a08b53ab9..ab114673d84 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 6abe0f5ea99..2a540bd8aa9 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 d4317d506c6..cc71b1dacf3 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 adfc52566f1..8bab8fd847b 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 063611d415e..90356435d93 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 94d4b1a9034..9e47ebfaeaf 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).
-
-
-
-
-
-
- Returns the vector with a maximum length by limiting its length to [code]length[/code].
-
-
@@ -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.
+
+
+
+
+
+
+ Returns the vector with a maximum length by limiting its length to [code]length[/code].
+
+
diff --git a/doc/classes/Vector3.xml b/doc/classes/Vector3.xml
index 0a86369506e..94ecce5e317 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.
+
+
+
+
+
+
+ Returns the vector with a maximum length by limiting its length to [code]length[/code].
+
+
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs
index ebfe70aa820..c9bf4156222 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs
@@ -159,25 +159,6 @@ namespace Godot
return new Vector2(Mathf.Ceil(x), Mathf.Ceil(y));
}
- ///
- /// Returns the vector with a maximum length by limiting its length to `length`.
- ///
- /// The length to limit to.
- /// The vector with its length limited.
- public Vector2 Clamped(real_t length)
- {
- var v = this;
- real_t l = Length();
-
- if (l > 0 && length < l)
- {
- v /= l;
- v *= length;
- }
-
- return v;
- }
-
///
/// Returns the cross product of this vector and `b`.
///
@@ -334,6 +315,25 @@ namespace Godot
);
}
+ ///
+ /// Returns the vector with a maximum length by limiting its length to `length`.
+ ///
+ /// The length to limit to.
+ /// The vector with its length limited.
+ 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;
+ }
+
///
/// Returns the axis of the vector's largest value. See .
/// If both components are equal, this method returns .
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs
index 3b895bbbf66..14aeac41d04 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs
@@ -312,6 +312,25 @@ namespace Godot
);
}
+ ///
+ /// Returns the vector with a maximum length by limiting its length to `length`.
+ ///
+ /// The length to limit to.
+ /// The vector with its length limited.
+ 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;
+ }
+
///
/// Returns the axis of the vector's largest value. See .
/// If all components are equal, this method returns .
diff --git a/servers/physics_2d/joints_2d_sw.cpp b/servers/physics_2d/joints_2d_sw.cpp
index eaec582f9ba..5a0a628fbc5 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;