diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 1d8e8190d1..9fb6bdfe26 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -8,7 +8,7 @@ // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// TODO: make _incomingEntityChanges to be list of MotionState*'s. +// TODO DONE: make _incomingChanges to be list of MotionState*'s. // TODO: make MotionState able to clear incoming flags // TODO: make MotionState::setWorldTransform() put itself on _incomingChanges list // TODO: give PhysicsEngine instance an _entityPacketSender @@ -38,8 +38,6 @@ PhysicsEngine::~PhysicsEngine() { // begin EntitySimulation overrides void PhysicsEngine::updateEntitiesInternal(const quint64& now) { - // relay outgoing changes: from physics engine to EntityItem's - QSet::iterator stateItr = _outgoingPhysics.begin(); uint32_t frame = getFrameCount(); float subStepRemainder = getSubStepRemainder(); @@ -84,7 +82,12 @@ void PhysicsEngine::removeEntityInternal(EntityItem* entity) { void PhysicsEngine::entityChangedInternal(EntityItem* entity) { // queue incoming changes: from external sources (script, EntityServer, etc) to physics engine - _incomingEntityChanges.insert(entity); + assert(entity); + void* physicsInfo = entity->getPhysicsInfo(); + if (physicsInfo) { + ObjectMotionState* motionState = static_cast(physicsInfo); + _incomingChanges.insert(motionState); + } } void PhysicsEngine::clearEntitiesInternal() { @@ -94,29 +97,34 @@ void PhysicsEngine::clearEntitiesInternal() { removeObject(*stateItr); } _entityMotionStates.clear(); - _incomingEntityChanges.clear(); + _incomingChanges.clear(); } // end EntitySimulation overrides void PhysicsEngine::relayIncomingChangesToSimulation() { // process incoming changes - QSet::iterator itemItr = _incomingEntityChanges.begin(); - while (itemItr != _incomingEntityChanges.end()) { - EntityItem* entity = *itemItr; - void* physicsInfo = entity->getPhysicsInfo(); - if (physicsInfo) { - ObjectMotionState* motionState = static_cast(physicsInfo); - uint32_t flags = entity->getDirtyFlags(); - if (flags & DIRTY_PHYSICS_FLAGS) { - updateObject(motionState, flags); - // this incoming change will override any outgoing changes to the same parameters - motionState->clearOutgoingDirtyFlags(flags); + 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; + if (body) { + if (flags & HARD_DIRTY_PHYSICS_FLAGS) { + // a HARD update requires the body be pulled out of physics engine, changed, then reinserted + // but it also handles all EASY changes + updateObjectHard(body, motionState, flags); + } else if (flags) { + // an EASY update does NOT require that the body be pulled out of physics engine + updateObjectEasy(body, motionState, flags); } } - entity->clearDirtyFlags(); - ++itemItr; - } - _incomingEntityChanges.clear(); + + motionState->clearIncomingDirtyFlags(flags); + ++stateItr; + } + _incomingChanges.clear(); } // virtual @@ -295,22 +303,6 @@ bool PhysicsEngine::removeObject(ObjectMotionState* motionState) { return false; } -bool PhysicsEngine::updateObject(ObjectMotionState* motionState, uint32_t flags) { - btRigidBody* body = motionState->_body; - if (!body) { - return false; - } - - if (flags & HARD_DIRTY_PHYSICS_FLAGS) { - // a hard update requires the body be pulled out of physics engine, changed, then reinserted - updateObjectHard(body, motionState, flags); - } else if (flags & EASY_DIRTY_PHYSICS_FLAGS) { - // an easy update does not require that the body be pulled out of physics engine - updateObjectEasy(body, motionState, flags); - } - return true; -} - // private void PhysicsEngine::updateObjectHard(btRigidBody* body, ObjectMotionState* motionState, uint32_t flags) { MotionType newType = motionState->computeMotionType(); diff --git a/libraries/physics/src/PhysicsEngine.h b/libraries/physics/src/PhysicsEngine.h index 99a23a9b65..c0145cca0f 100644 --- a/libraries/physics/src/PhysicsEngine.h +++ b/libraries/physics/src/PhysicsEngine.h @@ -73,11 +73,6 @@ public: /// \return true if Object removed bool removeObject(ObjectMotionState* motionState); - /// \param motionState pointer to Object's MotionState - /// \param flags set of bits indicating what categories of properties need to be updated - /// \return true if entity updated - bool updateObject(ObjectMotionState* motionState, uint32_t flags); - /// process queue of changed from external sources void relayIncomingChangesToSimulation(); @@ -111,7 +106,7 @@ private: // EntitySimulation stuff QSet _entityMotionStates; // all entities that we track - QSet _incomingEntityChanges; // entities with pending physics changes by script or packet + QSet _incomingChanges; // entities with pending physics changes by script or packet QSet _outgoingPhysics; // MotionStates with pending transform changes from physics simulation EntityEditPacketSender* _entityPacketSender;