From a88de3cae15a90fc4fdd1b82e91a3c0da14a0229 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Tue, 3 Sep 2024 03:44:22 +0200 Subject: [PATCH] Sync math checks with main repo There were mismatches between Core and C#. The match checks should now match with the Core implementation in C++. Also took the opportunity to remove some unused code. --- src/Godot.Bindings/Core/Variants/Basis.cs | 8 +++++++ .../Core/Variants/Quaternion.cs | 14 ++++++------- .../Core/Variants/Transform2D.cs | 2 ++ .../Core/Variants/Transform3D.cs | 21 +++++++------------ src/Godot.Bindings/Core/Variants/Vector2.cs | 6 ++++++ src/Godot.Bindings/Core/Variants/Vector3.cs | 12 +++++------ 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/Godot.Bindings/Core/Variants/Basis.cs b/src/Godot.Bindings/Core/Variants/Basis.cs index 51691b0..ba8b44b 100644 --- a/src/Godot.Bindings/Core/Variants/Basis.cs +++ b/src/Godot.Bindings/Core/Variants/Basis.cs @@ -565,10 +565,12 @@ public readonly Basis Inverse() real_t det = Row0[0] * cofac00 + Row0[1] * cofac10 + Row0[2] * cofac20; +#if DEBUG if (det == 0) { throw new InvalidOperationException("Matrix determinant is zero and cannot be inverted."); } +#endif real_t detInv = 1.0f / det; @@ -833,6 +835,12 @@ public Basis(Quaternion quaternion) /// The angle to rotate, in radians. public Basis(Vector3 axis, real_t angle) { +#if DEBUG + if (!axis.IsNormalized()) + { + throw new ArgumentException("Argument is not normalized.", nameof(axis)); + } +#endif Vector3 axisSq = new Vector3(axis.X * axis.X, axis.Y * axis.Y, axis.Z * axis.Z); (real_t sin, real_t cos) = real_t.SinCos(angle); diff --git a/src/Godot.Bindings/Core/Variants/Quaternion.cs b/src/Godot.Bindings/Core/Variants/Quaternion.cs index 9270b76..a348477 100644 --- a/src/Godot.Bindings/Core/Variants/Quaternion.cs +++ b/src/Godot.Bindings/Core/Variants/Quaternion.cs @@ -123,11 +123,11 @@ public readonly Quaternion SphericalCubicInterpolate(Quaternion b, Quaternion pr #if DEBUG if (!IsNormalized()) { - throw new InvalidOperationException("Quaternion is not normalized"); + throw new InvalidOperationException("Quaternion is not normalized."); } if (!b.IsNormalized()) { - throw new ArgumentException("Argument is not normalized", nameof(b)); + throw new ArgumentException("Argument is not normalized.", nameof(b)); } #endif @@ -196,11 +196,11 @@ public readonly Quaternion SphericalCubicInterpolateInTime(Quaternion b, Quatern #if DEBUG if (!IsNormalized()) { - throw new InvalidOperationException("Quaternion is not normalized"); + throw new InvalidOperationException("Quaternion is not normalized."); } if (!b.IsNormalized()) { - throw new ArgumentException("Argument is not normalized", nameof(b)); + throw new ArgumentException("Argument is not normalized.", nameof(b)); } #endif @@ -478,11 +478,11 @@ public readonly Quaternion Slerpni(Quaternion to, real_t weight) #if DEBUG if (!IsNormalized()) { - throw new InvalidOperationException("Quaternion is not normalized"); + throw new InvalidOperationException("Quaternion is not normalized."); } if (!to.IsNormalized()) { - throw new ArgumentException("Argument is not normalized", nameof(to)); + throw new ArgumentException("Argument is not normalized.", nameof(to)); } #endif @@ -665,7 +665,7 @@ public static Quaternion FromEuler(Vector3 eulerYXZ) #if DEBUG if (!quaternion.IsNormalized()) { - throw new InvalidOperationException("Quaternion is not normalized."); + throw new ArgumentException("Quaternion is not normalized.", nameof(quaternion)); } #endif var u = new Vector3(quaternion.X, quaternion.Y, quaternion.Z); diff --git a/src/Godot.Bindings/Core/Variants/Transform2D.cs b/src/Godot.Bindings/Core/Variants/Transform2D.cs index 52a0992..72a05fc 100644 --- a/src/Godot.Bindings/Core/Variants/Transform2D.cs +++ b/src/Godot.Bindings/Core/Variants/Transform2D.cs @@ -131,10 +131,12 @@ public readonly Transform2D AffineInverse() { real_t det = BasisDeterminant(); +#if DEBUG if (det == 0) { throw new InvalidOperationException("Matrix determinant is zero and cannot be inverted."); } +#endif Transform2D inv = this; diff --git a/src/Godot.Bindings/Core/Variants/Transform3D.cs b/src/Godot.Bindings/Core/Variants/Transform3D.cs index 9ea0e5b..f41999e 100644 --- a/src/Godot.Bindings/Core/Variants/Transform3D.cs +++ b/src/Godot.Bindings/Core/Variants/Transform3D.cs @@ -180,18 +180,17 @@ public readonly bool IsFinite() /// The resulting transform. public readonly Transform3D LookingAt(Vector3 target, Vector3? up = null, bool useModelFront = false) { +#if DEBUG + if (target.IsEqualApprox(Origin)) + { + throw new ArgumentException("The transform's origin and target can't be equal.", nameof(target)); + } +#endif Transform3D t = this; - t.SetLookAt(Origin, target, up ?? Vector3.Up, useModelFront); + t.Basis = Basis.LookingAt(target - Origin, up, useModelFront); return t; } - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public readonly Transform3D LookingAt(Vector3 target, Vector3 up) - { - return LookingAt(target, up, false); - } - /// /// Returns the transform with the basis orthogonal (90 degrees), /// and normalized axis vectors (scale of 1 or -1). @@ -256,12 +255,6 @@ public readonly Transform3D ScaledLocal(Vector3 scale) return new Transform3D(Basis * tmpBasis, Origin); } - private void SetLookAt(Vector3 eye, Vector3 target, Vector3 up, bool useModelFront = false) - { - Basis = Basis.LookingAt(target - eye, up, useModelFront); - Origin = eye; - } - /// /// Translates the transform by the given . /// The operation is done in the parent/global frame, equivalent to diff --git a/src/Godot.Bindings/Core/Variants/Vector2.cs b/src/Godot.Bindings/Core/Variants/Vector2.cs index 8e03402..84d5ed0 100644 --- a/src/Godot.Bindings/Core/Variants/Vector2.cs +++ b/src/Godot.Bindings/Core/Variants/Vector2.cs @@ -683,6 +683,12 @@ public readonly Vector2 Slerp(Vector2 to, real_t weight) /// The slid vector. public readonly Vector2 Slide(Vector2 normal) { +#if DEBUG + if (!normal.IsNormalized()) + { + throw new ArgumentException("Argument is not normalized.", nameof(normal)); + } +#endif return this - (normal * Dot(normal)); } diff --git a/src/Godot.Bindings/Core/Variants/Vector3.cs b/src/Godot.Bindings/Core/Variants/Vector3.cs index 894c796..bee0fc4 100644 --- a/src/Godot.Bindings/Core/Variants/Vector3.cs +++ b/src/Godot.Bindings/Core/Variants/Vector3.cs @@ -661,12 +661,6 @@ public readonly Vector3 Reflect(Vector3 normal) /// The rotated vector. public readonly Vector3 Rotated(Vector3 axis, real_t angle) { -#if DEBUG - if (!axis.IsNormalized()) - { - throw new ArgumentException("Argument is not normalized.", nameof(axis)); - } -#endif return new Basis(axis, angle) * this; } @@ -750,6 +744,12 @@ public readonly Vector3 Slerp(Vector3 to, real_t weight) /// The slid vector. public readonly Vector3 Slide(Vector3 normal) { +#if DEBUG + if (!normal.IsNormalized()) + { + throw new ArgumentException("Argument is not normalized.", nameof(normal)); + } +#endif return this - (normal * Dot(normal)); }