ShapeManager hands out const shape pointers

This commit is contained in:
Andrew Meadows 2016-07-26 07:53:11 -07:00
parent 6b0ae654ba
commit 25fb7aacad
9 changed files with 24 additions and 20 deletions

View file

@ -367,7 +367,7 @@ void AvatarManager::addAvatarToSimulation(Avatar* avatar) {
ShapeInfo shapeInfo; ShapeInfo shapeInfo;
avatar->computeShapeInfo(shapeInfo); avatar->computeShapeInfo(shapeInfo);
btCollisionShape* shape = ObjectMotionState::getShapeManager()->getShape(shapeInfo); btCollisionShape* shape = const_cast<btCollisionShape*>(ObjectMotionState::getShapeManager()->getShape(shapeInfo));
if (shape) { if (shape) {
// we don't add to the simulation now, we put it on a list to be added later // we don't add to the simulation now, we put it on a list to be added later
AvatarMotionState* motionState = new AvatarMotionState(avatar, shape); AvatarMotionState* motionState = new AvatarMotionState(avatar, shape);

View file

@ -17,7 +17,7 @@
#include "AvatarMotionState.h" #include "AvatarMotionState.h"
#include "BulletUtil.h" #include "BulletUtil.h"
AvatarMotionState::AvatarMotionState(Avatar* avatar, btCollisionShape* shape) : ObjectMotionState(shape), _avatar(avatar) { AvatarMotionState::AvatarMotionState(Avatar* avatar, const btCollisionShape* shape) : ObjectMotionState(shape), _avatar(avatar) {
assert(_avatar); assert(_avatar);
_type = MOTIONSTATE_TYPE_AVATAR; _type = MOTIONSTATE_TYPE_AVATAR;
if (_shape) { if (_shape) {
@ -47,7 +47,7 @@ PhysicsMotionType AvatarMotionState::computePhysicsMotionType() const {
} }
// virtual and protected // virtual and protected
btCollisionShape* AvatarMotionState::computeNewShape() { const btCollisionShape* AvatarMotionState::computeNewShape() {
ShapeInfo shapeInfo; ShapeInfo shapeInfo;
_avatar->computeShapeInfo(shapeInfo); _avatar->computeShapeInfo(shapeInfo);
return getShapeManager()->getShape(shapeInfo); return getShapeManager()->getShape(shapeInfo);

View file

@ -20,7 +20,7 @@ class Avatar;
class AvatarMotionState : public ObjectMotionState { class AvatarMotionState : public ObjectMotionState {
public: public:
AvatarMotionState(Avatar* avatar, btCollisionShape* shape); AvatarMotionState(Avatar* avatar, const btCollisionShape* shape);
virtual PhysicsMotionType getMotionType() const override { return _motionType; } virtual PhysicsMotionType getMotionType() const override { return _motionType; }
@ -72,7 +72,7 @@ protected:
~AvatarMotionState(); ~AvatarMotionState();
virtual bool isReadyToComputeShape() const override { return true; } virtual bool isReadyToComputeShape() const override { return true; }
virtual btCollisionShape* computeNewShape() override; virtual const btCollisionShape* computeNewShape() override;
// The AvatarMotionState keeps a RAW backpointer to its Avatar because all AvatarMotionState // The AvatarMotionState keeps a RAW backpointer to its Avatar because all AvatarMotionState
// instances are "owned" by their corresponding Avatar instance and are deleted in the Avatar dtor. // instances are "owned" by their corresponding Avatar instance and are deleted in the Avatar dtor.

View file

@ -225,7 +225,7 @@ void PhysicalEntitySimulation::getObjectsToAddToPhysics(VectorOfMotionStates& re
<< "at" << entity->getPosition() << " will be reduced"; << "at" << entity->getPosition() << " will be reduced";
} }
} }
btCollisionShape* shape = ObjectMotionState::getShapeManager()->getShape(shapeInfo); btCollisionShape* shape = const_cast<btCollisionShape*>(ObjectMotionState::getShapeManager()->getShape(shapeInfo));
if (shape) { if (shape) {
EntityMotionState* motionState = new EntityMotionState(shape, entity); EntityMotionState* motionState = new EntityMotionState(shape, entity);
entity->setPhysicsInfo(static_cast<void*>(motionState)); entity->setPhysicsInfo(static_cast<void*>(motionState));

View file

@ -76,7 +76,7 @@ void PhysicsEngine::addObjectToDynamicsWorld(ObjectMotionState* motionState) {
switch(motionType) { switch(motionType) {
case MOTION_TYPE_KINEMATIC: { case MOTION_TYPE_KINEMATIC: {
if (!body) { if (!body) {
btCollisionShape* shape = motionState->getShape(); btCollisionShape* shape = const_cast<btCollisionShape*>(motionState->getShape());
assert(shape); assert(shape);
body = new btRigidBody(mass, motionState, shape, inertia); body = new btRigidBody(mass, motionState, shape, inertia);
motionState->setRigidBody(body); motionState->setRigidBody(body);
@ -93,7 +93,7 @@ void PhysicsEngine::addObjectToDynamicsWorld(ObjectMotionState* motionState) {
} }
case MOTION_TYPE_DYNAMIC: { case MOTION_TYPE_DYNAMIC: {
mass = motionState->getMass(); mass = motionState->getMass();
btCollisionShape* shape = motionState->getShape(); btCollisionShape* shape = const_cast<btCollisionShape*>(motionState->getShape());
assert(shape); assert(shape);
shape->calculateLocalInertia(mass, inertia); shape->calculateLocalInertia(mass, inertia);
if (!body) { if (!body) {
@ -120,7 +120,7 @@ void PhysicsEngine::addObjectToDynamicsWorld(ObjectMotionState* motionState) {
default: { default: {
if (!body) { if (!body) {
assert(motionState->getShape()); assert(motionState->getShape());
body = new btRigidBody(mass, motionState, motionState->getShape(), inertia); body = new btRigidBody(mass, motionState, const_cast<btCollisionShape*>(motionState->getShape()), inertia);
motionState->setRigidBody(body); motionState->setRigidBody(body);
} else { } else {
body->setMassProps(mass, inertia); body->setMassProps(mass, inertia);

View file

@ -247,7 +247,7 @@ void deleteStaticMeshArray(btTriangleIndexVertexArray* dataArray) {
delete dataArray; delete dataArray;
} }
btCollisionShape* ShapeFactory::createShapeFromInfo(const ShapeInfo& info) { const btCollisionShape* ShapeFactory::createShapeFromInfo(const ShapeInfo& info) {
btCollisionShape* shape = NULL; btCollisionShape* shape = NULL;
int type = info.getType(); int type = info.getType();
switch(type) { switch(type) {
@ -359,10 +359,14 @@ btCollisionShape* ShapeFactory::createShapeFromInfo(const ShapeInfo& info) {
return shape; return shape;
} }
void ShapeFactory::deleteShape(btCollisionShape* shape) { void ShapeFactory::deleteShape(const btCollisionShape* shape) {
assert(shape); assert(shape);
if (shape->getShapeType() == (int)COMPOUND_SHAPE_PROXYTYPE) { // ShapeFactory is responsible for deleting all shapes, even the const ones that are stored
btCompoundShape* compoundShape = static_cast<btCompoundShape*>(shape); // in the ShapeManager, so we must cast to non-const here when deleting.
// so we cast to non-const here when deleting memory.
btCollisionShape* nonConstShape = const_cast<btCollisionShape*>(shape);
if (nonConstShape->getShapeType() == (int)COMPOUND_SHAPE_PROXYTYPE) {
btCompoundShape* compoundShape = static_cast<btCompoundShape*>(nonConstShape);
const int numChildShapes = compoundShape->getNumChildShapes(); const int numChildShapes = compoundShape->getNumChildShapes();
for (int i = 0; i < numChildShapes; i ++) { for (int i = 0; i < numChildShapes; i ++) {
btCollisionShape* childShape = compoundShape->getChildShape(i); btCollisionShape* childShape = compoundShape->getChildShape(i);
@ -374,7 +378,7 @@ void ShapeFactory::deleteShape(btCollisionShape* shape) {
} }
} }
} }
delete shape; delete nonConstShape;
} }
// the dataArray must be created before we create the StaticMeshShape // the dataArray must be created before we create the StaticMeshShape

View file

@ -20,8 +20,8 @@
// translates between ShapeInfo and btShape // translates between ShapeInfo and btShape
namespace ShapeFactory { namespace ShapeFactory {
btCollisionShape* createShapeFromInfo(const ShapeInfo& info); const btCollisionShape* createShapeFromInfo(const ShapeInfo& info);
void deleteShape(btCollisionShape* shape); void deleteShape(const btCollisionShape* shape);
//btTriangleIndexVertexArray* createStaticMeshArray(const ShapeInfo& info); //btTriangleIndexVertexArray* createStaticMeshArray(const ShapeInfo& info);
//void deleteStaticMeshArray(btTriangleIndexVertexArray* dataArray); //void deleteStaticMeshArray(btTriangleIndexVertexArray* dataArray);

View file

@ -28,7 +28,7 @@ ShapeManager::~ShapeManager() {
_shapeMap.clear(); _shapeMap.clear();
} }
btCollisionShape* ShapeManager::getShape(const ShapeInfo& info) { const btCollisionShape* ShapeManager::getShape(const ShapeInfo& info) {
if (info.getType() == SHAPE_TYPE_NONE) { if (info.getType() == SHAPE_TYPE_NONE) {
return nullptr; return nullptr;
} }
@ -45,7 +45,7 @@ btCollisionShape* ShapeManager::getShape(const ShapeInfo& info) {
shapeRef->refCount++; shapeRef->refCount++;
return shapeRef->shape; return shapeRef->shape;
} }
btCollisionShape* shape = ShapeFactory::createShapeFromInfo(info); const btCollisionShape* shape = ShapeFactory::createShapeFromInfo(info);
if (shape) { if (shape) {
ShapeReference newRef; ShapeReference newRef;
newRef.refCount = 1; newRef.refCount = 1;

View file

@ -26,7 +26,7 @@ public:
~ShapeManager(); ~ShapeManager();
/// \return pointer to shape /// \return pointer to shape
btCollisionShape* getShape(const ShapeInfo& info); const btCollisionShape* getShape(const ShapeInfo& info);
/// \return true if shape was found and released /// \return true if shape was found and released
bool releaseShape(const btCollisionShape* shape); bool releaseShape(const btCollisionShape* shape);
@ -46,7 +46,7 @@ private:
class ShapeReference { class ShapeReference {
public: public:
int refCount; int refCount;
btCollisionShape* shape; const btCollisionShape* shape;
DoubleHashKey key; DoubleHashKey key;
ShapeReference() : refCount(0), shape(nullptr) {} ShapeReference() : refCount(0), shape(nullptr) {}
}; };