mirror of
https://github.com/overte-org/overte.git
synced 2025-04-21 09:24:00 +02:00
try harder to get children flags right when something moves. added worldToLocal methods
This commit is contained in:
parent
61269c3ce8
commit
f3c61a823e
5 changed files with 114 additions and 50 deletions
|
@ -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<bool(EntityItemPointer)> actor) {
|
||||
bool result = true;
|
||||
void EntityItem::forSelfAndEachChildEntity(std::function<void(EntityItemPointer)> actor) {
|
||||
QQueue<SpatiallyNestablePointer> toProcess;
|
||||
toProcess.enqueue(shared_from_this());
|
||||
|
||||
while (!toProcess.empty()) {
|
||||
EntityItemPointer entity = std::static_pointer_cast<EntityItem>(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
|
||||
|
|
|
@ -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<bool(EntityItemPointer)> actor);
|
||||
void forSelfAndEachChildEntity(std::function<void(EntityItemPointer)> actor);
|
||||
|
||||
static bool _sendPhysicsUpdates;
|
||||
EntityTypes::EntityType _type;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -34,11 +34,7 @@ public:
|
|||
|
||||
class SpatiallyNestable : public std::enable_shared_from_this<SpatiallyNestable> {
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue