From f16b62df83767642ee11f522b00ed1bdccb0317b Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 20 Oct 2015 17:34:35 -0700 Subject: [PATCH 1/5] - grab script: possible fix for line disappearing - grab script: go from 3 pickrays back to 1 - grab script: put "sphere" test back in, but with fixed distance test --- examples/controllers/handControllerGrab.js | 102 ++++++++++++--------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index f912761b43..9f174bd3e6 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -19,7 +19,7 @@ Script.include("../libraries/utils.js"); // // add lines where the hand ray picking is happening // -var DEBUG_HAND_RAY_PICKING = false; +var WANT_DEBUG = false; ///////////////////////////////////////////////////////////////// // @@ -193,22 +193,23 @@ function MyController(hand, triggerAction) { }; this.setState = function(newState) { - // print("STATE: " + this.state + " --> " + newState); + if (WANT_DEBUG) { + print("STATE: " + this.state + " --> " + newState); + } this.state = newState; } - this.debugLine = function(closePoint, farPoint, color){ - Entities.addEntity({ - type: "Line", - name: "Debug Line", - dimensions: LINE_ENTITY_DIMENSIONS, - visible: true, - position: closePoint, - linePoints: [ZERO_VEC, farPoint], - color: color, - lifetime: 0.1 - }); + Entities.addEntity({ + type: "Line", + name: "Debug Line", + dimensions: LINE_ENTITY_DIMENSIONS, + visible: true, + position: closePoint, + linePoints: [ZERO_VEC, farPoint], + color: color, + lifetime: 0.1 + }); } this.lineOn = function(closePoint, farPoint, color) { @@ -226,14 +227,13 @@ function MyController(hand, triggerAction) { }); } else { var age = Entities.getEntityProperties(this.pointer, "age").age; - Entities.editEntity(this.pointer, { + this.pointer = Entities.editEntity(this.pointer, { position: closePoint, linePoints: [ZERO_VEC, farPoint], color: color, lifetime: age + LIFETIME }); } - }; this.lineOff = function() { @@ -282,7 +282,6 @@ function MyController(hand, triggerAction) { return; } - // the trigger is being pressed, do a ray test var handPosition = this.getHandPosition(); var distantPickRay = { @@ -290,29 +289,17 @@ function MyController(hand, triggerAction) { direction: Quat.getUp(this.getHandRotation()), length: PICK_MAX_DISTANCE }; - var palmPickRay = { - origin: handPosition, - direction: Quat.getFront(this.getHandRotation()), - length: NEAR_PICK_MAX_DISTANCE - }; - - var otherPickRay = { - origin: handPosition, - direction: Quat.getRight(this.getHandRotation()), - length: NEAR_PICK_MAX_DISTANCE - }; - this.lineOn(distantPickRay.origin, Vec3.multiply(distantPickRay.direction, LINE_LENGTH), NO_INTERSECT_COLOR); - // don't pick 60x per second. do this check after updating the line so it's not jumpy. + // don't pick 60x per second. + var pickRays = []; var now = Date.now(); - if (now - this.lastPickTime < MSECS_PER_SEC / PICKS_PER_SECOND_PER_HAND) { - return; + if (now - this.lastPickTime > MSECS_PER_SEC / PICKS_PER_SECOND_PER_HAND) { + pickRays = [distantPickRay]; + this.lastPickTime = now; } - this.lastPickTime = now; - var pickRays = [distantPickRay, palmPickRay, otherPickRay]; for (var index=0; index < pickRays.length; ++index) { var pickRay = pickRays[index]; var directionNormalized = Vec3.normalize(pickRay.direction); @@ -322,12 +309,13 @@ function MyController(hand, triggerAction) { direction: pickRay.direction }; - if (DEBUG_HAND_RAY_PICKING) + if (WANT_DEBUG) { this.debugLine(pickRayBacked.origin, Vec3.multiply(pickRayBacked.direction, NEAR_PICK_MAX_DISTANCE), { red: 0, green: 255, blue: 0 }) + } var intersection = Entities.findRayIntersection(pickRayBacked, true); @@ -336,7 +324,6 @@ function MyController(hand, triggerAction) { var intersectionDistance = Vec3.distance(pickRay.origin, intersection.intersection); this.grabbedEntity = intersection.entityID; - //this code will disabled the beam for the opposite hand of the one that grabbed it if the entity says so var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, intersection.entityID, DEFAULT_GRABBABLE_DATA); if (grabbableData["turnOffOppositeBeam"] === true) { @@ -345,7 +332,6 @@ function MyController(hand, triggerAction) { } else { disabledHand = RIGHT_HAND; } - } else { disabledHand = 'none'; } @@ -380,6 +366,35 @@ function MyController(hand, triggerAction) { } } } + + if (this.grabbedEntity === null) { + // forward ray test failed, try sphere test. + var nearbyEntities = Entities.findEntities(handPosition, GRAB_RADIUS); + var minDistance = PICK_MAX_DISTANCE; + var i, props, distance; + for (i = 0; i < nearbyEntities.length; i++) { + var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, nearbyEntities[i], DEFAULT_GRABBABLE_DATA); + if (grabbableData.grabbable === false) { + continue; + } + var propsForCandidate = + Entities.getEntityProperties(nearbyEntities[i], ["position", "name", "collisionsWillMove", "locked"]); + distance = Vec3.distance(propsForCandidate.position, handPosition); + if (distance < minDistance && props.name !== "pointer") { + this.grabbedEntity = nearbyEntities[i]; + minDistance = distance; + props = propsForCandidate; + } + } + if (this.grabbedEntity === null) { + return; + } else if (props.locked === 0 && props.collisionsWillMove === 1) { + this.setState(STATE_NEAR_GRABBING); + } else if (props.collisionsWillMove === 0) { + // We have grabbed a non-physical object, so we want to trigger a non-colliding event as opposed to a grab event + this.setState(STATE_NEAR_GRABBING_NON_COLLIDING); + } + } }; this.distanceHolding = function() { @@ -441,7 +456,8 @@ function MyController(hand, triggerAction) { this.lineOn(handPosition, Vec3.subtract(grabbedProperties.position, handPosition), INTERSECT_COLOR); // the action was set up on a previous call. update the targets. - var radius = Math.max(Vec3.distance(this.currentObjectPosition, handControllerPosition) * DISTANCE_HOLDING_RADIUS_FACTOR, DISTANCE_HOLDING_RADIUS_FACTOR); + var radius = Math.max(Vec3.distance(this.currentObjectPosition, handControllerPosition) * + DISTANCE_HOLDING_RADIUS_FACTOR, DISTANCE_HOLDING_RADIUS_FACTOR); // how far did avatar move this timestep? var currentPosition = MyAvatar.position; var avatarDeltaPosition = Vec3.subtract(currentPosition, this.currentAvatarPosition); @@ -491,7 +507,10 @@ function MyController(hand, triggerAction) { this.currentObjectTime = now; // this doubles hand rotation - var handChange = Quat.multiply(Quat.slerp(this.handPreviousRotation, handRotation, DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR), Quat.inverse(this.handPreviousRotation)); + var handChange = Quat.multiply(Quat.slerp(this.handPreviousRotation, + handRotation, + DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR), + Quat.inverse(this.handPreviousRotation)); this.handPreviousRotation = handRotation; this.currentObjectRotation = Quat.multiply(handChange, this.currentObjectRotation); @@ -526,7 +545,8 @@ function MyController(hand, triggerAction) { this.lineOff(); - var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, ["position", "rotation", "gravity", "ignoreForCollisions"]); + var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, + ["position", "rotation", "gravity", "ignoreForCollisions"]); this.activateEntity(this.grabbedEntity, grabbedProperties); var handRotation = this.getHandRotation(); @@ -764,9 +784,9 @@ function MyController(hand, triggerAction) { this.release = function() { - if(this.hand!==disabledHand){ + if(this.hand !== disabledHand){ //release the disabled hand when we let go with the main one - disabledHand='none'; + disabledHand = 'none'; } this.lineOff(); From ed480f651f06a916b40988a7494277c7b23e66be Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 20 Oct 2015 17:43:31 -0700 Subject: [PATCH 2/5] missed a line --- examples/controllers/handControllerGrab.js | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 9f174bd3e6..1ed3406c61 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -49,6 +49,7 @@ var PICK_MAX_DISTANCE = 500; // max length of pick-ray // near grabbing // +var GRAB_RADIUS = 0.3; // if the ray misses but an object is this close, it will still be selected var NEAR_GRABBING_ACTION_TIMEFRAME = 0.05; // how quickly objects move to their new position var NEAR_GRABBING_VELOCITY_SMOOTH_RATIO = 1.0; // adjust time-averaging of held object's velocity. 1.0 to disable. var NEAR_PICK_MAX_DISTANCE = 0.3; // max length of pick-ray for close grabbing to be selected From 7dcbb5849ae0ccfe45db2b717aa88829f78840cc Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 20 Oct 2015 17:44:12 -0700 Subject: [PATCH 3/5] another bug --- examples/controllers/handControllerGrab.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 1ed3406c61..9176394a95 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -381,7 +381,7 @@ function MyController(hand, triggerAction) { var propsForCandidate = Entities.getEntityProperties(nearbyEntities[i], ["position", "name", "collisionsWillMove", "locked"]); distance = Vec3.distance(propsForCandidate.position, handPosition); - if (distance < minDistance && props.name !== "pointer") { + if (distance < minDistance && propsForCandidate.name !== "pointer") { this.grabbedEntity = nearbyEntities[i]; minDistance = distance; props = propsForCandidate; From 311254c395d5db9dd11bb2999bac126b23feab79 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 20 Oct 2015 18:26:12 -0700 Subject: [PATCH 4/5] require user-data to have wantsTrigger in it before non-colliding calls will happen --- examples/controllers/handControllerGrab.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 9176394a95..a087236abd 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -372,10 +372,11 @@ function MyController(hand, triggerAction) { // forward ray test failed, try sphere test. var nearbyEntities = Entities.findEntities(handPosition, GRAB_RADIUS); var minDistance = PICK_MAX_DISTANCE; - var i, props, distance; + var i, props, distance, grabbableData; for (i = 0; i < nearbyEntities.length; i++) { - var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, nearbyEntities[i], DEFAULT_GRABBABLE_DATA); - if (grabbableData.grabbable === false) { + var grabbableDataForCandidate = + getEntityCustomData(GRABBABLE_DATA_KEY, nearbyEntities[i], DEFAULT_GRABBABLE_DATA); + if (grabbableDataForCandidate.grabbable === false) { continue; } var propsForCandidate = @@ -385,13 +386,14 @@ function MyController(hand, triggerAction) { this.grabbedEntity = nearbyEntities[i]; minDistance = distance; props = propsForCandidate; + grabbableData = grabbableDataForCandidate; } } if (this.grabbedEntity === null) { return; } else if (props.locked === 0 && props.collisionsWillMove === 1) { this.setState(STATE_NEAR_GRABBING); - } else if (props.collisionsWillMove === 0) { + } else if (props.collisionsWillMove === 0 & grabbableData.wantsTrigger) { // We have grabbed a non-physical object, so we want to trigger a non-colliding event as opposed to a grab event this.setState(STATE_NEAR_GRABBING_NON_COLLIDING); } From f85ef9267baeebdf1dc8ca4e7b49a22ff9564948 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Tue, 20 Oct 2015 18:29:27 -0700 Subject: [PATCH 5/5] oops --- examples/controllers/handControllerGrab.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index a087236abd..80fb4c8e40 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -393,7 +393,7 @@ function MyController(hand, triggerAction) { return; } else if (props.locked === 0 && props.collisionsWillMove === 1) { this.setState(STATE_NEAR_GRABBING); - } else if (props.collisionsWillMove === 0 & grabbableData.wantsTrigger) { + } else if (props.collisionsWillMove === 0 && grabbableData.wantsTrigger) { // We have grabbed a non-physical object, so we want to trigger a non-colliding event as opposed to a grab event this.setState(STATE_NEAR_GRABBING_NON_COLLIDING); }