From 251333a6ef299e0167a857092dc2eceb4edebfde Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Fri, 24 May 2019 07:15:31 +0200 Subject: [PATCH] Working on contacts --- src/collision/CollisionDetection.cpp | 60 +++++++++++++++++----------- src/collision/CollisionDetection.h | 5 ++- src/collision/ProxyShape.cpp | 8 ++++ 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/collision/CollisionDetection.cpp b/src/collision/CollisionDetection.cpp index 9f2e588b..04d266ba 100644 --- a/src/collision/CollisionDetection.cpp +++ b/src/collision/CollisionDetection.cpp @@ -109,6 +109,36 @@ void CollisionDetection::computeBroadPhase() { // Create new overlapping pairs if necessary updateOverlappingPairs(overlappingNodes); + + // Remove non overlapping pairs + removeNonOverlappingPairs(); +} + +// Remove pairs that are not overlapping anymore +void CollisionDetection::removeNonOverlappingPairs() { + + // For each possible collision pair of bodies + for (auto it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); ) { + + OverlappingPair* pair = it->second; + + ProxyShape* shape1 = pair->getShape1(); + ProxyShape* shape2 = pair->getShape2(); + + // Check if the two shapes are still overlapping. Otherwise, we destroy the overlapping pair + if (!mBroadPhaseSystem.testOverlappingShapes(shape1, shape2)) { + + // Destroy the overlapping pair + pair->~OverlappingPair(); + + mWorld->mMemoryManager.release(MemoryManager::AllocationType::Pool, pair, sizeof(OverlappingPair)); + it = mOverlappingPairs.remove(it); + continue; + } + else { + ++it; + } + } } // Take a list of overlapping nodes in the broad-phase and create new overlapping pairs if necessary @@ -183,7 +213,7 @@ void CollisionDetection::computeMiddlePhase() { mNarrowPhaseInput.reserveMemory(); // For each possible collision pair of bodies - for (auto it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); ) { + for (auto it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); ++it) { OverlappingPair* pair = it->second; @@ -200,21 +230,6 @@ void CollisionDetection::computeMiddlePhase() { assert(mProxyShapesComponents.getBroadPhaseId(proxyShape2Entity) != -1); assert(mProxyShapesComponents.getBroadPhaseId(proxyShape1Entity) != mProxyShapesComponents.getBroadPhaseId(proxyShape2Entity)); - // Check if the two shapes are still overlapping. Otherwise, we destroy the - // overlapping pair - if (!mBroadPhaseSystem.testOverlappingShapes(shape1, shape2)) { - - // Destroy the overlapping pair - pair->~OverlappingPair(); - - mWorld->mMemoryManager.release(MemoryManager::AllocationType::Pool, pair, sizeof(OverlappingPair)); - it = mOverlappingPairs.remove(it); - continue; - } - else { - ++it; - } - // Check if the collision filtering allows collision between the two shapes if ((mProxyShapesComponents.getCollideWithMaskBits(proxyShape1Entity) & mProxyShapesComponents.getCollisionCategoryBits(proxyShape2Entity)) != 0 && (mProxyShapesComponents.getCollisionCategoryBits(proxyShape1Entity) & mProxyShapesComponents.getCollideWithMaskBits(proxyShape2Entity)) != 0) { @@ -398,9 +413,6 @@ void CollisionDetection::processAllPotentialContacts(NarrowPhaseInput& narrowPha // Compute the narrow-phase collision detection void CollisionDetection::computeNarrowPhase() { - // TODO : DELETE THIS - //std::cout << "---------------------- NARROW PHASE ----------------------------------------" << std::endl; - RP3D_PROFILE("CollisionDetection::computeNarrowPhase()", mProfiler); MemoryAllocator& allocator = mMemoryManager.getSingleFrameAllocator(); @@ -415,7 +427,7 @@ void CollisionDetection::computeNarrowPhase() { processAllPotentialContacts(mNarrowPhaseInput, true); // Reduce the number of contact points in the manifolds - reducePotentialContactManifolds(mOverlappingPairs); + reducePotentialContactManifolds(); // Report contacts to the user reportAllContacts(); @@ -840,7 +852,7 @@ void CollisionDetection::processPotentialContacts(NarrowPhaseInfoBatch& narrowPh } // Clear the obsolete manifolds and contact points and reduce the number of contacts points of the remaining manifolds -void CollisionDetection::reducePotentialContactManifolds(const OverlappingPairMap& overlappingPairs) { +void CollisionDetection::reducePotentialContactManifolds() { RP3D_PROFILE("CollisionDetection::reducePotentialContactManifolds()", mProfiler); @@ -1369,7 +1381,7 @@ void CollisionDetection::testCollision(CollisionBody* body1, CollisionBody* body processAllPotentialContacts(narrowPhaseInput, false); // Reduce the number of contact points in the manifolds - reducePotentialContactManifolds(overlappingPairs); + //reducePotentialContactManifolds(overlappingPairs); // TODO : Rework how we report contacts /* @@ -1467,7 +1479,7 @@ void CollisionDetection::testCollision(CollisionBody* body, CollisionCallback* c processAllPotentialContacts(narrowPhaseInput, false); // Reduce the number of contact points in the manifolds - reducePotentialContactManifolds(overlappingPairs); + //reducePotentialContactManifolds(overlappingPairs); // TODO : Rework how we report contacts /* @@ -1549,7 +1561,7 @@ void CollisionDetection::testCollision(CollisionCallback* callback) { processAllPotentialContacts(narrowPhaseInput, false); // Reduce the number of contact points in the manifolds - reducePotentialContactManifolds(overlappingPairs); + //reducePotentialContactManifolds(overlappingPairs); // TODO : Rework how we report contacts /* diff --git a/src/collision/CollisionDetection.h b/src/collision/CollisionDetection.h index 4829db37..a0d52357 100644 --- a/src/collision/CollisionDetection.h +++ b/src/collision/CollisionDetection.h @@ -189,6 +189,9 @@ class CollisionDetection { /// Take a list of overlapping nodes in the broad-phase and create new overlapping pairs if necessary void updateOverlappingPairs(List >& overlappingNodes); + /// Remove pairs that are not overlapping anymore + void removeNonOverlappingPairs(); + /// Execute the narrow-phase collision detection algorithm on batches bool testNarrowPhaseCollision(NarrowPhaseInput& narrowPhaseInput, bool stopFirstContactFound, bool reportContacts, MemoryAllocator& allocator); @@ -211,7 +214,7 @@ class CollisionDetection { void processAllPotentialContacts(NarrowPhaseInput& narrowPhaseInput, bool updateLastFrameInfo); /// Reduce the potential contact manifolds and contact points of the overlapping pair contacts - void reducePotentialContactManifolds(const OverlappingPairMap& overlappingPairs); + void reducePotentialContactManifolds(); /// Create the actual contact manifolds and contacts points (from potential contacts) for a given contact pair void createContacts(); diff --git a/src/collision/ProxyShape.cpp b/src/collision/ProxyShape.cpp index d8dbeacd..cbb34788 100644 --- a/src/collision/ProxyShape.cpp +++ b/src/collision/ProxyShape.cpp @@ -78,6 +78,10 @@ bool ProxyShape::testPointInside(const Vector3& worldPoint) { */ void ProxyShape::setCollisionCategoryBits(unsigned short collisionCategoryBits) { + // TODO : Here we should probably remove all overlapping pairs with this shape in the + // broad-phase and add the shape in the "has moved" shape list so it is reevaluated + // with the new mask bits + mBody->mWorld.mProxyShapesComponents.setCollisionCategoryBits(mEntity, collisionCategoryBits); int broadPhaseId = mBody->mWorld.mProxyShapesComponents.getBroadPhaseId(mEntity); @@ -93,6 +97,10 @@ void ProxyShape::setCollisionCategoryBits(unsigned short collisionCategoryBits) */ void ProxyShape::setCollideWithMaskBits(unsigned short collideWithMaskBits) { + // TODO : Here we should probably remove all overlapping pairs with this shape in the + // broad-phase and add the shape in the "has moved" shape list so it is reevaluated + // with the new mask bits + mBody->mWorld.mProxyShapesComponents.setCollideWithMaskBits(mEntity, collideWithMaskBits); int broadPhaseId = mBody->mWorld.mProxyShapesComponents.getBroadPhaseId(mEntity);