Browse Source

Bugfix: When specifying a -1 for min/max distance of a distance constraint and the calculated distance is incompatible with the other limit, we'll clamp it to that value now (#920)

Jorrit Rouwe 1 year ago
parent
commit
4615f9ca7f
1 changed files with 11 additions and 1 deletions
  1. 11 1
      Jolt/Physics/Constraints/DistanceConstraint.cpp

+ 11 - 1
Jolt/Physics/Constraints/DistanceConstraint.cpp

@@ -84,7 +84,17 @@ DistanceConstraint::DistanceConstraint(Body &inBody1, Body &inBody2, const Dista
 
 	// Store distance we want to keep between the world space points
 	float distance = Vec3(mWorldSpacePosition2 - mWorldSpacePosition1).Length();
-	SetDistance(mMinDistance < 0.0f? distance : mMinDistance, mMaxDistance < 0.0f? distance : mMaxDistance);
+	float min_distance, max_distance;
+	if (mMinDistance < 0.0f && mMaxDistance < 0.0f)
+	{
+		min_distance = max_distance = distance;
+	}
+	else
+	{
+		min_distance = mMinDistance < 0.0f? min(distance, mMaxDistance) : mMinDistance;
+		max_distance = mMaxDistance < 0.0f? max(distance, mMinDistance) : mMaxDistance;
+	}
+	SetDistance(min_distance, max_distance);
 
 	// Most likely gravity is going to tear us apart (this is only used when the distance between the points = 0)
 	mWorldSpaceNormal = Vec3::sAxisY();