From 051616d7c3045d14132ae142453bf3066efc3ad5 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Fri, 8 Jul 2016 16:16:06 -0700 Subject: [PATCH 01/13] experimenting --- interface/src/avatar/AvatarActionHold.cpp | 2 +- scripts/system/controllers/handControllerGrab.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index c84cfecb40..1babb478b8 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -168,7 +168,7 @@ bool AvatarActionHold::getTarget(float deltaTimeStep, glm::quat& rotation, glm:: position = palmPosition + rotation * _relativePosition; // update linearVelocity based on offset via _relativePosition; - linearVelocity = linearVelocity + glm::cross(angularVelocity, position - palmPosition); + // linearVelocity = linearVelocity + glm::cross(angularVelocity, position - palmPosition); }); return true; diff --git a/scripts/system/controllers/handControllerGrab.js b/scripts/system/controllers/handControllerGrab.js index 373f203e80..4e4269df28 100644 --- a/scripts/system/controllers/handControllerGrab.js +++ b/scripts/system/controllers/handControllerGrab.js @@ -13,7 +13,7 @@ /* global setEntityCustomData, getEntityCustomData, vec3toStr, flatten, Xform */ Script.include("/~/system/libraries/utils.js"); -Script.include("../libraries/Xform.js"); +Script.include("/~/system/libraries/Xform.js"); // // add lines where the hand ray picking is happening From eac144f35476762dfb6d99155320d22a00445b69 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 11 Jul 2016 09:52:24 -0700 Subject: [PATCH 02/13] experimenting --- interface/src/avatar/AvatarActionHold.cpp | 9 ++++++++- interface/src/avatar/AvatarActionHold.h | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 1babb478b8..2baaae6ee4 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -204,8 +204,15 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { } withWriteLock([&]{ + if (_previousSet) { + _measuredLinearVelocity = (_positionalTarget - _previousPositionalTarget) / deltaTimeStep; + } else { + _measuredLinearVelocity = glm::vec3(); + } + if (_kinematicSetVelocity) { - rigidBody->setLinearVelocity(glmToBullet(_linearVelocityTarget)); + // rigidBody->setLinearVelocity(glmToBullet(_linearVelocityTarget)); + rigidBody->setLinearVelocity(glmToBullet(_measuredLinearVelocity)); rigidBody->setAngularVelocity(glmToBullet(_angularVelocityTarget)); } diff --git a/interface/src/avatar/AvatarActionHold.h b/interface/src/avatar/AvatarActionHold.h index 609fd57ff3..e246ac5f36 100644 --- a/interface/src/avatar/AvatarActionHold.h +++ b/interface/src/avatar/AvatarActionHold.h @@ -64,6 +64,8 @@ private: glm::vec3 _palmOffsetFromRigidBody; // leaving this here for future refernece. // glm::quat _palmRotationFromRigidBody; + + glm::vec3 _measuredLinearVelocity; }; #endif // hifi_AvatarActionHold_h From 7295e5c101fe1c0fe06b605f8087d7da53738059 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 11 Jul 2016 10:22:44 -0700 Subject: [PATCH 03/13] experimenting --- interface/src/avatar/AvatarActionHold.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 2baaae6ee4..d1750ddf53 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -206,6 +206,8 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { withWriteLock([&]{ if (_previousSet) { _measuredLinearVelocity = (_positionalTarget - _previousPositionalTarget) / deltaTimeStep; + qDebug() << _measuredLinearVelocity.x << _measuredLinearVelocity.y << _measuredLinearVelocity.z + << deltaTimeStep; } else { _measuredLinearVelocity = glm::vec3(); } From 53a366d4e882c8255aeabc45f8c327f3e9c5671d Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 11 Jul 2016 13:25:00 -0700 Subject: [PATCH 04/13] measure velocity over 6 frames --- interface/src/avatar/AvatarActionHold.cpp | 23 ++++++++++++++++------- interface/src/avatar/AvatarActionHold.h | 4 +++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index d1750ddf53..ce0cf886d5 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -17,11 +17,14 @@ #include "CharacterController.h" const uint16_t AvatarActionHold::holdVersion = 1; +const int AvatarActionHold::velocitySmoothFrames = 6; + AvatarActionHold::AvatarActionHold(const QUuid& id, EntityItemPointer ownerEntity) : ObjectActionSpring(id, ownerEntity) { _type = ACTION_TYPE_HOLD; + _measuredLinearVelocities.resize(AvatarActionHold::velocitySmoothFrames); #if WANT_DEBUG qDebug() << "AvatarActionHold::AvatarActionHold"; #endif @@ -168,7 +171,7 @@ bool AvatarActionHold::getTarget(float deltaTimeStep, glm::quat& rotation, glm:: position = palmPosition + rotation * _relativePosition; // update linearVelocity based on offset via _relativePosition; - // linearVelocity = linearVelocity + glm::cross(angularVelocity, position - palmPosition); + linearVelocity = linearVelocity + glm::cross(angularVelocity, position - palmPosition); }); return true; @@ -205,16 +208,22 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { withWriteLock([&]{ if (_previousSet) { - _measuredLinearVelocity = (_positionalTarget - _previousPositionalTarget) / deltaTimeStep; - qDebug() << _measuredLinearVelocity.x << _measuredLinearVelocity.y << _measuredLinearVelocity.z - << deltaTimeStep; - } else { - _measuredLinearVelocity = glm::vec3(); + glm::vec3 oneFrameVelocity = (_positionalTarget - _previousPositionalTarget) / deltaTimeStep; + _measuredLinearVelocities[_measuredLinearVelocitiesIndex++] = oneFrameVelocity; + if (_measuredLinearVelocitiesIndex >= AvatarActionHold::velocitySmoothFrames) { + _measuredLinearVelocitiesIndex = 0; + } } + glm::vec3 measuredLinearVelocity; + for (int i = 0; i < AvatarActionHold::velocitySmoothFrames; i++) { + measuredLinearVelocity += _measuredLinearVelocities[i]; + } + measuredLinearVelocity /= (float)AvatarActionHold::velocitySmoothFrames; + if (_kinematicSetVelocity) { // rigidBody->setLinearVelocity(glmToBullet(_linearVelocityTarget)); - rigidBody->setLinearVelocity(glmToBullet(_measuredLinearVelocity)); + rigidBody->setLinearVelocity(glmToBullet(measuredLinearVelocity)); rigidBody->setAngularVelocity(glmToBullet(_angularVelocityTarget)); } diff --git a/interface/src/avatar/AvatarActionHold.h b/interface/src/avatar/AvatarActionHold.h index e246ac5f36..bfa392172d 100644 --- a/interface/src/avatar/AvatarActionHold.h +++ b/interface/src/avatar/AvatarActionHold.h @@ -65,7 +65,9 @@ private: // leaving this here for future refernece. // glm::quat _palmRotationFromRigidBody; - glm::vec3 _measuredLinearVelocity; + static const int velocitySmoothFrames; + QVector _measuredLinearVelocities; + int _measuredLinearVelocitiesIndex { 0 }; }; #endif // hifi_AvatarActionHold_h From 9c17890035422ecbc0bc0031ecc72f8d6e699c0f Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 11 Jul 2016 14:18:28 -0700 Subject: [PATCH 05/13] try not including the most recent sample in the smoothed velocity --- interface/src/avatar/AvatarActionHold.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index ce0cf886d5..4583813131 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -207,6 +207,15 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { } withWriteLock([&]{ + glm::vec3 measuredLinearVelocity; + // there is a bit of lag between when someone releases the trigger and the software reacts to + // the release. we calculate the velocity from previous frames but not this frame (by having + // this code before where _measuredLinearVelocities is set, below) in order to help mask this. + for (int i = 0; i < AvatarActionHold::velocitySmoothFrames; i++) { + measuredLinearVelocity += _measuredLinearVelocities[i]; + } + measuredLinearVelocity /= (float)AvatarActionHold::velocitySmoothFrames; + if (_previousSet) { glm::vec3 oneFrameVelocity = (_positionalTarget - _previousPositionalTarget) / deltaTimeStep; _measuredLinearVelocities[_measuredLinearVelocitiesIndex++] = oneFrameVelocity; @@ -215,12 +224,6 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { } } - glm::vec3 measuredLinearVelocity; - for (int i = 0; i < AvatarActionHold::velocitySmoothFrames; i++) { - measuredLinearVelocity += _measuredLinearVelocities[i]; - } - measuredLinearVelocity /= (float)AvatarActionHold::velocitySmoothFrames; - if (_kinematicSetVelocity) { // rigidBody->setLinearVelocity(glmToBullet(_linearVelocityTarget)); rigidBody->setLinearVelocity(glmToBullet(measuredLinearVelocity)); From b0f1492ac7b3844fd5440dc9542069bdfe687ea5 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 11 Jul 2016 15:08:00 -0700 Subject: [PATCH 06/13] ignore 3 frames for current velocity rather than just 1 --- interface/src/avatar/AvatarActionHold.cpp | 34 +++++++++++++++++------ 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 4583813131..3ab81c5462 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -207,15 +207,6 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { } withWriteLock([&]{ - glm::vec3 measuredLinearVelocity; - // there is a bit of lag between when someone releases the trigger and the software reacts to - // the release. we calculate the velocity from previous frames but not this frame (by having - // this code before where _measuredLinearVelocities is set, below) in order to help mask this. - for (int i = 0; i < AvatarActionHold::velocitySmoothFrames; i++) { - measuredLinearVelocity += _measuredLinearVelocities[i]; - } - measuredLinearVelocity /= (float)AvatarActionHold::velocitySmoothFrames; - if (_previousSet) { glm::vec3 oneFrameVelocity = (_positionalTarget - _previousPositionalTarget) / deltaTimeStep; _measuredLinearVelocities[_measuredLinearVelocitiesIndex++] = oneFrameVelocity; @@ -224,6 +215,31 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { } } + glm::vec3 measuredLinearVelocity; + for (int i = 0; i < AvatarActionHold::velocitySmoothFrames; i++) { + // there is a bit of lag between when someone releases the trigger and when the software reacts to + // the release. we calculate the velocity from previous frames but we don't include several + // of the most recent. + // + // if _measuredLinearVelocitiesIndex is + // 0 -- ignore i of 3 4 5 + // 1 -- ignore i of 4 5 0 + // 2 -- ignore i of 5 0 1 + // 3 -- ignore i of 0 1 2 + // 4 -- ignore i of 1 2 3 + // 5 -- ignore i of 2 3 4 + if ((i + 1) % 6 == _measuredLinearVelocitiesIndex || + (i + 2) % 6 == _measuredLinearVelocitiesIndex || + (i + 3) % 6 == _measuredLinearVelocitiesIndex) { + continue; + } + + measuredLinearVelocity += _measuredLinearVelocities[i]; + } + measuredLinearVelocity /= (float)AvatarActionHold::velocitySmoothFrames; + + + if (_kinematicSetVelocity) { // rigidBody->setLinearVelocity(glmToBullet(_linearVelocityTarget)); rigidBody->setLinearVelocity(glmToBullet(measuredLinearVelocity)); From 1ef26d39eb43162f1fe5253841ab5ad56bf79a8c Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 11 Jul 2016 15:31:15 -0700 Subject: [PATCH 07/13] blah --- interface/src/avatar/AvatarActionHold.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 3ab81c5462..ce623cf604 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -236,7 +236,7 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { measuredLinearVelocity += _measuredLinearVelocities[i]; } - measuredLinearVelocity /= (float)AvatarActionHold::velocitySmoothFrames; + measuredLinearVelocity /= (float)(AvatarActionHold::velocitySmoothFrames - 3); From 0d0e0925e5b27f4944bc4cc074cf3e333da49386 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 11 Jul 2016 16:00:55 -0700 Subject: [PATCH 08/13] try skipping 2 rather than 3 --- interface/src/avatar/AvatarActionHold.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index ce623cf604..3bbdf9f099 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -222,21 +222,20 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { // of the most recent. // // if _measuredLinearVelocitiesIndex is - // 0 -- ignore i of 3 4 5 - // 1 -- ignore i of 4 5 0 - // 2 -- ignore i of 5 0 1 - // 3 -- ignore i of 0 1 2 - // 4 -- ignore i of 1 2 3 - // 5 -- ignore i of 2 3 4 + // 0 -- ignore i of 4 5 + // 1 -- ignore i of 5 0 + // 2 -- ignore i of 0 1 + // 3 -- ignore i of 1 2 + // 4 -- ignore i of 2 3 + // 5 -- ignore i of 3 4 if ((i + 1) % 6 == _measuredLinearVelocitiesIndex || - (i + 2) % 6 == _measuredLinearVelocitiesIndex || - (i + 3) % 6 == _measuredLinearVelocitiesIndex) { + (i + 2) % 6 == _measuredLinearVelocitiesIndex) { continue; } measuredLinearVelocity += _measuredLinearVelocities[i]; } - measuredLinearVelocity /= (float)(AvatarActionHold::velocitySmoothFrames - 3); + measuredLinearVelocity /= (float)(AvatarActionHold::velocitySmoothFrames - 2); From 84c5bef487fe6d16e38c3aa4e8ffd9b6f5802f8a Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 11 Jul 2016 17:02:11 -0700 Subject: [PATCH 09/13] back to ignore more recent 3 frames. don't have released entities collide with myAvatar until 0.25 seconds after release --- interface/src/avatar/AvatarActionHold.cpp | 20 ++++++++--------- .../system/controllers/handControllerGrab.js | 22 ++++++++++++++++++- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 3bbdf9f099..32905361dd 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -222,22 +222,20 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { // of the most recent. // // if _measuredLinearVelocitiesIndex is - // 0 -- ignore i of 4 5 - // 1 -- ignore i of 5 0 - // 2 -- ignore i of 0 1 - // 3 -- ignore i of 1 2 - // 4 -- ignore i of 2 3 - // 5 -- ignore i of 3 4 + // 0 -- ignore i of 3 4 5 + // 1 -- ignore i of 4 5 0 + // 2 -- ignore i of 5 0 1 + // 3 -- ignore i of 0 1 2 + // 4 -- ignore i of 1 2 3 + // 5 -- ignore i of 2 3 4 if ((i + 1) % 6 == _measuredLinearVelocitiesIndex || - (i + 2) % 6 == _measuredLinearVelocitiesIndex) { + (i + 2) % 6 == _measuredLinearVelocitiesIndex || + (i + 3) % 6 == _measuredLinearVelocitiesIndex) { continue; } - measuredLinearVelocity += _measuredLinearVelocities[i]; } - measuredLinearVelocity /= (float)(AvatarActionHold::velocitySmoothFrames - 2); - - + measuredLinearVelocity /= (float)(AvatarActionHold::velocitySmoothFrames - 3); if (_kinematicSetVelocity) { // rigidBody->setLinearVelocity(glmToBullet(_linearVelocityTarget)); diff --git a/scripts/system/controllers/handControllerGrab.js b/scripts/system/controllers/handControllerGrab.js index 2cef48290c..36cef8ec2d 100644 --- a/scripts/system/controllers/handControllerGrab.js +++ b/scripts/system/controllers/handControllerGrab.js @@ -33,6 +33,8 @@ var TRIGGER_ON_VALUE = 0.4; // Squeezed just enough to activate search or near var TRIGGER_GRAB_VALUE = 0.85; // Squeezed far enough to complete distant grab var TRIGGER_OFF_VALUE = 0.15; +var COLLIDE_WITH_AV_AFTER_RELEASE_DELAY = 0.25; // seconds + var BUMPER_ON_VALUE = 0.5; var THUMB_ON_VALUE = 0.5; @@ -263,6 +265,17 @@ function propsArePhysical(props) { return isPhysical; } +function removeMyAvatarFromCollidesWith(origCollidesWith) { + var collidesWithSplit = origCollidesWith.split(","); + // remove myAvatar from the array + for (var i = collidesWithSplit.length - 1; i >= 0; i--) { + if (collidesWithSplit[i] === "myAvatar") { + collidesWithSplit.splice(i, 1); + } + } + return collidesWithSplit.join(); +} + // If another script is managing the reticle (as is done by HandControllerPointer), we should not be setting it here, // and we should not be showing lasers when someone else is using the Reticle to indicate a 2D minor mode. var EXTERNALLY_MANAGED_2D_MINOR_MODE = true; @@ -2128,13 +2141,20 @@ function MyController(hand) { if (data["refCount"] < 1) { deactiveProps = { gravity: data["gravity"], - collidesWith: data["collidesWith"], + // don't set collidesWith back right away, because thrown things tend to bounce off the + // avatar's capsule. + collidesWith: removeMyAvatarFromCollidesWith(data["collidesWith"]), collisionless: data["collisionless"], dynamic: data["dynamic"], parentID: data["parentID"], parentJointIndex: data["parentJointIndex"] }; + Script.setTimeout(function () { + // set collidesWith back to original value a bit later than the rest + Entities.editEntity(entityID, { collidesWith: data["collidesWith"] }); + }, COLLIDE_WITH_AV_AFTER_RELEASE_DELAY); + // things that are held by parenting and dropped with no velocity will end up as "static" in bullet. If // it looks like the dropped thing should fall, give it a little velocity. var props = Entities.getEntityProperties(entityID, ["parentID", "velocity", "dynamic", "shapeType"]); From 367c26a5eef1ba352ac5178d476b5b85cc5412bd Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 11 Jul 2016 18:53:47 -0700 Subject: [PATCH 10/13] wait a bit before resetting collides-with-my-avatar --- .../system/controllers/handControllerGrab.js | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/scripts/system/controllers/handControllerGrab.js b/scripts/system/controllers/handControllerGrab.js index 36cef8ec2d..aaaa7c5962 100644 --- a/scripts/system/controllers/handControllerGrab.js +++ b/scripts/system/controllers/handControllerGrab.js @@ -181,6 +181,10 @@ var COLLIDES_WITH_WHILE_MULTI_GRABBED = "dynamic"; var HEART_BEAT_INTERVAL = 5 * MSECS_PER_SEC; var HEART_BEAT_TIMEOUT = 15 * MSECS_PER_SEC; +var avCollideLater; +var setCollidesLaterTimeout; +var collideLaterID; + var CONTROLLER_STATE_MACHINE = {}; CONTROLLER_STATE_MACHINE[STATE_OFF] = { @@ -2060,6 +2064,17 @@ function MyController(hand) { } this.entityActivated = true; + if (setCollidesLaterTimeout && collideLaterID == entityID) { + // we have a timeout waiting to set collisions with myAvatar back on (so that when something + // is thrown it doesn't collide with the avatar's capsule the moment it's released). We've + // regrabbed the entity before the timeout fired, so cancel the timeout, run the function now + // and adjust the grabbedProperties. This will make the saved set of properties (the ones that + // get re-instated after all the grabs have been released) be correct. + Script.clearTimeout(setCollidesLaterTimeout); + setCollidesLaterTimeout = null; + grabbedProperties["collidesWith"] = avCollideLater(); + } + var data = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, {}); var now = Date.now(); @@ -2150,10 +2165,18 @@ function MyController(hand) { parentJointIndex: data["parentJointIndex"] }; - Script.setTimeout(function () { - // set collidesWith back to original value a bit later than the rest - Entities.editEntity(entityID, { collidesWith: data["collidesWith"] }); - }, COLLIDE_WITH_AV_AFTER_RELEASE_DELAY); + if (data["collidesWith"].indexOf("myAvatar") >= 0) { + var javaScriptScopingIsBrokenData = data; + avCollideLater = function () { + // set collidesWith back to original value a bit later than the rest + _this.setCollidesLaterTimeout = null; + Entities.editEntity(entityID, { collidesWith: javaScriptScopingIsBrokenData["collidesWith"] }); + return javaScriptScopingIsBrokenData["collidesWith"]; + } + setCollidesLaterTimeout = + Script.setTimeout(avCollideLater, COLLIDE_WITH_AV_AFTER_RELEASE_DELAY * MSECS_PER_SEC); + collideLaterID = entityID; + } // things that are held by parenting and dropped with no velocity will end up as "static" in bullet. If // it looks like the dropped thing should fall, give it a little velocity. From 19991939073353002b098df01a6d193e67890832 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 12 Jul 2016 11:42:58 -0700 Subject: [PATCH 11/13] split the grab script's deactivation on an entity into two parts -- one that happens during release and a delayed part which finishes up. This makes collisions with a thrower's capsule less likely --- .../system/controllers/handControllerGrab.js | 65 +++++++++++++------ 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/scripts/system/controllers/handControllerGrab.js b/scripts/system/controllers/handControllerGrab.js index aaaa7c5962..ac903190c4 100644 --- a/scripts/system/controllers/handControllerGrab.js +++ b/scripts/system/controllers/handControllerGrab.js @@ -181,9 +181,9 @@ var COLLIDES_WITH_WHILE_MULTI_GRABBED = "dynamic"; var HEART_BEAT_INTERVAL = 5 * MSECS_PER_SEC; var HEART_BEAT_TIMEOUT = 15 * MSECS_PER_SEC; -var avCollideLater; -var setCollidesLaterTimeout; -var collideLaterID; +var delayedDeactivateFunc; +var delayedDeactivateTimeout; +var delayedDeactivateEntityID; var CONTROLLER_STATE_MACHINE = {}; @@ -2064,15 +2064,15 @@ function MyController(hand) { } this.entityActivated = true; - if (setCollidesLaterTimeout && collideLaterID == entityID) { + if (delayedDeactivateTimeout && delayedDeactivateEntityID == entityID) { // we have a timeout waiting to set collisions with myAvatar back on (so that when something // is thrown it doesn't collide with the avatar's capsule the moment it's released). We've // regrabbed the entity before the timeout fired, so cancel the timeout, run the function now // and adjust the grabbedProperties. This will make the saved set of properties (the ones that // get re-instated after all the grabs have been released) be correct. - Script.clearTimeout(setCollidesLaterTimeout); - setCollidesLaterTimeout = null; - grabbedProperties["collidesWith"] = avCollideLater(); + Script.clearTimeout(delayedDeactivateTimeout); + delayedDeactivateTimeout = null; + grabbedProperties["collidesWith"] = delayedDeactivateFunc(); } var data = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, {}); @@ -2142,7 +2142,27 @@ function MyController(hand) { }); }; - this.deactivateEntity = function (entityID, noVelocity) { + this.delayedDeactivateEntity = function (entityID, collidesWith) { + // If, before the grab started, the held entity collided with myAvatar, we do the deactivation in + // two parts. Most of it is done in deactivateEntity(), but the final collidesWith and refcount + // are delayed a bit. This keeps thrown things from colliding with the avatar's capsule so often. + // The refcount is handled in this delayed fashion so things don't get confused if someone else + // grabs the entity before the timeout fires. + Entities.editEntity(entityID, { collidesWith: collidesWith }); + var data = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, {}); + if (data && data["refCount"]) { + data["refCount"] = data["refCount"] - 1; + if (data["refCount"] < 1) { + data = null; + } + } else { + data = null; + } + + setEntityCustomData(GRAB_USER_DATA_KEY, entityID, data); + }; + + this.deactivateEntity = function (entityID, noVelocity, delayed) { var deactiveProps; if (!this.entityActivated) { @@ -2151,12 +2171,13 @@ function MyController(hand) { this.entityActivated = false; var data = getEntityCustomData(GRAB_USER_DATA_KEY, entityID, {}); + var doDelayedDeactivate = false; if (data && data["refCount"]) { data["refCount"] = data["refCount"] - 1; if (data["refCount"] < 1) { deactiveProps = { gravity: data["gravity"], - // don't set collidesWith back right away, because thrown things tend to bounce off the + // don't set collidesWith myAvatar back right away, because thrown things tend to bounce off the // avatar's capsule. collidesWith: removeMyAvatarFromCollidesWith(data["collidesWith"]), collisionless: data["collisionless"], @@ -2165,17 +2186,20 @@ function MyController(hand) { parentJointIndex: data["parentJointIndex"] }; - if (data["collidesWith"].indexOf("myAvatar") >= 0) { - var javaScriptScopingIsBrokenData = data; - avCollideLater = function () { + doDelayedDeactivate = (data["collidesWith"].indexOf("myAvatar") >= 0); + + if (doDelayedDeactivate) { + var delayedCollidesWith = data["collidesWith"]; + var delayedEntityID = entityID; + delayedDeactivateFunc = function () { // set collidesWith back to original value a bit later than the rest - _this.setCollidesLaterTimeout = null; - Entities.editEntity(entityID, { collidesWith: javaScriptScopingIsBrokenData["collidesWith"] }); - return javaScriptScopingIsBrokenData["collidesWith"]; + delayedDeactivateTimeout = null; + _this.delayedDeactivateEntity(delayedEntityID, delayedCollidesWith); + return delayedCollidesWith; } - setCollidesLaterTimeout = - Script.setTimeout(avCollideLater, COLLIDE_WITH_AV_AFTER_RELEASE_DELAY * MSECS_PER_SEC); - collideLaterID = entityID; + delayedDeactivateTimeout = + Script.setTimeout(delayedDeactivateFunc, COLLIDE_WITH_AV_AFTER_RELEASE_DELAY * MSECS_PER_SEC); + delayedDeactivateEntityID = entityID; } // things that are held by parenting and dropped with no velocity will end up as "static" in bullet. If @@ -2220,7 +2244,6 @@ function MyController(hand) { // angularVelocity: this.currentAngularVelocity }); } - data = null; } else if (this.shouldResetParentOnRelease) { // we parent-grabbed this from another parent grab. try to put it back where we found it. @@ -2239,7 +2262,9 @@ function MyController(hand) { } else { data = null; } - setEntityCustomData(GRAB_USER_DATA_KEY, entityID, data); + if (!doDelayedDeactivate) { + setEntityCustomData(GRAB_USER_DATA_KEY, entityID, data); + } }; this.getOtherHandController = function () { From 6c14ef3392dbdbef2ffa577dabf567f22494d6c6 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 12 Jul 2016 11:56:57 -0700 Subject: [PATCH 12/13] added a comment --- interface/src/avatar/AvatarActionHold.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 32905361dd..a2130240f5 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -235,7 +235,7 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { } measuredLinearVelocity += _measuredLinearVelocities[i]; } - measuredLinearVelocity /= (float)(AvatarActionHold::velocitySmoothFrames - 3); + measuredLinearVelocity /= (float)(AvatarActionHold::velocitySmoothFrames - 3); // 3 because of the 3 we skipped, above if (_kinematicSetVelocity) { // rigidBody->setLinearVelocity(glmToBullet(_linearVelocityTarget)); From 0dd2171b759c1c135485501c32d1b80ada52354b Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 12 Jul 2016 11:57:57 -0700 Subject: [PATCH 13/13] remove commented-out code --- interface/src/avatar/AvatarActionHold.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index a2130240f5..bac3b1e02f 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -238,7 +238,6 @@ void AvatarActionHold::doKinematicUpdate(float deltaTimeStep) { measuredLinearVelocity /= (float)(AvatarActionHold::velocitySmoothFrames - 3); // 3 because of the 3 we skipped, above if (_kinematicSetVelocity) { - // rigidBody->setLinearVelocity(glmToBullet(_linearVelocityTarget)); rigidBody->setLinearVelocity(glmToBullet(measuredLinearVelocity)); rigidBody->setAngularVelocity(glmToBullet(_angularVelocityTarget)); }