Merge pull request #6854 from sethalves/access-avatar-parent-id-from-js

Access avatar parent information from js
This commit is contained in:
Andrew Meadows 2016-01-21 09:19:40 -08:00
commit 58a7528b68
10 changed files with 90 additions and 27 deletions

View file

@ -1230,3 +1230,33 @@ void Avatar::updatePalms() {
_leftPalmPositionCache.set(getUncachedLeftPalmPosition());
_rightPalmPositionCache.set(getUncachedRightPalmPosition());
}
void Avatar::setParentID(const QUuid& parentID) {
if (!isMyAvatar()) {
return;
}
bool success;
Transform beforeChangeTransform = getTransform(success);
SpatiallyNestable::setParentID(parentID);
if (success) {
setTransform(beforeChangeTransform, success);
if (!success) {
qDebug() << "Avatar::setParentID failed to reset avatar's location.";
}
}
}
void Avatar::setParentJointIndex(quint16 parentJointIndex) {
if (!isMyAvatar()) {
return;
}
bool success;
Transform beforeChangeTransform = getTransform(success);
SpatiallyNestable::setParentJointIndex(parentJointIndex);
if (success) {
setTransform(beforeChangeTransform, success);
if (!success) {
qDebug() << "Avatar::setParentJointIndex failed to reset avatar's location.";
}
}
}

View file

@ -167,6 +167,12 @@ public:
using SpatiallyNestable::setOrientation;
virtual void setOrientation(const glm::quat& orientation) override;
// these call through to the SpatiallyNestable versions, but they are here to expose these to javascript.
Q_INVOKABLE virtual const QUuid getParentID() const { return SpatiallyNestable::getParentID(); }
Q_INVOKABLE virtual void setParentID(const QUuid& parentID);
Q_INVOKABLE virtual quint16 getParentJointIndex() const { return SpatiallyNestable::getParentJointIndex(); }
Q_INVOKABLE virtual void setParentJointIndex(quint16 parentJointIndex);
// NOT thread safe, must be called on main thread.
glm::vec3 getUncachedLeftPalmPosition() const;
glm::quat getUncachedLeftPalmRotation() const;

View file

