Skip to content

Commit

Permalink
Guard against non-invertible inertia matrix (#144)
Browse files Browse the repository at this point in the history
Fixes issues/142.
  • Loading branch information
notgiven688 authored May 30, 2024
1 parent bbc6c41 commit b10c16e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/Jitter2/Dynamics/RigidBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,11 @@ public void SetMassInertia()
mass += shapes[i].Mass;
}

JMatrix.Inverse(inertia, out inverseInertia);
if (!JMatrix.Inverse(inertia, out inverseInertia))
{
throw new ArgumentException("Inertia matrix is not invertible.", nameof(inertia));
}

this.inverseMass = 1.0f / mass;

UpdateWorldInertia();
Expand Down
6 changes: 2 additions & 4 deletions src/Jitter2/LinearMath/JMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ public readonly float Determinant()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Inverse(in JMatrix matrix, out JMatrix result)
{
float det = matrix.Determinant();
float idet = 1.0f / matrix.Determinant();

if (float.IsNaN(det) || float.IsInfinity(det))
if (!float.IsNormal(idet))
{
result = new JMatrix();
return false;
Expand All @@ -353,8 +353,6 @@ public static bool Inverse(in JMatrix matrix, out JMatrix result)
float num32 = matrix.M12 * matrix.M31 - matrix.M32 * matrix.M11;
float num33 = matrix.M11 * matrix.M22 - matrix.M21 * matrix.M12;

float idet = 1.0f / det;

result.M11 = num11 * idet;
result.M12 = num12 * idet;
result.M13 = num13 * idet;
Expand Down

0 comments on commit b10c16e

Please sign in to comment.