Add proxy-shapes mapping in ProxyShapesComponents

This commit is contained in:
Daniel Chappuis 2019-01-10 07:41:10 +01:00
parent d8d490bff9
commit 5a1d10a991
5 changed files with 33 additions and 11 deletions

View File

@ -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);

View File

@ -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);

View File

@ -26,6 +26,7 @@
// Libraries
#include "ProxyShapesComponents.h"
#include "engine/EntityManager.h"
#include "collision/ProxyShape.h"
#include <cassert>
#include <random>
@ -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<Entity*>(newBuffer);
int* newBroadPhaseIds = reinterpret_cast<int*>(newEntities + nbComponentsToAllocate);
ProxyShape** newProxyShapes = reinterpret_cast<ProxyShape**>(newEntities + nbComponentsToAllocate);
int* newBroadPhaseIds = reinterpret_cast<int*>(newProxyShapes + nbComponentsToAllocate);
AABB* newLocalBounds = reinterpret_cast<AABB*>(newBroadPhaseIds + nbComponentsToAllocate);
Transform* newLocalToBodyTransforms = reinterpret_cast<Transform*>(newLocalBounds + nbComponentsToAllocate);
CollisionShape** newCollisionShapes = reinterpret_cast<CollisionShape**>(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<ProxyShape*, uint32>(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<ProxyShape*, uint32>(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<ProxyShape*, uint32>(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;

View File

@ -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<Entity, uint32> mMapEntityToComponentIndex;
/// Map a proxy shape to the index of the corresponding component in the array
Map<ProxyShape*, uint32> 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) {
}

View File

@ -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);
}