From 9404f0ee18300c9895b394d4a7b6e982dd7d31b2 Mon Sep 17 00:00:00 2001
From: Jesse Louis-Rosenberg <jesse@n-e-r-v-o-u-s.com>
Date: Mon, 17 Jul 2017 09:44:45 -0400
Subject: [PATCH] fix edge edge constraint

when intersection is past the end points need to recalculate closest
position
---
 .../PositionBasedDynamics.cpp                 | 37 +++++++++++++++++--
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/PositionBasedDynamics/PositionBasedDynamics.cpp b/PositionBasedDynamics/PositionBasedDynamics.cpp
index 79ec0f2b..735c8a85 100644
--- a/PositionBasedDynamics/PositionBasedDynamics.cpp
+++ b/PositionBasedDynamics/PositionBasedDynamics.cpp
@@ -449,10 +449,39 @@ bool PositionBasedDynamics::solve_EdgeEdgeDistanceConstraint(
 			t = (t0 == t1) ? 0.5 : (mid - t0) / (t1 - t0);
 		}
 	}
-	if (s < 0.0) s = 0.0;
-	if (s > 1.0) s = 1.0;
-	if (t < 0.0) t = 0.0;
-	if (t > 1.0) t = 1.0;
+  
+	if (s < 0.0) {
+		s = 0.0;
+		t = f / d;
+	}
+	else if (s > 1.0) {
+		s = 1.0;
+		t = (f + b) / d;
+	}
+	if (t < 0.0) {
+		t = 0.0;
+		if (e<0) {
+			s = 0.0;
+		}
+		else if (e>a) {
+			s = 1.0;
+		}
+		else {
+			s = e / a;
+		}
+	}
+	else if (t > 1.0) {
+		t = 1.0;
+		if (e - b < 0.0) {
+			s = 0.0;
+		}
+		else if ((e - b) > a) {
+			s = 1.0;
+		}
+		else {
+			s = (e - b) / a;
+		}
+	}
 
 	Real b0 = 1.0 - s;
 	Real b1 = s;