update grab.js to use pull-to-point action, various other fixes

This commit is contained in:
Seth Alves 2015-06-05 07:10:44 -07:00
parent 9a67f35b59
commit b2db5f7fee
6 changed files with 47 additions and 19 deletions

View file

@ -13,6 +13,7 @@
var isGrabbing = false;
var grabbedEntity = null;
var actionID = null;
var prevMouse = {};
var deltaMouse = {
z: 0
@ -88,6 +89,7 @@ function mousePressEvent(event) {
gravity: {x: 0, y: 0, z: 0}
});
Controller.mouseMoveEvent.connect(mouseMoveEvent);
}
}
@ -110,7 +112,10 @@ function updateDropLine(position) {
function mouseReleaseEvent() {
if (isGrabbing) {
Controller.mouseMoveEvent.disconnect(mouseMoveEvent);
isGrabbing = false;
Entities.deleteAction(grabbedEntity, actionID);
actionID = null;
// only restore the original gravity if it's not zero. This is to avoid...
// 1. interface A grabs an entity and locally saves off its gravity
@ -228,21 +233,25 @@ function update(deltaTime) {
}
if (shouldRotate) {
angularVelocity = Vec3.subtract(angularVelocity, Vec3.multiply(angularVelocity, ANGULAR_DAMPING_RATE));
Entities.editEntity(grabbedEntity, {
rotation: currentRotation,
angularVelocity: angularVelocity
});
} else {
angularVelocity = entityProps.angularVelocity;
}
Entities.editEntity(grabbedEntity, {
position: currentPosition,
rotation: currentRotation,
velocity: newVelocity,
angularVelocity: angularVelocity
});
var newSpeed = Vec3.length(newVelocity);
if (!actionID) {
actionID = Entities.addAction("pull-to-point", grabbedEntity, {target: targetPosition, speed: newSpeed});
} else {
Entities.updateAction(grabbedEntity, actionID, {target: targetPosition, speed: newSpeed});
}
updateDropLine(targetPosition);
}
}
Controller.mouseMoveEvent.connect(mouseMoveEvent);
Controller.mousePressEvent.connect(mousePressEvent);
Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
Controller.keyPressEvent.connect(keyPressEvent);

View file

@ -152,6 +152,7 @@ public:
bool hasEntities() const { return _entityItems ? _entityItems->size() > 0 : false; }
void setTree(EntityTree* tree) { _myTree = tree; }
EntityTree* getTree() const { return _myTree; }
bool updateEntity(const EntityItem& entity);
void addEntityItem(EntityItemPointer entity);

View file

@ -16,6 +16,7 @@
ObjectAction::ObjectAction(QUuid id, EntityItemPointer ownerEntity) :
btActionInterface(),
_id(id),
_active(false),
_ownerEntity(ownerEntity) {
}

View file

@ -36,8 +36,14 @@ public:
private:
QUuid _id;
QReadWriteLock _lock;
protected:
bool tryLockForRead() { return _lock.tryLockForRead(); }
void lockForWrite() { _lock.lockForWrite(); }
void unlock() { _lock.unlock(); }
bool _active;
EntityItemPointer _ownerEntity;
};

View file

@ -16,33 +16,40 @@
ObjectActionPullToPoint::ObjectActionPullToPoint(QUuid id, EntityItemPointer ownerEntity) :
ObjectAction(id, ownerEntity) {
#if WANT_DEBUG
qDebug() << "ObjectActionPullToPoint::ObjectActionPullToPoint";
#endif
}
ObjectActionPullToPoint::~ObjectActionPullToPoint() {
#if WANT_DEBUG
qDebug() << "ObjectActionPullToPoint::~ObjectActionPullToPoint";
#endif
}
void ObjectActionPullToPoint::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) {
glm::vec3 offset = _target - _ownerEntity->getPosition();
if (glm::length(offset) < IGNORE_POSITION_DELTA) {
offset = glm::vec3(0.0f, 0.0f, 0.0f);
if (!tryLockForRead()) {
// don't risk hanging the thread running the physics simulation
return;
}
glm::vec3 newVelocity = glm::normalize(offset) * _speed;
void* physicsInfo = _ownerEntity->getPhysicsInfo();
if (physicsInfo) {
if (_active && physicsInfo) {
ObjectMotionState* motionState = static_cast<ObjectMotionState*>(physicsInfo);
btRigidBody* rigidBody = motionState->getRigidBody();
if (rigidBody) {
rigidBody->setLinearVelocity(glmToBullet(newVelocity));
return;
glm::vec3 offset = _target - bulletToGLM(rigidBody->getCenterOfMassPosition());
float offsetLength = glm::length(offset);
if (offsetLength > IGNORE_POSITION_DELTA) {
glm::vec3 newVelocity = glm::normalize(offset) * _speed;
rigidBody->setLinearVelocity(glmToBullet(newVelocity));
rigidBody->activate(); // ??
} else {
rigidBody->setLinearVelocity(glmToBullet(glm::vec3()));
}
}
}
_ownerEntity->updateVelocity(newVelocity);
unlock();
}
@ -51,8 +58,11 @@ bool ObjectActionPullToPoint::updateArguments(QVariantMap arguments) {
glm::vec3 target = EntityActionInterface::extractVec3Argument("pull-to-point action", arguments, "target", ok);
float speed = EntityActionInterface::extractFloatArgument("pull-to-point action", arguments, "speed", ok);
if (ok) {
lockForWrite();
_target = target;
_speed = speed;
_active = true;
unlock();
return true;
}
return false;

View file

@ -26,6 +26,7 @@ public:
virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep);
private:
glm::vec3 _target;
float _speed;
};