diff --git a/src/body/CollisionBody.cpp b/src/body/CollisionBody.cpp index 65724b16..b2e0d860 100644 --- a/src/body/CollisionBody.cpp +++ b/src/body/CollisionBody.cpp @@ -86,7 +86,7 @@ ProxyShape* CollisionBody::addCollisionShape(CollisionShape* collisionShape, // TODO : Maybe this method can directly returns an AABB proxyShape->getCollisionShape()->getLocalBounds(localBoundsMin, localBoundsMax); - ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape->getBroadPhaseId(), + ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape, proxyShape->getBroadPhaseId(), 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 40b43c6d..fc7fea1b 100644 --- a/src/body/RigidBody.cpp +++ b/src/body/RigidBody.cpp @@ -292,7 +292,7 @@ ProxyShape* RigidBody::addCollisionShape(CollisionShape* collisionShape, // TODO : Maybe this method can directly returns an AABB proxyShape->getCollisionShape()->getLocalBounds(localBoundsMin, localBoundsMax); - ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape->getBroadPhaseId(), + ProxyShapesComponents::ProxyShapeComponent proxyShapeComponent(proxyShape, proxyShape->getBroadPhaseId(), AABB(localBoundsMin, localBoundsMax), transform, collisionShape, mass); mWorld.mProxyShapesComponents.addComponent(mEntity, mIsSleeping, proxyShapeComponent); diff --git a/src/components/ProxyShapesComponents.cpp b/src/components/ProxyShapesComponents.cpp index ddbb773b..ca3122ab 100644 --- a/src/components/ProxyShapesComponents.cpp +++ b/src/components/ProxyShapesComponents.cpp @@ -26,6 +26,7 @@ // Libraries #include "ProxyShapesComponents.h" #include "engine/EntityManager.h" +#include "collision/ProxyShape.h" #include #include @@ -35,7 +36,8 @@ using namespace reactphysics3d; // Constructor ProxyShapesComponents::ProxyShapesComponents(MemoryAllocator& allocator) :mMemoryAllocator(allocator), mNbComponents(0), mNbAllocatedComponents(0), - mSleepingStartIndex(0), mBuffer(nullptr), mMapEntityToComponentIndex(allocator) { + mSleepingStartIndex(0), mBuffer(nullptr), mMapEntityToComponentIndex(allocator), + mMapProxyShapeToComponentIndex(allocator) { // Allocate memory for the components data allocate(INIT_ALLOCATED_COMPONENTS); @@ -75,7 +77,8 @@ void ProxyShapesComponents::allocate(uint32 nbComponentsToAllocate) { // New pointers to components data Entity* newEntities = static_cast(newBuffer); - int* newBroadPhaseIds = reinterpret_cast(newEntities + nbComponentsToAllocate); + ProxyShape** newProxyShapes = reinterpret_cast(newEntities + nbComponentsToAllocate); + int* newBroadPhaseIds = reinterpret_cast(newProxyShapes + nbComponentsToAllocate); AABB* newLocalBounds = reinterpret_cast(newBroadPhaseIds + nbComponentsToAllocate); Transform* newLocalToBodyTransforms = reinterpret_cast(newLocalBounds + nbComponentsToAllocate); CollisionShape** newCollisionShapes = reinterpret_cast(newLocalToBodyTransforms + nbComponentsToAllocate); @@ -88,6 +91,7 @@ void ProxyShapesComponents::allocate(uint32 nbComponentsToAllocate) { // Copy component data from the previous buffer to the new one memcpy(newEntities, mEntities, mNbComponents * sizeof(Entity)); + memcpy(newProxyShapes, mProxyShapes, mNbComponents * sizeof(ProxyShape*)); memcpy(newBroadPhaseIds, mBroadPhaseIds, mNbComponents * sizeof(int)); memcpy(newLocalBounds, mLocalBounds, mNbComponents * sizeof(AABB)); memcpy(newLocalToBodyTransforms, mLocalToBodyTransforms, mNbComponents * sizeof(Transform)); @@ -102,6 +106,7 @@ void ProxyShapesComponents::allocate(uint32 nbComponentsToAllocate) { mBuffer = newBuffer; mEntities = newEntities; + mProxyShapes = newProxyShapes; mBroadPhaseIds = newBroadPhaseIds; mLocalBounds = newLocalBounds; mLocalToBodyTransforms = newLocalToBodyTransforms; @@ -188,12 +193,15 @@ void ProxyShapesComponents::addComponent(Entity entity, bool isSleeping, const P // Insert the new component data new (mEntities + index) Entity(entity); + mProxyShapes[index] = (component.proxyShape); new (mBroadPhaseIds + index) int(component.broadPhaseId); new (mLocalBounds + index) AABB(component.localBounds); new (mLocalToBodyTransforms + index) Transform(component.localToBodyTransform); mCollisionShapes[index] = component.collisionShape; new (mMasses + index) decimal(component.mass); + mMapProxyShapeToComponentIndex.add(Pair(component.proxyShape, index)); + mNbComponents++; // Map the entity with the new component lookup index @@ -216,6 +224,7 @@ void ProxyShapesComponents::moveComponentToIndex(uint32 srcIndex, uint32 destInd // Copy the data of the source component to the destination location new (mEntities + destIndex) Entity(mEntities[srcIndex]); + mProxyShapes[destIndex] = mProxyShapes[srcIndex]; new (mBroadPhaseIds + destIndex) int(mBroadPhaseIds[srcIndex]); new (mLocalBounds + destIndex) AABB(mLocalBounds[srcIndex]); new (mLocalToBodyTransforms + destIndex) Transform(mLocalToBodyTransforms[srcIndex]); @@ -246,6 +255,8 @@ void ProxyShapesComponents::moveComponentToIndex(uint32 srcIndex, uint32 destInd assert(mMapEntityToComponentIndex[mEntities[destIndex]] == destIndex); } + mMapProxyShapeToComponentIndex.add(Pair(mProxyShapes[destIndex], destIndex)); + assert(mPreviousBodyProxyShapes[mNextBodyProxyShapes[destIndex]] == destIndex || !hasNextProxyShape(destIndex)); assert(mNextBodyProxyShapes[mPreviousBodyProxyShapes[destIndex]] == destIndex || !hasPreviousProxyShape(destIndex)); } @@ -255,6 +266,7 @@ void ProxyShapesComponents::swapComponents(uint32 index1, uint32 index2) { // Copy component 1 data Entity entity1(mEntities[index1]); + ProxyShape* proxyShape1 = mProxyShapes[index1]; int broadPhaseId1 = mBroadPhaseIds[index1]; AABB localBounds1 = mLocalBounds[index1]; Transform localToBodyTransform1 = mLocalToBodyTransforms[index1]; @@ -272,6 +284,7 @@ void ProxyShapesComponents::swapComponents(uint32 index1, uint32 index2) { // Reconstruct component 1 at component 2 location new (mEntities + index2) Entity(entity1); + mProxyShapes[index2] = proxyShape1; new (mBroadPhaseIds + index2) int(broadPhaseId1); new (mLocalBounds + index2) AABB(localBounds1); new (mLocalToBodyTransforms + index2) Transform(localToBodyTransform1); @@ -292,6 +305,8 @@ void ProxyShapesComponents::swapComponents(uint32 index1, uint32 index2) { mPreviousBodyProxyShapes[mNextBodyProxyShapes[index2]] = index2; } + mMapProxyShapeToComponentIndex.add(Pair(mProxyShapes[index2], index2)); + // Update the entity to component index mapping if it is the first body proxy-shape if (isFirstBodyProxyShape1) { assert(!hasPreviousProxyShape(index2)); @@ -419,7 +434,10 @@ void ProxyShapesComponents::destroyComponent(uint32 index) { assert(index < mNbComponents); + mMapProxyShapeToComponentIndex.remove(mProxyShapes[index]); + mEntities[index].~Entity(); + mProxyShapes[index] = nullptr; mLocalBounds[index].~AABB(); mLocalToBodyTransforms[index].~Transform(); mCollisionShapes[index] = nullptr; diff --git a/src/components/ProxyShapesComponents.h b/src/components/ProxyShapesComponents.h index 7d481699..41f7a065 100644 --- a/src/components/ProxyShapesComponents.h +++ b/src/components/ProxyShapesComponents.h @@ -40,6 +40,7 @@ class MemoryAllocator; class EntityManager; class AABB; class CollisionShape; +class ProxyShape; // Class ProxyShapesComponents /** @@ -61,7 +62,7 @@ class ProxyShapesComponents { /// Number of valid entities to hit before stopping garbage collection const uint32 GARBAGE_COLLECTION_MAX_VALID_ENTITIES = 5; - const size_t COMPONENT_DATA_SIZE = sizeof(Entity) + sizeof(int) + sizeof(AABB) + + const size_t COMPONENT_DATA_SIZE = sizeof(Entity) + sizeof(ProxyShape*) + sizeof(int) + sizeof(AABB) + sizeof(Transform) + sizeof(CollisionShape*) + sizeof(decimal) + sizeof(uint32) + sizeof(uint32); @@ -85,9 +86,15 @@ class ProxyShapesComponents { /// Map an entity to the index of its component in the array Map mMapEntityToComponentIndex; + /// Map a proxy shape to the index of the corresponding component in the array + Map mMapProxyShapeToComponentIndex; + /// Array of entities of each component Entity* mEntities; + /// Array of pointers to the proxy-shapes + ProxyShape** mProxyShapes; + /// Ids of the proxy-shapes for the broad-phase algorithm // TODO : Try to change type to uint32 int* mBroadPhaseIds; @@ -140,6 +147,7 @@ class ProxyShapesComponents { /// Structure for the data of a proxy shape component struct ProxyShapeComponent { + ProxyShape* proxyShape; int broadPhaseId; AABB localBounds; Transform localToBodyTransform; @@ -147,9 +155,9 @@ class ProxyShapesComponents { decimal mass; /// Constructor - ProxyShapeComponent(int broadPhaseId, AABB localBounds, Transform localToBodyTransform, + ProxyShapeComponent(ProxyShape* proxyShape, int broadPhaseId, AABB localBounds, Transform localToBodyTransform, CollisionShape* collisionShape, decimal mass) - :broadPhaseId(broadPhaseId), localBounds(localBounds), localToBodyTransform(localToBodyTransform), + :proxyShape(proxyShape), broadPhaseId(broadPhaseId), localBounds(localBounds), localToBodyTransform(localToBodyTransform), collisionShape(collisionShape), mass(mass) { } diff --git a/src/engine/Entity.cpp b/src/engine/Entity.cpp index aa5e2401..68f9f3f8 100644 --- a/src/engine/Entity.cpp +++ b/src/engine/Entity.cpp @@ -40,10 +40,6 @@ const uint32 Entity::MINIMUM_FREE_INDICES = 1024; Entity::Entity(uint32 index, uint32 generation) :id((index & ENTITY_INDEX_MASK) | ((generation & ENTITY_GENERATION_MASK) << ENTITY_INDEX_BITS)) { - uint32 test1 = index & ENTITY_INDEX_MASK; - uint32 test2 = (generation & ENTITY_INDEX_MASK) << ENTITY_INDEX_BITS; - uint32 test3 = test1 | test2; - uint32 test = getIndex(); assert(getIndex() == index); assert(getGeneration() == generation); }