Skip to content

Commit 9ecec6f

Browse files
kbayescopybara-github
authored andcommitted
Fix bad contact from passing through polytope2 in nativeccd.
PiperOrigin-RevId: 742666700 Change-Id: I76034579b4e663f9abfa265ec3e2bc608f631f59
1 parent 0f45648 commit 9ecec6f

File tree

3 files changed

+155
-78
lines changed

3 files changed

+155
-78
lines changed

src/engine/engine_collision_gjk.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,26 @@ static void rotmat(mjtNum R[9], const mjtNum axis[3]) {
903903

904904

905905

906+
// return nonzero if the ray v1v2 intersects the triangle v3v4v5
907+
static inline int rayTriangle(const mjtNum v1[3], const mjtNum v2[3], const mjtNum v3[3],
908+
const mjtNum v4[3], const mjtNum v5[3]) {
909+
mjtNum diff12[3], diff13[3], diff14[3], diff15[3];
910+
sub3(diff12, v2, v1);
911+
sub3(diff13, v3, v1);
912+
sub3(diff14, v4, v1);
913+
sub3(diff15, v5, v1);
914+
915+
mjtNum vol1 = det3(diff13, diff14, diff12);
916+
mjtNum vol2 = det3(diff14, diff15, diff12);
917+
mjtNum vol3 = det3(diff15, diff13, diff12);
918+
919+
if (vol1 >= 0 && vol2 >= 0 && vol3 >= 0) return 1;
920+
if (vol1 <= 0 && vol2 <= 0 && vol3 <= 0) return -1;
921+
return 0;
922+
}
923+
924+
925+
906926
// create a polytope from a 1-simplex (returns 0 on success)
907927
static int polytope2(Polytope* pt, mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj* obj2) {
908928
mjtNum *v1 = status->simplex[0].vert, *v2 = status->simplex[1].vert;
@@ -970,9 +990,9 @@ static int polytope2(Polytope* pt, mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj
970990
return polytope3(pt, status, obj1, obj2);
971991
}
972992

973-
// check that origin is in the hexahedron
974-
if (status->dist > 10*mjMINVAL && !testTetra(v1, v3, v4, v5) && !testTetra(v2, v3, v4, v5)) {
975-
return mjEPA_P2_MISSING_ORIGIN;
993+
// check hexahedron is convex
994+
if (!rayTriangle(v1, v2, v3, v4, v5)) {
995+
return mjEPA_P2_NONCONVEX;
976996
}
977997

978998
for (int i = 0; i < 6; i++) {

src/engine/engine_collision_gjk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ typedef enum {
4444
mjEPA_NOCONTACT = -1,
4545
mjEPA_SUCCESS = 0,
4646
mjEPA_P2_INVALID_FACES,
47-
mjEPA_P2_MISSING_ORIGIN,
47+
mjEPA_P2_NONCONVEX,
4848
mjEPA_P2_ORIGIN_ON_FACE,
4949
mjEPA_P3_BAD_NORMAL,
5050
mjEPA_P3_INVALID_V4,

0 commit comments

Comments
 (0)