diff --git a/include/reactphysics3d/engine/Islands.h b/include/reactphysics3d/engine/Islands.h index bfe57081..cc3d42b9 100644 --- a/include/reactphysics3d/engine/Islands.h +++ b/include/reactphysics3d/engine/Islands.h @@ -49,6 +49,9 @@ struct Islands { /// Number of islands in the previous frame uint32 mNbIslandsPreviousFrame; + /// Number of items in the bodyEntities array in the previous frame + uint32 mNbBodyEntitiesPreviousFrame; + /// Maximum number of bodies in a single island in the previous frame uint32 mNbMaxBodiesInIslandPreviousFrame; @@ -79,7 +82,7 @@ struct Islands { /// Constructor Islands(MemoryAllocator& allocator) - :mNbIslandsPreviousFrame(10), mNbMaxBodiesInIslandPreviousFrame(0), mNbMaxBodiesInIslandCurrentFrame(0), + :mNbIslandsPreviousFrame(16), mNbBodyEntitiesPreviousFrame(32), mNbMaxBodiesInIslandPreviousFrame(0), mNbMaxBodiesInIslandCurrentFrame(0), contactManifoldsIndices(allocator), nbContactManifolds(allocator), bodyEntities(allocator), startBodyEntitiesIndex(allocator), nbBodiesInIsland(allocator) { @@ -133,7 +136,7 @@ struct Islands { startBodyEntitiesIndex.reserve(mNbIslandsPreviousFrame); nbBodiesInIsland.reserve(mNbIslandsPreviousFrame); - bodyEntities.reserve(mNbMaxBodiesInIslandPreviousFrame); + bodyEntities.reserve(mNbBodyEntitiesPreviousFrame); } /// Clear all the islands @@ -145,9 +148,10 @@ struct Islands { mNbMaxBodiesInIslandCurrentFrame = nbBodiesInIsland[nbIslands-1]; } - mNbIslandsPreviousFrame = nbContactManifolds.size(); - mNbIslandsPreviousFrame = mNbMaxBodiesInIslandCurrentFrame; + mNbMaxBodiesInIslandPreviousFrame = mNbMaxBodiesInIslandCurrentFrame; + mNbIslandsPreviousFrame = nbIslands; mNbMaxBodiesInIslandCurrentFrame = 0; + mNbBodyEntitiesPreviousFrame = bodyEntities.size(); contactManifoldsIndices.clear(true); nbContactManifolds.clear(true); diff --git a/include/reactphysics3d/systems/CollisionDetectionSystem.h b/include/reactphysics3d/systems/CollisionDetectionSystem.h index 5cb28461..65c9d4b8 100644 --- a/include/reactphysics3d/systems/CollisionDetectionSystem.h +++ b/include/reactphysics3d/systems/CollisionDetectionSystem.h @@ -98,6 +98,9 @@ class CollisionDetectionSystem { /// Broad-phase overlapping pairs OverlappingPairs mOverlappingPairs; + /// Overlapping nodes during broad-phase computation + List> mBroadPhaseOverlappingNodes; + /// Broad-phase system BroadPhaseSystem mBroadPhaseSystem; diff --git a/src/collision/CollisionCallback.cpp b/src/collision/CollisionCallback.cpp index c9fbc5ff..34d40f51 100644 --- a/src/collision/CollisionCallback.cpp +++ b/src/collision/CollisionCallback.cpp @@ -79,7 +79,8 @@ CollisionCallback::ContactPair::EventType CollisionCallback::ContactPair::getEve CollisionCallback::CallbackData::CallbackData(List* contactPairs, List* manifolds, List* contactPoints, List& lostContactPairs, PhysicsWorld& world) :mContactPairs(contactPairs), mContactManifolds(manifolds), mContactPoints(contactPoints), mLostContactPairs(lostContactPairs), - mContactPairsIndices(world.mMemoryManager.getHeapAllocator()), mLostContactPairsIndices(world.mMemoryManager.getHeapAllocator()), mWorld(world) { + mContactPairsIndices(world.mMemoryManager.getHeapAllocator(), contactPairs->size()), mLostContactPairsIndices(world.mMemoryManager.getHeapAllocator(), lostContactPairs.size()), + mWorld(world) { // Filter the contact pairs to only keep the contact events (not the overlap/trigger events) for (uint32 i=0; i < mContactPairs->size(); i++) { diff --git a/src/collision/narrowphase/SphereVsConvexPolyhedronAlgorithm.cpp b/src/collision/narrowphase/SphereVsConvexPolyhedronAlgorithm.cpp index 7126b35f..f0c199d5 100644 --- a/src/collision/narrowphase/SphereVsConvexPolyhedronAlgorithm.cpp +++ b/src/collision/narrowphase/SphereVsConvexPolyhedronAlgorithm.cpp @@ -50,7 +50,7 @@ bool SphereVsConvexPolyhedronAlgorithm::testCollision(NarrowPhaseInfoBatch& narr #endif - List gjkResults(memoryAllocator); + List gjkResults(memoryAllocator, batchNbItems); gjkAlgorithm.testCollision(narrowPhaseInfoBatch, batchStartIndex, batchNbItems, gjkResults); assert(gjkResults.size() == batchNbItems); diff --git a/src/engine/PhysicsWorld.cpp b/src/engine/PhysicsWorld.cpp index 5b67261a..29a66590 100644 --- a/src/engine/PhysicsWorld.cpp +++ b/src/engine/PhysicsWorld.cpp @@ -771,7 +771,7 @@ void PhysicsWorld::createIslands() { Stack bodyEntitiesToVisit(mMemoryManager.getSingleFrameAllocator(), mIslands.getNbMaxBodiesInIslandPreviousFrame()); // List of static bodies added to the current island (used to reset the isAlreadyInIsland variable of static bodies) - List staticBodiesAddedToIsland(mMemoryManager.getSingleFrameAllocator()); + List staticBodiesAddedToIsland(mMemoryManager.getSingleFrameAllocator(), 16); uint nbTotalManifolds = 0; diff --git a/src/systems/CollisionDetectionSystem.cpp b/src/systems/CollisionDetectionSystem.cpp index 0529ddaf..acc61f30 100644 --- a/src/systems/CollisionDetectionSystem.cpp +++ b/src/systems/CollisionDetectionSystem.cpp @@ -57,6 +57,7 @@ CollisionDetectionSystem::CollisionDetectionSystem(PhysicsWorld* world, Collider mNoCollisionPairs(mMemoryManager.getPoolAllocator()), mOverlappingPairs(mMemoryManager, mCollidersComponents, collisionBodyComponents, rigidBodyComponents, mNoCollisionPairs, mCollisionDispatch), + mBroadPhaseOverlappingNodes(mMemoryManager.getHeapAllocator(), 32), mBroadPhaseSystem(*this, mCollidersComponents, transformComponents, rigidBodyComponents), mMapBroadPhaseIdToColliderEntity(memoryManager.getPoolAllocator()), mNarrowPhaseInput(mMemoryManager.getSingleFrameAllocator(), mOverlappingPairs), mPotentialContactPoints(mMemoryManager.getSingleFrameAllocator()), @@ -99,17 +100,20 @@ void CollisionDetectionSystem::computeBroadPhase() { RP3D_PROFILE("CollisionDetectionSystem::computeBroadPhase()", mProfiler); + assert(mBroadPhaseOverlappingNodes.size() == 0); + // Ask the broad-phase to compute all the shapes overlapping with the shapes that // have moved or have been added in the last frame. This call can only add new // overlapping pairs in the collision detection. - List> overlappingNodes(mMemoryManager.getHeapAllocator(), 32); - mBroadPhaseSystem.computeOverlappingPairs(mMemoryManager, overlappingNodes); + mBroadPhaseSystem.computeOverlappingPairs(mMemoryManager, mBroadPhaseOverlappingNodes); // Create new overlapping pairs if necessary - updateOverlappingPairs(overlappingNodes); + updateOverlappingPairs(mBroadPhaseOverlappingNodes); // Remove non overlapping pairs removeNonOverlappingPairs(); + + mBroadPhaseOverlappingNodes.clear(); } // Remove pairs that are not overlapping anymore