From 83634119d42595498e42685556a577c12145e20b Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Tue, 1 Nov 2022 08:11:09 -0500 Subject: Replace Quaternion Euler constructor with `from_euler` method --- .../glue/GodotSharp/GodotSharp/Core/Quaternion.cs | 60 +++++++++++----------- 1 file changed, 31 insertions(+), 29 deletions(-) (limited to 'modules/mono/glue') diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs index d459fe8c96..c4eb3b6ad0 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Quaternion.cs @@ -506,35 +506,6 @@ namespace Godot this = basis.GetQuaternion(); } - /// - /// Constructs a that will perform a rotation specified by - /// Euler angles (in the YXZ convention: when decomposing, first Z, then X, and Y last), - /// given in the vector format as (X angle, Y angle, Z angle). - /// - /// Euler angles that the quaternion will be rotated by. - public Quaternion(Vector3 eulerYXZ) - { - real_t halfA1 = eulerYXZ.y * 0.5f; - real_t halfA2 = eulerYXZ.x * 0.5f; - real_t halfA3 = eulerYXZ.z * 0.5f; - - // R = Y(a1).X(a2).Z(a3) convention for Euler angles. - // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6) - // a3 is the angle of the first rotation, following the notation in this reference. - - real_t cosA1 = Mathf.Cos(halfA1); - real_t sinA1 = Mathf.Sin(halfA1); - real_t cosA2 = Mathf.Cos(halfA2); - real_t sinA2 = Mathf.Sin(halfA2); - real_t cosA3 = Mathf.Cos(halfA3); - real_t sinA3 = Mathf.Sin(halfA3); - - x = (sinA1 * cosA2 * sinA3) + (cosA1 * sinA2 * cosA3); - y = (sinA1 * cosA2 * cosA3) - (cosA1 * sinA2 * sinA3); - z = (cosA1 * cosA2 * sinA3) - (sinA1 * sinA2 * cosA3); - w = (sinA1 * sinA2 * sinA3) + (cosA1 * cosA2 * cosA3); - } - /// /// Constructs a that will rotate around the given axis /// by the specified angle. The axis must be a normalized vector. @@ -572,6 +543,37 @@ namespace Godot } } + /// + /// Constructs a that will perform a rotation specified by + /// Euler angles (in the YXZ convention: when decomposing, first Z, then X, and Y last), + /// given in the vector format as (X angle, Y angle, Z angle). + /// + /// Euler angles that the quaternion will be rotated by. + public static Quaternion FromEuler(Vector3 eulerYXZ) + { + real_t halfA1 = eulerYXZ.y * 0.5f; + real_t halfA2 = eulerYXZ.x * 0.5f; + real_t halfA3 = eulerYXZ.z * 0.5f; + + // R = Y(a1).X(a2).Z(a3) convention for Euler angles. + // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6) + // a3 is the angle of the first rotation, following the notation in this reference. + + real_t cosA1 = Mathf.Cos(halfA1); + real_t sinA1 = Mathf.Sin(halfA1); + real_t cosA2 = Mathf.Cos(halfA2); + real_t sinA2 = Mathf.Sin(halfA2); + real_t cosA3 = Mathf.Cos(halfA3); + real_t sinA3 = Mathf.Sin(halfA3); + + return new Quaternion( + (sinA1 * cosA2 * sinA3) + (cosA1 * sinA2 * cosA3), + (sinA1 * cosA2 * cosA3) - (cosA1 * sinA2 * sinA3), + (cosA1 * cosA2 * sinA3) - (sinA1 * sinA2 * cosA3), + (sinA1 * sinA2 * sinA3) + (cosA1 * cosA2 * cosA3) + ); + } + /// /// Composes these two quaternions by multiplying them together. /// This has the effect of rotating the second quaternion -- cgit v1.2.3