Replacing ProxyShape data by corresponding component data

This commit is contained in:
Daniel Chappuis 2019-01-11 07:25:35 +01:00
parent 5a1d10a991
commit 176186e126
6 changed files with 71 additions and 35 deletions

View File

@ -78,7 +78,7 @@ 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,
transform, decimal(1), mWorld.mMemoryManager);
mWorld.mMemoryManager);
// Add the proxy-shape component to the entity of the body
Vector3 localBoundsMin;
@ -289,7 +289,7 @@ void CollisionBody::setIsActive(bool isActive) {
// Compute the world-space AABB of the new collision shape
AABB aabb;
shape->getCollisionShape()->computeAABB(aabb, transform * shape->mLocalToBodyTransform);
shape->getCollisionShape()->computeAABB(aabb, transform * mWorld.mProxyShapesComponents.getLocalToBodyTransform(shape));
// Add the proxy shape to the collision detection
mWorld.mCollisionDetection.addProxyCollisionShape(shape, aabb);

View File

@ -284,7 +284,7 @@ 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,
transform, mass, mWorld.mMemoryManager);
mWorld.mMemoryManager);
// Add the proxy-shape component to the entity of the body
Vector3 localBoundsMin;

View File

@ -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, const Transform& transform, decimal mass, MemoryManager& memoryManager)
:mMemoryManager(memoryManager), mBody(body), mCollisionShape(shape), mLocalToBodyTransform(transform), mMass(mass),
ProxyShape::ProxyShape(CollisionBody* body, CollisionShape* shape, MemoryManager& memoryManager)
:mMemoryManager(memoryManager), mBody(body), mCollisionShape(shape),
mNext(nullptr), mBroadPhaseID(-1), mUserData(nullptr), mCollisionCategoryBits(0x0001), mCollideWithMaskBits(0xFFFF) {
}
@ -50,13 +50,23 @@ ProxyShape::~ProxyShape() {
}
// Return the mass of the collision shape
/**
* @return Mass of the collision shape (in kilograms)
*/
decimal ProxyShape::getMass() const {
return mBody->mWorld.mProxyShapesComponents.getMass(this);
}
// Return true if a point is inside the collision shape
/**
* @param worldPoint Point to test in world-space coordinates
* @return True if the point is inside the collision shape
*/
bool ProxyShape::testPointInside(const Vector3& worldPoint) {
const Transform localToWorld = mBody->mWorld.mTransformComponents.getTransform(mBody->getEntity()) * mLocalToBodyTransform;
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);
}
@ -88,7 +98,8 @@ void ProxyShape::setCollideWithMaskBits(unsigned short collideWithMaskBits) {
// Set the local to parent body transform
void ProxyShape::setLocalToBodyTransform(const Transform& transform) {
mLocalToBodyTransform = transform;
//mLocalToBodyTransform = transform;
mBody->mWorld.mProxyShapesComponents.setLocalToBodyTransform(this, transform);
mBody->setIsSleeping(false);
@ -97,7 +108,16 @@ void ProxyShape::setLocalToBodyTransform(const Transform& transform) {
RP3D_LOG(mLogger, Logger::Level::Information, Logger::Category::ProxyShape,
"ProxyShape " + std::to_string(mBroadPhaseID) + ": Set localToBodyTransform=" +
mLocalToBodyTransform.to_string());
transform.to_string());
}
// Return the local to parent body transform
/**
* @return The transformation that transforms the local-space of the collision shape
* to the local-space of the parent body
*/
const Transform& ProxyShape::getLocalToBodyTransform() const {
return mBody->mWorld.mProxyShapesComponents.getLocalToBodyTransform(this);
}
// Raycast method with feedback information
@ -135,6 +155,7 @@ bool ProxyShape::raycast(const Ray& ray, RaycastInfo& raycastInfo) {
* shape to the world-space
*/
const Transform ProxyShape::getLocalToWorldTransform() const {
return mBody->mWorld.mTransformComponents.getTransform(mBody->getEntity()) * mLocalToBodyTransform;
return mBody->mWorld.mTransformComponents.getTransform(mBody->getEntity()) *
mBody->mWorld.mProxyShapesComponents.getLocalToBodyTransform(this);
}

View File

@ -61,10 +61,10 @@ class ProxyShape {
CollisionShape* mCollisionShape;
/// Local-space to parent body-space transform (does not change over time)
Transform mLocalToBodyTransform;
//Transform mLocalToBodyTransform;
/// Mass (in kilogramms) of the corresponding collision shape
decimal mMass;
//decimal mMass;
/// Pointer to the next proxy shape of the body (linked list)
ProxyShape* mNext;
@ -111,8 +111,7 @@ class ProxyShape {
// -------------------- Methods -------------------- //
/// Constructor
ProxyShape(CollisionBody* body, CollisionShape* shape,
const Transform& transform, decimal mass, MemoryManager& memoryManager);
ProxyShape(CollisionBody* body, CollisionShape* shape, MemoryManager& memoryManager);
/// Destructor
virtual ~ProxyShape();
@ -234,14 +233,6 @@ inline CollisionBody* ProxyShape::getBody() const {
return mBody;
}
// Return the mass of the collision shape
/**
* @return Mass of the collision shape (in kilograms)
*/
inline decimal ProxyShape::getMass() const {
return mMass;
}
// Return a pointer to the user data attached to this body
/**
* @return A pointer to the user data stored into the proxy shape
@ -258,15 +249,6 @@ inline void ProxyShape::setUserData(void* userData) {
mUserData = userData;
}
// Return the local to parent body transform
/**
* @return The transformation that transforms the local-space of the collision shape
* to the local-space of the parent body
*/
inline const Transform& ProxyShape::getLocalToBodyTransform() const {
return mLocalToBodyTransform;
}
// Return the AABB of the proxy shape in world-space
/**
* @return The AABB of the proxy shape in world-space

View File

@ -200,7 +200,7 @@ void ProxyShapesComponents::addComponent(Entity entity, bool isSleeping, const P
mCollisionShapes[index] = component.collisionShape;
new (mMasses + index) decimal(component.mass);
mMapProxyShapeToComponentIndex.add(Pair<ProxyShape*, uint32>(component.proxyShape, index));
mMapProxyShapeToComponentIndex.add(Pair<const ProxyShape*, uint32>(component.proxyShape, index));
mNbComponents++;
@ -255,7 +255,7 @@ void ProxyShapesComponents::moveComponentToIndex(uint32 srcIndex, uint32 destInd
assert(mMapEntityToComponentIndex[mEntities[destIndex]] == destIndex);
}
mMapProxyShapeToComponentIndex.add(Pair<ProxyShape*, uint32>(mProxyShapes[destIndex], destIndex));
mMapProxyShapeToComponentIndex.add(Pair<const ProxyShape*, uint32>(mProxyShapes[destIndex], destIndex));
assert(mPreviousBodyProxyShapes[mNextBodyProxyShapes[destIndex]] == destIndex || !hasNextProxyShape(destIndex));
assert(mNextBodyProxyShapes[mPreviousBodyProxyShapes[destIndex]] == destIndex || !hasPreviousProxyShape(destIndex));
@ -305,7 +305,7 @@ void ProxyShapesComponents::swapComponents(uint32 index1, uint32 index2) {
mPreviousBodyProxyShapes[mNextBodyProxyShapes[index2]] = index2;
}
mMapProxyShapeToComponentIndex.add(Pair<ProxyShape*, uint32>(mProxyShapes[index2], index2));
mMapProxyShapeToComponentIndex.add(Pair<const ProxyShape*, uint32>(mProxyShapes[index2], index2));
// Update the entity to component index mapping if it is the first body proxy-shape
if (isFirstBodyProxyShape1) {

View File

@ -87,7 +87,7 @@ class ProxyShapesComponents {
Map<Entity, uint32> mMapEntityToComponentIndex;
/// Map a proxy shape to the index of the corresponding component in the array
Map<ProxyShape*, uint32> mMapProxyShapeToComponentIndex;
Map<const ProxyShape*, uint32> mMapProxyShapeToComponentIndex;
/// Array of entities of each component
Entity* mEntities;
@ -108,7 +108,7 @@ class ProxyShapesComponents {
/// Pointers to the collision shapes of the proxy-shapes
CollisionShape** mCollisionShapes;
/// Masses of the proxy-shapes
/// Masses (in kilogramms) of the proxy-shapes
decimal* mMasses;
/// Index of the previous proxy-shape in the same body
@ -182,8 +182,41 @@ class ProxyShapesComponents {
/// Notify if a given entity is sleeping or not
void setIsEntitySleeping(Entity entity, bool isSleeping);
/// Return the mass of a proxy-shape
decimal getMass(const ProxyShape* proxyShape) const;
/// Return the local-to-body transform of a proxy-shape
const Transform& getLocalToBodyTransform(const ProxyShape* proxyShape) const;
/// Set the local-to-body transform of a proxy-shape
void setLocalToBodyTransform(const ProxyShape* proxyShape, const Transform& transform);
};
// Return the mass of a proxy-shape
inline decimal ProxyShapesComponents::getMass(const ProxyShape* proxyShape) const {
assert(mMapProxyShapeToComponentIndex.containsKey(proxyShape));
return mMasses[mMapProxyShapeToComponentIndex[proxyShape]];
}
// Return the local-to-body transform of a proxy-shape
inline const Transform& ProxyShapesComponents::getLocalToBodyTransform(const ProxyShape* proxyShape) const {
assert(mMapProxyShapeToComponentIndex.containsKey(proxyShape));
return mLocalToBodyTransforms[mMapProxyShapeToComponentIndex[proxyShape]];
}
// Set the local-to-body transform of a proxy-shape
inline void ProxyShapesComponents::setLocalToBodyTransform(const ProxyShape* proxyShape, const Transform& transform) {
assert(mMapProxyShapeToComponentIndex.containsKey(proxyShape));
mLocalToBodyTransforms[mMapProxyShapeToComponentIndex[proxyShape]] = transform;
}
}
#endif