Skip to content

Commit

Permalink
Sync math checks with main repo
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
raulsntos committed Sep 3, 2024
1 parent 0e5da45 commit a88de3c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
8 changes: 8 additions & 0 deletions src/Godot.Bindings/Core/Variants/Basis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -833,6 +835,12 @@ public Basis(Quaternion quaternion)
/// <param name="angle">The angle to rotate, in radians.</param>
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);

Expand Down
14 changes: 7 additions & 7 deletions src/Godot.Bindings/Core/Variants/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/Godot.Bindings/Core/Variants/Transform2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 7 additions & 14 deletions src/Godot.Bindings/Core/Variants/Transform3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,17 @@ public readonly bool IsFinite()
/// <returns>The resulting transform.</returns>
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;
}

/// <inheritdoc cref="LookingAt(Vector3, Nullable{Vector3}, bool)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly Transform3D LookingAt(Vector3 target, Vector3 up)
{
return LookingAt(target, up, false);
}

/// <summary>
/// Returns the transform with the basis orthogonal (90 degrees),
/// and normalized axis vectors (scale of 1 or -1).
Expand Down Expand Up @@ -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;
}

/// <summary>
/// Translates the transform by the given <paramref name="offset"/>.
/// The operation is done in the parent/global frame, equivalent to
Expand Down
6 changes: 6 additions & 0 deletions src/Godot.Bindings/Core/Variants/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,12 @@ public readonly Vector2 Slerp(Vector2 to, real_t weight)
/// <returns>The slid vector.</returns>
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));
}

Expand Down
12 changes: 6 additions & 6 deletions src/Godot.Bindings/Core/Variants/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,6 @@ public readonly Vector3 Reflect(Vector3 normal)
/// <returns>The rotated vector.</returns>
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;
}

Expand Down Expand Up @@ -750,6 +744,12 @@ public readonly Vector3 Slerp(Vector3 to, real_t weight)
/// <returns>The slid vector.</returns>
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));
}

Expand Down

0 comments on commit a88de3c

Please sign in to comment.