change how hold action works

This commit is contained in:
Seth Alves 2015-10-13 11:36:58 -07:00
parent ef3775c44d
commit 7c52849740
8 changed files with 188 additions and 127 deletions

View file

@ -56,6 +56,7 @@ const float DISPLAYNAME_FADE_TIME = 0.5f;
const float DISPLAYNAME_FADE_FACTOR = pow(0.01f, 1.0f / DISPLAYNAME_FADE_TIME); const float DISPLAYNAME_FADE_FACTOR = pow(0.01f, 1.0f / DISPLAYNAME_FADE_TIME);
const float DISPLAYNAME_ALPHA = 1.0f; const float DISPLAYNAME_ALPHA = 1.0f;
const float DISPLAYNAME_BACKGROUND_ALPHA = 0.4f; const float DISPLAYNAME_BACKGROUND_ALPHA = 0.4f;
const glm::vec3 HAND_TO_PALM_OFFSET(0.0f, 0.12f, 0.08f);
namespace render { namespace render {
template <> const ItemKey payloadGetKey(const AvatarSharedPointer& avatar) { template <> const ItemKey payloadGetKey(const AvatarSharedPointer& avatar) {
@ -1167,3 +1168,64 @@ void Avatar::rebuildSkeletonBody() {
DependencyManager::get<AvatarManager>()->updateAvatarPhysicsShape(getSessionUUID()); DependencyManager::get<AvatarManager>()->updateAvatarPhysicsShape(getSessionUUID());
} }
glm::vec3 Avatar::getLeftPalmPosition() {
glm::vec3 leftHandPosition;
getSkeletonModel().getLeftHandPosition(leftHandPosition);
glm::quat leftRotation;
getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getLeftHandJointIndex(), leftRotation);
leftHandPosition += HAND_TO_PALM_OFFSET * glm::inverse(leftRotation);
return leftHandPosition;
}
glm::vec3 Avatar::getLeftPalmVelocity() {
const PalmData* palm = getHand()->getPalm(LEFT_HAND_INDEX);
if (palm != NULL) {
return palm->getVelocity();
}
return glm::vec3(0.0f);
}
glm::vec3 Avatar::getLeftPalmAngularVelocity() {
const PalmData* palm = getHand()->getPalm(LEFT_HAND_INDEX);
if (palm != NULL) {
return palm->getRawAngularVelocity();
}
return glm::vec3(0.0f);
}
glm::quat Avatar::getLeftPalmRotation() {
glm::quat leftRotation;
getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getLeftHandJointIndex(), leftRotation);
return leftRotation;
}
glm::vec3 Avatar::getRightPalmPosition() {
glm::vec3 rightHandPosition;
getSkeletonModel().getRightHandPosition(rightHandPosition);
glm::quat rightRotation;
getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getRightHandJointIndex(), rightRotation);
rightHandPosition += HAND_TO_PALM_OFFSET * glm::inverse(rightRotation);
return rightHandPosition;
}
glm::vec3 Avatar::getRightPalmVelocity() {
const PalmData* palm = getHand()->getPalm(RIGHT_HAND_INDEX);
if (palm != NULL) {
return palm->getVelocity();
}
return glm::vec3(0.0f);
}
glm::vec3 Avatar::getRightPalmAngularVelocity() {
const PalmData* palm = getHand()->getPalm(RIGHT_HAND_INDEX);
if (palm != NULL) {
return palm->getRawAngularVelocity();
}
return glm::vec3(0.0f);
}
glm::quat Avatar::getRightPalmRotation() {
glm::quat rightRotation;
getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getRightHandJointIndex(), rightRotation);
return rightRotation;
}

View file

@ -174,6 +174,16 @@ public:
void setMotionState(AvatarMotionState* motionState) { _motionState = motionState; } void setMotionState(AvatarMotionState* motionState) { _motionState = motionState; }
AvatarMotionState* getMotionState() { return _motionState; } AvatarMotionState* getMotionState() { return _motionState; }
public slots:
glm::vec3 getLeftPalmPosition();
glm::vec3 getLeftPalmVelocity();
glm::vec3 getLeftPalmAngularVelocity();
glm::quat getLeftPalmRotation();
glm::vec3 getRightPalmPosition();
glm::vec3 getRightPalmVelocity();
glm::vec3 getRightPalmAngularVelocity();
glm::quat getRightPalmRotation();
protected: protected:
SkeletonModel _skeletonModel; SkeletonModel _skeletonModel;
glm::vec3 _skeletonOffset; glm::vec3 _skeletonOffset;

