diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 7c9f3139df..2dee8a9d44 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -79,8 +79,6 @@ EntityItem::EntityItem(const EntityItemID& entityItemID) : _simulated(false) { // explicitly set transform parts to set dirty flags used by batch rendering - setPosition(ENTITY_ITEM_DEFAULT_POSITION); - setRotation(ENTITY_ITEM_DEFAULT_ROTATION); setScale(ENTITY_ITEM_DEFAULT_DIMENSIONS); quint64 now = usecTimestampNow(); _lastSimulated = now; @@ -1309,24 +1307,19 @@ void EntityItem::computeShapeInfo(ShapeInfo& info) { info.setParams(getShapeType(), 0.5f * getDimensions()); } -bool EntityItem::forSelfAndEachChildEntity(std::function actor) { - bool result = true; +void EntityItem::forSelfAndEachChildEntity(std::function actor) { QQueue toProcess; toProcess.enqueue(shared_from_this()); while (!toProcess.empty()) { EntityItemPointer entity = std::static_pointer_cast(toProcess.dequeue()); - - result &= actor(entity); - + actor(entity); foreach (SpatiallyNestablePointer child, entity->getChildren()) { if (child && child->getNestableType() == NestableTypes::Entity) { toProcess.enqueue(child); } } } - - return result; } void EntityItem::updatePosition(const glm::vec3& value) { @@ -1336,16 +1329,76 @@ void EntityItem::updatePosition(const glm::vec3& value) { auto delta = glm::distance(getLocalPosition(), value); if (delta > IGNORE_POSITION_DELTA) { setLocalPosition(value); + if (delta > ACTIVATION_POSITION_DELTA) { + _dirtyFlags |= Simulation::DIRTY_PHYSICS_ACTIVATION; + } forSelfAndEachChildEntity([&](EntityItemPointer entity) { entity->_dirtyFlags |= Simulation::DIRTY_POSITION; - if (delta > ACTIVATION_POSITION_DELTA) { - entity->_dirtyFlags |= Simulation::DIRTY_PHYSICS_ACTIVATION; - } - return true; }); } } +void EntityItem::setTransform(const Transform& transform) { + SpatiallyNestable::setTransform(transform); + forSelfAndEachChildEntity([&](EntityItemPointer entity) { + entity->requiresRecalcBoxes(); + }); +} + +void EntityItem::setLocalTransform(const Transform& transform) { + SpatiallyNestable::setLocalTransform(transform); + forSelfAndEachChildEntity([&](EntityItemPointer entity) { + entity->requiresRecalcBoxes(); + }); +} + +void EntityItem::setPosition(const glm::vec3& position) { + SpatiallyNestable::setPosition(position); + forSelfAndEachChildEntity([&](EntityItemPointer entity) { + entity->requiresRecalcBoxes(); + }); +} + +void EntityItem::setLocalPosition(const glm::vec3& position) { + SpatiallyNestable::setLocalPosition(position); + forSelfAndEachChildEntity([&](EntityItemPointer entity) { + entity->requiresRecalcBoxes(); + }); +} + +void EntityItem::updateRotation(const glm::quat& rotation) { + if (shouldSuppressLocationEdits()) { + return; + } + if (getRotation() != rotation) { + auto alignmentDot = glm::abs(glm::dot(getRotation(), rotation)); + setRotation(rotation); + if (alignmentDot < ACTIVATION_ALIGNMENT_DOT) { + _dirtyFlags |= Simulation::DIRTY_PHYSICS_ACTIVATION; + } + forSelfAndEachChildEntity([&](EntityItemPointer entity) { + if (alignmentDot < IGNORE_ALIGNMENT_DOT) { + entity->_dirtyFlags |= Simulation::DIRTY_ROTATION; + } + entity->requiresRecalcBoxes(); + }); + } +} + +void EntityItem::setRotation(const glm::quat& orientation) { + SpatiallyNestable::setOrientation(orientation); + forSelfAndEachChildEntity([&](EntityItemPointer entity) { + entity->requiresRecalcBoxes(); + }); +} + +void EntityItem::setLocalRotation(const glm::quat& orientation) { + SpatiallyNestable::setLocalOrientation(orientation); + forSelfAndEachChildEntity([&](EntityItemPointer entity) { + entity->requiresRecalcBoxes(); + }); +} + void EntityItem::updateDimensions(const glm::vec3& value) { auto delta = glm::distance(getDimensions(), value); if (delta > IGNORE_DIMENSIONS_DELTA) { @@ -1357,26 +1410,6 @@ void EntityItem::updateDimensions(const glm::vec3& value) { } } -void EntityItem::updateRotation(const glm::quat& rotation) { - if (shouldSuppressLocationEdits()) { - return; - } - if (getRotation() != rotation) { - setRotation(rotation); - - auto alignmentDot = glm::abs(glm::dot(getRotation(), rotation)); - - forSelfAndEachChildEntity([&](EntityItemPointer entity) { - if (alignmentDot < IGNORE_ALIGNMENT_DOT) { - entity->_dirtyFlags |= Simulation::DIRTY_ROTATION; - } - if (alignmentDot < ACTIVATION_ALIGNMENT_DOT) { - entity->_dirtyFlags |= Simulation::DIRTY_PHYSICS_ACTIVATION; - } - return true; - }); - } -} void EntityItem::updateMass(float mass) { // Setting the mass actually changes the _density (at fixed volume), however diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 6e03e52679..ffcf24854d 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -183,15 +183,6 @@ public: const Transform getTransformToCenter() const; void setTranformToCenter(const Transform& transform); - virtual void setTransform(const Transform& transform) { SpatiallyNestable::setTransform(transform); requiresRecalcBoxes(); } - - /// Position in meters (-TREE_SCALE - TREE_SCALE) - virtual const glm::vec3& getPosition() const { return SpatiallyNestable::getPosition(); } - virtual void setPosition(const glm::vec3& value) { SpatiallyNestable::setPosition(value); requiresRecalcBoxes(); } - - virtual const glm::quat& getRotation() const { return SpatiallyNestable::getOrientation(); } - virtual void setRotation(const glm::quat& rotation) { SpatiallyNestable::setOrientation(rotation); requiresRecalcBoxes(); } - inline void requiresRecalcBoxes() { _recalcAABox = true; _recalcMinAACube = true; _recalcMaxAACube = true; } // Hyperlink related getters and setters @@ -330,6 +321,16 @@ public: /// return preferred shape type (actual physical shape may differ) virtual ShapeType getShapeType() const { return SHAPE_TYPE_NONE; } + virtual void setTransform(const Transform& transform); + virtual void setLocalTransform(const Transform& transform); + // virtual const glm::vec3& getPosition() const { return SpatiallyNestable::getPosition(); } + virtual const glm::quat& getRotation() const { return SpatiallyNestable::getOrientation(); } + + virtual void setPosition(const glm::vec3& position); + virtual void setLocalPosition(const glm::vec3& position); + virtual void setRotation(const glm::quat& orientation); + virtual void setLocalRotation(const glm::quat& orientation); + // updateFoo() methods to be used when changes need to be accumulated in the _dirtyFlags void updatePosition(const glm::vec3& value); void updateDimensions(const glm::vec3& value); @@ -396,7 +397,7 @@ protected: const QByteArray getActionDataInternal() const; void setActionDataInternal(QByteArray actionData); - bool forSelfAndEachChildEntity(std::function actor); + void forSelfAndEachChildEntity(std::function actor); static bool _sendPhysicsUpdates; EntityTypes::EntityType _type; diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 6832b8daff..98d0bb245e 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -445,8 +445,8 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, const Q EntityItemProperties properties; // explicitly set the properties that changed so that they will be packed - properties.setPosition(_serverPosition); - properties.setRotation(_serverRotation); + properties.setPosition(_entity->worldToLocal(_serverPosition)); + properties.setRotation(_entity->worldToLocal(_serverRotation)); properties.setVelocity(_serverVelocity); properties.setAcceleration(_serverAcceleration); properties.setAngularVelocity(_serverAngularVelocity); diff --git a/libraries/shared/src/SpatiallyNestable.cpp b/libraries/shared/src/SpatiallyNestable.cpp index a813a37bfe..300d9a5728 100644 --- a/libraries/shared/src/SpatiallyNestable.cpp +++ b/libraries/shared/src/SpatiallyNestable.cpp @@ -14,6 +14,16 @@ // TODO -- make use of parent joint index + +SpatiallyNestable::SpatiallyNestable(NestableTypes::NestableType nestableType, QUuid id) : + _nestableType(nestableType), + _id(id), + _transform() { + // set flags in _transform + _transform.setTranslation(glm::vec3(0.0f)); + _transform.setRotation(glm::quat()); +} + Transform SpatiallyNestable::getParentTransform() const { Transform result; SpatiallyNestablePointer parent = getParentPointer(); @@ -76,6 +86,26 @@ void SpatiallyNestable::setParentID(const QUuid& parentID) { _parentKnowsMe = false; } +glm::vec3 SpatiallyNestable::worldToLocal(const glm::vec3& position) { + Transform parentTransform = getParentTransform(); + Transform myWorldTransform; + Transform::mult(myWorldTransform, parentTransform, _transform); + myWorldTransform.setTranslation(position); + Transform result; + Transform::inverseMult(result, parentTransform, myWorldTransform); + return result.getTranslation(); +} + +glm::quat SpatiallyNestable::worldToLocal(const glm::quat& orientation) { + Transform parentTransform = getParentTransform(); + Transform myWorldTransform; + Transform::mult(myWorldTransform, parentTransform, _transform); + myWorldTransform.setRotation(orientation); + Transform result; + Transform::inverseMult(result, parentTransform, myWorldTransform); + return result.getRotation(); +} + const glm::vec3& SpatiallyNestable::getPosition() const { Transform parentTransformDescaled = getParentTransform(); glm::mat4 parentMat; @@ -131,6 +161,7 @@ const Transform& SpatiallyNestable::getLocalTransform() const { } void SpatiallyNestable::setLocalTransform(const Transform& transform) { + _transform = transform; } const glm::vec3& SpatiallyNestable::getLocalPosition() const { diff --git a/libraries/shared/src/SpatiallyNestable.h b/libraries/shared/src/SpatiallyNestable.h index e0ee5bdb74..986a282891 100644 --- a/libraries/shared/src/SpatiallyNestable.h +++ b/libraries/shared/src/SpatiallyNestable.h @@ -34,11 +34,7 @@ public: class SpatiallyNestable : public std::enable_shared_from_this { public: - // SpatiallyNestable() : _transform() { } // XXX get rid of this one? - SpatiallyNestable(NestableTypes::NestableType nestableType, QUuid id) : - _nestableType(nestableType), - _id(id), - _transform() { } + SpatiallyNestable(NestableTypes::NestableType nestableType, QUuid id); virtual ~SpatiallyNestable() { } virtual const QUuid& getID() const { return _id; } @@ -50,6 +46,9 @@ public: virtual quint16 getParentJointIndex() const { return _parentJointIndex; } virtual void setParentJointIndex(quint16 parentJointIndex) { _parentJointIndex = parentJointIndex; } + glm::vec3 worldToLocal(const glm::vec3& position); + glm::quat worldToLocal(const glm::quat& orientation); + // world frame virtual const Transform& getTransform() const; virtual void setTransform(const Transform& transform);