From 59abc1d5b5ac2363990f6d74da7e2e9a36c23160 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 30 Mar 2018 17:04:31 -0700 Subject: [PATCH] DRY sendBid() and sendUpdate() --- libraries/physics/src/EntityMotionState.cpp | 47 ++++----------------- libraries/physics/src/EntityMotionState.h | 1 + 2 files changed, 10 insertions(+), 38 deletions(-) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 42c1213c41..3ca69080ec 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -453,10 +453,7 @@ bool EntityMotionState::shouldSendUpdate(uint32_t simulationStep) { return remoteSimulationOutOfSync(simulationStep); } -void EntityMotionState::sendBid(OctreeEditPacketSender* packetSender, uint32_t step) { - DETAILED_PROFILE_RANGE(simulation_physics, "Bid"); - assert(entityTreeIsLocked()); - +void EntityMotionState::updateSendVelocities() { if (!_body->isActive()) { // make sure all derivatives are zero clearObjectVelocities(); @@ -491,6 +488,13 @@ void EntityMotionState::sendBid(OctreeEditPacketSender* packetSender, uint32_t s } _numInactiveUpdates = 0; } +} + +void EntityMotionState::sendBid(OctreeEditPacketSender* packetSender, uint32_t step) { + DETAILED_PROFILE_RANGE(simulation_physics, "Bid"); + assert(entityTreeIsLocked()); + + updateSendVelocities(); EntityItemProperties properties; Transform localTransform; @@ -552,40 +556,7 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, uint32_ assert(entityTreeIsLocked()); assert(isLocallyOwned()); - if (!_body->isActive()) { - // make sure all derivatives are zero - clearObjectVelocities(); - _numInactiveUpdates++; - } else { - glm::vec3 gravity = _entity->getGravity(); - - // if this entity has been accelerated at close to gravity for a certain number of simulation-steps, let - // the entity server's estimates include gravity. - const uint8_t STEPS_TO_DECIDE_BALLISTIC = 4; - if (_accelerationNearlyGravityCount >= STEPS_TO_DECIDE_BALLISTIC) { - _entity->setAcceleration(gravity); - } else { - _entity->setAcceleration(Vectors::ZERO); - } - - if (!_body->isStaticOrKinematicObject()) { - const float DYNAMIC_LINEAR_VELOCITY_THRESHOLD = 0.05f; // 5 cm/sec - const float DYNAMIC_ANGULAR_VELOCITY_THRESHOLD = 0.087266f; // ~5 deg/sec - - bool movingSlowlyLinear = - glm::length2(_entity->getWorldVelocity()) < (DYNAMIC_LINEAR_VELOCITY_THRESHOLD * DYNAMIC_LINEAR_VELOCITY_THRESHOLD); - bool movingSlowlyAngular = glm::length2(_entity->getWorldAngularVelocity()) < - (DYNAMIC_ANGULAR_VELOCITY_THRESHOLD * DYNAMIC_ANGULAR_VELOCITY_THRESHOLD); - bool movingSlowly = movingSlowlyLinear && movingSlowlyAngular && _entity->getAcceleration() == Vectors::ZERO; - - 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 - clearObjectVelocities(); - } - } - _numInactiveUpdates = 0; - } + updateSendVelocities(); // remember _serverFoo data for local prediction of server state Transform localTransform; diff --git a/libraries/physics/src/EntityMotionState.h b/libraries/physics/src/EntityMotionState.h index 978bafd539..e94b84572c 100644 --- a/libraries/physics/src/EntityMotionState.h +++ b/libraries/physics/src/EntityMotionState.h @@ -92,6 +92,7 @@ public: OwnershipState getOwnershipState() const { return _ownershipState; } protected: + void updateSendVelocities(); uint64_t getNextBidExpiry() const { return _nextBidExpiry; } void initForBid(); void initForOwned();