@ -134,7 +134,7 @@ glm::vec3 AvatarMotionState::getObjectGravity() const {
}
// virtual
const QUuid& AvatarMotionState::getObjectID() const {
const QUuid AvatarMotionState::getObjectID() const {
return _avatar->getSessionUUID();
}

View file

@ -53,7 +53,7 @@ public:
virtual glm::vec3 getObjectAngularVelocity() const override;
virtual glm::vec3 getObjectGravity() const override;
virtual const QUuid& getObjectID() const override;
virtual const QUuid getObjectID() const override;
virtual QUuid getSimulatorID() const override;

View file

@ -216,15 +216,14 @@ QByteArray AvatarData::toByteArray(bool cullSmallChanges, bool sendAll) {
setAtBit(bitItems, IS_EYE_TRACKER_CONNECTED);
}
// referential state
bool success;
SpatiallyNestablePointer parent = getParentPointer(success);
if (parent && success) {
QUuid parentID = getParentID();
if (!parentID.isNull()) {
setAtBit(bitItems, HAS_REFERENTIAL);
}
*destinationBuffer++ = bitItems;
if (parent) {
QByteArray referentialAsBytes = parent->getID().toRfc4122();
if (!parentID.isNull()) {
QByteArray referentialAsBytes = parentID.toRfc4122();
memcpy(destinationBuffer, referentialAsBytes.data(), referentialAsBytes.size());
destinationBuffer += referentialAsBytes.size();
memcpy(destinationBuffer, &_parentJointIndex, sizeof(_parentJointIndex));
@ -492,10 +491,12 @@ int AvatarData::parseDataFromBuffer(const QByteArray& buffer) {
// avatar's SkeletonModel might fall into the CPU expensive part of Model::updateClusterMatrices() when otherwise it
// would not have required it. However, we know we can update many simultaneously animating avatars, and most
// avatars will be moving constantly anyway, so I don't think we need to worry.
if (getBodyYaw() != yaw || getBodyPitch() != pitch || getBodyRoll() != roll) {
glm::quat currentOrientation = getLocalOrientation();
glm::vec3 newEulerAngles(pitch, yaw, roll);
glm::quat newOrientation = glm::quat(glm::radians(newEulerAngles));
if (currentOrientation != newOrientation) {
_hasNewJointRotations = true;
glm::vec3 eulerAngles(pitch, yaw, roll);
setLocalOrientation(glm::quat(glm::radians(eulerAngles)));
setLocalOrientation(newOrientation);
}
// scale

View file

@ -177,7 +177,7 @@ public:
virtual bool isMyAvatar() const { return false; }
const QUuid& getSessionUUID() const { return getID(); }
const QUuid getSessionUUID() const { return getID(); }
glm::vec3 getHandPosition() const;
void setHandPosition(const glm::vec3& handPosition);

View file

@ -67,7 +67,7 @@ public:
virtual glm::vec3 getObjectGravity() const override { return _entity->getGravity(); }
virtual glm::vec3 getObjectLinearVelocityChange() const override;
virtual const QUuid& getObjectID() const override { return _entity->getID(); }
virtual const QUuid getObjectID() const override { return _entity->getID(); }
virtual quint8 getSimulationPriority() const override;
virtual QUuid getSimulatorID() const override;

View file

@ -128,7 +128,7 @@ public:
virtual glm::vec3 getObjectAngularVelocity() const = 0;
virtual glm::vec3 getObjectGravity() const = 0;
virtual const QUuid& getObjectID() const = 0;
virtual const QUuid getObjectID() const = 0;
virtual quint8 getSimulationPriority() const { return 0; }
virtual QUuid getSimulatorID() const = 0;

View file

@ -25,6 +25,37 @@ SpatiallyNestable::SpatiallyNestable(NestableType nestableType, QUuid id) :
_transform.setRotation(glm::quat());
}
const QUuid SpatiallyNestable::getID() const {
QUuid result;
_idLock.withReadLock([&] {
result = _id;
});
return result;
}
void SpatiallyNestable::setID(const QUuid& id) {
_idLock.withWriteLock([&] {
_id = id;
});
}
const QUuid SpatiallyNestable::getParentID() const {
QUuid result;
_idLock.withReadLock([&] {
result = _parentID;
});
return result;
}
void SpatiallyNestable::setParentID(const QUuid& parentID) {
_idLock.withWriteLock([&] {
if (_parentID != parentID) {
_parentID = parentID;
_parentKnowsMe = false;
}
});
}
Transform SpatiallyNestable::getParentTransform(bool& success) const {
Transform result;
SpatiallyNestablePointer parent = getParentPointer(success);
@ -40,14 +71,15 @@ Transform SpatiallyNestable::getParentTransform(bool& success) const {
SpatiallyNestablePointer SpatiallyNestable::getParentPointer(bool& success) const {
SpatiallyNestablePointer parent = _parent.lock();
QUuid parentID = getParentID(); // used for its locking
if (!parent && _parentID.isNull()) {
if (!parent && parentID.isNull()) {
// no parent
success = true;
return nullptr;
}
if (parent && parent->getID() == _parentID) {
if (parent && parent->getID() == parentID) {
// parent pointer is up-to-date
if (!_parentKnowsMe) {
parent->beParentOfChild(getThisPointer());
@ -72,7 +104,7 @@ SpatiallyNestablePointer SpatiallyNestable::getParentPointer(bool& success) cons
success = false;
return nullptr;
}
_parent = parentFinder->find(_parentID, success);
_parent = parentFinder->find(parentID, success);
if (!success) {
return nullptr;
}
@ -83,7 +115,7 @@ SpatiallyNestablePointer SpatiallyNestable::getParentPointer(bool& success) cons
_parentKnowsMe = true;
}
if (parent || _parentID.isNull()) {
if (parent || parentID.isNull()) {
success = true;
} else {
success = false;
@ -104,13 +136,6 @@ void SpatiallyNestable::forgetChild(SpatiallyNestablePointer newChild) const {
});
}
void SpatiallyNestable::setParentID(const QUuid& parentID) {
if (_parentID != parentID) {
_parentID = parentID;
_parentKnowsMe = false;
}
}
void SpatiallyNestable::setParentJointIndex(quint16 parentJointIndex) {
_parentJointIndex = parentJointIndex;
}

View file

@ -36,10 +36,10 @@ public:
SpatiallyNestable(NestableType nestableType, QUuid id);
virtual ~SpatiallyNestable() { }
virtual const QUuid& getID() const { return _id; }
virtual void setID(const QUuid& id) { _id = id; }
virtual const QUuid getID() const;
virtual void setID(const QUuid& id);
virtual QUuid getParentID() const { return _parentID; }
virtual const QUuid getParentID() const;
virtual void setParentID(const QUuid& parentID);
virtual quint16 getParentJointIndex() const { return _parentJointIndex; }
@ -145,6 +145,7 @@ protected:
private:
mutable ReadWriteLockable _transformLock;
mutable ReadWriteLockable _idLock;
Transform _transform; // this is to be combined with parent's world-transform to produce this' world-transform.
mutable bool _parentKnowsMe { false };
bool _isDead { false };