From 160759bb63bf993e05189a7bf6829472b0bd9549 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Sat, 21 Jul 2018 23:25:34 +0200 Subject: [PATCH] Replace array by Set for the moved shapes in broad-phase and fix a bug with added and removed collision body (crash in BroadPhaseAlgorithm::computeOverlappingPairs()) --- .../broadphase/BroadPhaseAlgorithm.cpp | 86 ++----------------- .../broadphase/BroadPhaseAlgorithm.h | 33 ++++--- 2 files changed, 24 insertions(+), 95 deletions(-) diff --git a/src/collision/broadphase/BroadPhaseAlgorithm.cpp b/src/collision/broadphase/BroadPhaseAlgorithm.cpp index 83253346..43a1122e 100644 --- a/src/collision/broadphase/BroadPhaseAlgorithm.cpp +++ b/src/collision/broadphase/BroadPhaseAlgorithm.cpp @@ -36,16 +36,12 @@ using namespace reactphysics3d; // Constructor BroadPhaseAlgorithm::BroadPhaseAlgorithm(CollisionDetection& collisionDetection) :mDynamicAABBTree(collisionDetection.getMemoryManager().getPoolAllocator(), DYNAMIC_TREE_AABB_GAP), - mNbMovedShapes(0), mNbAllocatedMovedShapes(8), - mNbNonUsedMovedShapes(0), mNbPotentialPairs(0), mNbAllocatedPotentialPairs(8), + mMovedShapes(collisionDetection.getMemoryManager().getPoolAllocator()), + mNbPotentialPairs(0), mNbAllocatedPotentialPairs(8), mCollisionDetection(collisionDetection) { PoolAllocator& poolAllocator = collisionDetection.getMemoryManager().getPoolAllocator(); - // Allocate memory for the array of non-static proxy shapes IDs - mMovedShapes = static_cast(poolAllocator.allocate(mNbAllocatedMovedShapes * sizeof(int))); - assert(mMovedShapes != nullptr); - // Allocate memory for the array of potential overlapping pairs mPotentialPairs = static_cast(poolAllocator.allocate(mNbAllocatedPotentialPairs * sizeof(BroadPhasePair))); assert(mPotentialPairs != nullptr); @@ -64,40 +60,10 @@ BroadPhaseAlgorithm::~BroadPhaseAlgorithm() { // Get the memory pool allocatory PoolAllocator& poolAllocator = mCollisionDetection.getMemoryManager().getPoolAllocator(); - // Release the memory for the array of non-static proxy shapes IDs - poolAllocator.release(mMovedShapes, mNbAllocatedMovedShapes * sizeof (int)); - // Release the memory for the array of potential overlapping pairs poolAllocator.release(mPotentialPairs, mNbAllocatedPotentialPairs * sizeof(BroadPhasePair)); } -// Add a collision shape in the array of shapes that have moved in the last simulation step -// and that need to be tested again for broad-phase overlapping. -void BroadPhaseAlgorithm::addMovedCollisionShape(int broadPhaseID) { - - - // Allocate more elements in the array of shapes that have moved if necessary - if (mNbAllocatedMovedShapes == mNbMovedShapes) { - - // Get the memory pool allocatory - PoolAllocator& poolAllocator = mCollisionDetection.getMemoryManager().getPoolAllocator(); - - uint oldNbAllocatedMovedShapes = mNbAllocatedMovedShapes; - mNbAllocatedMovedShapes *= 2; - int* oldArray = mMovedShapes; - mMovedShapes = static_cast(poolAllocator.allocate(mNbAllocatedMovedShapes * sizeof(int))); - assert(mMovedShapes != nullptr); - std::memcpy(mMovedShapes, oldArray, mNbMovedShapes * sizeof(int)); - poolAllocator.release(oldArray, oldNbAllocatedMovedShapes * sizeof(int)); - } - - // Store the broad-phase ID into the array of shapes that have moved - assert(mNbMovedShapes < mNbAllocatedMovedShapes); - assert(mMovedShapes != nullptr); - mMovedShapes[mNbMovedShapes] = broadPhaseID; - mNbMovedShapes++; -} - // Return true if the two broad-phase collision shapes are overlapping bool BroadPhaseAlgorithm::testOverlappingShapes(const ProxyShape* shape1, const ProxyShape* shape2) const { @@ -123,47 +89,6 @@ void BroadPhaseAlgorithm::raycast(const Ray& ray, RaycastTest& raycastTest, mDynamicAABBTree.raycast(ray, broadPhaseRaycastCallback); } -// Remove a collision shape from the array of shapes that have moved in the last simulation step -// and that need to be tested again for broad-phase overlapping. -void BroadPhaseAlgorithm::removeMovedCollisionShape(int broadPhaseID) { - - assert(mNbNonUsedMovedShapes <= mNbMovedShapes); - - // If less than the quarter of allocated elements of the non-static shapes IDs array - // are used, we release some allocated memory - if ((mNbMovedShapes - mNbNonUsedMovedShapes) < mNbAllocatedMovedShapes / 4 && - mNbAllocatedMovedShapes > 8) { - - // Get the memory pool allocatory - PoolAllocator& poolAllocator = mCollisionDetection.getMemoryManager().getPoolAllocator(); - - uint oldNbAllocatedMovedShapes = mNbAllocatedMovedShapes; - mNbAllocatedMovedShapes /= 2; - int* oldArray = mMovedShapes; - mMovedShapes = static_cast(poolAllocator.allocate(mNbAllocatedMovedShapes * sizeof(int))); - assert(mMovedShapes != nullptr); - uint nbElements = 0; - for (uint i=0; i mMovedShapes; /// Temporary array of potential overlapping pairs (with potential duplicates) BroadPhasePair* mPotentialPairs; @@ -248,6 +237,22 @@ inline const AABB& BroadPhaseAlgorithm::getFatAABB(int broadPhaseId) const { return mDynamicAABBTree.getFatAABB(broadPhaseId); } +// Add a collision shape in the array of shapes that have moved in the last simulation step +// and that need to be tested again for broad-phase overlapping. +inline void BroadPhaseAlgorithm::addMovedCollisionShape(int broadPhaseID) { + + // Store the broad-phase ID into the array of shapes that have moved + mMovedShapes.add(broadPhaseID); +} + +// Remove a collision shape from the array of shapes that have moved in the last simulation step +// and that need to be tested again for broad-phase overlapping. +inline void BroadPhaseAlgorithm::removeMovedCollisionShape(int broadPhaseID) { + + // Remove the broad-phase ID from the set + mMovedShapes.remove(broadPhaseID); +} + // Return the proxy shape corresponding to the broad-phase node id in parameter inline ProxyShape* BroadPhaseAlgorithm::getProxyShapeForBroadPhaseId(int broadPhaseId) const { return static_cast(mDynamicAABBTree.getNodeDataPointer(broadPhaseId));