From c2cbca88cbc0dda409abc2f120523f96b1b29ed2 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 9 Feb 2016 16:54:32 -0800 Subject: [PATCH] allow grabbing with one hand and then equipping with other --- examples/attachedEntitiesManager.js | 14 +++++++++----- examples/controllers/handControllerGrab.js | 11 +++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/examples/attachedEntitiesManager.js b/examples/attachedEntitiesManager.js index 8cd159ec14..b5d023c70c 100644 --- a/examples/attachedEntitiesManager.js +++ b/examples/attachedEntitiesManager.js @@ -148,7 +148,7 @@ function AttachedEntitiesManager() { this.checkIfWearable = function(grabbedEntity, releasedFromJoint) { var allowedJoints = getEntityCustomData('wearable', grabbedEntity, DEFAULT_WEARABLE_DATA).joints; - var props = Entities.getEntityProperties(grabbedEntity, ["position", "parentID"]); + var props = Entities.getEntityProperties(grabbedEntity, ["position", "parentID", "parentJointIndex"]); if (props.parentID === NULL_UUID || props.parentID === MyAvatar.sessionUUID) { var bestJointName = ""; var bestJointIndex = -1; @@ -192,10 +192,14 @@ function AttachedEntitiesManager() { } Entities.editEntity(grabbedEntity, wearProps); } else if (props.parentID != NULL_UUID) { - // drop the entity with no parent (not on the avatar) - Entities.editEntity(grabbedEntity, { - parentID: NULL_UUID - }); + // drop the entity and set it to have no parent (not on the avatar), unless it's being equipped in a hand. + if (props.parentID === MyAvatar.sessionUUID && + (props.parentJointIndex == MyAvatar.getJointIndex("RightHand") || + props.parentJointIndex == MyAvatar.getJointIndex("LeftHand"))) { + // this is equipped on a hand -- don't clear the parent. + } else { + Entities.editEntity(grabbedEntity, { parentID: NULL_UUID }); + } } } } diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 61c57fd86f..51814ad5d7 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -946,13 +946,15 @@ function MyController(hand) { return; } // near grab or equip with action - if (near && (grabbableData.refCount < 1 || entityHasActions(this.grabbedEntity))) { + var grabData = getEntityCustomData(GRAB_USER_DATA_KEY, this.grabbedEntity, {}); + var refCount = ("refCount" in grabData) ? grabData.refCount : 0; + if (near && (refCount < 1 || entityHasActions(this.grabbedEntity))) { this.setState(this.state == STATE_SEARCHING ? STATE_NEAR_GRABBING : STATE_EQUIP); return; } // far grab or equip with action if ((isPhysical || this.state == STATE_EQUIP_SEARCHING) && !near) { - if (entityIsGrabbedByOther(intersection.entityID)) { + if (entityIsGrabbedByOther(this.grabbedEntity)) { // don't distance grab something that is already grabbed. if (WANT_DEBUG_SEARCH_NAME && props.name == WANT_DEBUG_SEARCH_NAME) { print("grab is skipping '" + WANT_DEBUG_SEARCH_NAME + "': already grabbed by another."); @@ -1449,11 +1451,12 @@ function MyController(hand) { return; } - var props = Entities.getEntityProperties(this.grabbedEntity, ["localPosition", "parentID"]); + var props = Entities.getEntityProperties(this.grabbedEntity, ["localPosition", "parentID", "position"]); 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."); + print("handControllerGrab -- autoreleasing held or equipped item because it is far from hand." + + props.parentID + " " + vec3toStr(props.position)); this.setState(STATE_RELEASE); this.callEntityMethodOnGrabbed(this.state == STATE_NEAR_GRABBING ? "releaseGrab" : "releaseEquip", [JSON.stringify(this.hand)]);