From b2db5f7fee76fc064428f0c06ef21c793d092c56 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Fri, 5 Jun 2015 07:10:44 -0700 Subject: [PATCH] update grab.js to use pull-to-point action, various other fixes --- examples/grab.js | 23 +++++++++---- libraries/entities/src/EntityTreeElement.h | 1 + libraries/physics/src/ObjectAction.cpp | 1 + libraries/physics/src/ObjectAction.h | 6 ++++ .../physics/src/ObjectActionPullToPoint.cpp | 34 ++++++++++++------- .../physics/src/ObjectActionPullToPoint.h | 1 + 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/examples/grab.js b/examples/grab.js index 7ed69e9664..3d95592068 100644 --- a/examples/grab.js +++ b/examples/grab.js @@ -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); diff --git a/libraries/entities/src/EntityTreeElement.h b/libraries/entities/src/EntityTreeElement.h index 2504f947bd..05d1904384 100644 --- a/libraries/entities/src/EntityTreeElement.h +++ b/libraries/entities/src/EntityTreeElement.h @@ -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); diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index 5a2465bfcd..6ff4098ba8 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -16,6 +16,7 @@ ObjectAction::ObjectAction(QUuid id, EntityItemPointer ownerEntity) : btActionInterface(), _id(id), + _active(false), _ownerEntity(ownerEntity) { } diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index b819f25515..10b086c07d 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -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; }; diff --git a/libraries/physics/src/ObjectActionPullToPoint.cpp b/libraries/physics/src/ObjectActionPullToPoint.cpp index 70743904a0..85bde3161c 100644 --- a/libraries/physics/src/ObjectActionPullToPoint.cpp +++ b/libraries/physics/src/ObjectActionPullToPoint.cpp @@ -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(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; diff --git a/libraries/physics/src/ObjectActionPullToPoint.h b/libraries/physics/src/ObjectActionPullToPoint.h index 78005da5bd..3aca70d640 100644 --- a/libraries/physics/src/ObjectActionPullToPoint.h +++ b/libraries/physics/src/ObjectActionPullToPoint.h @@ -26,6 +26,7 @@ public: virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep); private: + glm::vec3 _target; float _speed; };