From ca86ce59c27e5753de58907f430f06eb9bc2ce48 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 30 Aug 2017 17:44:42 -0700 Subject: [PATCH 1/3] fix a bug that cuased dynamic children to wink in and out and then vanish --- libraries/physics/src/EntityMotionState.cpp | 26 ++++++++++++--------- libraries/physics/src/EntityMotionState.h | 1 + libraries/shared/src/SpatiallyNestable.cpp | 2 +- libraries/shared/src/SpatiallyNestable.h | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index f02dcee8f6..d370398ff2 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(); @@ -100,17 +101,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) { + bool success; + 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 +342,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; From 3f0a95d8db2ceb08185a987c797a4862f2e08205 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 30 Aug 2017 17:53:20 -0700 Subject: [PATCH 2/3] remove unneeded line --- libraries/physics/src/EntityMotionState.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index d370398ff2..33de98b158 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -102,7 +102,6 @@ void EntityMotionState::updateServerPhysicsVariables() { void EntityMotionState::handleDeactivation() { // copy _server data to entity if (_serverVariablesSet) { - bool success; Transform localTransform = _entity->getLocalTransform(); localTransform.setTranslation(_serverPosition); localTransform.setRotation(_serverRotation); From 41b51804dc3cad47de9bfe8d1e406272ca96a964 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 30 Aug 2017 17:53:55 -0700 Subject: [PATCH 3/3] fix comment --- libraries/physics/src/EntityMotionState.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 33de98b158..7c84017758 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -100,8 +100,8 @@ void EntityMotionState::updateServerPhysicsVariables() { } void EntityMotionState::handleDeactivation() { - // copy _server data to entity if (_serverVariablesSet) { + // copy _server data to entity Transform localTransform = _entity->getLocalTransform(); localTransform.setTranslation(_serverPosition); localTransform.setRotation(_serverRotation);