From daff897fc4bd16e0a7b71a77b6fda6355aafbf4d Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 26 Jul 2016 07:54:31 -0700 Subject: [PATCH] const shapes, and use *MotionState::setShape() --- libraries/physics/src/EntityMotionState.cpp | 14 ++++++++------ libraries/physics/src/EntityMotionState.h | 4 ++-- libraries/physics/src/ObjectMotionState.cpp | 12 ++++++------ libraries/physics/src/ObjectMotionState.h | 10 +++++----- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 4b6823dd8a..b70927d501 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -46,7 +46,7 @@ bool entityTreeIsLocked() { EntityMotionState::EntityMotionState(btCollisionShape* shape, EntityItemPointer entity) : - ObjectMotionState(shape), + ObjectMotionState(nullptr), _entityPtr(entity), _entity(entity.get()), _serverPosition(0.0f), @@ -71,6 +71,9 @@ EntityMotionState::EntityMotionState(btCollisionShape* shape, EntityItemPointer assert(_entity); assert(entityTreeIsLocked()); setMass(_entity->computeMass()); + // we need the side-effects of EntityMotionState::setShape() so we call it explicitly here + // rather than pass the legit shape pointer to the ObjectMotionState ctor above. + setShape(shape); } EntityMotionState::~EntityMotionState() { @@ -264,17 +267,16 @@ bool EntityMotionState::isReadyToComputeShape() const { } // virtual and protected -btCollisionShape* EntityMotionState::computeNewShape() { +const btCollisionShape* EntityMotionState::computeNewShape() { ShapeInfo shapeInfo; assert(entityTreeIsLocked()); _entity->computeShapeInfo(shapeInfo); return getShapeManager()->getShape(shapeInfo); } -void EntityMotionState::setShape(btCollisionShape* shape) { - bool shapeChanged = (_shape != shape); - ObjectMotionState::setShape(shape); - if (shapeChanged) { +void EntityMotionState::setShape(const btCollisionShape* shape) { + if (_shape != shape) { + ObjectMotionState::setShape(shape); _entity->setCollisionShape(_shape); } } diff --git a/libraries/physics/src/EntityMotionState.h b/libraries/physics/src/EntityMotionState.h index 2c39da9164..194d82805f 100644 --- a/libraries/physics/src/EntityMotionState.h +++ b/libraries/physics/src/EntityMotionState.h @@ -89,8 +89,8 @@ protected: #endif bool isReadyToComputeShape() const override; - btCollisionShape* computeNewShape() override; - void setShape(btCollisionShape* shape) override; + const btCollisionShape* computeNewShape() override; + void setShape(const btCollisionShape* shape) override; void setMotionType(PhysicsMotionType motionType) override; // In the glorious future (when entities lib depends on physics lib) the EntityMotionState will be diff --git a/libraries/physics/src/ObjectMotionState.cpp b/libraries/physics/src/ObjectMotionState.cpp index 64b1e9c527..38f079c1d4 100644 --- a/libraries/physics/src/ObjectMotionState.cpp +++ b/libraries/physics/src/ObjectMotionState.cpp @@ -62,14 +62,13 @@ ShapeManager* ObjectMotionState::getShapeManager() { return shapeManager; } -ObjectMotionState::ObjectMotionState(btCollisionShape* shape) : +ObjectMotionState::ObjectMotionState(const btCollisionShape* shape) : _motionType(MOTION_TYPE_STATIC), - _shape(nullptr), + _shape(shape), _body(nullptr), _mass(0.0f), _lastKinematicStep(worldSimulationStep) { - setShape(shape); } ObjectMotionState::~ObjectMotionState() { @@ -154,12 +153,13 @@ void ObjectMotionState::setRigidBody(btRigidBody* body) { _body = body; if (_body) { _body->setUserPointer(this); + assert(_body->getCollisionShape() == _shape); } updateCCDConfiguration(); } } -void ObjectMotionState::setShape(btCollisionShape* shape) { +void ObjectMotionState::setShape(const btCollisionShape* shape) { if (_shape != shape) { if (_shape) { getShapeManager()->releaseShape(_shape); @@ -254,7 +254,7 @@ bool ObjectMotionState::handleHardAndEasyChanges(uint32_t& flags, PhysicsEngine* if (!isReadyToComputeShape()) { return false; } - btCollisionShape* newShape = computeNewShape(); + const btCollisionShape* newShape = computeNewShape(); if (!newShape) { qCDebug(physics) << "Warning: failed to generate new shape!"; // failed to generate new shape! --> keep old shape and remove shape-change flag @@ -274,7 +274,7 @@ bool ObjectMotionState::handleHardAndEasyChanges(uint32_t& flags, PhysicsEngine* // and clear the reference we just created getShapeManager()->releaseShape(_shape); } else { - _body->setCollisionShape(newShape); + _body->setCollisionShape(const_cast(newShape)); setShape(newShape); updateCCDConfiguration(); } diff --git a/libraries/physics/src/ObjectMotionState.h b/libraries/physics/src/ObjectMotionState.h index ccaef17fd1..a7894998a8 100644 --- a/libraries/physics/src/ObjectMotionState.h +++ b/libraries/physics/src/ObjectMotionState.h @@ -78,7 +78,7 @@ public: static void setShapeManager(ShapeManager* manager); static ShapeManager* getShapeManager(); - ObjectMotionState(btCollisionShape* shape); + ObjectMotionState(const btCollisionShape* shape); ~ObjectMotionState(); virtual void handleEasyChanges(uint32_t& flags); @@ -110,7 +110,7 @@ public: virtual PhysicsMotionType computePhysicsMotionType() const = 0; - btCollisionShape* getShape() const { return _shape; } + const btCollisionShape* getShape() const { return _shape; } btRigidBody* getRigidBody() const { return _body; } virtual bool isMoving() const = 0; @@ -150,17 +150,17 @@ public: protected: virtual bool isReadyToComputeShape() const = 0; - virtual btCollisionShape* computeNewShape() = 0; + virtual const btCollisionShape* computeNewShape() = 0; virtual void setMotionType(PhysicsMotionType motionType); void updateCCDConfiguration(); void setRigidBody(btRigidBody* body); - virtual void setShape(btCollisionShape* shape); + virtual void setShape(const btCollisionShape* shape); MotionStateType _type = MOTIONSTATE_TYPE_INVALID; // type of MotionState PhysicsMotionType _motionType; // type of motion: KINEMATIC, DYNAMIC, or STATIC - btCollisionShape* _shape; + const btCollisionShape* _shape; btRigidBody* _body; float _mass;