From b34ea6ded499a401f92057b1b7ac5936ba82d52f Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 30 Sep 2015 13:46:23 -0700 Subject: [PATCH] store action lifetime as an expires integer --- examples/grab.js | 37 +++++++++++++++++++++++--- libraries/physics/src/ObjectAction.cpp | 20 +++++++------- libraries/physics/src/ObjectAction.h | 2 +- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/examples/grab.js b/examples/grab.js index 15bab17f20..52b2a66546 100644 --- a/examples/grab.js +++ b/examples/grab.js @@ -15,6 +15,33 @@ var ZERO_VEC3 = {x: 0, y: 0, z: 0}; var IDENTITY_QUAT = {x: 0, y: 0, z: 0, w: 0}; +if (typeof String.prototype.startsWith != 'function') { + // see below for better implementation! + String.prototype.startsWith = function (str){ + return this.indexOf(str) === 0; + }; +} + +function getTag() { + return "grab-" + MyAvatar.sessionUUID; +} + +function entityIsGrabbedByOther(entityID) { + var actionIDs = Entities.getActionIDs(entityID); + for (var actionIndex = 0; actionIndex < actionIDs.length; actionIndex++) { + var actionID = actionIDs[actionIndex]; + var actionArguments = Entities.getActionArguments(entityID, actionID); + var tag = actionArguments["tag"]; + if (tag == getTag()) { + continue; + } + if (tag.startsWith("grab-")) { + return true; + } + } + return false; +} + // helper function function mouseIntersectionWithPlane(pointOnPlane, planeNormal, event, maxDistance) { var cameraPosition = Camera.getPosition(); @@ -288,7 +315,7 @@ Grabber.prototype.moveEvent = function(event) { } this.currentPosition = entityProperties.position; - var actionArgs = {tag: "grab", lifetime: 5}; + var actionArgs = {tag: getTag(), lifetime: 5}; if (this.mode === "rotate") { var drag = mouse.getDrag(); @@ -303,7 +330,7 @@ Grabber.prototype.moveEvent = function(event) { // var qZero = entityProperties.rotation; //var qZero = this.lastRotation; this.lastRotation = Quat.multiply(deltaQ, this.lastRotation); - actionArgs = {targetRotation: this.lastRotation, angularTimeScale: 0.1, tag: "grab", lifetime: 5}; + actionArgs = {targetRotation: this.lastRotation, angularTimeScale: 0.1, tag: getTag(), lifetime: 5}; } else { var newPointOnPlane; if (this.mode === "verticalCylinder") { @@ -327,13 +354,15 @@ Grabber.prototype.moveEvent = function(event) { } } this.targetPosition = Vec3.subtract(newPointOnPlane, this.offset); - actionArgs = {targetPosition: this.targetPosition, linearTimeScale: 0.1, tag: "grab", lifetime: 5}; + actionArgs = {targetPosition: this.targetPosition, linearTimeScale: 0.1, tag: getTag(), lifetime: 5}; beacon.updatePosition(this.targetPosition); } if (!this.actionID) { - this.actionID = Entities.addAction("spring", this.entityID, actionArgs); + if (!entityIsGrabbedByOther(this.entityID)) { + this.actionID = Entities.addAction("spring", this.entityID, actionArgs); + } } else { Entities.updateAction(this.entityID, this.actionID, actionArgs); } diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index 8c382f8dad..82395c21cf 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -31,8 +31,8 @@ void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar delta return; } - if (_expires > 0.0f) { - float now = (float)usecTimestampNow() / USECS_PER_SECOND; + if (_expires > 0) { + quint64 now = usecTimestampNow(); if (now > _expires) { EntityItemPointer ownerEntity = _ownerEntity.lock(); _active = false; @@ -53,10 +53,10 @@ bool ObjectAction::updateArguments(QVariantMap arguments) { bool lifetimeSet = true; float lifetime = EntityActionInterface::extractFloatArgument("action", arguments, "lifetime", lifetimeSet, false); if (lifetimeSet) { - float now = (float)usecTimestampNow() / USECS_PER_SECOND; - _expires = now + lifetime; + quint64 now = usecTimestampNow(); + _expires = now + (quint64)(lifetime * USECS_PER_SECOND); } else { - _expires = 0.0f; + _expires = 0; } bool tagSet = true; @@ -72,11 +72,11 @@ bool ObjectAction::updateArguments(QVariantMap arguments) { QVariantMap ObjectAction::getArguments() { QVariantMap arguments; - float now = (float)usecTimestampNow() / USECS_PER_SECOND; - if (_expires == 0.0f) { + if (_expires == 0) { arguments["lifetime"] = 0.0f; } else { - arguments["lifetime"] = _expires - now; + quint64 now = usecTimestampNow(); + arguments["lifetime"] = (float)(_expires - now) / (float)USECS_PER_SECOND; } arguments["tag"] = _tag; return arguments; @@ -183,11 +183,11 @@ void ObjectAction::activateBody() { } bool ObjectAction::lifetimeIsOver() { - if (_expires == 0.0f) { + if (_expires == 0) { return false; } - float now = (float)usecTimestampNow() / USECS_PER_SECOND; + quint64 now = usecTimestampNow(); if (now >= _expires) { return true; } diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index 380263577a..5c29ac9892 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -64,7 +64,7 @@ protected: bool _active; EntityItemWeakPointer _ownerEntity; - float _expires; // in seconds since epoch + quint64 _expires; // in seconds since epoch QString _tag; };