summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/math/quaternion.cpp5
-rw-r--r--core/math/quaternion.h7
-rw-r--r--core/variant/variant_call.cpp1
-rw-r--r--doc/classes/Quaternion.xml10
-rw-r--r--modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs17
5 files changed, 37 insertions, 3 deletions
diff --git a/core/math/quaternion.cpp b/core/math/quaternion.cpp
index 7037db7112..3f1d2c58e5 100644
--- a/core/math/quaternion.cpp
+++ b/core/math/quaternion.cpp
@@ -33,6 +33,11 @@
#include "core/math/basis.h"
#include "core/string/print_string.h"
+real_t Quaternion::angle_to(const Quaternion &p_to) const {
+ real_t d = dot(p_to);
+ return Math::acos(CLAMP(d * d * 2 - 1, -1, 1));
+}
+
// get_euler_xyz returns a vector containing the Euler angles in the format
// (ax,ay,az), where ax is the angle of rotation around x axis,
// and similar for other axes.
diff --git a/core/math/quaternion.h b/core/math/quaternion.h
index 796214b79e..35324323b3 100644
--- a/core/math/quaternion.h
+++ b/core/math/quaternion.h
@@ -28,8 +28,8 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
-#ifndef QUAT_H
-#define QUAT_H
+#ifndef QUATERNION_H
+#define QUATERNION_H
#include "core/math/math_defs.h"
#include "core/math/math_funcs.h"
@@ -62,6 +62,7 @@ public:
bool is_normalized() const;
Quaternion inverse() const;
_FORCE_INLINE_ real_t dot(const Quaternion &p_q) const;
+ real_t angle_to(const Quaternion &p_to) const;
Vector3 get_euler_xyz() const;
Vector3 get_euler_yxz() const;
@@ -235,4 +236,4 @@ _FORCE_INLINE_ Quaternion operator*(const real_t &p_real, const Quaternion &p_qu
return p_quaternion * p_real;
}
-#endif // QUAT_H
+#endif // QUATERNION_H
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index 05ed35c760..4064eba7fb 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1552,6 +1552,7 @@ static void _register_variant_builtin_methods() {
bind_method(Quaternion, is_normalized, sarray(), varray());
bind_method(Quaternion, is_equal_approx, sarray("to"), varray());
bind_method(Quaternion, inverse, sarray(), varray());
+ bind_method(Quaternion, angle_to, sarray("to"), varray());
bind_method(Quaternion, dot, sarray("with"), varray());
bind_method(Quaternion, slerp, sarray("to", "weight"), varray());
bind_method(Quaternion, slerpni, sarray("to", "weight"), varray());
diff --git a/doc/classes/Quaternion.xml b/doc/classes/Quaternion.xml
index 3f2c4f3085..660204ee7d 100644
--- a/doc/classes/Quaternion.xml
+++ b/doc/classes/Quaternion.xml
@@ -83,6 +83,16 @@
Constructs a quaternion defined by the given values.
</description>
</method>
+ <method name="angle_to" qualifiers="const">
+ <return type="float">
+ </return>
+ <argument index="0" name="to" type="Quaternion">
+ </argument>
+ <description>
+ Returns the angle between this quaternion and [code]to[/code]. This is the magnitude of the angle you would need to rotate by to get from one to the other.
+ [b]Note:[/b] This method has an abnormally high amount of floating-point error, so methods such as [code]is_zero_approx[/code] will not work reliably.
+ </description>
+ </method>
<method name="cubic_slerp" qualifiers="const">
<return type="Quaternion">
</return>
diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs
index b087b4c200..817103994a 100644
--- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs
+++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs
@@ -114,6 +114,23 @@ namespace Godot
}
/// <summary>
+ /// Returns the angle between this quaternion and `to`.
+ /// This is the magnitude of the angle you would need to rotate
+ /// by to get from one to the other.
+ ///
+ /// Note: This method has an abnormally high amount
+ /// of floating-point error, so methods such as
+ /// <see cref="Mathf.IsZeroApprox"/> will not work reliably.
+ /// </summary>
+ /// <param name="to">The other quaternion.</param>
+ /// <returns>The angle between the quaternions.</returns>
+ public real_t AngleTo(Quaternion to)
+ {
+ real_t dot = Dot(to);
+ return Mathf.Acos(Mathf.Clamp(dot * dot * 2 - 1, -1, 1));
+ }
+
+ /// <summary>
/// Performs a cubic spherical interpolation between quaternions `preA`,
/// this vector, `b`, and `postB`, by the given amount `t`.
/// </summary>