From b8587e0711591ac852739ce300217a7a37cd405d Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 4 Apr 2016 16:06:53 -0700 Subject: [PATCH 1/6] update collision shapes of entities which are children of avatars --- libraries/entities/src/EntityItem.cpp | 6 +++++- libraries/physics/src/EntityMotionState.cpp | 7 ++++++- libraries/shared/src/SpatiallyNestable.cpp | 15 +++++++++++++++ libraries/shared/src/SpatiallyNestable.h | 2 ++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 9fa58c9187..6306277431 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1963,7 +1963,11 @@ QList EntityItem::getActionsOfType(EntityActionType typeToG } void EntityItem::locationChanged() { - requiresRecalcBoxes(); + _dirtyFlags |= Simulation::DIRTY_TRANSFORM; + EntityTreePointer tree = getTree(); + if (tree) { + tree->entityChanged(getThisPointer()); + } SpatiallyNestable::locationChanged(); // tell all the children, also } diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 5f168d2e33..f232827fc2 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -159,7 +159,12 @@ PhysicsMotionType EntityMotionState::computePhysicsMotionType() const { } return MOTION_TYPE_DYNAMIC; } - return (_entity->isMovingRelativeToParent() || _entity->hasActions()) ? MOTION_TYPE_KINEMATIC : MOTION_TYPE_STATIC; + if (_entity->isMovingRelativeToParent() || + _entity->hasActions() || + _entity->hasAncestorOfType(NestableType::Avatar)) { + return MOTION_TYPE_KINEMATIC; + } + return MOTION_TYPE_STATIC; } bool EntityMotionState::isMoving() const { diff --git a/libraries/shared/src/SpatiallyNestable.cpp b/libraries/shared/src/SpatiallyNestable.cpp index 3cbd3c5ac9..dfe1f2c0e4 100644 --- a/libraries/shared/src/SpatiallyNestable.cpp +++ b/libraries/shared/src/SpatiallyNestable.cpp @@ -879,3 +879,18 @@ void SpatiallyNestable::setLocalTransformAndVelocities( }); locationChanged(); } + + +bool SpatiallyNestable::hasAncestorOfType(NestableType nestableType) { + if (_nestableType == nestableType) { + return true; + } + + bool success; + SpatiallyNestablePointer parent = getParentPointer(success); + if (!success || !parent) { + return false; + } + + return parent->hasAncestorOfType(nestableType); +} diff --git a/libraries/shared/src/SpatiallyNestable.h b/libraries/shared/src/SpatiallyNestable.h index ef70d0231b..8b225d5ae8 100644 --- a/libraries/shared/src/SpatiallyNestable.h +++ b/libraries/shared/src/SpatiallyNestable.h @@ -142,6 +142,8 @@ public: bool isParentIDValid() const { bool success = false; getParentPointer(success); return success; } virtual SpatialParentTree* getParentTree() const { return nullptr; } + bool hasAncestorOfType(NestableType nestableType); + protected: const NestableType _nestableType; // EntityItem or an AvatarData QUuid _id; From cecb3a6ff12eeb3cf12e174b91cd391dbe009629 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 4 Apr 2016 17:07:08 -0700 Subject: [PATCH 2/6] unmangle merge --- libraries/shared/src/SpatiallyNestable.cpp | 38 ---------------------- 1 file changed, 38 deletions(-) diff --git a/libraries/shared/src/SpatiallyNestable.cpp b/libraries/shared/src/SpatiallyNestable.cpp index 22d423a49e..3275420f5d 100644 --- a/libraries/shared/src/SpatiallyNestable.cpp +++ b/libraries/shared/src/SpatiallyNestable.cpp @@ -849,44 +849,6 @@ AACube SpatiallyNestable::getQueryAACube() const { } return result; } -<<<<<<< HEAD - -void SpatiallyNestable::getLocalTransformAndVelocities( - Transform& transform, - glm::vec3& velocity, - glm::vec3& angularVelocity) const { - // transform - _transformLock.withReadLock([&] { - transform = _transform; - }); - // linear velocity - _velocityLock.withReadLock([&] { - velocity = _velocity; - }); - // angular velocity - _angularVelocityLock.withReadLock([&] { - angularVelocity = _angularVelocity; - }); -} - -void SpatiallyNestable::setLocalTransformAndVelocities( - const Transform& localTransform, - const glm::vec3& localVelocity, - const glm::vec3& localAngularVelocity) { - // transform - _transformLock.withWriteLock([&] { - _transform = localTransform; - }); - // linear velocity - _velocityLock.withWriteLock([&] { - _velocity = localVelocity; - }); - // angular velocity - _angularVelocityLock.withWriteLock([&] { - _angularVelocity = localAngularVelocity; - }); - locationChanged(); -} bool SpatiallyNestable::hasAncestorOfType(NestableType nestableType) { if (_nestableType == nestableType) { From af05b97c1e9418a727053c155875d8085ab8c78e Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 4 Apr 2016 17:31:59 -0700 Subject: [PATCH 3/6] put back accidently deleted line. cleanups --- libraries/entities/src/EntityItem.cpp | 2 ++ libraries/physics/src/EntityMotionState.cpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index acdcc4a8a2..17b4e6a87d 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1974,6 +1974,7 @@ QList EntityItem::getActionsOfType(EntityActionType typeToG } void EntityItem::locationChanged() { + requiresRecalcBoxes(); _dirtyFlags |= Simulation::DIRTY_TRANSFORM; EntityTreePointer tree = getTree(); if (tree) { @@ -1988,6 +1989,7 @@ void EntityItem::dimensionsChanged() { } void EntityItem::globalizeProperties(EntityItemProperties& properties, const QString& messageTemplate, const glm::vec3& offset) const { + // TODO -- combine this with convertLocationToScriptSemantics bool success; auto globalPosition = getPosition(success); if (success) { diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 6f6094c99b..700160514c 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -162,7 +162,7 @@ PhysicsMotionType EntityMotionState::computePhysicsMotionType() const { return MOTION_TYPE_DYNAMIC; } if (_entity->isMovingRelativeToParent() || - _entity->hasActions() || + _entity->hasActions() || _entity->hasAncestorOfType(NestableType::Avatar)) { return MOTION_TYPE_KINEMATIC; } From 294bdcb18eaaa24f7df5b5d3db077b28e033a274 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Mon, 4 Apr 2016 17:41:01 -0700 Subject: [PATCH 4/6] has-ancestor should test self --- libraries/shared/src/SpatiallyNestable.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/shared/src/SpatiallyNestable.cpp b/libraries/shared/src/SpatiallyNestable.cpp index 3275420f5d..96a5a1d8ae 100644 --- a/libraries/shared/src/SpatiallyNestable.cpp +++ b/libraries/shared/src/SpatiallyNestable.cpp @@ -851,15 +851,15 @@ AACube SpatiallyNestable::getQueryAACube() const { } bool SpatiallyNestable::hasAncestorOfType(NestableType nestableType) { - if (_nestableType == nestableType) { - return true; - } - bool success; SpatiallyNestablePointer parent = getParentPointer(success); if (!success || !parent) { return false; } + if (parent->_nestableType == nestableType) { + return true; + } + return parent->hasAncestorOfType(nestableType); } From 5375fdd0f05acd0dee050c2b00d9d708a51e41a6 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 6 Apr 2016 16:42:14 -0700 Subject: [PATCH 5/6] don't do simple kinematic simulation on things that have parents --- libraries/entities/src/EntityItem.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 17b4e6a87d..4cd8a34248 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -888,6 +888,9 @@ void EntityItem::simulateKinematicMotion(float timeElapsed, bool setFlags) { if (hasActions()) { return; } + if (!_parentID.isNull()) { + return; + } if (hasLocalAngularVelocity()) { glm::vec3 localAngularVelocity = getLocalAngularVelocity(); From 45733478da8f14d5bfbd5b41e92fe3a90adf9770 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 7 Apr 2016 13:15:39 -0700 Subject: [PATCH 6/6] make auto-unequip code smarter -- equipping a long thing by the end is now possible --- examples/controllers/handControllerGrab.js | 41 +++++++++++++++------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 2e9129f3ac..4b20651899 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -12,7 +12,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html /*global print, MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt, pointInExtents, vec3equal, setEntityCustomData, getEntityCustomData */ -Script.include("../libraries/utils.js"); +Script.include("/~/libraries/utils.js"); // @@ -79,6 +79,7 @@ var NEAR_PICK_MAX_DISTANCE = 0.3; // max length of pick-ray for close grabbing t var PICK_BACKOFF_DISTANCE = 0.2; // helps when hand is intersecting the grabble object var NEAR_GRABBING_KINEMATIC = true; // force objects to be kinematic when near-grabbed var SHOW_GRAB_SPHERE = false; // draw a green sphere to show the grab search position and size +var CHECK_TOO_FAR_UNEQUIP_TIME = 1.0; // seconds // // equip @@ -290,11 +291,13 @@ function MyController(hand) { this.intersectionDistance = 0.0; this.searchSphereDistance = DEFAULT_SEARCH_SPHERE_DISTANCE; - this.ignoreIK = false; this.offsetPosition = Vec3.ZERO; this.offsetRotation = Quat.IDENTITY; + this.lastPickTime = 0; + this.lastUnequipCheckTime = 0; + var _this = this; this.update = function() { @@ -1523,18 +1526,30 @@ function MyController(hand) { return; } - if (props.parentID == MyAvatar.sessionUUID && - Vec3.length(props.localPosition) > NEAR_PICK_MAX_DISTANCE * 2.0) { - // for whatever reason, the held/equipped entity has been pulled away. ungrab or unequip. - print("handControllerGrab -- autoreleasing held or equipped item because it is far from hand." + - props.parentID + " " + vec3toStr(props.position)); - this.setState(STATE_RELEASE); - if (this.state == STATE_CONTINUE_NEAR_GRABBING) { - this.callEntityMethodOnGrabbed("releaseGrab"); - } else { // (this.state == STATE_CONTINUE_EQUIP || this.state == STATE_CONTINUE_HOLD) - this.callEntityMethodOnGrabbed("releaseEquip"); + + var now = Date.now(); + if (now - this.lastUnequipCheckTime > MSECS_PER_SEC * CHECK_TOO_FAR_UNEQUIP_TIME) { + this.lastUnequipCheckTime = now; + + if (props.parentID == MyAvatar.sessionUUID && + Vec3.length(props.localPosition) > NEAR_PICK_MAX_DISTANCE * 2.0) { + var handPosition = this.getHandPosition(); + // the center of the equipped object being far from the hand isn't enough to autoequip -- we also + // need to fail the findEntities test. + nearPickedCandidateEntities = Entities.findEntities(handPosition, GRAB_RADIUS); + if (nearPickedCandidateEntities.indexOf(this.grabbedEntity) == -1) { + // for whatever reason, the held/equipped entity has been pulled away. ungrab or unequip. + print("handControllerGrab -- autoreleasing held or equipped item because it is far from hand." + + props.parentID + " " + vec3toStr(props.position)); + this.setState(STATE_RELEASE); + if (this.state == STATE_CONTINUE_NEAR_GRABBING) { + this.callEntityMethodOnGrabbed("releaseGrab"); + } else { // (this.state == STATE_CONTINUE_EQUIP || this.state == STATE_CONTINUE_HOLD) + this.callEntityMethodOnGrabbed("releaseEquip"); + } + return; + } } - return; } // Keep track of the fingertip velocity to impart when we release the object.