const shapes, and use *MotionState::setShape()

This commit is contained in:
Andrew Meadows 2016-07-26 07:54:31 -07:00
parent 25fb7aacad
commit daff897fc4
4 changed files with 21 additions and 19 deletions

View file

@ -46,7 +46,7 @@ bool entityTreeIsLocked() {
EntityMotionState::EntityMotionState(btCollisionShape* shape, EntityItemPointer entity) : EntityMotionState::EntityMotionState(btCollisionShape* shape, EntityItemPointer entity) :
ObjectMotionState(shape), ObjectMotionState(nullptr),
_entityPtr(entity), _entityPtr(entity),
_entity(entity.get()), _entity(entity.get()),
_serverPosition(0.0f), _serverPosition(0.0f),
@ -71,6 +71,9 @@ EntityMotionState::EntityMotionState(btCollisionShape* shape, EntityItemPointer
assert(_entity); assert(_entity);
assert(entityTreeIsLocked()); assert(entityTreeIsLocked());
setMass(_entity->computeMass()); 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() { EntityMotionState::~EntityMotionState() {
@ -264,17 +267,16 @@ bool EntityMotionState::isReadyToComputeShape() const {
} }
// virtual and protected // virtual and protected
btCollisionShape* EntityMotionState::computeNewShape() { const btCollisionShape* EntityMotionState::computeNewShape() {
ShapeInfo shapeInfo; ShapeInfo shapeInfo;
assert(entityTreeIsLocked()); assert(entityTreeIsLocked());
_entity->computeShapeInfo(shapeInfo); _entity->computeShapeInfo(shapeInfo);
return getShapeManager()->getShape(shapeInfo); return getShapeManager()->getShape(shapeInfo);
} }
void EntityMotionState::setShape(btCollisionShape* shape) { void EntityMotionState::setShape(const btCollisionShape* shape) {
bool shapeChanged = (_shape != shape); if (_shape != shape) {
ObjectMotionState::setShape(shape); ObjectMotionState::setShape(shape);
if (shapeChanged) {
_entity->setCollisionShape(_shape); _entity->setCollisionShape(_shape);
} }
} }

View file

@ -89,8 +89,8 @@ protected:
#endif #endif
bool isReadyToComputeShape() const override; bool isReadyToComputeShape() const override;
btCollisionShape* computeNewShape() override; const btCollisionShape* computeNewShape() override;
void setShape(btCollisionShape* shape) override; void setShape(const btCollisionShape* shape) override;
void setMotionType(PhysicsMotionType motionType) override; void setMotionType(PhysicsMotionType motionType) override;
// In the glorious future (when entities lib depends on physics lib) the EntityMotionState will be // In the glorious future (when entities lib depends on physics lib) the EntityMotionState will be

View file

@ -62,14 +62,13 @@ ShapeManager* ObjectMotionState::getShapeManager() {
return shapeManager; return shapeManager;
} }
ObjectMotionState::ObjectMotionState(btCollisionShape* shape) : ObjectMotionState::ObjectMotionState(const btCollisionShape* shape) :
_motionType(MOTION_TYPE_STATIC), _motionType(MOTION_TYPE_STATIC),
_shape(nullptr), _shape(shape),
_body(nullptr), _body(nullptr),
_mass(0.0f), _mass(0.0f),
_lastKinematicStep(worldSimulationStep) _lastKinematicStep(worldSimulationStep)
{ {
setShape(shape);
} }
ObjectMotionState::~ObjectMotionState() { ObjectMotionState::~ObjectMotionState() {
@ -154,12 +153,13 @@ void ObjectMotionState::setRigidBody(btRigidBody* body) {
_body = body; _body = body;
if (_body) { if (_body) {
_body->setUserPointer(this); _body->setUserPointer(this);
assert(_body->getCollisionShape() == _shape);
} }
updateCCDConfiguration(); updateCCDConfiguration();
} }
} }
void ObjectMotionState::setShape(btCollisionShape* shape) { void ObjectMotionState::setShape(const btCollisionShape* shape) {
if (_shape != shape) { if (_shape != shape) {
if (_shape) { if (_shape) {
getShapeManager()->releaseShape(_shape); getShapeManager()->releaseShape(_shape);
@ -254,7 +254,7 @@ bool ObjectMotionState::handleHardAndEasyChanges(uint32_t& flags, PhysicsEngine*
if (!isReadyToComputeShape()) { if (!isReadyToComputeShape()) {
return false; return false;
} }
btCollisionShape* newShape = computeNewShape(); const btCollisionShape* newShape = computeNewShape();
if (!newShape) { if (!newShape) {
qCDebug(physics) << "Warning: failed to generate new shape!"; qCDebug(physics) << "Warning: failed to generate new shape!";
// failed to generate new shape! --> keep old shape and remove shape-change flag // 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 // and clear the reference we just created
getShapeManager()->releaseShape(_shape); getShapeManager()->releaseShape(_shape);
} else { } else {
_body->setCollisionShape(newShape); _body->setCollisionShape(const_cast<btCollisionShape*>(newShape));
setShape(newShape); setShape(newShape);
updateCCDConfiguration(); updateCCDConfiguration();
} }

View file

@ -78,7 +78,7 @@ public:
static void setShapeManager(ShapeManager* manager); static void setShapeManager(ShapeManager* manager);
static ShapeManager* getShapeManager(); static ShapeManager* getShapeManager();
ObjectMotionState(btCollisionShape* shape); ObjectMotionState(const btCollisionShape* shape);
~ObjectMotionState(); ~ObjectMotionState();
virtual void handleEasyChanges(uint32_t& flags); virtual void handleEasyChanges(uint32_t& flags);
@ -110,7 +110,7 @@ public:
virtual PhysicsMotionType computePhysicsMotionType() const = 0; virtual PhysicsMotionType computePhysicsMotionType() const = 0;
btCollisionShape* getShape() const { return _shape; } const btCollisionShape* getShape() const { return _shape; }
btRigidBody* getRigidBody() const { return _body; } btRigidBody* getRigidBody() const { return _body; }
virtual bool isMoving() const = 0; virtual bool isMoving() const = 0;
@ -150,17 +150,17 @@ public:
protected: protected:
virtual bool isReadyToComputeShape() const = 0; virtual bool isReadyToComputeShape() const = 0;
virtual btCollisionShape* computeNewShape() = 0; virtual const btCollisionShape* computeNewShape() = 0;
virtual void setMotionType(PhysicsMotionType motionType); virtual void setMotionType(PhysicsMotionType motionType);
void updateCCDConfiguration(); void updateCCDConfiguration();
void setRigidBody(btRigidBody* body); void setRigidBody(btRigidBody* body);
virtual void setShape(btCollisionShape* shape); virtual void setShape(const btCollisionShape* shape);
MotionStateType _type = MOTIONSTATE_TYPE_INVALID; // type of MotionState MotionStateType _type = MOTIONSTATE_TYPE_INVALID; // type of MotionState
PhysicsMotionType _motionType; // type of motion: KINEMATIC, DYNAMIC, or STATIC PhysicsMotionType _motionType; // type of motion: KINEMATIC, DYNAMIC, or STATIC
btCollisionShape* _shape; const btCollisionShape* _shape;
btRigidBody* _body; btRigidBody* _body;
float _mass; float _mass;