diff --git a/src/body/CollisionBody.cpp b/src/body/CollisionBody.cpp index 89998a9b..33a70b27 100644 --- a/src/body/CollisionBody.cpp +++ b/src/body/CollisionBody.cpp @@ -85,7 +85,7 @@ ProxyShape* CollisionBody::addCollisionShape(CollisionShape* collisionShape, // TODO : Maybe this method can directly returns an AABB collisionShape->getLocalBounds(localBoundsMin, localBoundsMax); - ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape, proxyShape->getBroadPhaseId(), + ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape, -1, AABB(localBoundsMin, localBoundsMax), transform, collisionShape, decimal(1)); mWorld.mProxyShapesComponents.addComponent(mEntity, mIsSleeping, proxyShapeComponent); diff --git a/src/body/RigidBody.cpp b/src/body/RigidBody.cpp index 593ce715..7b175c38 100644 --- a/src/body/RigidBody.cpp +++ b/src/body/RigidBody.cpp @@ -291,7 +291,7 @@ ProxyShape* RigidBody::addCollisionShape(CollisionShape* collisionShape, // TODO : Maybe this method can directly returns an AABB collisionShape->getLocalBounds(localBoundsMin, localBoundsMax); - ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape, proxyShape->getBroadPhaseId(), + ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape, -1, AABB(localBoundsMin, localBoundsMax), transform, collisionShape, mass); mWorld.mProxyShapesComponents.addComponent(mEntity, mIsSleeping, proxyShapeComponent); diff --git a/src/collision/CollisionDetection.cpp b/src/collision/CollisionDetection.cpp index b1e80bda..0c62e7b3 100644 --- a/src/collision/CollisionDetection.cpp +++ b/src/collision/CollisionDetection.cpp @@ -48,9 +48,9 @@ using namespace std; // Constructor -CollisionDetection::CollisionDetection(CollisionWorld* world, MemoryManager& memoryManager) - : mMemoryManager(memoryManager), mCollisionDispatch(mMemoryManager.getPoolAllocator()), mWorld(world), - mOverlappingPairs(mMemoryManager.getPoolAllocator()), mBroadPhaseAlgorithm(*this), +CollisionDetection::CollisionDetection(CollisionWorld* world, ProxyShapesComponents& proxyShapesComponents, MemoryManager& memoryManager) + : mMemoryManager(memoryManager), mProxyShapesComponents(proxyShapesComponents), mCollisionDispatch(mMemoryManager.getPoolAllocator()), mWorld(world), + mOverlappingPairs(mMemoryManager.getPoolAllocator()), mBroadPhaseAlgorithm(*this, mProxyShapesComponents), mNoCollisionPairs(mMemoryManager.getPoolAllocator()), mIsCollisionShapesAdded(false), mNarrowPhaseInput(mMemoryManager.getSingleFrameAllocator()) { diff --git a/src/collision/CollisionDetection.h b/src/collision/CollisionDetection.h index b10c83e3..bf499c9d 100644 --- a/src/collision/CollisionDetection.h +++ b/src/collision/CollisionDetection.h @@ -35,6 +35,7 @@ #include "collision/narrowphase/CollisionDispatch.h" #include "containers/Map.h" #include "containers/Set.h" +#include "components/ProxyShapesComponents.h" /// ReactPhysics3D namespace namespace reactphysics3d { @@ -67,6 +68,9 @@ class CollisionDetection { /// Memory manager MemoryManager& mMemoryManager; + /// Reference the the proxy-shapes components + ProxyShapesComponents& mProxyShapesComponents; + /// Collision Detection Dispatch configuration CollisionDispatch mCollisionDispatch; @@ -145,7 +149,7 @@ class CollisionDetection { // -------------------- Methods -------------------- // /// Constructor - CollisionDetection(CollisionWorld* world, MemoryManager& memoryManager); + CollisionDetection(CollisionWorld* world, ProxyShapesComponents& proxyShapesComponents, MemoryManager& memoryManager); /// Destructor ~CollisionDetection() = default; diff --git a/src/collision/ProxyShape.cpp b/src/collision/ProxyShape.cpp index 5acb2e71..2d7fe5da 100644 --- a/src/collision/ProxyShape.cpp +++ b/src/collision/ProxyShape.cpp @@ -41,7 +41,7 @@ using namespace reactphysics3d; */ ProxyShape::ProxyShape(CollisionBody* body, MemoryManager& memoryManager) :mMemoryManager(memoryManager), mBody(body), - mNext(nullptr), mBroadPhaseID(-1), mUserData(nullptr), mCollisionCategoryBits(0x0001), mCollideWithMaskBits(0xFFFF) { + mNext(nullptr), mUserData(nullptr), mCollisionCategoryBits(0x0001), mCollideWithMaskBits(0xFFFF) { } @@ -79,8 +79,10 @@ bool ProxyShape::testPointInside(const Vector3& worldPoint) { void ProxyShape::setCollisionCategoryBits(unsigned short collisionCategoryBits) { mCollisionCategoryBits = collisionCategoryBits; + int broadPhaseId = mBody->mWorld.mProxyShapesComponents.getBroadPhaseId(this); + RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::ProxyShape, - "ProxyShape " + std::to_string(mBroadPhaseID) + ": Set collisionCategoryBits=" + + "ProxyShape " + std::to_string(broadPhaseId) + ": Set collisionCategoryBits=" + std::to_string(mCollisionCategoryBits)); } @@ -91,8 +93,10 @@ void ProxyShape::setCollisionCategoryBits(unsigned short collisionCategoryBits) void ProxyShape::setCollideWithMaskBits(unsigned short collideWithMaskBits) { mCollideWithMaskBits = collideWithMaskBits; + int broadPhaseId = mBody->mWorld.mProxyShapesComponents.getBroadPhaseId(this); + RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::ProxyShape, - "ProxyShape " + std::to_string(mBroadPhaseID) + ": Set collideWithMaskBits=" + + "ProxyShape " + std::to_string(broadPhaseId) + ": Set collideWithMaskBits=" + std::to_string(mCollideWithMaskBits)); } @@ -107,8 +111,10 @@ void ProxyShape::setLocalToBodyTransform(const Transform& transform) { // Notify the body that the proxy shape has to be updated in the broad-phase mBody->updateProxyShapeInBroadPhase(this, true); + int broadPhaseId = mBody->mWorld.mProxyShapesComponents.getBroadPhaseId(this); + RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::ProxyShape, - "ProxyShape " + std::to_string(mBroadPhaseID) + ": Set localToBodyTransform=" + + "ProxyShape " + std::to_string(broadPhaseId) + ": Set localToBodyTransform=" + transform.to_string()); } @@ -139,6 +145,11 @@ CollisionShape* ProxyShape::getCollisionShape() { return mBody->mWorld.mProxyShapesComponents.getCollisionShape(this); } +// Return the broad-phase id +int ProxyShape::getBroadPhaseId() const { + return mBody->mWorld.mProxyShapesComponents.getBroadPhaseId(this); +} + // Return the local to parent body transform /** * @return The transformation that transforms the local-space of the collision shape diff --git a/src/collision/ProxyShape.h b/src/collision/ProxyShape.h index deba0332..bd2bb8bf 100644 --- a/src/collision/ProxyShape.h +++ b/src/collision/ProxyShape.h @@ -70,7 +70,7 @@ class ProxyShape { ProxyShape* mNext; /// Broad-phase ID (node ID in the dynamic AABB tree) - int mBroadPhaseID; + //int mBroadPhaseID; /// Pointer to user data void* mUserData; @@ -265,11 +265,6 @@ inline unsigned short ProxyShape::getCollideWithMaskBits() const { return mCollideWithMaskBits; } -// Return the broad-phase id -inline int ProxyShape::getBroadPhaseId() const { - return mBroadPhaseID; -} - /// Test if the proxy shape overlaps with a given AABB /** * @param worldAABB The AABB (in world-space coordinates) that will be used to test overlap diff --git a/src/collision/broadphase/BroadPhaseAlgorithm.cpp b/src/collision/broadphase/BroadPhaseAlgorithm.cpp index fe577490..43b9a5d2 100644 --- a/src/collision/broadphase/BroadPhaseAlgorithm.cpp +++ b/src/collision/broadphase/BroadPhaseAlgorithm.cpp @@ -29,13 +29,15 @@ #include "utils/Profiler.h" #include "collision/RaycastInfo.h" #include "memory/MemoryManager.h" +#include "engine/CollisionWorld.h" // We want to use the ReactPhysics3D namespace using namespace reactphysics3d; // Constructor -BroadPhaseAlgorithm::BroadPhaseAlgorithm(CollisionDetection& collisionDetection) +BroadPhaseAlgorithm::BroadPhaseAlgorithm(CollisionDetection& collisionDetection, ProxyShapesComponents& proxyShapesComponents) :mDynamicAABBTree(collisionDetection.getMemoryManager().getPoolAllocator(), DYNAMIC_TREE_AABB_GAP), + mProxyShapesComponents(proxyShapesComponents), mMovedShapes(collisionDetection.getMemoryManager().getPoolAllocator()), mPotentialPairs(collisionDetection.getMemoryManager().getPoolAllocator()), mCollisionDetection(collisionDetection) { @@ -82,7 +84,7 @@ void BroadPhaseAlgorithm::addProxyCollisionShape(ProxyShape* proxyShape, const A int nodeId = mDynamicAABBTree.addObject(aabb, proxyShape); // Set the broad-phase ID of the proxy shape - proxyShape->mBroadPhaseID = nodeId; + mProxyShapesComponents.setBroadPhaseId(proxyShape, nodeId); // Add the collision shape into the array of bodies that have moved (or have been created) // during the last simulation step @@ -96,7 +98,7 @@ void BroadPhaseAlgorithm::removeProxyCollisionShape(ProxyShape* proxyShape) { int broadPhaseID = proxyShape->getBroadPhaseId(); - proxyShape->mBroadPhaseID = -1; + mProxyShapesComponents.setBroadPhaseId(proxyShape, -1); // Remove the collision shape from the dynamic AABB tree mDynamicAABBTree.removeObject(broadPhaseID); diff --git a/src/collision/broadphase/BroadPhaseAlgorithm.h b/src/collision/broadphase/BroadPhaseAlgorithm.h index 037edb4c..df3e2b7c 100644 --- a/src/collision/broadphase/BroadPhaseAlgorithm.h +++ b/src/collision/broadphase/BroadPhaseAlgorithm.h @@ -30,6 +30,7 @@ #include "DynamicAABBTree.h" #include "containers/LinkedList.h" #include "containers/Set.h" +#include "components/ProxyShapesComponents.h" /// Namespace ReactPhysics3D namespace reactphysics3d { @@ -140,6 +141,9 @@ class BroadPhaseAlgorithm { /// Dynamic AABB tree DynamicAABBTree mDynamicAABBTree; + /// Reference to the proxy-shapes components + ProxyShapesComponents& mProxyShapesComponents; + /// Set with the broad-phase IDs of all collision shapes that have moved (or have been /// created) during the last simulation step. Those are the shapes that need to be tested /// for overlapping in the next simulation step. @@ -163,7 +167,7 @@ class BroadPhaseAlgorithm { // -------------------- Methods -------------------- // /// Constructor - BroadPhaseAlgorithm(CollisionDetection& collisionDetection); + BroadPhaseAlgorithm(CollisionDetection& collisionDetection, ProxyShapesComponents& proxyShapesComponents); /// Destructor ~BroadPhaseAlgorithm() = default; diff --git a/src/components/ProxyShapesComponents.h b/src/components/ProxyShapesComponents.h index 1e081fb0..92d7b795 100644 --- a/src/components/ProxyShapesComponents.h +++ b/src/components/ProxyShapesComponents.h @@ -194,6 +194,12 @@ class ProxyShapesComponents { /// Return a pointer to the collision shape of a proxy-shape CollisionShape* getCollisionShape(const ProxyShape* proxyShape) const; + + /// Return the broad-phase id of a given proxy shape + int getBroadPhaseId(const ProxyShape* proxyShape) const; + + /// Set the broad-phase id of a given proxy shape + void setBroadPhaseId(const ProxyShape* proxyShape, int broadPhaseId); }; // Return the mass of a proxy-shape @@ -227,6 +233,23 @@ inline CollisionShape* ProxyShapesComponents::getCollisionShape(const ProxyShape return mCollisionShapes[mMapProxyShapeToComponentIndex[proxyShape]]; } + +// Return the broad-phase id of a given proxy shape +inline int ProxyShapesComponents::getBroadPhaseId(const ProxyShape* proxyShape) const { + + assert(mMapProxyShapeToComponentIndex.containsKey(proxyShape)); + + return mBroadPhaseIds[mMapProxyShapeToComponentIndex[proxyShape]]; +} + +// Set the broad-phase id of a given proxy shape +inline void ProxyShapesComponents::setBroadPhaseId(const ProxyShape* proxyShape, int broadPhaseId) { + + assert(mMapProxyShapeToComponentIndex.containsKey(proxyShape)); + + mBroadPhaseIds[mMapProxyShapeToComponentIndex[proxyShape]] = broadPhaseId; +} + } #endif diff --git a/src/engine/CollisionWorld.cpp b/src/engine/CollisionWorld.cpp index 8c5967ba..ef711c1c 100644 --- a/src/engine/CollisionWorld.cpp +++ b/src/engine/CollisionWorld.cpp @@ -39,7 +39,7 @@ uint CollisionWorld::mNbWorlds = 0; CollisionWorld::CollisionWorld(const WorldSettings& worldSettings, Logger* logger, Profiler* profiler) : mConfig(worldSettings), mEntityManager(mMemoryManager.getPoolAllocator()), mTransformComponents(mMemoryManager.getBaseAllocator()), mProxyShapesComponents(mMemoryManager.getBaseAllocator()), - mCollisionDetection(this, mMemoryManager), mBodies(mMemoryManager.getPoolAllocator()), mCurrentBodyId(0), + mCollisionDetection(this, mProxyShapesComponents, mMemoryManager), mBodies(mMemoryManager.getPoolAllocator()), mCurrentBodyId(0), mFreeBodiesIds(mMemoryManager.getPoolAllocator()), mEventListener(nullptr), mName(worldSettings.worldName), mIsProfilerCreatedByUser(profiler != nullptr), mIsLoggerCreatedByUser(logger != nullptr) {