Skip to content

Commit

Permalink
Fix issue in contacts creation with integer overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielChappuis committed Feb 3, 2023
1 parent e22ba24 commit 3da2a2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/reactphysics3d/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <utility>
#include <sstream>
#include <string>
#include <cmath>
#include <reactphysics3d/decimal.h>
#include <reactphysics3d/containers/Pair.h>

Expand Down Expand Up @@ -124,7 +125,7 @@ constexpr uint8 NB_MAX_CONTACT_MANIFOLDS = 3;
constexpr uint8 NB_MAX_POTENTIAL_CONTACT_MANIFOLDS = 4 * NB_MAX_CONTACT_MANIFOLDS;

/// Maximum number of contact points in potential contact manifold
constexpr uint16 NB_MAX_CONTACT_POINTS_IN_POTENTIAL_MANIFOLD = 256;
constexpr uint8 NB_MAX_CONTACT_POINTS_IN_POTENTIAL_MANIFOLD = 255;

/// Distance threshold to consider that two contact points in a manifold are the same
constexpr decimal SAME_CONTACT_POINT_DISTANCE_THRESHOLD = decimal(0.01);
Expand Down
21 changes: 19 additions & 2 deletions src/systems/CollisionDetectionSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ void CollisionDetectionSystem::createContacts() {
for (uint32 m=0; m < contactPair.nbPotentialContactManifolds; m++) {

ContactManifoldInfo& potentialManifold = mPotentialContactManifolds[contactPair.potentialContactManifoldsIndices[m]];
assert(potentialManifold.nbPotentialContactPoints > 0);

// Start index and number of contact points for this manifold
const uint32 contactPointsIndex = static_cast<uint32>(mCurrentContactPoints->size());
Expand Down Expand Up @@ -985,6 +986,7 @@ void CollisionDetectionSystem::createSnapshotContacts(Array<ContactPair>& contac
for (uint32 m=0; m < contactPair.nbPotentialContactManifolds; m++) {

ContactManifoldInfo& potentialManifold = potentialContactManifolds[contactPair.potentialContactManifoldsIndices[m]];
assert(potentialManifold.nbPotentialContactPoints > 0);

// Start index and number of contact points for this manifold
const uint32 contactPointsIndex = static_cast<uint32>(contactPoints.size());
Expand Down Expand Up @@ -1186,8 +1188,11 @@ void CollisionDetectionSystem::processPotentialContacts(NarrowPhaseInfoBatch& na
const Entity body1Entity = mCollidersComponents.mBodiesEntities[collider1Index];
const Entity body2Entity = mCollidersComponents.mBodiesEntities[collider2Index];

assert(narrowPhaseInfoBatch.narrowPhaseInfos[i].nbContactPoints > 0);

// If we have a convex vs convex collision (if we consider the base collision shapes of the colliders)
if (mCollidersComponents.mCollisionShapes[collider1Index]->isConvex() && mCollidersComponents.mCollisionShapes[collider2Index]->isConvex()) {
if (mCollidersComponents.mCollisionShapes[collider1Index]->isConvex() &&
mCollidersComponents.mCollisionShapes[collider2Index]->isConvex()) {

// Create a new ContactPair

Expand Down Expand Up @@ -1226,6 +1231,7 @@ void CollisionDetectionSystem::processPotentialContacts(NarrowPhaseInfoBatch& na
}

// Add the contact manifold to the overlapping pair contact
assert(potentialContactManifolds[contactManifoldIndex].nbPotentialContactPoints > 0);
assert(pairId == contactManifoldInfo.pairId);
pairContact->potentialContactManifoldsIndices[0] = contactManifoldIndex;
pairContact->nbPotentialContactManifolds = 1;
Expand Down Expand Up @@ -1259,6 +1265,7 @@ void CollisionDetectionSystem::processPotentialContacts(NarrowPhaseInfoBatch& na
}

assert(pairContact != nullptr);
assert(narrowPhaseInfoBatch.narrowPhaseInfos[i].nbContactPoints > 0);

// Add the potential contacts
for (uint32 j=0; j < narrowPhaseInfoBatch.narrowPhaseInfos[i].nbContactPoints; j++) {
Expand All @@ -1275,12 +1282,15 @@ void CollisionDetectionSystem::processPotentialContacts(NarrowPhaseInfoBatch& na
// For each contact manifold of the overlapping pair
for (uint32 m=0; m < pairContact->nbPotentialContactManifolds; m++) {

assert(m < pairContact->nbPotentialContactManifolds);

uint32 contactManifoldIndex = pairContact->potentialContactManifoldsIndices[m];

assert(potentialContactManifolds[contactManifoldIndex].nbPotentialContactPoints > 0);

if (potentialContactManifolds[contactManifoldIndex].nbPotentialContactPoints < NB_MAX_CONTACT_POINTS_IN_POTENTIAL_MANIFOLD) {

// Get the first contact point of the current manifold
assert(potentialContactManifolds[contactManifoldIndex].nbPotentialContactPoints > 0);
const uint manifoldContactPointIndex = potentialContactManifolds[contactManifoldIndex].potentialContactPointsIndices[0];
const ContactPointInfo& manifoldContactPoint = potentialContactPoints[manifoldContactPointIndex];

Expand Down Expand Up @@ -1314,6 +1324,7 @@ void CollisionDetectionSystem::processPotentialContacts(NarrowPhaseInfoBatch& na
assert(pairContact != nullptr);

// Add the contact manifold to the overlapping pair contact
assert(potentialContactManifolds[contactManifoldIndex].nbPotentialContactPoints > 0);
assert(pairContact->pairId == contactManifoldInfo.pairId);
pairContact->potentialContactManifoldsIndices[pairContact->nbPotentialContactManifolds] = contactManifoldIndex;
pairContact->nbPotentialContactManifolds++;
Expand Down Expand Up @@ -1350,6 +1361,7 @@ void CollisionDetectionSystem::reducePotentialContactManifolds(Array<ContactPair
for (uint32 j=0; j < contactPair.nbPotentialContactManifolds; j++) {

ContactManifoldInfo& manifold = potentialContactManifolds[contactPair.potentialContactManifoldsIndices[j]];
assert(manifold.nbPotentialContactPoints > 0);

// Get the largest contact point penetration depth of the manifold
const decimal depth = computePotentialManifoldLargestContactDepth(manifold, potentialContactPoints);
Expand All @@ -1375,6 +1387,7 @@ void CollisionDetectionSystem::reducePotentialContactManifolds(Array<ContactPair
for (uint32 j=0; j < pairContact.nbPotentialContactManifolds; j++) {

ContactManifoldInfo& manifold = potentialContactManifolds[pairContact.potentialContactManifoldsIndices[j]];
assert(manifold.nbPotentialContactPoints > 0);

// If there are two many contact points in the manifold
if (manifold.nbPotentialContactPoints > MAX_CONTACT_POINTS_IN_MANIFOLD) {
Expand Down Expand Up @@ -1611,6 +1624,8 @@ void CollisionDetectionSystem::removeDuplicatedContactPointsInManifold(ContactMa

RP3D_PROFILE("CollisionDetectionSystem::removeDuplicatedContactPointsInManifold()", mProfiler);

assert(manifold.nbPotentialContactPoints > 0);

const decimal distThresholdSqr = SAME_CONTACT_POINT_DISTANCE_THRESHOLD * SAME_CONTACT_POINT_DISTANCE_THRESHOLD;

// For each contact point of the manifold
Expand All @@ -1634,6 +1649,8 @@ void CollisionDetectionSystem::removeDuplicatedContactPointsInManifold(ContactMa
}
}
}

assert(manifold.nbPotentialContactPoints > 0);
}

// Report contacts and triggers
Expand Down

0 comments on commit 3da2a2c

Please sign in to comment.