From 7133fc94f5dfc9df25d4def931407bbc2831bb97 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 8 Sep 2015 12:54:02 -0700 Subject: [PATCH 1/2] hydraGrab now grabs entities based on how they want to be grabbed (through setting of userData field with a key called spatialKey --- .../hydraGrab.js => handControllerGrab.js} | 41 +++++++++++-------- examples/entityScripts/sprayPaintCan.js | 36 ++++++++-------- 2 files changed, 43 insertions(+), 34 deletions(-) rename examples/controllers/{hydra/hydraGrab.js => handControllerGrab.js} (89%) diff --git a/examples/controllers/hydra/hydraGrab.js b/examples/controllers/handControllerGrab.js similarity index 89% rename from examples/controllers/hydra/hydraGrab.js rename to examples/controllers/handControllerGrab.js index bd060ecc14..87b21602c5 100644 --- a/examples/controllers/hydra/hydraGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -11,7 +11,8 @@ // -Script.include("../../libraries/utils.js"); +Script.include("../libraries/utils.js"); + var RIGHT_HAND_CLICK = Controller.findAction("RIGHT_HAND_CLICK"); var rightTriggerAction = RIGHT_HAND_CLICK; @@ -41,7 +42,7 @@ var INTERSECT_COLOR = { blue: 10 }; -var GRAB_RADIUS = 0.5; +var GRAB_RADIUS = 1.0; var GRAB_COLOR = { red: 250, @@ -135,7 +136,9 @@ controller.prototype.checkForIntersections = function(origin, direction) { var intersection = Entities.findRayIntersection(pickRay, true); if (intersection.intersects && intersection.properties.collisionsWillMove === 1) { - this.distanceToEntity = Vec3.distance(origin, intersection.properties.position); + var handPosition = Controller.getSpatialControlPosition(this.palm); + this.distanceToEntity = Vec3.distance(handPosition, intersection.properties.position); + print("distance to entity " + JSON.stringify(this.distanceToEntity)); Entities.editEntity(this.pointer, { linePoints: [ ZERO_VEC, @@ -188,6 +191,7 @@ controller.prototype.hidePointer = function() { controller.prototype.letGo = function() { if (this.grabbedEntity && this.actionID) { + print("DELETE ACTION") this.deactivateEntity(this.grabbedEntity); Entities.deleteAction(this.grabbedEntity, this.actionID); } @@ -236,19 +240,24 @@ controller.prototype.update = function() { controller.prototype.grabEntity = function() { var handRotation = this.getHandRotation(); var handPosition = this.getHandPosition(); - - var objectRotation = Entities.getEntityProperties(this.grabbedEntity).rotation; - var offsetRotation = Quat.multiply(Quat.inverse(handRotation), objectRotation); - - var objectPosition = Entities.getEntityProperties(this.grabbedEntity).position; - var offset = Vec3.subtract(objectPosition, handPosition); - var offsetPosition = Vec3.multiplyQbyV(Quat.inverse(Quat.multiply(handRotation, offsetRotation)), offset); this.closeGrabbing = true; + //check if our entity has instructions on how to be grabbed, otherwise, just use default relative position and rotation + var userData = getEntityUserData(this.grabbedEntity); + var relativePosition = ZERO_VEC; + var relativeRotation = Quat.fromPitchYawRollDegrees(0, 0, 0); + if(userData.spatialKey) { + if(userData.spatialKey.relativePosition) { + relativePosition = userData.spatialKey.relativePosition; + } + if(userData.spatialKey.relativeRotation) { + relativeRotation = userData.spatialKey.relativeRotation; + } + } this.actionID = Entities.addAction("hold", this.grabbedEntity, { - relativePosition: offsetPosition, - relativeRotation: offsetRotation, hand: this.hand, - timeScale: 0.05 + timeScale: 0.05, + relativePosition: relativePosition, + relativeRotation: relativeRotation }); } @@ -305,10 +314,10 @@ controller.prototype.onActionEvent = function(action, state) { self.checkForEntityArrival = true; }, 500); var handPosition = Controller.getSpatialControlPosition(this.palm); - var direction = Controller.getSpatialControlNormal(this.tip); + var direction = Vec3.normalize(Controller.getSpatialControlNormal(this.tip)); //move final destination along line a bit, so it doesnt hit avatar hand Entities.updateAction(this.grabbedEntity, this.actionID, { - targetPosition: Vec3.sum(handPosition, Vec3.multiply(2, direction)) + targetPosition: Vec3.sum(handPosition, Vec3.multiply(3, direction)) }); } } @@ -339,8 +348,6 @@ function cleanup() { leftController.cleanup(); } - - Script.scriptEnding.connect(cleanup); Script.update.connect(update) Controller.actionEvent.connect(onActionEvent); \ No newline at end of file diff --git a/examples/entityScripts/sprayPaintCan.js b/examples/entityScripts/sprayPaintCan.js index 8ec107f779..914e855349 100644 --- a/examples/entityScripts/sprayPaintCan.js +++ b/examples/entityScripts/sprayPaintCan.js @@ -1,16 +1,6 @@ -// -// sprayPaintCan.js -// examples/entityScripts -// -// Created by Eric Levin on 9/3/15 -// Copyright 2015 High Fidelity, Inc. -// -// This is an example of an entity script for painting with a spraypaint can -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html - (function() { + Script.include("../libraries/utils.js"); + SPATIAL_USER_DATA_KEY = "spatialKey"; this.userData = {}; var TIP_OFFSET_Z = 0.14; @@ -36,6 +26,8 @@ }); this.getUserData = function() { + + if (this.properties.userData) { this.userData = JSON.parse(this.properties.userData); } @@ -50,8 +42,7 @@ this.update = function(deltaTime) { self.properties = Entities.getEntityProperties(self.entityId); self.getUserData(); - //Only run the logic if this is the client whose avatar is grabbing - if (self.userData.grabKey && self.userData.grabKey.activated === true && self.userData.grabKey.avatarId === MyAvatar.sessionUUID) { + if (self.userData.grabKey && self.userData.grabKey.activated === true) { if (self.activated !== true) { Entities.editEntity(self.paintStream, { animationSettings: startSetting @@ -70,6 +61,8 @@ this.sprayStream = function() { var forwardVec = Quat.getFront(self.properties.rotation); + forwardVec = Vec3.multiplyQbyV(Quat.fromPitchYawRollDegrees(0, 90, 0), forwardVec); + var upVec = Quat.getUp(self.properties.rotation); var position = Vec3.sum(self.properties.position, Vec3.multiply(forwardVec, TIP_OFFSET_Z)); position = Vec3.sum(position, Vec3.multiply(upVec, TIP_OFFSET_Y)) @@ -92,11 +85,12 @@ var normal = Vec3.multiply(-1, Quat.getFront(intersection.properties.rotation)); this.paint(intersection.intersection, normal); } + + } this.paint = function(position, normal) { if (!this.painting) { - print("position " + JSON.stringify(position)) this.newStroke(position); this.painting = true; @@ -125,6 +119,7 @@ strokeWidths: this.strokeWidths }); + } this.newStroke = function(position) { @@ -157,10 +152,16 @@ this.entityId = entityId; this.properties = Entities.getEntityProperties(self.entityId); this.getUserData(); - print("USER DATA " + JSON.stringify(this.userData)) - if (this.userData.activated) { + if (this.userData.grabKey && this.userData.grabKey.activated) { this.activated = true; } + if(!this.userData.spatialKey) { + var data = { + relativePosition: {x: 0, y: 0, z: 0}, + relativeRotation: Quat.fromPitchYawRollDegrees(0, 0,0) + } + setEntityCustomData(SPATIAL_USER_DATA_KEY, this.entityId, data); + } this.initialize(); } @@ -173,6 +174,7 @@ running: false }); + this.paintStream = Entities.addEntity({ type: "ParticleEffect", animationSettings: animationSettings, From 572b120bf0af9c1ccdc20cca487f9024ae790083 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 8 Sep 2015 15:57:31 -0700 Subject: [PATCH 2/2] removed print statements --- examples/controllers/handControllerGrab.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 87b21602c5..2ebea75abc 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -138,7 +138,6 @@ controller.prototype.checkForIntersections = function(origin, direction) { if (intersection.intersects && intersection.properties.collisionsWillMove === 1) { var handPosition = Controller.getSpatialControlPosition(this.palm); this.distanceToEntity = Vec3.distance(handPosition, intersection.properties.position); - print("distance to entity " + JSON.stringify(this.distanceToEntity)); Entities.editEntity(this.pointer, { linePoints: [ ZERO_VEC, @@ -191,7 +190,6 @@ controller.prototype.hidePointer = function() { controller.prototype.letGo = function() { if (this.grabbedEntity && this.actionID) { - print("DELETE ACTION") this.deactivateEntity(this.grabbedEntity); Entities.deleteAction(this.grabbedEntity, this.actionID); }