if a hold action is edited by a local script, mark it as 'mine'. if it's not 'mine', let the spring action handle the wire protocol

This commit is contained in:
Seth Alves 2015-06-29 19:27:10 -07:00
parent 0a734c9d3d
commit 88fc74374b
3 changed files with 32 additions and 55 deletions

View file

@ -18,7 +18,12 @@
const uint16_t AvatarActionHold::holdVersion = 1; const uint16_t AvatarActionHold::holdVersion = 1;
AvatarActionHold::AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : AvatarActionHold::AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) :
ObjectActionSpring(type, id, ownerEntity) { ObjectActionSpring(type, id, ownerEntity),
_relativePosition(glm::vec3(0.0f)),
_relativeRotation(glm::quat()),
_hand("right"),
_mine(false)
{
#if WANT_DEBUG #if WANT_DEBUG
qDebug() << "AvatarActionHold::AvatarActionHold"; qDebug() << "AvatarActionHold::AvatarActionHold";
#endif #endif
@ -31,6 +36,13 @@ AvatarActionHold::~AvatarActionHold() {
} }
void AvatarActionHold::updateActionWorker(float deltaTimeStep) { void AvatarActionHold::updateActionWorker(float deltaTimeStep) {
if (!_mine) {
// if a local script isn't updating this, then we are just getting spring-action data over the wire.
// let the super-class handle it.
ObjectActionSpring::updateActionWorker(deltaTimeStep);
return;
}
auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar(); auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar();
if (!tryLockForRead()) { if (!tryLockForRead()) {
@ -95,20 +107,20 @@ bool AvatarActionHold::updateArguments(QVariantMap arguments) {
lockForWrite(); lockForWrite();
if (rPOk) { if (rPOk) {
_relativePosition = relativePosition; _relativePosition = relativePosition;
} else if (!_parametersSet) { } else {
_relativePosition = glm::vec3(0.0f, 0.0f, 1.0f); _relativePosition = glm::vec3(0.0f, 0.0f, 1.0f);
} }
if (rROk) { if (rROk) {
_relativeRotation = relativeRotation; _relativeRotation = relativeRotation;
} else if (!_parametersSet) { } else {
_relativeRotation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f); _relativeRotation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f);
} }
if (tSOk) { if (tSOk) {
_linearTimeScale = timeScale; _linearTimeScale = timeScale;
_angularTimeScale = timeScale; _angularTimeScale = timeScale;
} else if (!_parametersSet) { } else {
_linearTimeScale = 0.2f; _linearTimeScale = 0.2f;
_angularTimeScale = 0.2f; _angularTimeScale = 0.2f;
} }
@ -123,11 +135,11 @@ bool AvatarActionHold::updateArguments(QVariantMap arguments) {
qDebug() << "hold action -- invalid hand argument:" << hand; qDebug() << "hold action -- invalid hand argument:" << hand;
_hand = "right"; _hand = "right";
} }
} else if (!_parametersSet) { } else {
_hand = "right"; _hand = "right";
} }
_parametersSet = true; _mine = true;
_positionalTargetSet = true; _positionalTargetSet = true;
_rotationalTargetSet = true; _rotationalTargetSet = true;
_active = true; _active = true;
@ -139,57 +151,15 @@ bool AvatarActionHold::updateArguments(QVariantMap arguments) {
QVariantMap AvatarActionHold::getArguments() { QVariantMap AvatarActionHold::getArguments() {
QVariantMap arguments; QVariantMap arguments;
lockForRead(); lockForRead();
if (_parametersSet) { if (_mine) {
arguments["relativePosition"] = glmToQMap(_relativePosition); arguments["relativePosition"] = glmToQMap(_relativePosition);
arguments["relativeRotation"] = glmToQMap(_relativeRotation); arguments["relativeRotation"] = glmToQMap(_relativeRotation);
arguments["timeScale"] = _linearTimeScale; arguments["timeScale"] = _linearTimeScale;
arguments["hand"] = _hand; arguments["hand"] = _hand;
} else {
unlock();
return ObjectActionSpring::getArguments();
} }
unlock(); unlock();
return arguments; return arguments;
} }
QByteArray AvatarActionHold::serialize() {
QByteArray ba;
QDataStream dataStream(&ba, QIODevice::WriteOnly);
dataStream << getType();
dataStream << getID();
dataStream << AvatarActionHold::holdVersion;
dataStream << _relativePosition;
dataStream << _relativeRotation;
dataStream << _hand;
dataStream << _linearTimeScale;
return ba;
}
void AvatarActionHold::deserialize(QByteArray serializedArguments) {
QDataStream dataStream(serializedArguments);
EntityActionType type;
QUuid id;
uint16_t serializationVersion;
dataStream >> type;
assert(type == getType());
dataStream >> id;
assert(id == getID());
dataStream >> serializationVersion;
if (serializationVersion != AvatarActionHold::holdVersion) {
return;
}
dataStream >> _relativePosition;
dataStream >> _relativeRotation;
dataStream >> _hand;
dataStream >> _linearTimeScale;
_angularTimeScale = _linearTimeScale;
_parametersSet = true;
// XXX don't enable hold actions from remote nodes
// _active = true;
}

View file

@ -29,8 +29,8 @@ public:
virtual void updateActionWorker(float deltaTimeStep); virtual void updateActionWorker(float deltaTimeStep);
virtual QByteArray serialize(); // virtual QByteArray serialize();
virtual void deserialize(QByteArray serializedArguments); // virtual void deserialize(QByteArray serializedArguments);
private: private:
static const uint16_t holdVersion; static const uint16_t holdVersion;
@ -38,7 +38,7 @@ private:
glm::vec3 _relativePosition; glm::vec3 _relativePosition;
glm::quat _relativeRotation; glm::quat _relativeRotation;
QString _hand; QString _hand;
bool _parametersSet = false; bool _mine = false;
}; };
#endif // hifi_AvatarActionHold_h #endif // hifi_AvatarActionHold_h

View file

@ -13,6 +13,8 @@
#include "ObjectActionSpring.h" #include "ObjectActionSpring.h"
const float SPRING_MAX_SPEED = 10.0f;
const uint16_t ObjectActionSpring::springVersion = 1; const uint16_t ObjectActionSpring::springVersion = 1;
ObjectActionSpring::ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : ObjectActionSpring::ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) :
@ -70,6 +72,11 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) {
float offsetLength = glm::length(offset); float offsetLength = glm::length(offset);
float speed = offsetLength / _linearTimeScale; float speed = offsetLength / _linearTimeScale;
// cap speed
if (speed > SPRING_MAX_SPEED) {
speed = SPRING_MAX_SPEED;
}
if (offsetLength > IGNORE_POSITION_DELTA) { if (offsetLength > IGNORE_POSITION_DELTA) {
glm::vec3 newVelocity = glm::normalize(offset) * speed; glm::vec3 newVelocity = glm::normalize(offset) * speed;
rigidBody->setLinearVelocity(glmToBullet(newVelocity)); rigidBody->setLinearVelocity(glmToBullet(newVelocity));