diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 5fc5f7688b..2d01d49639 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -52,25 +52,22 @@ void EntityMotionState::getWorldTransform (btTransform &worldTrans) const { // This callback is invoked by the physics simulation at the end of each simulation frame... // iff the corresponding RigidBody is DYNAMIC and has moved. void EntityMotionState::setWorldTransform (const btTransform &worldTrans) { - uint32_t dirytFlags = _entity->getDirtyFlags(); - if (! (dirytFlags & EntityItem::DIRTY_POSITION)) { - glm::vec3 pos; - bulletToGLM(worldTrans.getOrigin(), pos); - _entity->setPositionInMeters(pos + ObjectMotionState::getWorldOffset()); - - glm::quat rot; - bulletToGLM(worldTrans.getRotation(), rot); - _entity->setRotation(rot); - } + glm::vec3 pos; + bulletToGLM(worldTrans.getOrigin(), pos); + _entity->setPositionInMeters(pos + ObjectMotionState::getWorldOffset()); - if (! (dirytFlags & EntityItem::DIRTY_VELOCITY)) { - glm::vec3 v; - getVelocity(v); - _entity->setVelocityInMeters(v); - getAngularVelocity(v); - _entity->setAngularVelocity(v); - } - _outgoingDirtyFlags = OUTGOING_DIRTY_PHYSICS_FLAGS; + glm::quat rot; + bulletToGLM(worldTrans.getRotation(), rot); + _entity->setRotation(rot); + + glm::vec3 v; + getVelocity(v); + _entity->setVelocityInMeters(v); + + getAngularVelocity(v); + _entity->setAngularVelocity(v); + + _outgoingPacketFlags = DIRTY_PHYSICS_FLAGS; } #endif // USE_BULLET_PHYSICS diff --git a/libraries/physics/src/EntityMotionState.h b/libraries/physics/src/EntityMotionState.h index 562efde87e..3391ed3020 100644 --- a/libraries/physics/src/EntityMotionState.h +++ b/libraries/physics/src/EntityMotionState.h @@ -56,7 +56,6 @@ public: uint32_t getIncomingDirtyFlags() const { return _entity->getDirtyFlags(); } void clearIncomingDirtyFlags(uint32_t flags) { _entity->clearDirtyFlags(flags); } - void clearConflictingDirtyFlags() { _outgoingDirtyFlags &= ~_entity->getDirtyFlags(); } protected: EntityItem* _entity; diff --git a/libraries/physics/src/ObjectMotionState.cpp b/libraries/physics/src/ObjectMotionState.cpp index b80a996d0c..e2fd2b7a66 100644 --- a/libraries/physics/src/ObjectMotionState.cpp +++ b/libraries/physics/src/ObjectMotionState.cpp @@ -52,7 +52,7 @@ ObjectMotionState::ObjectMotionState() : _body(NULL), _sentMoving(false), _weKnowRecipientHasReceivedNotMoving(false), - _outgoingDirtyFlags(0), + _outgoingPacketFlags(0), _sentFrame(0), _sentPosition(0.0f), _sentRotation(), diff --git a/libraries/physics/src/ObjectMotionState.h b/libraries/physics/src/ObjectMotionState.h index 1341cb6168..ec9900a6be 100644 --- a/libraries/physics/src/ObjectMotionState.h +++ b/libraries/physics/src/ObjectMotionState.h @@ -81,8 +81,7 @@ public: virtual uint32_t getIncomingDirtyFlags() const = 0; virtual void clearIncomingDirtyFlags(uint32_t flags) = 0; - void clearOutgoingDirtyFlags(uint32_t flags) { _outgoingDirtyFlags &= ~flags; } - virtual void clearConflictingDirtyFlags() = 0; + void clearOutgoingPacketFlags(uint32_t flags) { _outgoingPacketFlags &= ~flags; } bool isAtRest() const { return !(_body->isActive()) && _weKnowRecipientHasReceivedNotMoving; } virtual bool shouldSendUpdate(uint32_t simulationFrame, float subStepRemainder) const; @@ -105,7 +104,7 @@ protected: bool _sentMoving; // true if last update was moving bool _weKnowRecipientHasReceivedNotMoving; - uint32_t _outgoingDirtyFlags; + uint32_t _outgoingPacketFlags; uint32_t _sentFrame; glm::vec3 _sentPosition; glm::quat _sentRotation;; diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 9fb6bdfe26..9d3f8d80b7 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -9,8 +9,9 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // // TODO DONE: make _incomingChanges to be list of MotionState*'s. -// TODO: make MotionState able to clear incoming flags +// TODO DONE: make MotionState able to clear incoming flags // TODO: make MotionState::setWorldTransform() put itself on _incomingChanges list +// TODO: make sure incoming and outgoing pipelines are connected // TODO: give PhysicsEngine instance an _entityPacketSender // TODO: provide some sort of "reliable" send for "stopped" update @@ -38,16 +39,16 @@ PhysicsEngine::~PhysicsEngine() { // begin EntitySimulation overrides void PhysicsEngine::updateEntitiesInternal(const quint64& now) { - QSet::iterator stateItr = _outgoingPhysics.begin(); + QSet::iterator stateItr = _outgoingPackets.begin(); uint32_t frame = getFrameCount(); float subStepRemainder = getSubStepRemainder(); - while (stateItr != _outgoingPhysics.end()) { + while (stateItr != _outgoingPackets.end()) { ObjectMotionState* state = *stateItr; if (state->shouldSendUpdate(frame, subStepRemainder)) { state->sendUpdate(_entityPacketSender); ++stateItr; } else if (state->isAtRest()) { - stateItr = _outgoingPhysics.erase(stateItr); + stateItr = _outgoingPackets.erase(stateItr); } else { ++stateItr; } @@ -106,7 +107,6 @@ void PhysicsEngine::relayIncomingChangesToSimulation() { QSet::iterator stateItr = _incomingChanges.begin(); while (stateItr != _incomingChanges.end()) { ObjectMotionState* motionState = *stateItr; - motionState->clearConflictingDirtyFlags(); uint32_t flags = motionState->getIncomingDirtyFlags() & DIRTY_PHYSICS_FLAGS; btRigidBody* body = motionState->_body; @@ -121,7 +121,13 @@ void PhysicsEngine::relayIncomingChangesToSimulation() { } } - motionState->clearIncomingDirtyFlags(flags); + // NOTE: the order of operations is: + // (1) relayIncomingChanges() + // (2) step simulation + // (3) send outgoing packets + // This means incoming changes here (step (1)) should trump corresponding outgoing changes + motionState->clearOutgoingPacketFlags(flags); // trump + motionState->clearIncomingDirtyFlags(flags); // processed ++stateItr; } _incomingChanges.clear(); diff --git a/libraries/physics/src/PhysicsEngine.h b/libraries/physics/src/PhysicsEngine.h index c0145cca0f..9d02486ec1 100644 --- a/libraries/physics/src/PhysicsEngine.h +++ b/libraries/physics/src/PhysicsEngine.h @@ -107,7 +107,7 @@ private: // EntitySimulation stuff QSet _entityMotionStates; // all entities that we track QSet _incomingChanges; // entities with pending physics changes by script or packet - QSet _outgoingPhysics; // MotionStates with pending transform changes from physics simulation + QSet _outgoingPackets; // MotionStates with pending changes that need to be sent over wire EntityEditPacketSender* _entityPacketSender;