From 9513354f6822db43c43c112366ac2127995b53d0 Mon Sep 17 00:00:00 2001 From: JestemStefan Date: Fri, 9 Jul 2021 07:45:35 +0200 Subject: [PATCH] Added signed_angle_to for Vector3 Added signed_angle_to method for Vector3 Added signed_angle_to for Vector3 formatting fix... --- core/math/vector3.h | 8 +++++++ core/variant_call.cpp | 2 ++ doc/classes/Vector3.xml | 13 +++++++++++- .../GodotSharp/GodotSharp/Core/Vector3.cs | 21 +++++++++++++++++-- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/core/math/vector3.h b/core/math/vector3.h index 3a45614e0b2..1157addcf8c 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -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(); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 0064507ca1f..00b4cb2c613 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -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()); diff --git a/doc/classes/Vector3.xml b/doc/classes/Vector3.xml index e23b526a60c..020dbcab61a 100644 --- a/doc/classes/Vector3.xml +++ b/doc/classes/Vector3.xml @@ -42,7 +42,7 @@ - Returns the minimum angle to the given vector, in radians. + Returns the unsigned minimum angle to the given vector, in radians. @@ -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. + + + + + + + + + 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]. + + diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs index 6c5496122fe..9528cecea25 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Vector3.cs @@ -111,10 +111,10 @@ namespace Godot } /// - /// Returns the minimum angle to the given vector, in radians. + /// Returns the unsigned minimum angle to the given vector, in radians. /// /// The other vector to compare this vector to. - /// The angle between the two vectors, in radians. + /// The unsigned angle between the two vectors, in radians. public real_t AngleTo(Vector3 to) { return Mathf.Atan2(Cross(to).Length(), Dot(to)); @@ -483,6 +483,23 @@ namespace Godot return v; } + /// + /// 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`. + /// + /// The other vector to compare this vector to. + /// The reference axis to use for the angle sign. + /// The signed angle between the two vectors, in radians. + 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; + } + /// /// Returns the result of the spherical linear interpolation between /// this vector and `to` by amount `weight`.