Skip to content

Commit 3da2a2c

Browse files
Fix issue in contacts creation with integer overflow
1 parent e22ba24 commit 3da2a2c

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

include/reactphysics3d/configuration.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <utility>
3333
#include <sstream>
3434
#include <string>
35+
#include <cmath>
3536
#include <reactphysics3d/decimal.h>
3637
#include <reactphysics3d/containers/Pair.h>
3738

@@ -124,7 +125,7 @@ constexpr uint8 NB_MAX_CONTACT_MANIFOLDS = 3;
124125
constexpr uint8 NB_MAX_POTENTIAL_CONTACT_MANIFOLDS = 4 * NB_MAX_CONTACT_MANIFOLDS;
125126

126127
/// Maximum number of contact points in potential contact manifold
127-
constexpr uint16 NB_MAX_CONTACT_POINTS_IN_POTENTIAL_MANIFOLD = 256;
128+
constexpr uint8 NB_MAX_CONTACT_POINTS_IN_POTENTIAL_MANIFOLD = 255;
128129

129130
/// Distance threshold to consider that two contact points in a manifold are the same
130131
constexpr decimal SAME_CONTACT_POINT_DISTANCE_THRESHOLD = decimal(0.01);

src/systems/CollisionDetectionSystem.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ void CollisionDetectionSystem::createContacts() {
874874
for (uint32 m=0; m < contactPair.nbPotentialContactManifolds; m++) {
875875

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

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

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

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

1191+
assert(narrowPhaseInfoBatch.narrowPhaseInfos[i].nbContactPoints > 0);
1192+
11891193
// If we have a convex vs convex collision (if we consider the base collision shapes of the colliders)
1190-
if (mCollidersComponents.mCollisionShapes[collider1Index]->isConvex() && mCollidersComponents.mCollisionShapes[collider2Index]->isConvex()) {
1194+
if (mCollidersComponents.mCollisionShapes[collider1Index]->isConvex() &&
1195+
mCollidersComponents.mCollisionShapes[collider2Index]->isConvex()) {
11911196

11921197
// Create a new ContactPair
11931198

@@ -1226,6 +1231,7 @@ void CollisionDetectionSystem::processPotentialContacts(NarrowPhaseInfoBatch& na
12261231
}
12271232

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

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

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

1285+
assert(m < pairContact->nbPotentialContactManifolds);
1286+
12781287
uint32 contactManifoldIndex = pairContact->potentialContactManifoldsIndices[m];
12791288

1289+
assert(potentialContactManifolds[contactManifoldIndex].nbPotentialContactPoints > 0);
1290+
12801291
if (potentialContactManifolds[contactManifoldIndex].nbPotentialContactPoints < NB_MAX_CONTACT_POINTS_IN_POTENTIAL_MANIFOLD) {
12811292

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

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

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

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

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

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

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

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

1627+
assert(manifold.nbPotentialContactPoints > 0);
1628+
16141629
const decimal distThresholdSqr = SAME_CONTACT_POINT_DISTANCE_THRESHOLD * SAME_CONTACT_POINT_DISTANCE_THRESHOLD;
16151630

16161631
// For each contact point of the manifold
@@ -1634,6 +1649,8 @@ void CollisionDetectionSystem::removeDuplicatedContactPointsInManifold(ContactMa
16341649
}
16351650
}
16361651
}
1652+
1653+
assert(manifold.nbPotentialContactPoints > 0);
16371654
}
16381655

16391656
// Report contacts and triggers

0 commit comments

Comments
 (0)