diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index f02dcee8f6..7c84017758 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -92,6 +92,7 @@ void EntityMotionState::updateServerPhysicsVariables() { Transform localTransform; _entity->getLocalTransformAndVelocities(localTransform, _serverVelocity, _serverAngularVelocity); + _serverVariablesSet = true; _serverPosition = localTransform.getTranslation(); _serverRotation = localTransform.getRotation(); _serverAcceleration = _entity->getAcceleration(); @@ -99,18 +100,19 @@ void EntityMotionState::updateServerPhysicsVariables() { } void EntityMotionState::handleDeactivation() { - // copy _server data to entity - bool success; - _entity->setPosition(_serverPosition, success, false); - _entity->setOrientation(_serverRotation, success, false); - _entity->setVelocity(ENTITY_ITEM_ZERO_VEC3); - _entity->setAngularVelocity(ENTITY_ITEM_ZERO_VEC3); - // and also to RigidBody - btTransform worldTrans; - worldTrans.setOrigin(glmToBullet(_serverPosition)); - worldTrans.setRotation(glmToBullet(_serverRotation)); - _body->setWorldTransform(worldTrans); - // no need to update velocities... should already be zero + if (_serverVariablesSet) { + // copy _server data to entity + Transform localTransform = _entity->getLocalTransform(); + localTransform.setTranslation(_serverPosition); + localTransform.setRotation(_serverRotation); + _entity->setLocalTransformAndVelocities(localTransform, ENTITY_ITEM_ZERO_VEC3, ENTITY_ITEM_ZERO_VEC3); + // and also to RigidBody + btTransform worldTrans; + worldTrans.setOrigin(glmToBullet(_entity->getPosition())); + worldTrans.setRotation(glmToBullet(_entity->getRotation())); + _body->setWorldTransform(worldTrans); + // no need to update velocities... should already be zero + } } // virtual @@ -339,6 +341,7 @@ bool EntityMotionState::remoteSimulationOutOfSync(uint32_t simulationStep) { // if we've never checked before, our _lastStep will be 0, and we need to initialize our state if (_lastStep == 0) { btTransform xform = _body->getWorldTransform(); + _serverVariablesSet = true; _serverPosition = worldToLocal.transform(bulletToGLM(xform.getOrigin())); _serverRotation = worldToLocal.getRotation() * bulletToGLM(xform.getRotation()); _serverVelocity = worldVelocityToLocal.transform(getBodyLinearVelocityGTSigma()); diff --git a/libraries/physics/src/EntityMotionState.h b/libraries/physics/src/EntityMotionState.h index 541ad7c93c..968f53700c 100644 --- a/libraries/physics/src/EntityMotionState.h +++ b/libraries/physics/src/EntityMotionState.h @@ -107,6 +107,7 @@ protected: // Meanwhile we also keep a raw EntityItem* for internal stuff where the pointer is guaranteed valid. EntityItem* _entity; + bool _serverVariablesSet { false }; glm::vec3 _serverPosition; // in simulation-frame (not world-frame) glm::quat _serverRotation; glm::vec3 _serverVelocity; diff --git a/libraries/shared/src/SpatiallyNestable.cpp b/libraries/shared/src/SpatiallyNestable.cpp index 0545fd6ffd..5b0095078a 100644 --- a/libraries/shared/src/SpatiallyNestable.cpp +++ b/libraries/shared/src/SpatiallyNestable.cpp @@ -731,7 +731,7 @@ void SpatiallyNestable::setScale(float value) { } } -const Transform SpatiallyNestable::getLocalTransform() const { +Transform SpatiallyNestable::getLocalTransform() const { Transform result; _transformLock.withReadLock([&] { result =_transform; diff --git a/libraries/shared/src/SpatiallyNestable.h b/libraries/shared/src/SpatiallyNestable.h index cf63f96a31..a1b31d555f 100644 --- a/libraries/shared/src/SpatiallyNestable.h +++ b/libraries/shared/src/SpatiallyNestable.h @@ -121,7 +121,7 @@ public: virtual glm::vec3 getScale(int jointIndex) const; // object's parent's frame - virtual const Transform getLocalTransform() const; + virtual Transform getLocalTransform() const; virtual void setLocalTransform(const Transform& transform); virtual glm::vec3 getLocalPosition() const;