From 4b919fb4fc139309fcb813bdc4818e91d88ca6cb Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Mon, 4 Mar 2019 17:29:27 +0100 Subject: [PATCH] Make BroadPhaseAlgorithm a system --- CMakeLists.txt | 5 ++-- src/collision/CollisionDetection.cpp | 30 +++++++++---------- src/collision/CollisionDetection.h | 14 ++++----- src/collision/broadphase/DynamicAABBTree.cpp | 2 +- src/collision/broadphase/DynamicAABBTree.h | 2 +- .../BroadPhaseSystem.cpp} | 20 ++++++------- .../BroadPhaseSystem.h} | 24 +++++++-------- 7 files changed, 48 insertions(+), 49 deletions(-) rename src/{collision/broadphase/BroadPhaseAlgorithm.cpp => systems/BroadPhaseSystem.cpp} (92%) rename src/{collision/broadphase/BroadPhaseAlgorithm.h => systems/BroadPhaseSystem.h} (92%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f23c134a..7933c812 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,6 @@ SET (REACTPHYSICS3D_HEADERS "src/body/CollisionBody.h" "src/body/RigidBody.h" "src/collision/ContactPointInfo.h" - "src/collision/broadphase/BroadPhaseAlgorithm.h" "src/collision/broadphase/DynamicAABBTree.h" "src/collision/narrowphase/CollisionDispatch.h" "src/collision/narrowphase/GJK/VoronoiSimplex.h" @@ -139,7 +138,7 @@ SET (REACTPHYSICS3D_HEADERS "src/engine/Material.h" "src/engine/OverlappingPair.h" "src/engine/Timer.h" - "src/engine/Timer.cpp" + "src/systems/BroadPhaseSystem.h" "src/components/Components.h" "src/components/TransformComponents.h" "src/components/ProxyShapesComponents.h" @@ -175,7 +174,6 @@ SET (REACTPHYSICS3D_SOURCES "src/body/Body.cpp" "src/body/CollisionBody.cpp" "src/body/RigidBody.cpp" - "src/collision/broadphase/BroadPhaseAlgorithm.cpp" "src/collision/broadphase/DynamicAABBTree.cpp" "src/collision/narrowphase/CollisionDispatch.cpp" "src/collision/narrowphase/GJK/VoronoiSimplex.cpp" @@ -231,6 +229,7 @@ SET (REACTPHYSICS3D_SOURCES "src/engine/Timer.cpp" "src/engine/Entity.cpp" "src/engine/EntityManager.cpp" + "src/systems/BroadPhaseSystem.cpp" "src/components/TransformComponents.cpp" "src/components/ProxyShapesComponents.cpp" "src/collision/CollisionCallback.cpp" diff --git a/src/collision/CollisionDetection.cpp b/src/collision/CollisionDetection.cpp index 0c62e7b3..698883fd 100644 --- a/src/collision/CollisionDetection.cpp +++ b/src/collision/CollisionDetection.cpp @@ -50,7 +50,7 @@ using namespace std; // Constructor CollisionDetection::CollisionDetection(CollisionWorld* world, ProxyShapesComponents& proxyShapesComponents, MemoryManager& memoryManager) : mMemoryManager(memoryManager), mProxyShapesComponents(proxyShapesComponents), mCollisionDispatch(mMemoryManager.getPoolAllocator()), mWorld(world), - mOverlappingPairs(mMemoryManager.getPoolAllocator()), mBroadPhaseAlgorithm(*this, mProxyShapesComponents), + mOverlappingPairs(mMemoryManager.getPoolAllocator()), mBroadPhaseSystem(*this, mProxyShapesComponents), mNoCollisionPairs(mMemoryManager.getPoolAllocator()), mIsCollisionShapesAdded(false), mNarrowPhaseInput(mMemoryManager.getSingleFrameAllocator()) { @@ -89,7 +89,7 @@ void CollisionDetection::computeBroadPhase() { // Ask the broad-phase to recompute the overlapping pairs of collision // shapes. This call can only add new overlapping pairs in the collision // detection. - mBroadPhaseAlgorithm.computeOverlappingPairs(mMemoryManager); + mBroadPhaseSystem.computeOverlappingPairs(mMemoryManager); } } @@ -121,7 +121,7 @@ void CollisionDetection::computeMiddlePhase() { // Check if the two shapes are still overlapping. Otherwise, we destroy the // overlapping pair - if (!mBroadPhaseAlgorithm.testOverlappingShapes(shape1, shape2)) { + if (!mBroadPhaseSystem.testOverlappingShapes(shape1, shape2)) { // Destroy the overlapping pair pair->~OverlappingPair(); @@ -393,7 +393,7 @@ void CollisionDetection::removeProxyCollisionShape(ProxyShape* proxyShape) { } // Remove the body from the broad-phase - mBroadPhaseAlgorithm.removeProxyCollisionShape(proxyShape); + mBroadPhaseSystem.removeProxyCollisionShape(proxyShape); } void CollisionDetection::addAllContactManifoldsToBodies() { @@ -420,7 +420,7 @@ void CollisionDetection::raycast(RaycastCallback* raycastCallback, // Ask the broad-phase algorithm to call the testRaycastAgainstShape() // callback method for each proxy shape hit by the ray in the broad-phase - mBroadPhaseAlgorithm.raycast(ray, rayCastTest, raycastWithCategoryMaskBits); + mBroadPhaseSystem.raycast(ray, rayCastTest, raycastWithCategoryMaskBits); } // Add a contact manifold to the linked list of contact manifolds of the two bodies involved @@ -571,7 +571,7 @@ void CollisionDetection::testAABBOverlap(const AABB& aabb, OverlapCallback* over // Ask the broad-phase to get all the overlapping shapes LinkedList overlappingNodes(mMemoryManager.getPoolAllocator()); - mBroadPhaseAlgorithm.reportAllShapesOverlappingWithAABB(aabb, overlappingNodes); + mBroadPhaseSystem.reportAllShapesOverlappingWithAABB(aabb, overlappingNodes); // For each overlaping proxy shape LinkedList::ListElement* element = overlappingNodes.getListHead(); @@ -579,7 +579,7 @@ void CollisionDetection::testAABBOverlap(const AABB& aabb, OverlapCallback* over // Get the overlapping proxy shape int broadPhaseId = element->data; - ProxyShape* proxyShape = mBroadPhaseAlgorithm.getProxyShapeForBroadPhaseId(broadPhaseId); + ProxyShape* proxyShape = mBroadPhaseSystem.getProxyShapeForBroadPhaseId(broadPhaseId); CollisionBody* overlapBody = proxyShape->getBody(); @@ -662,11 +662,11 @@ void CollisionDetection::testOverlap(CollisionBody* body, OverlapCallback* overl if (bodyProxyShape->getBroadPhaseId() != -1) { // Get the AABB of the shape - const AABB& shapeAABB = mBroadPhaseAlgorithm.getFatAABB(bodyProxyShape->getBroadPhaseId()); + const AABB& shapeAABB = mBroadPhaseSystem.getFatAABB(bodyProxyShape->getBroadPhaseId()); // Ask the broad-phase to get all the overlapping shapes LinkedList overlappingNodes(mMemoryManager.getPoolAllocator()); - mBroadPhaseAlgorithm.reportAllShapesOverlappingWithAABB(shapeAABB, overlappingNodes); + mBroadPhaseSystem.reportAllShapesOverlappingWithAABB(shapeAABB, overlappingNodes); const bodyindex bodyId = body->getId(); @@ -676,7 +676,7 @@ void CollisionDetection::testOverlap(CollisionBody* body, OverlapCallback* overl // Get the overlapping proxy shape int broadPhaseId = element->data; - ProxyShape* proxyShape = mBroadPhaseAlgorithm.getProxyShapeForBroadPhaseId(broadPhaseId); + ProxyShape* proxyShape = mBroadPhaseSystem.getProxyShapeForBroadPhaseId(broadPhaseId); // If the proxy shape is from a body that we have not already reported collision and the // two proxy collision shapes are not from the same body @@ -818,11 +818,11 @@ void CollisionDetection::testCollision(CollisionBody* body, CollisionCallback* c if (bodyProxyShape->getBroadPhaseId() != -1) { // Get the AABB of the shape - const AABB& shapeAABB = mBroadPhaseAlgorithm.getFatAABB(bodyProxyShape->getBroadPhaseId()); + const AABB& shapeAABB = mBroadPhaseSystem.getFatAABB(bodyProxyShape->getBroadPhaseId()); // Ask the broad-phase to get all the overlapping shapes LinkedList overlappingNodes(mMemoryManager.getPoolAllocator()); - mBroadPhaseAlgorithm.reportAllShapesOverlappingWithAABB(shapeAABB, overlappingNodes); + mBroadPhaseSystem.reportAllShapesOverlappingWithAABB(shapeAABB, overlappingNodes); const bodyindex bodyId = body->getId(); @@ -832,7 +832,7 @@ void CollisionDetection::testCollision(CollisionBody* body, CollisionCallback* c // Get the overlapping proxy shape int broadPhaseId = element->data; - ProxyShape* proxyShape = mBroadPhaseAlgorithm.getProxyShapeForBroadPhaseId(broadPhaseId); + ProxyShape* proxyShape = mBroadPhaseSystem.getProxyShapeForBroadPhaseId(broadPhaseId); // If the two proxy collision shapes are not from the same body if (proxyShape->getBody()->getId() != bodyId) { @@ -948,7 +948,7 @@ void CollisionDetection::testCollision(CollisionCallback* callback) { // that the two shapes are still overlapping. if (((shape1->getCollideWithMaskBits() & shape2->getCollisionCategoryBits()) != 0 && (shape1->getCollisionCategoryBits() & shape2->getCollideWithMaskBits()) != 0) && - mBroadPhaseAlgorithm.testOverlappingShapes(shape1, shape2)) { + mBroadPhaseSystem.testOverlappingShapes(shape1, shape2)) { // Compute the middle-phase collision detection between the two shapes computeMiddlePhaseForProxyShapes(pair, narrowPhaseInput); @@ -990,5 +990,5 @@ EventListener* CollisionDetection::getWorldEventListener() { // Return the world-space AABB of a given proxy shape const AABB CollisionDetection::getWorldAABB(const ProxyShape* proxyShape) const { assert(proxyShape->getBroadPhaseId() > -1); - return mBroadPhaseAlgorithm.getFatAABB(proxyShape->getBroadPhaseId()); + return mBroadPhaseSystem.getFatAABB(proxyShape->getBroadPhaseId()); } diff --git a/src/collision/CollisionDetection.h b/src/collision/CollisionDetection.h index bf499c9d..976932ef 100644 --- a/src/collision/CollisionDetection.h +++ b/src/collision/CollisionDetection.h @@ -28,7 +28,7 @@ // Libraries #include "body/CollisionBody.h" -#include "broadphase/BroadPhaseAlgorithm.h" +#include "systems/BroadPhaseSystem.h" #include "collision/shapes/CollisionShape.h" #include "engine/OverlappingPair.h" #include "collision/narrowphase/NarrowPhaseInput.h" @@ -80,8 +80,8 @@ class CollisionDetection { /// Broad-phase overlapping pairs OverlappingPairMap mOverlappingPairs; - /// Broad-phase algorithm - BroadPhaseAlgorithm mBroadPhaseAlgorithm; + /// Broad-phase system + BroadPhaseSystem mBroadPhaseSystem; /// Set of pair of bodies that cannot collide between each other Set mNoCollisionPairs; @@ -245,7 +245,7 @@ inline void CollisionDetection::addProxyCollisionShape(ProxyShape* proxyShape, const AABB& aabb) { // Add the body to the broad-phase - mBroadPhaseAlgorithm.addProxyCollisionShape(proxyShape, aabb); + mBroadPhaseSystem.addProxyCollisionShape(proxyShape, aabb); mIsCollisionShapesAdded = true; } @@ -268,14 +268,14 @@ inline void CollisionDetection::removeNoCollisionPair(CollisionBody* body1, inline void CollisionDetection::askForBroadPhaseCollisionCheck(ProxyShape* shape) { if (shape->getBroadPhaseId() != -1) { - mBroadPhaseAlgorithm.addMovedCollisionShape(shape->getBroadPhaseId()); + mBroadPhaseSystem.addMovedCollisionShape(shape->getBroadPhaseId()); } } // Update a proxy collision shape (that has moved for instance) inline void CollisionDetection::updateProxyCollisionShape(ProxyShape* shape, const AABB& aabb, const Vector3& displacement, bool forceReinsert) { - mBroadPhaseAlgorithm.updateProxyCollisionShape(shape, aabb, displacement); + mBroadPhaseSystem.updateProxyCollisionShape(shape, aabb, displacement); } @@ -294,7 +294,7 @@ inline MemoryManager& CollisionDetection::getMemoryManager() const { // Set the profiler inline void CollisionDetection::setProfiler(Profiler* profiler) { mProfiler = profiler; - mBroadPhaseAlgorithm.setProfiler(profiler); + mBroadPhaseSystem.setProfiler(profiler); mCollisionDispatch.setProfiler(profiler); } diff --git a/src/collision/broadphase/DynamicAABBTree.cpp b/src/collision/broadphase/DynamicAABBTree.cpp index 61f347d2..58639fc9 100644 --- a/src/collision/broadphase/DynamicAABBTree.cpp +++ b/src/collision/broadphase/DynamicAABBTree.cpp @@ -25,7 +25,7 @@ // Libraries #include "DynamicAABBTree.h" -#include "BroadPhaseAlgorithm.h" +#include "systems/BroadPhaseSystem.h" #include "containers/Stack.h" #include "utils/Profiler.h" diff --git a/src/collision/broadphase/DynamicAABBTree.h b/src/collision/broadphase/DynamicAABBTree.h index 7619bca8..1939594f 100644 --- a/src/collision/broadphase/DynamicAABBTree.h +++ b/src/collision/broadphase/DynamicAABBTree.h @@ -34,7 +34,7 @@ namespace reactphysics3d { // Declarations -class BroadPhaseAlgorithm; +class BroadPhaseSystem; class BroadPhaseRaycastTestCallback; class DynamicAABBTreeOverlapCallback; class CollisionBody; diff --git a/src/collision/broadphase/BroadPhaseAlgorithm.cpp b/src/systems/BroadPhaseSystem.cpp similarity index 92% rename from src/collision/broadphase/BroadPhaseAlgorithm.cpp rename to src/systems/BroadPhaseSystem.cpp index ebd6912b..0a75b5dd 100644 --- a/src/collision/broadphase/BroadPhaseAlgorithm.cpp +++ b/src/systems/BroadPhaseSystem.cpp @@ -24,7 +24,7 @@ ********************************************************************************/ // Libraries -#include "BroadPhaseAlgorithm.h" +#include "BroadPhaseSystem.h" #include "collision/CollisionDetection.h" #include "utils/Profiler.h" #include "collision/RaycastInfo.h" @@ -35,7 +35,7 @@ using namespace reactphysics3d; // Constructor -BroadPhaseAlgorithm::BroadPhaseAlgorithm(CollisionDetection& collisionDetection, ProxyShapesComponents& proxyShapesComponents) +BroadPhaseSystem::BroadPhaseSystem(CollisionDetection& collisionDetection, ProxyShapesComponents& proxyShapesComponents) :mDynamicAABBTree(collisionDetection.getMemoryManager().getPoolAllocator(), DYNAMIC_TREE_AABB_GAP), mProxyShapesComponents(proxyShapesComponents), mMovedShapes(collisionDetection.getMemoryManager().getPoolAllocator()), @@ -51,7 +51,7 @@ BroadPhaseAlgorithm::BroadPhaseAlgorithm(CollisionDetection& collisionDetection, } // Return true if the two broad-phase collision shapes are overlapping -bool BroadPhaseAlgorithm::testOverlappingShapes(const ProxyShape* shape1, +bool BroadPhaseSystem::testOverlappingShapes(const ProxyShape* shape1, const ProxyShape* shape2) const { if (shape1->getBroadPhaseId() == -1 || shape2->getBroadPhaseId() == -1) return false; @@ -65,7 +65,7 @@ bool BroadPhaseAlgorithm::testOverlappingShapes(const ProxyShape* shape1, } // Ray casting method -void BroadPhaseAlgorithm::raycast(const Ray& ray, RaycastTest& raycastTest, +void BroadPhaseSystem::raycast(const Ray& ray, RaycastTest& raycastTest, unsigned short raycastWithCategoryMaskBits) const { RP3D_PROFILE("BroadPhaseAlgorithm::raycast()", mProfiler); @@ -76,7 +76,7 @@ void BroadPhaseAlgorithm::raycast(const Ray& ray, RaycastTest& raycastTest, } // Add a proxy collision shape into the broad-phase collision detection -void BroadPhaseAlgorithm::addProxyCollisionShape(ProxyShape* proxyShape, const AABB& aabb) { +void BroadPhaseSystem::addProxyCollisionShape(ProxyShape* proxyShape, const AABB& aabb) { assert(proxyShape->getBroadPhaseId() == -1); @@ -92,7 +92,7 @@ void BroadPhaseAlgorithm::addProxyCollisionShape(ProxyShape* proxyShape, const A } // Remove a proxy collision shape from the broad-phase collision detection -void BroadPhaseAlgorithm::removeProxyCollisionShape(ProxyShape* proxyShape) { +void BroadPhaseSystem::removeProxyCollisionShape(ProxyShape* proxyShape) { assert(proxyShape->getBroadPhaseId() != -1); @@ -109,7 +109,7 @@ void BroadPhaseAlgorithm::removeProxyCollisionShape(ProxyShape* proxyShape) { } // Notify the broad-phase that a collision shape has moved and need to be updated -void BroadPhaseAlgorithm::updateProxyCollisionShape(ProxyShape* proxyShape, const AABB& aabb, +void BroadPhaseSystem::updateProxyCollisionShape(ProxyShape* proxyShape, const AABB& aabb, const Vector3& displacement, bool forceReinsert) { int broadPhaseID = proxyShape->getBroadPhaseId(); @@ -129,7 +129,7 @@ void BroadPhaseAlgorithm::updateProxyCollisionShape(ProxyShape* proxyShape, cons } } -void BroadPhaseAlgorithm::reportAllShapesOverlappingWithAABB(const AABB& aabb, +void BroadPhaseSystem::reportAllShapesOverlappingWithAABB(const AABB& aabb, LinkedList& overlappingNodes) const { AABBOverlapCallback callback(overlappingNodes); @@ -139,7 +139,7 @@ void BroadPhaseAlgorithm::reportAllShapesOverlappingWithAABB(const AABB& aabb, } // Compute all the overlapping pairs of collision shapes -void BroadPhaseAlgorithm::computeOverlappingPairs(MemoryManager& memoryManager) { +void BroadPhaseSystem::computeOverlappingPairs(MemoryManager& memoryManager) { // TODO : Try to see if we can allocate potential pairs in single frame allocator @@ -217,7 +217,7 @@ void BroadPhaseAlgorithm::computeOverlappingPairs(MemoryManager& memoryManager) } // Notify the broad-phase about a potential overlapping pair in the dynamic AABB tree -void BroadPhaseAlgorithm::addOverlappingNodes(int referenceNodeId, const LinkedList& overlappingNodes) { +void BroadPhaseSystem::addOverlappingNodes(int referenceNodeId, const LinkedList& overlappingNodes) { // For each overlapping node in the linked list LinkedList::ListElement* elem = overlappingNodes.getListHead(); diff --git a/src/collision/broadphase/BroadPhaseAlgorithm.h b/src/systems/BroadPhaseSystem.h similarity index 92% rename from src/collision/broadphase/BroadPhaseAlgorithm.h rename to src/systems/BroadPhaseSystem.h index df3e2b7c..4d2bbd5b 100644 --- a/src/collision/broadphase/BroadPhaseAlgorithm.h +++ b/src/systems/BroadPhaseSystem.h @@ -27,7 +27,7 @@ #define REACTPHYSICS3D_BROAD_PHASE_ALGORITHM_H // Libraries -#include "DynamicAABBTree.h" +#include "collision/broadphase/DynamicAABBTree.h" #include "containers/LinkedList.h" #include "containers/Set.h" #include "components/ProxyShapesComponents.h" @@ -37,7 +37,7 @@ namespace reactphysics3d { // Declarations class CollisionDetection; -class BroadPhaseAlgorithm; +class BroadPhaseSystem; class CollisionBody; class ProxyShape; class MemoryManager; @@ -132,7 +132,7 @@ class BroadPhaseRaycastCallback : public DynamicAABBTreeRaycastCallback { * later for collision during the narrow-phase collision detection. A dynamic AABB * tree data structure is used for fast broad-phase collision detection. */ -class BroadPhaseAlgorithm { +class BroadPhaseSystem { protected : @@ -167,16 +167,16 @@ class BroadPhaseAlgorithm { // -------------------- Methods -------------------- // /// Constructor - BroadPhaseAlgorithm(CollisionDetection& collisionDetection, ProxyShapesComponents& proxyShapesComponents); + BroadPhaseSystem(CollisionDetection& collisionDetection, ProxyShapesComponents& proxyShapesComponents); /// Destructor - ~BroadPhaseAlgorithm() = default; + ~BroadPhaseSystem() = default; /// Deleted copy-constructor - BroadPhaseAlgorithm(const BroadPhaseAlgorithm& algorithm) = delete; + BroadPhaseSystem(const BroadPhaseSystem& algorithm) = delete; /// Deleted assignment operator - BroadPhaseAlgorithm& operator=(const BroadPhaseAlgorithm& algorithm) = delete; + BroadPhaseSystem& operator=(const BroadPhaseSystem& algorithm) = delete; /// Add a proxy collision shape into the broad-phase collision detection void addProxyCollisionShape(ProxyShape* proxyShape, const AABB& aabb); @@ -238,13 +238,13 @@ inline bool BroadPhasePair::smallerThan(const BroadPhasePair& pair1, } // Return the fat AABB of a given broad-phase shape -inline const AABB& BroadPhaseAlgorithm::getFatAABB(int broadPhaseId) const { +inline const AABB& BroadPhaseSystem::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) { +inline void BroadPhaseSystem::addMovedCollisionShape(int broadPhaseID) { // Store the broad-phase ID into the array of shapes that have moved mMovedShapes.add(broadPhaseID); @@ -252,21 +252,21 @@ inline void BroadPhaseAlgorithm::addMovedCollisionShape(int 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) { +inline void BroadPhaseSystem::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 { +inline ProxyShape* BroadPhaseSystem::getProxyShapeForBroadPhaseId(int broadPhaseId) const { return static_cast(mDynamicAABBTree.getNodeDataPointer(broadPhaseId)); } #ifdef IS_PROFILING_ACTIVE // Set the profiler -inline void BroadPhaseAlgorithm::setProfiler(Profiler* profiler) { +inline void BroadPhaseSystem::setProfiler(Profiler* profiler) { mProfiler = profiler; mDynamicAABBTree.setProfiler(profiler); }