Skip to content

Commit 326038f

Browse files
Merge branch 'robertocapuano-capsule_collision_fix' into develop
2 parents 751bedd + e3ee98c commit 326038f

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/collision/narrowphase/CapsuleVsCapsuleAlgorithm.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,27 @@ bool CapsuleVsCapsuleAlgorithm::testCollision(NarrowPhaseInfoBatch& narrowPhaseI
182182
if (closestPointsDistanceSquare > MACHINE_EPSILON) {
183183

184184
decimal closestPointsDistance = std::sqrt(closestPointsDistanceSquare);
185-
closestPointsSeg1ToSeg2 /= closestPointsDistance;
185+
decimal penetrationDepth = sumRadius - closestPointsDistance;
186186

187-
const Vector3 contactPointCapsule1Local = capsule1ToCapsule2SpaceTransform.getInverse() * (closestPointCapsule1Seg + closestPointsSeg1ToSeg2 * capsule1Radius);
188-
const Vector3 contactPointCapsule2Local = closestPointCapsule2Seg - closestPointsSeg1ToSeg2 * capsule2Radius;
187+
// Make sure the penetration depth is not zero (even if the previous condition test was true the penetration depth can still be
188+
// zero because of precision issue of the computation at the previous line)
189+
if (penetrationDepth > 0) {
189190

190-
const Vector3 normalWorld = narrowPhaseInfoBatch.narrowPhaseInfos[batchIndex].shape2ToWorldTransform.getOrientation() * closestPointsSeg1ToSeg2;
191+
closestPointsSeg1ToSeg2 /= closestPointsDistance;
191192

192-
decimal penetrationDepth = sumRadius - closestPointsDistance;
193+
const Vector3 contactPointCapsule1Local = capsule1ToCapsule2SpaceTransform.getInverse() * (closestPointCapsule1Seg + closestPointsSeg1ToSeg2 * capsule1Radius);
194+
const Vector3 contactPointCapsule2Local = closestPointCapsule2Seg - closestPointsSeg1ToSeg2 * capsule2Radius;
193195

194-
// Create the contact info object
195-
narrowPhaseInfoBatch.addContactPoint(batchIndex, normalWorld, penetrationDepth, contactPointCapsule1Local, contactPointCapsule2Local);
196+
const Vector3 normalWorld = narrowPhaseInfoBatch.narrowPhaseInfos[batchIndex].shape2ToWorldTransform.getOrientation() * closestPointsSeg1ToSeg2;
197+
198+
// Create the contact info object
199+
narrowPhaseInfoBatch.addContactPoint(batchIndex, normalWorld, penetrationDepth, contactPointCapsule1Local, contactPointCapsule2Local);
200+
201+
narrowPhaseInfoBatch.narrowPhaseInfos[batchIndex].isColliding = true;
202+
isCollisionFound = true;
203+
}
196204
}
197-
else { // The segment are overlapping (degenerate case)
205+
else if (sumRadius > 0){ // The segment are overlapping (degenerate case)
198206

199207
// If the capsule segments are parralel
200208
if (areCapsuleInnerSegmentsParralel) {
@@ -231,11 +239,11 @@ bool CapsuleVsCapsuleAlgorithm::testCollision(NarrowPhaseInfoBatch& narrowPhaseI
231239
// Create the contact info object
232240
narrowPhaseInfoBatch.addContactPoint(batchIndex, normalWorld, sumRadius, contactPointCapsule1Local, contactPointCapsule2Local);
233241
}
242+
243+
narrowPhaseInfoBatch.narrowPhaseInfos[batchIndex].isColliding = true;
244+
isCollisionFound = true;
234245
}
235246
}
236-
237-
narrowPhaseInfoBatch.narrowPhaseInfos[batchIndex].isColliding = true;
238-
isCollisionFound = true;
239247
}
240248
}
241249

0 commit comments

Comments
 (0)