@@ -382,9 +382,11 @@ namespace sndx::collision {
382382 size_t iterations = 0 ;
383383
384384 auto dir = glm::normalize (-support.out );
385- while (true ) {
385+ while (iterations < 1024 ) {
386386 support = detail::gjkMinkowski (supportA, supportB, dir);
387- if (!detail::similarDir (support.out , dir)) {
387+
388+ // it is okay for the 2nd point to have a negative dot product.
389+ if (!detail::similarDir (support.out , dir) && iterations > 0 ) {
388390 return std::nullopt ;
389391 }
390392
@@ -534,8 +536,6 @@ namespace sndx::collision {
534536 float sDistance = glm::dot (minNormal, support.out );
535537
536538 if (abs (sDistance - minDistance) > 0 .0001f ) {
537- minDistance = FLT_MAX ;
538-
539539 std::vector<std::pair<size_t , size_t >> uniqueEdges;
540540
541541 for (size_t i = 0 ; i < normals.size (); i++) {
@@ -557,6 +557,12 @@ namespace sndx::collision {
557557 }
558558 }
559559
560+ if (uniqueEdges.empty ()) {
561+ // @TODO figure out why this happens
562+ return EpaResult{};
563+ }
564+ assert (uniqueEdges.size () > 0 );
565+
560566 std::vector<size_t > newFaces;
561567 for (auto [edgeIndex1, edgeIndex2] : uniqueEdges) {
562568 newFaces.emplace_back (edgeIndex1);
@@ -582,6 +588,8 @@ namespace sndx::collision {
582588
583589 faces.insert (faces.end (), newFaces.begin (), newFaces.end ());
584590 normals.insert (normals.end (), newNormals.begin (), newNormals.end ());
591+
592+ minDistance = FLT_MAX ;
585593 }
586594 }
587595
0 commit comments