Referentials do math using meters

This commit is contained in:
Andrew Meadows 2015-02-26 13:42:08 -08:00
parent 8d4ea143ce
commit 9ad88c3793
3 changed files with 25 additions and 33 deletions

View file

@ -21,7 +21,6 @@ ModelReferential::ModelReferential(Referential* referential, EntityTree* tree, A
{ {
_translation = referential->getTranslation(); _translation = referential->getTranslation();
_rotation = referential->getRotation(); _rotation = referential->getRotation();
_scale = referential->getScale();
unpackExtraData(reinterpret_cast<unsigned char*>(referential->getExtraData().data()), unpackExtraData(reinterpret_cast<unsigned char*>(referential->getExtraData().data()),
referential->getExtraData().size()); referential->getExtraData().size());
@ -32,7 +31,7 @@ ModelReferential::ModelReferential(Referential* referential, EntityTree* tree, A
const EntityItem* item = _tree->findEntityByID(_entityID); const EntityItem* item = _tree->findEntityByID(_entityID);
if (item != NULL) { if (item != NULL) {
_refScale = item->getLargestDimensionInDomainUnits(); _lastRefDimension = item->getDimensionsInMeters();
_refRotation = item->getRotation(); _refRotation = item->getRotation();
_refPosition = item->getPositionInMeters(); _refPosition = item->getPositionInMeters();
update(); update();
@ -51,14 +50,13 @@ ModelReferential::ModelReferential(const QUuid& entityID, EntityTree* tree, Avat
return; return;
} }
_refScale = item->getLargestDimensionInDomainUnits(); _lastRefDimension = item->getDimensionsInMeters();
_refRotation = item->getRotation(); _refRotation = item->getRotation();
_refPosition = item->getPositionInMeters(); _refPosition = item->getPositionInMeters();
glm::quat refInvRot = glm::inverse(_refRotation); glm::quat refInvRot = glm::inverse(_refRotation);
_scale = _avatar->getTargetScale() / _refScale;
_rotation = refInvRot * _avatar->getOrientation(); _rotation = refInvRot * _avatar->getOrientation();
_translation = refInvRot * (avatar->getPosition() - _refPosition) / _refScale; _translation = refInvRot * (avatar->getPosition() - _refPosition);
} }
void ModelReferential::update() { void ModelReferential::update() {
@ -68,9 +66,10 @@ void ModelReferential::update() {
} }
bool somethingChanged = false; bool somethingChanged = false;
if (item->getLargestDimensionInDomainUnits() != _refScale) { if (item->getDimensionsInMeters() != _lastRefDimension) {
_refScale = item->getLargestDimensionInDomainUnits(); glm::vec3 oldDimension = _lastRefDimension;
_avatar->setTargetScale(_refScale * _scale, true); _lastRefDimension = item->getDimensionsInMeters();
_translation *= _lastRefDimension / oldDimension;
somethingChanged = true; somethingChanged = true;
} }
if (item->getRotation() != _refRotation) { if (item->getRotation() != _refRotation) {
@ -78,9 +77,9 @@ void ModelReferential::update() {
_avatar->setOrientation(_refRotation * _rotation, true); _avatar->setOrientation(_refRotation * _rotation, true);
somethingChanged = true; somethingChanged = true;
} }
if (item->getPositionInDomainUnits() != _refPosition || somethingChanged) { if (item->getPositionInMeters() != _refPosition || somethingChanged) {
_refPosition = item->getPositionInDomainUnits(); _refPosition = item->getPositionInMeters();
_avatar->setPosition(_refPosition * (float)TREE_SCALE + _refRotation * (_translation * _refScale), true); _avatar->setPosition(_refPosition + _refRotation * _translation, true);
} }
} }
@ -108,7 +107,7 @@ JointReferential::JointReferential(Referential* referential, EntityTree* tree, A
const EntityItem* item = _tree->findEntityByID(_entityID); const EntityItem* item = _tree->findEntityByID(_entityID);
const Model* model = getModel(item); const Model* model = getModel(item);
if (!isValid() || model == NULL || _jointIndex >= (uint32_t)(model->getJointStateCount())) { if (!isValid() || model == NULL || _jointIndex >= (uint32_t)(model->getJointStateCount())) {
_refScale = item->getLargestDimensionInDomainUnits(); _lastRefDimension = item->getDimensionsInMeters();
model->getJointRotationInWorldFrame(_jointIndex, _refRotation); model->getJointRotationInWorldFrame(_jointIndex, _refRotation);
model->getJointPositionInWorldFrame(_jointIndex, _refPosition); model->getJointPositionInWorldFrame(_jointIndex, _refPosition);
} }
@ -119,7 +118,6 @@ JointReferential::JointReferential(uint32_t jointIndex, const QUuid& entityID, E
ModelReferential(entityID, tree, avatar), ModelReferential(entityID, tree, avatar),
_jointIndex(jointIndex) _jointIndex(jointIndex)
{ {
// TODO: Andrew to fix this using meters
_type = JOINT; _type = JOINT;
const EntityItem* item = _tree->findEntityByID(_entityID); const EntityItem* item = _tree->findEntityByID(_entityID);
const Model* model = getModel(item); const Model* model = getModel(item);
@ -129,19 +127,17 @@ JointReferential::JointReferential(uint32_t jointIndex, const QUuid& entityID, E
return; return;
} }
_refScale = item->getLargestDimensionInDomainUnits(); _lastRefDimension = item->getDimensionsInMeters();
model->getJointRotationInWorldFrame(_jointIndex, _refRotation); model->getJointRotationInWorldFrame(_jointIndex, _refRotation);
model->getJointPositionInWorldFrame(_jointIndex, _refPosition); model->getJointPositionInWorldFrame(_jointIndex, _refPosition);
glm::quat refInvRot = glm::inverse(_refRotation); glm::quat refInvRot = glm::inverse(_refRotation);
_scale = _avatar->getTargetScale() / _refScale;
_rotation = refInvRot * _avatar->getOrientation(); _rotation = refInvRot * _avatar->getOrientation();
// BUG! _refPosition is in domain units, but avatar is in meters // BUG! _refPosition is in domain units, but avatar is in meters
_translation = refInvRot * (avatar->getPosition() - _refPosition) / _refScale; _translation = refInvRot * (avatar->getPosition() - _refPosition);
} }
void JointReferential::update() { void JointReferential::update() {
// TODO: Andrew to fix this using meters
const EntityItem* item = _tree->findEntityByID(_entityID); const EntityItem* item = _tree->findEntityByID(_entityID);
const Model* model = getModel(item); const Model* model = getModel(item);
if (!isValid() || model == NULL || _jointIndex >= (uint32_t)(model->getJointStateCount())) { if (!isValid() || model == NULL || _jointIndex >= (uint32_t)(model->getJointStateCount())) {
@ -149,9 +145,10 @@ void JointReferential::update() {
} }
bool somethingChanged = false; bool somethingChanged = false;
if (item->getLargestDimensionInDomainUnits() != _refScale) { if (item->getDimensionsInMeters() != _lastRefDimension) {
_refScale = item->getLargestDimensionInDomainUnits(); glm::vec3 oldDimension = _lastRefDimension;
_avatar->setTargetScale(_refScale * _scale, true); _lastRefDimension = item->getDimensionsInMeters();
_translation *= _lastRefDimension / oldDimension;
somethingChanged = true; somethingChanged = true;
} }
if (item->getRotation() != _refRotation) { if (item->getRotation() != _refRotation) {
@ -159,10 +156,9 @@ void JointReferential::update() {
_avatar->setOrientation(_refRotation * _rotation, true); _avatar->setOrientation(_refRotation * _rotation, true);
somethingChanged = true; somethingChanged = true;
} }
if (item->getPositionInDomainUnits() != _refPosition || somethingChanged) { if (item->getPositionInMeters() != _refPosition || somethingChanged) {
model->getJointPositionInWorldFrame(_jointIndex, _refPosition); model->getJointPositionInWorldFrame(_jointIndex, _refPosition);
// BUG! _refPosition is in domain units, but avatar is in meters _avatar->setPosition(_refPosition + _refRotation * _translation, true);
_avatar->setPosition(_refPosition + _refRotation * (_translation * _refScale), true);
} }
} }

View file

@ -76,7 +76,6 @@ int Referential::pack(unsigned char* destinationBuffer) const {
destinationBuffer += packFloatVec3ToSignedTwoByteFixed(destinationBuffer, _translation, 0); destinationBuffer += packFloatVec3ToSignedTwoByteFixed(destinationBuffer, _translation, 0);
destinationBuffer += packOrientationQuatToBytes(destinationBuffer, _rotation); destinationBuffer += packOrientationQuatToBytes(destinationBuffer, _rotation);
destinationBuffer += packFloatScalarToSignedTwoByteFixed(destinationBuffer, _scale, 0);
return destinationBuffer - startPosition; return destinationBuffer - startPosition;
} }
@ -91,7 +90,6 @@ int Referential::unpack(const unsigned char* sourceBuffer) {
sourceBuffer += unpackFloatVec3FromSignedTwoByteFixed(sourceBuffer, _translation, 0); sourceBuffer += unpackFloatVec3FromSignedTwoByteFixed(sourceBuffer, _translation, 0);
sourceBuffer += unpackOrientationQuatFromBytes(sourceBuffer, _rotation); sourceBuffer += unpackOrientationQuatFromBytes(sourceBuffer, _rotation);
sourceBuffer += unpackFloatScalarFromSignedTwoByteFixed((const int16_t*) sourceBuffer, &_scale, 0);
return sourceBuffer - startPosition; return sourceBuffer - startPosition;
} }

View file

@ -39,7 +39,6 @@ public:
glm::vec3 getTranslation() const { return _translation; } glm::vec3 getTranslation() const { return _translation; }
glm::quat getRotation() const { return _rotation; } glm::quat getRotation() const { return _rotation; }
float getScale() const {return _scale; }
QByteArray getExtraData() const { return _extraDataBuffer; } QByteArray getExtraData() const { return _extraDataBuffer; }
virtual void update() {} virtual void update() {}
@ -62,14 +61,13 @@ protected:
AvatarData* _avatar; AvatarData* _avatar;
QByteArray _extraDataBuffer; QByteArray _extraDataBuffer;
glm::vec3 _refPosition; glm::vec3 _refPosition; // position of object in world-frame
glm::quat _refRotation; glm::quat _refRotation; // rotation of object in world-frame
float _refScale; glm::vec3 _lastRefDimension; // dimension of object when _translation was last computed
glm::vec3 _translation; glm::vec3 _translation; // offset of avatar in object local-frame
glm::quat _rotation; glm::quat _rotation; // rotation of avatar in object local-frame
float _scale;
}; };
#endif // hifi_Referential_h #endif // hifi_Referential_h