diff --git a/src/body/CollisionBody.cpp b/src/body/CollisionBody.cpp index 812ae47c..89998a9b 100644 --- a/src/body/CollisionBody.cpp +++ b/src/body/CollisionBody.cpp @@ -77,14 +77,13 @@ ProxyShape* CollisionBody::addCollisionShape(CollisionShape* collisionShape, // Create a new proxy collision shape to attach the collision shape to the body ProxyShape* proxyShape = new (mWorld.mMemoryManager.allocate(MemoryManager::AllocationType::Pool, - sizeof(ProxyShape))) ProxyShape(this, collisionShape, - mWorld.mMemoryManager); + sizeof(ProxyShape))) ProxyShape(this, mWorld.mMemoryManager); // Add the proxy-shape component to the entity of the body Vector3 localBoundsMin; Vector3 localBoundsMax; // TODO : Maybe this method can directly returns an AABB - proxyShape->getCollisionShape()->getLocalBounds(localBoundsMin, localBoundsMax); + collisionShape->getLocalBounds(localBoundsMin, localBoundsMax); ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape, proxyShape->getBroadPhaseId(), AABB(localBoundsMin, localBoundsMax), diff --git a/src/body/RigidBody.cpp b/src/body/RigidBody.cpp index 44dc2fa1..593ce715 100644 --- a/src/body/RigidBody.cpp +++ b/src/body/RigidBody.cpp @@ -283,14 +283,13 @@ ProxyShape* RigidBody::addCollisionShape(CollisionShape* collisionShape, // Create a new proxy collision shape to attach the collision shape to the body ProxyShape* proxyShape = new (mWorld.mMemoryManager.allocate(MemoryManager::AllocationType::Pool, - sizeof(ProxyShape))) ProxyShape(this, collisionShape, - mWorld.mMemoryManager); + sizeof(ProxyShape))) ProxyShape(this, mWorld.mMemoryManager); // Add the proxy-shape component to the entity of the body Vector3 localBoundsMin; Vector3 localBoundsMax; // TODO : Maybe this method can directly returns an AABB - proxyShape->getCollisionShape()->getLocalBounds(localBoundsMin, localBoundsMax); + collisionShape->getLocalBounds(localBoundsMin, localBoundsMax); ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape, proxyShape->getBroadPhaseId(), AABB(localBoundsMin, localBoundsMax), diff --git a/src/collision/ProxyShape.cpp b/src/collision/ProxyShape.cpp index c2ddbfd8..5acb2e71 100644 --- a/src/collision/ProxyShape.cpp +++ b/src/collision/ProxyShape.cpp @@ -39,8 +39,8 @@ using namespace reactphysics3d; * @param transform Transformation from collision shape local-space to body local-space * @param mass Mass of the collision shape (in kilograms) */ -ProxyShape::ProxyShape(CollisionBody* body, CollisionShape* shape, MemoryManager& memoryManager) - :mMemoryManager(memoryManager), mBody(body), mCollisionShape(shape), +ProxyShape::ProxyShape(CollisionBody* body, MemoryManager& memoryManager) + :mMemoryManager(memoryManager), mBody(body), mNext(nullptr), mBroadPhaseID(-1), mUserData(nullptr), mCollisionCategoryBits(0x0001), mCollideWithMaskBits(0xFFFF) { } @@ -68,7 +68,8 @@ bool ProxyShape::testPointInside(const Vector3& worldPoint) { const Transform localToWorld = mBody->mWorld.mTransformComponents.getTransform(mBody->getEntity()) * mBody->mWorld.mProxyShapesComponents.getLocalToBodyTransform(this); const Vector3 localPoint = localToWorld.getInverse() * worldPoint; - return mCollisionShape->testPointInside(localPoint, this); + const CollisionShape* collisionShape = mBody->mWorld.mProxyShapesComponents.getCollisionShape(this); + return collisionShape->testPointInside(localPoint, this); } // Set the collision category bits @@ -111,6 +112,33 @@ void ProxyShape::setLocalToBodyTransform(const Transform& transform) { transform.to_string()); } +// Return the AABB of the proxy shape in world-space +/** + * @return The AABB of the proxy shape in world-space + */ +const AABB ProxyShape::getWorldAABB() const { + AABB aabb; + CollisionShape* collisionShape = mBody->mWorld.mProxyShapesComponents.getCollisionShape(this); + collisionShape->computeAABB(aabb, getLocalToWorldTransform()); + return aabb; +} + +// Return the collision shape +/** + * @return Pointer to the internal collision shape + */ +const CollisionShape* ProxyShape::getCollisionShape() const { + return mBody->mWorld.mProxyShapesComponents.getCollisionShape(this); +} + +// Return the collision shape +/** +* @return Pointer to the internal collision shape +*/ +CollisionShape* ProxyShape::getCollisionShape() { + return mBody->mWorld.mProxyShapesComponents.getCollisionShape(this); +} + // Return the local to parent body transform /** * @return The transformation that transforms the local-space of the collision shape @@ -139,7 +167,8 @@ bool ProxyShape::raycast(const Ray& ray, RaycastInfo& raycastInfo) { worldToLocalTransform * ray.point2, ray.maxFraction); - bool isHit = mCollisionShape->raycast(rayLocal, raycastInfo, this, mMemoryManager.getPoolAllocator()); + const CollisionShape* collisionShape = mBody->mWorld.mProxyShapesComponents.getCollisionShape(this); + bool isHit = collisionShape->raycast(rayLocal, raycastInfo, this, mMemoryManager.getPoolAllocator()); // Convert the raycast info into world-space raycastInfo.worldPoint = localToWorldTransform * raycastInfo.worldPoint; @@ -159,3 +188,16 @@ const Transform ProxyShape::getLocalToWorldTransform() const { mBody->mWorld.mProxyShapesComponents.getLocalToBodyTransform(this); } +#ifdef IS_PROFILING_ACTIVE + +// Set the profiler +void ProxyShape::setProfiler(Profiler* profiler) { + + mProfiler = profiler; + + CollisionShape* collisionShape = mBody->mWorld.mProxyShapesComponents.getCollisionShape(this); + collisionShape->setProfiler(profiler); +} + +#endif + diff --git a/src/collision/ProxyShape.h b/src/collision/ProxyShape.h index 397a8d1f..deba0332 100644 --- a/src/collision/ProxyShape.h +++ b/src/collision/ProxyShape.h @@ -58,7 +58,7 @@ class ProxyShape { CollisionBody* mBody; /// Internal collision shape - CollisionShape* mCollisionShape; + //CollisionShape* mCollisionShape; /// Local-space to parent body-space transform (does not change over time) //Transform mLocalToBodyTransform; @@ -111,7 +111,7 @@ class ProxyShape { // -------------------- Methods -------------------- // /// Constructor - ProxyShape(CollisionBody* body, CollisionShape* shape, MemoryManager& memoryManager); + ProxyShape(CollisionBody* body, MemoryManager& memoryManager); /// Destructor virtual ~ProxyShape(); @@ -209,22 +209,6 @@ class ProxyShape { }; -// Return the collision shape -/** - * @return Pointer to the internal collision shape - */ -inline const CollisionShape* ProxyShape::getCollisionShape() const { - return mCollisionShape; -} - -// Return the collision shape -/** -* @return Pointer to the internal collision shape -*/ -inline CollisionShape* ProxyShape::getCollisionShape() { - return mCollisionShape; -} - // Return the parent body /** * @return Pointer to the parent body @@ -249,16 +233,6 @@ inline void ProxyShape::setUserData(void* userData) { mUserData = userData; } -// Return the AABB of the proxy shape in world-space -/** - * @return The AABB of the proxy shape in world-space - */ -inline const AABB ProxyShape::getWorldAABB() const { - AABB aabb; - mCollisionShape->computeAABB(aabb, getLocalToWorldTransform()); - return aabb; -} - // Return the next proxy shape in the linked list of proxy shapes /** * @return Pointer to the next proxy shape in the linked list of proxy shapes @@ -305,18 +279,6 @@ inline bool ProxyShape::testAABBOverlap(const AABB& worldAABB) const { return worldAABB.testCollision(getWorldAABB()); } -#ifdef IS_PROFILING_ACTIVE - -// Set the profiler -inline void ProxyShape::setProfiler(Profiler* profiler) { - - mProfiler = profiler; - - mCollisionShape->setProfiler(profiler); -} - -#endif - #ifdef IS_LOGGING_ACTIVE // Set the logger diff --git a/src/components/ProxyShapesComponents.h b/src/components/ProxyShapesComponents.h index e596a1c5..1e081fb0 100644 --- a/src/components/ProxyShapesComponents.h +++ b/src/components/ProxyShapesComponents.h @@ -191,6 +191,9 @@ class ProxyShapesComponents { /// Set the local-to-body transform of a proxy-shape void setLocalToBodyTransform(const ProxyShape* proxyShape, const Transform& transform); + + /// Return a pointer to the collision shape of a proxy-shape + CollisionShape* getCollisionShape(const ProxyShape* proxyShape) const; }; // Return the mass of a proxy-shape @@ -217,6 +220,13 @@ inline void ProxyShapesComponents::setLocalToBodyTransform(const ProxyShape* pro mLocalToBodyTransforms[mMapProxyShapeToComponentIndex[proxyShape]] = transform; } +// Return a pointer to the collision shape of a proxy-shape +inline CollisionShape* ProxyShapesComponents::getCollisionShape(const ProxyShape* proxyShape) const { + + assert(mMapProxyShapeToComponentIndex.containsKey(proxyShape)); + + return mCollisionShapes[mMapProxyShapeToComponentIndex[proxyShape]]; +} } #endif