Переглянути джерело

Rename Vector2 clamped to limit_length and add limit_length to Vector3

Aaron Franke 4 роки тому
батько
коміт
94bc0bd919

+ 2 - 2
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;

+ 1 - 2
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;

+ 11 - 0
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;

+ 1 - 0
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();
 

+ 2 - 1
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());

+ 9 - 9
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>

+ 9 - 0
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>

+ 19 - 19
modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs

@@ -159,25 +159,6 @@ namespace Godot
             return new Vector2(Mathf.Ceil(x), Mathf.Ceil(y));
         }
 
-        /// <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>
@@ -334,6 +315,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"/>.

+ 19 - 0
modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs

@@ -312,6 +312,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"/>.

+ 2 - 2
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;