Rename Vector2 clamped to limit_length and add limit_length to Vector3

This commit is contained in:
Aaron Franke 2021-02-01 00:42:00 -05:00
parent f288a79482
commit 94bc0bd919
No known key found for this signature in database
GPG key ID: 40A1750B977E56BF
10 changed files with 75 additions and 35 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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();

View file

@ -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());

View file

@ -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>

View file

@ -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>

View file

@ -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"/>.

View file

@ -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"/>.

View file

@ -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;