Browse Source

Prevent division by zero if normal and displacement are perpendicular due to float round off

Jorrit Rouwe 3 years ago
parent
commit
2f1d3865ce
1 changed files with 3 additions and 1 deletions
  1. 3 1
      Jolt/Physics/Character/CharacterVirtual.cpp

+ 3 - 1
Jolt/Physics/Character/CharacterVirtual.cpp

@@ -209,7 +209,9 @@ bool CharacterVirtual::GetFirstContactForSweep(Vec3Arg inPosition, Vec3Arg inDis
 	// <=> d' = -p |d| / n dot d
 	// The new fraction of collision is then:
 	// f' = f - d' / |d| = f + p / n dot d
-	outContact.mFraction = max(0.0f, outContact.mFraction + mCharacterPadding / outContact.mNormal.Dot(inDisplacement));
+	float dot = outContact.mNormal.Dot(inDisplacement);
+	if (dot < 0.0f) // We should not divide by zero and we should only update the fraction if normal is pointing towards displacement
+		outContact.mFraction = max(0.0f, outContact.mFraction + mCharacterPadding / dot);
 	return true;
 }