View file

@ -22,7 +22,7 @@ AvatarActionHold::AvatarActionHold(const QUuid& id, EntityItemPointer ownerEntit
_relativePosition(glm::vec3(0.0f)), _relativePosition(glm::vec3(0.0f)),
_relativeRotation(glm::quat()), _relativeRotation(glm::quat()),
_hand("right"), _hand("right"),
_mine(false) _holderID(QUuid())
{ {
_type = ACTION_TYPE_HOLD; _type = ACTION_TYPE_HOLD;
#if WANT_DEBUG #if WANT_DEBUG
@ -37,48 +37,46 @@ AvatarActionHold::~AvatarActionHold() {
} }
void AvatarActionHold::updateActionWorker(float deltaTimeStep) { void AvatarActionHold::updateActionWorker(float deltaTimeStep) {
if (!_mine) { QSharedPointer<AvatarManager> avatarManager = DependencyManager::get<AvatarManager>();
// if a local script isn't updating this, then we are just getting spring-action data over the wire. AvatarSharedPointer holdingAvatarData = avatarManager->getAvatarBySessionID(_holderID);
// let the super-class handle it. std::shared_ptr<Avatar> holdingAvatar = std::static_pointer_cast<Avatar>(holdingAvatarData);
ObjectActionSpring::updateActionWorker(deltaTimeStep);
return;
}
glm::quat rotation; if (holdingAvatar) {
glm::vec3 position; glm::quat rotation;
glm::vec3 offset; glm::vec3 position;
bool gotLock = withTryReadLock([&]{ glm::vec3 offset;
auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar(); bool gotLock = withTryReadLock([&]{
glm::vec3 palmPosition; glm::vec3 palmPosition;
glm::quat palmRotation; glm::quat palmRotation;
if (_hand == "right") { if (_hand == "right") {
palmPosition = myAvatar->getRightPalmPosition(); palmPosition = holdingAvatar->getRightPalmPosition();
palmRotation = myAvatar->getRightPalmRotation(); palmRotation = holdingAvatar->getRightPalmRotation();
} else { } else {
palmPosition = myAvatar->getLeftPalmPosition(); palmPosition = holdingAvatar->getLeftPalmPosition();
palmRotation = myAvatar->getLeftPalmRotation(); palmRotation = holdingAvatar->getLeftPalmRotation();
}
rotation = palmRotation * _relativeRotation;
offset = rotation * _relativePosition;
position = palmPosition + offset;
});
if (gotLock) {
gotLock = withTryWriteLock([&]{
if (_positionalTarget != position || _rotationalTarget != rotation) {
auto ownerEntity = _ownerEntity.lock();
if (ownerEntity) {
ownerEntity->setActionDataDirty(true);
}
_positionalTarget = position;
_rotationalTarget = rotation;
}
});
} }
rotation = palmRotation * _relativeRotation; if (gotLock) {
offset = rotation * _relativePosition; ObjectActionSpring::updateActionWorker(deltaTimeStep);
position = palmPosition + offset; }
});
if (gotLock) {
gotLock = withTryWriteLock([&]{
if (_positionalTarget != position || _rotationalTarget != rotation) {
auto ownerEntity = _ownerEntity.lock();
if (ownerEntity) {
ownerEntity->setActionDataDirty(true);
}
_positionalTarget = position;
_rotationalTarget = rotation;
}
});
}
if (gotLock) {
ObjectActionSpring::updateActionWorker(deltaTimeStep);
} }
} }
@ -100,7 +98,7 @@ bool AvatarActionHold::updateArguments(QVariantMap arguments) {
if (!ok) { if (!ok) {
relativeRotation = _relativeRotation; relativeRotation = _relativeRotation;
} }
ok = true; ok = true;
float timeScale = float timeScale =
EntityActionInterface::extractFloatArgument("hold", arguments, "timeScale", ok, false); EntityActionInterface::extractFloatArgument("hold", arguments, "timeScale", ok, false);
@ -115,35 +113,46 @@ bool AvatarActionHold::updateArguments(QVariantMap arguments) {
hand = _hand; hand = _hand;
} }
ok = true;
auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar();
auto holderID = myAvatar->getSessionUUID();
QString holderIDString =
EntityActionInterface::extractStringArgument("hold", arguments, "hand", ok, false);
if (ok) {
holderID = QUuid(holderIDString);
}
if (relativePosition != _relativePosition if (relativePosition != _relativePosition
|| relativeRotation != _relativeRotation || relativeRotation != _relativeRotation
|| timeScale != _linearTimeScale || timeScale != _linearTimeScale
|| hand != _hand) { || hand != _hand
|| holderID != _holderID) {
withWriteLock([&] { withWriteLock([&] {
_relativePosition = relativePosition; _relativePosition = relativePosition;
_relativeRotation = relativeRotation; _relativeRotation = relativeRotation;
const float MIN_TIMESCALE = 0.1f; const float MIN_TIMESCALE = 0.1f;
_linearTimeScale = glm::min(MIN_TIMESCALE, timeScale); _linearTimeScale = glm::max(MIN_TIMESCALE, timeScale);
_angularTimeScale = _linearTimeScale; _angularTimeScale = _linearTimeScale;
_hand = hand; _hand = hand;
_holderID = holderID;
_mine = true;
_active = true; _active = true;
activateBody(); activateBody();
auto ownerEntity = _ownerEntity.lock();
if (ownerEntity) {
ownerEntity->setActionDataDirty(true);
}
}); });
} }
return true; return true;
} }
QVariantMap AvatarActionHold::getArguments() { QVariantMap AvatarActionHold::getArguments() {
QVariantMap arguments = ObjectAction::getArguments(); QVariantMap arguments = ObjectAction::getArguments();
withReadLock([&]{ withReadLock([&]{
if (!_mine) { arguments["holderID"] = _holderID;
arguments = ObjectActionSpring::getArguments();
return;
}
arguments["relativePosition"] = glmToQMap(_relativePosition); arguments["relativePosition"] = glmToQMap(_relativePosition);
arguments["relativeRotation"] = glmToQMap(_relativeRotation); arguments["relativeRotation"] = glmToQMap(_relativeRotation);
arguments["timeScale"] = _linearTimeScale; arguments["timeScale"] = _linearTimeScale;
@ -152,9 +161,52 @@ QVariantMap AvatarActionHold::getArguments() {
return arguments; return arguments;
} }
QByteArray AvatarActionHold::serialize() const {
QByteArray serializedActionArguments;
QDataStream dataStream(&serializedActionArguments, QIODevice::WriteOnly);
dataStream << ACTION_TYPE_SPRING;
dataStream << getID();
dataStream << AvatarActionHold::holdVersion;
dataStream << _holderID;
dataStream << _relativePosition;
dataStream << _relativeRotation;
dataStream << _linearTimeScale;
dataStream << _hand;
dataStream << _expires;
dataStream << _tag;
return serializedActionArguments;
}
void AvatarActionHold::deserialize(QByteArray serializedArguments) { void AvatarActionHold::deserialize(QByteArray serializedArguments) {
if (!_mine) { QDataStream dataStream(serializedArguments);
ObjectActionSpring::deserialize(serializedArguments);
EntityActionType type;
dataStream >> type;
assert(type == getType());
QUuid id;
dataStream >> id;
assert(id == getID());
uint16_t serializationVersion;
dataStream >> serializationVersion;
if (serializationVersion != AvatarActionHold::holdVersion) {
return;
} }
dataStream >> _holderID;
dataStream >> _relativePosition;
dataStream >> _relativeRotation;
dataStream >> _linearTimeScale;
_angularTimeScale = _linearTimeScale;
dataStream >> _hand;
dataStream >> _expires;
dataStream >> _tag;
_active = true;
} }

View file

@ -27,6 +27,7 @@ public:
virtual void updateActionWorker(float deltaTimeStep); virtual void updateActionWorker(float deltaTimeStep);
QByteArray serialize() const;
virtual void deserialize(QByteArray serializedArguments); virtual void deserialize(QByteArray serializedArguments);
private: private:
@ -35,7 +36,7 @@ private:
glm::vec3 _relativePosition; glm::vec3 _relativePosition;
glm::quat _relativeRotation; glm::quat _relativeRotation;
QString _hand; QString _hand;
bool _mine = false; QUuid _holderID;
}; };
#endif // hifi_AvatarActionHold_h #endif // hifi_AvatarActionHold_h

View file

@ -343,3 +343,11 @@ void AvatarManager::updateAvatarRenderStatus(bool shouldRenderAvatars) {
} }
} }
} }
AvatarSharedPointer AvatarManager::getAvatarBySessionID(const QUuid& sessionID) {
if (sessionID == _myAvatar->getSessionUUID()) {
return std::static_pointer_cast<Avatar>(_myAvatar);
}
return getAvatarHash()[sessionID];
}

