hold action works

This commit is contained in:
Seth Alves 2015-06-10 17:05:49 -07:00
parent 1c0c7f0fe3
commit eccf4eb8a8
8 changed files with 67 additions and 82 deletions

View file

@ -15,7 +15,7 @@
#include "AvatarActionHold.h"
AvatarActionHold::AvatarActionHold(QUuid id, EntityItemPointer ownerEntity) :
ObjectAction(id, ownerEntity) {
ObjectActionSpring(id, ownerEntity) {
#if WANT_DEBUG
qDebug() << "AvatarActionHold::AvatarActionHold";
#endif
@ -28,83 +28,57 @@ AvatarActionHold::~AvatarActionHold() {
}
void AvatarActionHold::updateActionWorker(float deltaTimeStep) {
// handle the linear part
if (_linearOffsetSet) {
}
auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar();
glm::vec3 palmPosition = myAvatar->getRightPalmPosition();
glm::vec3 position = getPosition();
glm::vec3 positionalTarget = palmPosition + _linearOffset;
glm::vec3 offset = positionalTarget - position;
float offsetLength = glm::length(offset);
float speed = offsetLength / _timeScale;
auto rotation = myAvatar->getWorldAlignedOrientation();
auto offset = rotation * _relativePosition;
auto position = palmPosition + offset;
rotation *= _relativeRotation;
if (offsetLength > IGNORE_POSITION_DELTA) {
glm::vec3 newVelocity = glm::normalize(offset) * speed;
setLinearVelocity(newVelocity);
} else {
setLinearVelocity(glm::vec3(0.0f));
}
lockForWrite();
_positionalTarget = position;
_rotationalTarget = rotation;
unlock();
// handle rotation
if (_angularOffsetSet) {
glm::quat bodyRotation = getRotation();
// if qZero and qOne are too close to each other, we can get NaN for angle.
auto alignmentDot = glm::dot(bodyRotation, _angularOffset);
const float almostOne = 0.99999;
if (glm::abs(alignmentDot) < almostOne) {
glm::quat target = _angularOffset;
if (alignmentDot < 0) {
target = -target;
}
glm::quat qZeroInverse = glm::inverse(bodyRotation);
glm::quat deltaQ = target * qZeroInverse;
glm::vec3 axis = glm::axis(deltaQ);
float angle = glm::angle(deltaQ);
if (isNaN(angle)) {
qDebug() << "AvatarActionHold::updateAction angle =" << angle
<< "body-rotation =" << bodyRotation.x << bodyRotation.y << bodyRotation.z << bodyRotation.w
<< "target-rotation ="
<< target.x << target.y << target.z<< target.w;
}
assert(!isNaN(angle));
glm::vec3 newAngularVelocity = (angle / _timeScale) * glm::normalize(axis);
setAngularVelocity(newAngularVelocity);
} else {
setAngularVelocity(glm::vec3(0.0f));
}
}
ObjectActionSpring::updateActionWorker(deltaTimeStep);
}
bool AvatarActionHold::updateArguments(QVariantMap arguments) {
// targets are required, spring-constants are optional
bool ptOk = true;
glm::vec3 linearOffset =
EntityActionInterface::extractVec3Argument("spring action", arguments, "targetPosition", ptOk, false);
bool rtOk = true;
glm::quat angularOffset =
EntityActionInterface::extractQuatArgument("spring action", arguments, "targetRotation", rtOk, false);
bool rPOk = true;
glm::vec3 relativePosition =
EntityActionInterface::extractVec3Argument("hold", arguments, "relativePosition", rPOk, false);
bool rROk = true;
glm::quat relativeRotation =
EntityActionInterface::extractQuatArgument("hold", arguments, "relativeRotation", rROk, false);
bool tSOk = true;
float timeScale =
EntityActionInterface::extractFloatArgument("hold", arguments, "timeScale", tSOk, false);
lockForWrite();
_linearOffsetSet = _angularOffsetSet = false;
if (ptOk) {
_linearOffset = linearOffset;
_linearOffsetSet = true;
if (rPOk) {
_relativePosition = relativePosition;
} else {
_relativePosition = glm::vec3(0.0f, 0.0f, 1.0f);
}
if (rtOk) {
_angularOffset = angularOffset;
_angularOffsetSet = true;
if (rROk) {
_relativeRotation = relativeRotation;
} else {
_relativeRotation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f);
}
if (tSOk) {
_linearTimeScale = timeScale;
_angularTimeScale = timeScale;
} else {
_linearTimeScale = 0.2;
_angularTimeScale = 0.2;
}
_positionalTargetSet = true;
_rotationalTargetSet = true;
_active = true;
unlock();
return true;

View file

@ -15,9 +15,9 @@
#include <QUuid>
#include <EntityItem.h>
#include <ObjectAction.h>
#include <ObjectActionSpring.h>
class AvatarActionHold : public ObjectAction {
class AvatarActionHold : public ObjectActionSpring {
public:
AvatarActionHold(QUuid id, EntityItemPointer ownerEntity);
virtual ~AvatarActionHold();
@ -26,14 +26,8 @@ public:
virtual void updateActionWorker(float deltaTimeStep);
private:
glm::vec3 _linearOffset;
bool _linearOffsetSet;
glm::quat _angularOffset;
bool _angularOffsetSet;
float _timeScale = 0.01;
glm::vec3 _relativePosition;
glm::quat _relativeRotation;
};
#endif // hifi_AvatarActionHold_h

View file

@ -25,6 +25,9 @@ EntityActionType EntityActionInterface::actionTypeFromString(QString actionTypeS
if (normalizedActionTypeString == "spring") {
return ACTION_TYPE_SPRING;
}
if (normalizedActionTypeString == "hold") {
return ACTION_TYPE_HOLD;
}
qDebug() << "Warning -- EntityActionInterface::actionTypeFromString got unknown action-type name" << actionTypeString;
return ACTION_TYPE_NONE;

View file

@ -31,14 +31,7 @@ void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar delta
qDebug() << "ObjectActionPullToPoint::updateAction no owner entity";
return;
}
if (!tryLockForRead()) {
// don't risk hanging the thread running the physics simulation
return;
}
updateActionWorker(deltaTimeStep);
unlock();
}
void ObjectAction::debugDraw(btIDebugDraw* debugDrawer) {

View file

@ -47,7 +47,7 @@ private:
QReadWriteLock _lock;
protected:
btRigidBody* getRigidBody();
virtual btRigidBody* getRigidBody();
virtual glm::vec3 getPosition();
virtual void setPosition(glm::vec3 position);
virtual glm::quat getRotation();

View file

@ -25,13 +25,20 @@ ObjectActionPullToPoint::~ObjectActionPullToPoint() {
}
void ObjectActionPullToPoint::updateActionWorker(btScalar deltaTimeStep) {
if (!tryLockForRead()) {
// don't risk hanging the thread running the physics simulation
return;
}
void* physicsInfo = _ownerEntity->getPhysicsInfo();
if (!physicsInfo) {
unlock();
return;
}
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
btRigidBody* rigidBody = motionState->getRigidBody();
if (!rigidBody) {
unlock();
return;
}
@ -44,6 +51,8 @@ void ObjectActionPullToPoint::updateActionWorker(btScalar deltaTimeStep) {
} else {
rigidBody->setLinearVelocity(glmToBullet(glm::vec3()));
}
unlock();
}

View file

@ -25,13 +25,23 @@ ObjectActionSpring::~ObjectActionSpring() {
}
void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) {
if (!tryLockForRead()) {
// don't risk hanging the thread running the physics simulation
qDebug() << "ObjectActionSpring::updateActionWorker lock failed";
return;
}
void* physicsInfo = _ownerEntity->getPhysicsInfo();
if (!physicsInfo) {
unlock();
qDebug() << "ObjectActionSpring::updateActionWorker no physicsInfo";
return;
}
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
btRigidBody* rigidBody = motionState->getRigidBody();
if (!rigidBody) {
unlock();
qDebug() << "ObjectActionSpring::updateActionWorker no rigidBody";
return;
}
@ -79,6 +89,8 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) {
rigidBody->setAngularVelocity(glmToBullet(glm::vec3(0.0f)));
}
}
unlock();
}

View file

@ -25,7 +25,7 @@ public:
virtual bool updateArguments(QVariantMap arguments);
virtual void updateActionWorker(float deltaTimeStep);
private:
protected:
glm::vec3 _positionalTarget;
float _linearTimeScale;