diff --git a/core/math/vector3.h b/core/math/vector3.h index 6b4ff3f9a8a..377581bb456 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -110,6 +110,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; @@ -230,6 +231,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/variant_call.cpp b/core/variant/variant_call.cpp index 8f2cba138b4..48f98160740 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1059,6 +1059,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector3, min_axis, sarray(), varray()); bind_method(Vector3, max_axis, sarray(), varray()); bind_method(Vector3, angle_to, sarray("to"), varray()); + bind_method(Vector3, signed_angle_to, sarray("to", "axis"), varray()); bind_method(Vector3, direction_to, sarray("b"), varray()); bind_method(Vector3, distance_to, sarray("b"), varray()); bind_method(Vector3, distance_squared_to, sarray("b"), varray()); diff --git a/doc/classes/Vector3.xml b/doc/classes/Vector3.xml index 2d129a2c862..ea80b7c2486 100644 --- a/doc/classes/Vector3.xml +++ b/doc/classes/Vector3.xml @@ -68,7 +68,7 @@ - Returns the minimum angle to the given vector, in radians. + Returns the unsigned minimum angle to the given vector, in radians. @@ -465,6 +465,17 @@ Returns a vector with each component set to one or negative one, depending on the signs of this vector's components, or zero if the component is zero, by calling [method @GlobalScope.sign] on each component. + + + + + + + + + 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 42dbdf25c38..3b895bbbf66 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)); @@ -468,6 +468,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`.