View file

@ -35,7 +35,8 @@ public:
void init(); void init();
MyAvatar* getMyAvatar() { return _myAvatar.get(); } MyAvatar* getMyAvatar() { return _myAvatar.get(); }
AvatarSharedPointer getAvatarBySessionID(const QUuid& sessionID);
void updateMyAvatar(float deltaTime); void updateMyAvatar(float deltaTime);
void updateOtherAvatars(float deltaTime); void updateOtherAvatars(float deltaTime);

View file

@ -493,70 +493,6 @@ void MyAvatar::render(RenderArgs* renderArgs, const glm::vec3& cameraPosition) {
} }
} }
const glm::vec3 HAND_TO_PALM_OFFSET(0.0f, 0.12f, 0.08f);
glm::vec3 MyAvatar::getLeftPalmPosition() {
glm::vec3 leftHandPosition;
getSkeletonModel().getLeftHandPosition(leftHandPosition);
glm::quat leftRotation;
getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getLeftHandJointIndex(), leftRotation);
leftHandPosition += HAND_TO_PALM_OFFSET * glm::inverse(leftRotation);
return leftHandPosition;
}
glm::vec3 MyAvatar::getLeftPalmVelocity() {
const PalmData* palm = getHand()->getPalm(LEFT_HAND_INDEX);
if (palm != NULL) {
return palm->getVelocity();
}
return glm::vec3(0.0f);
}
glm::vec3 MyAvatar::getLeftPalmAngularVelocity() {
const PalmData* palm = getHand()->getPalm(LEFT_HAND_INDEX);
if (palm != NULL) {
return palm->getRawAngularVelocity();
}
return glm::vec3(0.0f);
}
glm::quat MyAvatar::getLeftPalmRotation() {
glm::quat leftRotation;
getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getLeftHandJointIndex(), leftRotation);
return leftRotation;
}
glm::vec3 MyAvatar::getRightPalmPosition() {
glm::vec3 rightHandPosition;
getSkeletonModel().getRightHandPosition(rightHandPosition);
glm::quat rightRotation;
getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getRightHandJointIndex(), rightRotation);
rightHandPosition += HAND_TO_PALM_OFFSET * glm::inverse(rightRotation);
return rightHandPosition;
}
glm::vec3 MyAvatar::getRightPalmVelocity() {
const PalmData* palm = getHand()->getPalm(RIGHT_HAND_INDEX);
if (palm != NULL) {
return palm->getVelocity();
}
return glm::vec3(0.0f);
}
glm::vec3 MyAvatar::getRightPalmAngularVelocity() {
const PalmData* palm = getHand()->getPalm(RIGHT_HAND_INDEX);
if (palm != NULL) {
return palm->getRawAngularVelocity();
}
return glm::vec3(0.0f);
}
glm::quat MyAvatar::getRightPalmRotation() {
glm::quat rightRotation;
getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getRightHandJointIndex(), rightRotation);
return rightRotation;
}
void MyAvatar::clearReferential() { void MyAvatar::clearReferential() {
changeReferential(NULL); changeReferential(NULL);
} }

View file

@ -199,15 +199,6 @@ public slots:
Q_INVOKABLE void updateMotionBehaviorFromMenu(); Q_INVOKABLE void updateMotionBehaviorFromMenu();
glm::vec3 getLeftPalmPosition();
glm::vec3 getLeftPalmVelocity();
glm::vec3 getLeftPalmAngularVelocity();
glm::quat getLeftPalmRotation();
glm::vec3 getRightPalmPosition();
glm::vec3 getRightPalmVelocity();
glm::vec3 getRightPalmAngularVelocity();
glm::quat getRightPalmRotation();
void clearReferential(); void clearReferential();
bool setModelReferential(const QUuid& id); bool setModelReferential(const QUuid& id);
bool setJointReferential(const QUuid& id, int jointIndex); bool setJointReferential(const QUuid& id, int jointIndex);