From 75b5635d2fbf9c0fad8ee4a041061deeefc284d3 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 17 Jan 2018 13:51:07 -0800 Subject: [PATCH] less word salad --- libraries/entities/src/EntityItem.cpp | 47 ------------------ libraries/entities/src/EntityItem.h | 7 --- libraries/physics/src/EntityMotionState.cpp | 53 ++++++++++++++++++--- libraries/physics/src/EntityMotionState.h | 1 + 4 files changed, 47 insertions(+), 61 deletions(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 8ea1de7666..fe5213baa8 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1610,37 +1610,6 @@ void EntityItem::setPosition(const glm::vec3& value) { } } -void EntityItem::setWorldTransformAndVelocitiesUnlessDirtyFlags( - const glm::vec3& position, - const glm::quat& orientation, - const glm::vec3& linearVelocity, - const glm::vec3& angularVelocity) { - // only ever call this for harvesting results of physics simulation - // if a dirty bit is set then an update arrived (via script or network) overriding the physics simulation - uint32_t flags = _dirtyFlags & (Simulation::DIRTY_TRANSFORM | Simulation::DIRTY_VELOCITIES); - if (!flags) { - // flags are clear - setWorldTransform(position, orientation); - setWorldVelocity(linearVelocity); - setWorldAngularVelocity(angularVelocity); - setLastSimulated(usecTimestampNow()); - } else { - // only set properties NOT flagged - if (!(flags & Simulation::DIRTY_TRANSFORM)) { - setWorldTransform(position, orientation); - } - if (!(flags & Simulation::DIRTY_LINEAR_VELOCITY)) { - setWorldVelocity(linearVelocity); - } - if (!(flags & Simulation::DIRTY_ANGULAR_VELOCITY)) { - setWorldAngularVelocity(angularVelocity); - } - if (flags != (Simulation::DIRTY_TRANSFORM | Simulation::DIRTY_VELOCITIES)) { - setLastSimulated(usecTimestampNow()); - } - } -} - void EntityItem::setParentID(const QUuid& value) { QUuid oldParentID = getParentID(); if (oldParentID != value) { @@ -1770,22 +1739,6 @@ void EntityItem::setVelocity(const glm::vec3& value) { } } -void EntityItem::zeroAllVelocitiesUnlessDirtyFlags() { - uint32_t flags = _dirtyFlags & (Simulation::DIRTY_TRANSFORM | Simulation::DIRTY_VELOCITIES); - if (!flags) { - setWorldVelocity(glm::vec3(0.0f)); - setWorldAngularVelocity(glm::vec3(0.0f)); - } else { - if (!(flags & Simulation::DIRTY_LINEAR_VELOCITY)) { - setWorldVelocity(glm::vec3(0.0f)); - } - if (!(flags & Simulation::DIRTY_ANGULAR_VELOCITY)) { - setWorldAngularVelocity(glm::vec3(0.0f)); - } - } - _acceleration = glm::vec3(0.0f); -} - void EntityItem::setDamping(float value) { auto clampedDamping = glm::clamp(value, 0.0f, 1.0f); withWriteLock([&] { diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index db2e7f7641..f9559a375b 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -355,7 +355,6 @@ public: void setRotation(glm::quat orientation); void setVelocity(const glm::vec3& velocity); - void zeroAllVelocitiesUnlessDirtyFlags(); uint32_t getDirtyFlags() const; void markDirtyFlags(uint32_t mask); @@ -370,12 +369,6 @@ public: void setPhysicsInfo(void* data) { _physicsInfo = data; } - void setWorldTransformAndVelocitiesUnlessDirtyFlags( - const glm::vec3& position, - const glm::quat& orientation, - const glm::vec3& linearVelocity, - const glm::vec3& angularVelocity); - EntityTreeElementPointer getElement() const { return _element; } EntityTreePointer getTree() const; virtual SpatialParentTree* getParentTree() const override; diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index fe3e242a70..420da5a1e0 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -257,11 +257,31 @@ void EntityMotionState::setWorldTransform(const btTransform& worldTrans) { assert(entityTreeIsLocked()); measureBodyAcceleration(); - _entity->setWorldTransformAndVelocitiesUnlessDirtyFlags( - bulletToGLM(worldTrans.getOrigin()), - bulletToGLM(worldTrans.getRotation()), - getBodyLinearVelocity(), - getBodyAngularVelocity()); + // If transform or velocities are flagged as dirty it means a network or scripted change + // occured between the beginning and end of the stepSimulation() and we DON'T want to apply + // these physics simulation results. + uint32_t flags = _entity->getDirtyFlags() & (Simulation::DIRTY_TRANSFORM | Simulation::DIRTY_VELOCITIES); + if (!flags) { + // flags are clear + _entity->setWorldTransform(bulletToGLM(worldTrans.getOrigin()), bulletToGLM(worldTrans.getRotation())); + _entity->setWorldVelocity(getBodyLinearVelocity()); + _entity->setWorldAngularVelocity(getBodyAngularVelocity()); + _entity->setLastSimulated(usecTimestampNow()); + } else { + // only set properties NOT flagged + if (!(flags & Simulation::DIRTY_TRANSFORM)) { + _entity->setWorldTransform(bulletToGLM(worldTrans.getOrigin()), bulletToGLM(worldTrans.getRotation())); + } + if (!(flags & Simulation::DIRTY_LINEAR_VELOCITY)) { + _entity->setWorldVelocity(getBodyLinearVelocity()); + } + if (!(flags & Simulation::DIRTY_ANGULAR_VELOCITY)) { + _entity->setWorldAngularVelocity(getBodyAngularVelocity()); + } + if (flags != (Simulation::DIRTY_TRANSFORM | Simulation::DIRTY_VELOCITIES)) { + _entity->setLastSimulated(usecTimestampNow()); + } + } if (_entity->getSimulatorID().isNull()) { _loopsWithoutOwner++; @@ -517,7 +537,7 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, uint32_ if (!_body->isActive()) { // make sure all derivatives are zero - _entity->zeroAllVelocitiesUnlessDirtyFlags(); + zeroCleanObjectVelocities(); _numInactiveUpdates++; } else { glm::vec3 gravity = _entity->getGravity(); @@ -544,7 +564,7 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, uint32_ if (movingSlowly) { // velocities might not be zero, but we'll fake them as such, which will hopefully help convince // other simulating observers to deactivate their own copies - _entity->zeroAllVelocitiesUnlessDirtyFlags(); + zeroCleanObjectVelocities(); } } _numInactiveUpdates = 0; @@ -801,3 +821,22 @@ bool EntityMotionState::shouldBeLocallyOwned() const { void EntityMotionState::upgradeOutgoingPriority(uint8_t priority) { _outgoingPriority = glm::max(_outgoingPriority, priority); } + +void EntityMotionState::zeroCleanObjectVelocities() const { + // If transform or velocities are flagged as dirty it means a network or scripted change + // occured between the beginning and end of the stepSimulation() and we DON'T want to apply + // these physics simulation results. + uint32_t flags = _entity->getDirtyFlags() & (Simulation::DIRTY_TRANSFORM | Simulation::DIRTY_VELOCITIES); + if (!flags) { + _entity->setWorldVelocity(glm::vec3(0.0f)); + _entity->setWorldAngularVelocity(glm::vec3(0.0f)); + } else { + if (!(flags & Simulation::DIRTY_LINEAR_VELOCITY)) { + _entity->setWorldVelocity(glm::vec3(0.0f)); + } + if (!(flags & Simulation::DIRTY_ANGULAR_VELOCITY)) { + _entity->setWorldAngularVelocity(glm::vec3(0.0f)); + } + } + _entity->setAcceleration(glm::vec3(0.0f)); +} diff --git a/libraries/physics/src/EntityMotionState.h b/libraries/physics/src/EntityMotionState.h index ddfd7e1e4c..784273d600 100644 --- a/libraries/physics/src/EntityMotionState.h +++ b/libraries/physics/src/EntityMotionState.h @@ -87,6 +87,7 @@ public: protected: // changes _outgoingPriority only if priority is larger void upgradeOutgoingPriority(uint8_t priority); + void zeroCleanObjectVelocities() const; #ifdef WANT_DEBUG_ENTITY_TREE_LOCKS bool entityTreeIsLocked() const;