From c7a67d509e8dea017493f7579aa6b8b6e71dee6b Mon Sep 17 00:00:00 2001 From: Matt Bennice Date: Thu, 11 Aug 2022 11:09:08 -0700 Subject: [PATCH 1/4] Update btSoftBody to handle close to degenerate triangles. --- src/BulletSoftBody/btSoftBody.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/BulletSoftBody/btSoftBody.cpp b/src/BulletSoftBody/btSoftBody.cpp index e91c1b9a4..5d5073549 100644 --- a/src/BulletSoftBody/btSoftBody.cpp +++ b/src/BulletSoftBody/btSoftBody.cpp @@ -2816,6 +2816,16 @@ static void getBarycentric(const btVector3& p, btVector3& a, btVector3& b, btVec btScalar d20 = v2.dot(v0); btScalar d21 = v2.dot(v1); btScalar denom = d00 * d11 - d01 * d01; + if (denom == btScalar(0.0)) + { + bary.setY(btScalar(0.0)); + bary.setZ(btScalar(0.0)); + } + else + { + bary.setY((d11 * d20 - d01 * d21) / denom); + bary.setZ((d00 * d21 - d01 * d20) / denom); + } bary.setY((d11 * d20 - d01 * d21) / denom); bary.setZ((d00 * d21 - d01 * d20) / denom); bary.setX(btScalar(1) - bary.getY() - bary.getZ()); From 82b7fa6df0ce50589cfa872d4971c14d799e8c7b Mon Sep 17 00:00:00 2001 From: Matt Bennice Date: Thu, 11 Aug 2022 11:46:39 -0700 Subject: [PATCH 2/4] Update parameters to be const. --- src/BulletSoftBody/btSoftBody.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/BulletSoftBody/btSoftBody.cpp b/src/BulletSoftBody/btSoftBody.cpp index 5d5073549..9c438088a 100644 --- a/src/BulletSoftBody/btSoftBody.cpp +++ b/src/BulletSoftBody/btSoftBody.cpp @@ -2807,7 +2807,7 @@ bool btSoftBody::checkDeformableContact(const btCollisionObjectWrapper* colObjWr // // Compute barycentric coordinates (u, v, w) for // point p with respect to triangle (a, b, c) -static void getBarycentric(const btVector3& p, btVector3& a, btVector3& b, btVector3& c, btVector3& bary) +static void getBarycentric(const btVector3& p, const btVector3& a, const btVector3& b, const btVector3& c, btVector3& bary) { btVector3 v0 = b - a, v1 = c - a, v2 = p - a; btScalar d00 = v0.dot(v0); @@ -2816,6 +2816,7 @@ static void getBarycentric(const btVector3& p, btVector3& a, btVector3& b, btVec btScalar d20 = v2.dot(v0); btScalar d21 = v2.dot(v1); btScalar denom = d00 * d11 - d01 * d01; + // In the case of a degenerate triangle, pick a vertex. if (denom == btScalar(0.0)) { bary.setY(btScalar(0.0)); From 555c0c6cd925a93e9be6812a2e235a4df6d2b0c4 Mon Sep 17 00:00:00 2001 From: Matt Bennice Date: Thu, 11 Aug 2022 11:55:50 -0700 Subject: [PATCH 3/4] Remove the duplicated code outside of else --- src/BulletSoftBody/btSoftBody.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/BulletSoftBody/btSoftBody.cpp b/src/BulletSoftBody/btSoftBody.cpp index 9c438088a..64005cc2c 100644 --- a/src/BulletSoftBody/btSoftBody.cpp +++ b/src/BulletSoftBody/btSoftBody.cpp @@ -2827,8 +2827,6 @@ static void getBarycentric(const btVector3& p, const btVector3& a, const btVecto bary.setY((d11 * d20 - d01 * d21) / denom); bary.setZ((d00 * d21 - d01 * d20) / denom); } - bary.setY((d11 * d20 - d01 * d21) / denom); - bary.setZ((d00 * d21 - d01 * d20) / denom); bary.setX(btScalar(1) - bary.getY() - bary.getZ()); } From ee0100abbfebc19de560700bb4ad73064aa4c982 Mon Sep 17 00:00:00 2001 From: Matt Bennice Date: Thu, 11 Aug 2022 14:00:12 -0700 Subject: [PATCH 4/4] compare to SIMD_EPSILON. --- src/BulletSoftBody/btSoftBody.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BulletSoftBody/btSoftBody.cpp b/src/BulletSoftBody/btSoftBody.cpp index 64005cc2c..c873099b4 100644 --- a/src/BulletSoftBody/btSoftBody.cpp +++ b/src/BulletSoftBody/btSoftBody.cpp @@ -2817,7 +2817,7 @@ static void getBarycentric(const btVector3& p, const btVector3& a, const btVecto btScalar d21 = v2.dot(v1); btScalar denom = d00 * d11 - d01 * d01; // In the case of a degenerate triangle, pick a vertex. - if (denom == btScalar(0.0)) + if (btFabs(denom) < SIMD_EPSILON) { bary.setY(btScalar(0.0)); bary.setZ(btScalar(0.0));