Merge pull request #50305 from JestemStefan/3.x-signed_angle_to

[3.x] Added signed_angle_to for Vector3
This commit is contained in:
Rémi Verschelde 2021-07-09 21:08:04 +02:00 committed by GitHub
commit 7c00a875f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 3 deletions

View file

@ -121,6 +121,7 @@ struct Vector3 {
_FORCE_INLINE_ Vector3 project(const Vector3 &p_to) const;
_FORCE_INLINE_ real_t angle_to(const Vector3 &p_to) const;
_FORCE_INLINE_ real_t signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const;
_FORCE_INLINE_ Vector3 direction_to(const Vector3 &p_to) const;
_FORCE_INLINE_ Vector3 slide(const Vector3 &p_normal) const;
@ -233,6 +234,13 @@ real_t Vector3::angle_to(const Vector3 &p_to) const {
return Math::atan2(cross(p_to).length(), dot(p_to));
}
real_t Vector3::signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const {
Vector3 cross_to = cross(p_to);
real_t unsigned_angle = Math::atan2(cross_to.length(), dot(p_to));
real_t sign = cross_to.dot(p_axis);
return (sign < 0) ? -unsigned_angle : unsigned_angle;
}
Vector3 Vector3::direction_to(const Vector3 &p_to) const {
Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
ret.normalize();

View file

@ -448,6 +448,7 @@ struct _VariantCall {
VCALL_LOCALMEM1R(Vector3, posmodv);
VCALL_LOCALMEM1R(Vector3, project);
VCALL_LOCALMEM1R(Vector3, angle_to);
VCALL_LOCALMEM2R(Vector3, signed_angle_to);
VCALL_LOCALMEM1R(Vector3, direction_to);
VCALL_LOCALMEM1R(Vector3, slide);
VCALL_LOCALMEM1R(Vector3, bounce);
@ -1768,6 +1769,7 @@ void register_variant_methods() {
ADDFUNC0R(VECTOR3, INT, Vector3, min_axis, varray());
ADDFUNC0R(VECTOR3, INT, Vector3, max_axis, varray());
ADDFUNC1R(VECTOR3, REAL, Vector3, angle_to, VECTOR3, "to", varray());
ADDFUNC2R(VECTOR3, REAL, Vector3, signed_angle_to, VECTOR3, "to", VECTOR3, "axis", varray());
ADDFUNC1R(VECTOR3, VECTOR3, Vector3, direction_to, VECTOR3, "b", varray());
ADDFUNC1R(VECTOR3, REAL, Vector3, distance_to, VECTOR3, "b", varray());
ADDFUNC1R(VECTOR3, REAL, Vector3, distance_squared_to, VECTOR3, "b", varray());

View file

@ -42,7 +42,7 @@
<argument index="0" name="to" type="Vector3">
</argument>
<description>
Returns the minimum angle to the given vector, in radians.
Returns the unsigned minimum angle to the given vector, in radians.
</description>
</method>
<method name="bounce">
@ -283,6 +283,17 @@
Returns a vector with each component set to one or negative one, depending on the signs of this vector's components. If a component is zero, it returns positive one.
</description>
</method>
<method name="signed_angle_to">
<return type="float">
</return>
<argument index="0" name="to" type="Vector3">
</argument>
<argument index="1" name="axis" type="Vector3">
</argument>
<description>
Returns the signed angle to the given vector, in radians. The sign of the angle is positive in a counter-clockwise direction and negative in a clockwise direction when viewed from the side specified by the [code]axis[/code].
</description>
</method>
<method name="slerp">
<return type="Vector3">
</return>

View file

@ -111,10 +111,10 @@ namespace Godot
}
/// <summary>
/// Returns the minimum angle to the given vector, in radians.
/// Returns the unsigned minimum angle to the given vector, in radians.
/// </summary>
/// <param name="to">The other vector to compare this vector to.</param>
/// <returns>The angle between the two vectors, in radians.</returns>
/// <returns>The unsigned angle between the two vectors, in radians.</returns>
public real_t AngleTo(Vector3 to)
{
return Mathf.Atan2(Cross(to).Length(), Dot(to));
@ -483,6 +483,23 @@ namespace Godot
return v;
}
/// <summary>
/// Returns the signed angle to the given vector, in radians.
/// The sign of the angle is positive in a counter-clockwise
/// direction and negative in a clockwise direction when viewed
/// from the side specified by the `axis`.
/// </summary>
/// <param name="to">The other vector to compare this vector to.</param>
/// <param name="axis">The reference axis to use for the angle sign.</param>
/// <returns>The signed angle between the two vectors, in radians.</returns>
public real_t SignedAngleTo(Vector3 to, Vector3 axis)
{
Vector3 crossTo = Cross(to);
real_t unsignedAngle = Mathf.Atan2(crossTo.Length(), Dot(to));
real_t sign = crossTo.Dot(axis);
return (sign < 0) ? -unsignedAngle : unsignedAngle;
}
/// <summary>
/// Returns the result of the spherical linear interpolation between
/// this vector and `to` by amount `weight`.