From 2158af0fe3bffafa037ca5299b54c4bc01d8a5f9 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Sun, 18 Oct 2015 22:26:50 -0700 Subject: [PATCH 01/79] making a bow --- examples/toys/bow/bow.js | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/toys/bow/bow.js diff --git a/examples/toys/bow/bow.js b/examples/toys/bow/bow.js new file mode 100644 index 0000000000..0299f83cd0 --- /dev/null +++ b/examples/toys/bow/bow.js @@ -0,0 +1,77 @@ +// +// bow.js +// +// This script creates a bow that you can pick up with a hand controller. Use your other hand and press the trigger to grab the line, and release the trigger to fire. +// Created by James B. Pollack @imgntn on 10/10/2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +(function() { + + var STRING_OFFSET = { + x: 0, + y: 0, + z: 0 + }; + var + + var _this; + + + function Recorder() { + _this = this; + return; + } + + Bow.prototype = { + isGrabbed: false, + init: function() { + + + }, + preload: function(entityID) { + this.entityID = entityID; + } + keepStringWithBow: function(bowLocation) { + var offset = { + + } + }, + + setLeftHand: function() { + this.hand = 'left'; + }, + + setRightHand: function() { + this.hand = 'right'; + }, + + startNearGrab: function() { + + var grabLocation = Entites.getEntityProperties(this.entityID,"position"); + this.isGrabbed = true; + this.initialHand = this.hand; + }, + + continueNearGrab: function() { + + }, + + releaseGrab: function() { + if (this.isGrabbed === true && this.hand === this.initialHand) { + + this.isGrabbed = false; + } + }, + + return new Bow; + }) + + function deleteInterval() { + Script.clearInterval(recorderInterval); + Entities.deletingEntity.disconnect(deleteInterval); + } + Entities.deletingEntity.connect(deleteInterval); \ No newline at end of file From 62206b747f2c81865f322ab32a341a18de6c10f0 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 19 Oct 2015 01:07:27 -0700 Subject: [PATCH 02/79] bow work --- examples/toys/bow/bow.js | 129 +++++++++++++++++++++++++++++++-------- 1 file changed, 105 insertions(+), 24 deletions(-) diff --git a/examples/toys/bow/bow.js b/examples/toys/bow/bow.js index 0299f83cd0..37d3264028 100644 --- a/examples/toys/bow/bow.js +++ b/examples/toys/bow/bow.js @@ -10,35 +10,38 @@ // (function() { - - var STRING_OFFSET = { + var ARROW_MODEL_URL = ''; + var TOP_NOTCH_OFFSET = { x: 0, y: 0, z: 0 }; - var + + var BOTTOM_NOTCH_OFFSET = { + x: 0, + y: 0, + z: 0 + }; + + var DRAW_STRING_THRESHOLD = 0.1; var _this; - - function Recorder() { + function Bow() { _this = this; return; } Bow.prototype = { isGrabbed: false, - init: function() { - - + stringDrawn: false, + stringData: { + top: {}, + bottom: {} }, + preload: function(entityID) { this.entityID = entityID; - } - keepStringWithBow: function(bowLocation) { - var offset = { - - } }, setLeftHand: function() { @@ -49,29 +52,107 @@ this.hand = 'right'; }, - startNearGrab: function() { + drawStrings: function() { - var grabLocation = Entites.getEntityProperties(this.entityID,"position"); + Entities.editEntity(this.topString, { + linePoints: this.stringData.top.points, + normals: this.stringData.top.normals, + strokeWidths: this.stringData.top.strokeWidths, + color: stringData.currentColor + }); + + Entities.editEntity(this.bottomString, { + linePoints: this.stringData.bottom.points, + normals: this.stringData.bottom.normals, + strokeWidths: this.stringData.bottom.strokeWidths, + color: stringData.currentColor + }); + }, + + createStrings: function() { + this.createTopString(); + this.createBottomString(); + }, + + deleteStrings: function() { + Entities.deleteEntity(this.topString); + Entities.deleteEntity(this.bottomString); + }, + + createTopString: function() { + + var stringProperties = { + type: 'Polyline' + position: Vec3.sum(this.position, this.TOP_NOTCH_OFFSET); + }; + + this.topString = Entities.addEntity(stringProperties); + }, + + createBottomString: function() { + + var stringProperties = { + type: 'Polyline' + position: Vec3.sum(this.position, this.TOP_NOTCH_OFFSET); + }; + + this.bottomString = Entities.addEntity(stringProperties); + }, + + startNearGrab: function() { this.isGrabbed = true; this.initialHand = this.hand; }, continueNearGrab: function() { - + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); + this.checkStringHand(); }, releaseGrab: function() { if (this.isGrabbed === true && this.hand === this.initialHand) { - this.isGrabbed = false; } }, - return new Bow; - }) + checkStringHand: function() { + //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with + if (this.hand === 'left') { + this.getStringHandPosition = MyAvatar.getRightPalmPosition; + this.getStringHandRotation = MyAvatar.getRightPalmRotation; + this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); + } else if (this.hand === 'right') { + this.getStringHandPosition = MyAvatar.getLeftPalmPosition; + this.getStringHandRotation = MyAvatar.getLeftPalmRotation; + this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); + } - function deleteInterval() { - Script.clearInterval(recorderInterval); - Entities.deletingEntity.disconnect(deleteInterval); - } - Entities.deletingEntity.connect(deleteInterval); \ No newline at end of file + this.triggerValue = Controller.getActionValue(handClick); + + if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + + this.releaseArrow(); + this.deleteStrings(); + } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + this.stringData.handPosition = this.getStringHandPosition(); + this.stringData.handRotation = this.getStringHandRotation(); + this.drawStrings(); + } + + + }, + releaseArrow: function() { + var handDistanceAtRelease = Vec3.length(this.bowProperties.position, this.stringData.handPosition); + var releaseDirection = this.stringData.handRotation; + var releaseVecotr = 'something'; + var arrowProperties = { + type: 'Model', + dimensions: ARROW_DIMENSIONS, + position: Vec3.sum(this.bowProperties.position, ARROW_OFFSET), + velocity: relesaeVector, + }; + this.arrow = Entities.addEntity(arrowProperties); + } + + return new Bow; + }) \ No newline at end of file From 3ddfce911e4da0dd560ab1c3c71f7545bc6799d5 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 19 Oct 2015 01:37:47 -0700 Subject: [PATCH 03/79] bow, arrow, createbow --- examples/toys/bow/arrow.js | 48 ++++++++++++++++++++++++++++++ examples/toys/bow/bow.js | 53 ++++++++++++++++++++++++++-------- examples/toys/bow/createBow.js | 45 +++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 examples/toys/bow/arrow.js create mode 100644 examples/toys/bow/createBow.js diff --git a/examples/toys/bow/arrow.js b/examples/toys/bow/arrow.js new file mode 100644 index 0000000000..124a829dc4 --- /dev/null +++ b/examples/toys/bow/arrow.js @@ -0,0 +1,48 @@ +// +// arrow.js +// +// This script attaches to an arrow to make it stop and stick when it hits something. Could use this to make it a fire arrow or really give any kind of property to itself or the entity it hits. +// +// Created by James B. Pollack @imgntn on 10/19/2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +(function() { + + var _this; + + function Arrow() { + _this = this; + return; + } + + Arrow.prototype = { + preload: function(entityID) { + this.entityID = entityID; + }, + collisionWithEntity: function(me, otherEntity, collision) { + + Vec3.print('penetration = ', collision.penetration); + Vec3.print('collision contact point = ', collision.contactPoint); + + Entities.editEntity(this.entityID, { + velocity: { + x: 0, + y: 0, + z: 0 + }, + gravity: { + x: 0, + y: 0, + z: 0 + } + collisionsWillMove: false + + }) + } + + return new Arrow; + }) \ No newline at end of file diff --git a/examples/toys/bow/bow.js b/examples/toys/bow/bow.js index 37d3264028..bc185aab89 100644 --- a/examples/toys/bow/bow.js +++ b/examples/toys/bow/bow.js @@ -1,8 +1,9 @@ // // bow.js // -// This script creates a bow that you can pick up with a hand controller. Use your other hand and press the trigger to grab the line, and release the trigger to fire. -// Created by James B. Pollack @imgntn on 10/10/2015 +// This script attaches to a bow that you can pick up with a hand controller. Use your other hand and press the trigger to grab the line, and release the trigger to fire. +// +// Created by James B. Pollack @imgntn on 10/19/2015 // Copyright 2015 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. @@ -10,7 +11,20 @@ // (function() { + var ARROW_MODEL_URL = ''; + var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); + var ARROW_OFFSET = { + x: 0, + y: 0, + z: 0 + }; + var ARROW_GRAVITY = { + x: 0, + y: -9.8, + z: 0 + }; + var TOP_NOTCH_OFFSET = { x: 0, y: 0, @@ -24,6 +38,7 @@ }; var DRAW_STRING_THRESHOLD = 0.1; + var RELEASE_STRING_THRESHOLD = 0.85; var _this; @@ -67,6 +82,8 @@ strokeWidths: this.stringData.bottom.strokeWidths, color: stringData.currentColor }); + + this.stringDrawn = true; }, createStrings: function() { @@ -80,7 +97,6 @@ }, createTopString: function() { - var stringProperties = { type: 'Polyline' position: Vec3.sum(this.position, this.TOP_NOTCH_OFFSET); @@ -90,7 +106,6 @@ }, createBottomString: function() { - var stringProperties = { type: 'Polyline' position: Vec3.sum(this.position, this.TOP_NOTCH_OFFSET); @@ -129,30 +144,44 @@ this.triggerValue = Controller.getActionValue(handClick); - if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + if (this.triggerValue < RELEASE_STRING_THRESHOLD && this.stringDrawn === true) { this.releaseArrow(); + this.stringDrawn = false; + this.deleteStrings(); + } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.drawStrings(); + } - }, + releaseArrow: function() { + var handDistanceAtRelease = Vec3.length(this.bowProperties.position, this.stringData.handPosition); - var releaseDirection = this.stringData.handRotation; - var releaseVecotr = 'something'; + var releaseDirection = Vec3.subtract(this.bowProperties.position, this.stringData.handPosition); + var releaseVector = Vec3.multiply(handDistanceAtRelease, releaseDirection); + var arrowProperties = { type: 'Model', dimensions: ARROW_DIMENSIONS, position: Vec3.sum(this.bowProperties.position, ARROW_OFFSET), - velocity: relesaeVector, + velocity: releaseVector, + rotation: this.bowProperties.rotation; + collisionsWillMove: true, + gravity: ARROW_GRAVITY, + script: ARROW_SCRIPT_URL, + lifetime: 30 }; - this.arrow = Entities.addEntity(arrowProperties); - } - return new Bow; + this.arrow = Entities.addEntity(arrowProperties); + }, + + + return new Bow; }) \ No newline at end of file diff --git a/examples/toys/bow/createBow.js b/examples/toys/bow/createBow.js new file mode 100644 index 0000000000..7fdab595e4 --- /dev/null +++ b/examples/toys/bow/createBow.js @@ -0,0 +1,45 @@ +// +// createBow.js +// +// Created byJames Pollack @imgntn on 10/19/2015 +// Copyright 2015 High Fidelity, Inc. +// +// This script creates a bow you can use to shoot an arrow. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + +var SCRIPT_URL = Script.resolvePath('bow.js'); + +var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow.fbx"; +var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_collision_hull.obj"; +var BOW_DIMENSIONS = { + x: 0.08, + y: 0.30, + z: 0.08 +}; + +var BOW_GRAVITY = { + x: 0, + y: 0, + z: 0 +} + +var center = Vec3.sum(Vec3.sum(MyAvatar.position, { + x: 0, + y: 0.5, + z: 0 +}), Vec3.multiply(1, Quat.getFront(Camera.getOrientation()))); + +var flashlight = Entities.addEntity({ + type: "Model", + modelURL: MODEL_URL, + position: center, + dimensions: BOW_DIMENSIONS, + collisionsWillMove: true, + gravity: BOW_GRAVITY, + shapeType: 'compound', + script: SCRIPT_URL +}); \ No newline at end of file From 1daeedec9bf650f77092386b36faaf9f9069cf01 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 19 Oct 2015 18:04:08 -0700 Subject: [PATCH 04/79] end of day --- examples/toys/bow/arrow.js | 63 +++--- examples/toys/bow/bow.js | 401 +++++++++++++++++++++------------ examples/toys/bow/createBow.js | 31 ++- 3 files changed, 310 insertions(+), 185 deletions(-) diff --git a/examples/toys/bow/arrow.js b/examples/toys/bow/arrow.js index 124a829dc4..50c4ce3b73 100644 --- a/examples/toys/bow/arrow.js +++ b/examples/toys/bow/arrow.js @@ -12,37 +12,38 @@ (function() { - var _this; + var _this; - function Arrow() { - _this = this; - return; + function Arrow() { + _this = this; + return; + } + + Arrow.prototype = { + preload: function(entityID) { + this.entityID = entityID; + }, + collisionWithEntity: function(me, otherEntity, collision) { + + Vec3.print('penetration = ', collision.penetration); + Vec3.print('collision contact point = ', collision.contactPoint); + + Entities.editEntity(this.entityID, { + velocity: { + x: 0, + y: 0, + z: 0 + }, + gravity: { + x: 0, + y: 0, + z: 0 + }, + collisionsWillMove: false + + }) } + } - Arrow.prototype = { - preload: function(entityID) { - this.entityID = entityID; - }, - collisionWithEntity: function(me, otherEntity, collision) { - - Vec3.print('penetration = ', collision.penetration); - Vec3.print('collision contact point = ', collision.contactPoint); - - Entities.editEntity(this.entityID, { - velocity: { - x: 0, - y: 0, - z: 0 - }, - gravity: { - x: 0, - y: 0, - z: 0 - } - collisionsWillMove: false - - }) - } - - return new Arrow; - }) \ No newline at end of file + return new Arrow; +}) \ No newline at end of file diff --git a/examples/toys/bow/bow.js b/examples/toys/bow/bow.js index bc185aab89..b4de1bce59 100644 --- a/examples/toys/bow/bow.js +++ b/examples/toys/bow/bow.js @@ -12,176 +12,285 @@ (function() { - var ARROW_MODEL_URL = ''; - var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); - var ARROW_OFFSET = { - x: 0, - y: 0, - z: 0 - }; - var ARROW_GRAVITY = { - x: 0, - y: -9.8, - z: 0 - }; + var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow.fbx"; + var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_collision_hull.obj"; + var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); + var ARROW_OFFSET = 0.25; + var ARROW_FORCE = 0.25; + var ARROW_DIMENSIONS = { + x: 0.08, + y: 0.02, + z: 0.08 + }; + var ARROW_GRAVITY = { + x: 0, + y: -9.8, + z: 0 + }; - var TOP_NOTCH_OFFSET = { - x: 0, - y: 0, - z: 0 - }; + var TOP_NOTCH_OFFSET = 0.5; + var BOTTOM_NOTCH_OFFSET = 0.5; - var BOTTOM_NOTCH_OFFSET = { - x: 0, - y: 0, - z: 0 - }; + var LINE_DIMENSIONS = { + x: 5, + y: 5, + z: 5 + }; - var DRAW_STRING_THRESHOLD = 0.1; - var RELEASE_STRING_THRESHOLD = 0.85; + var DRAW_STRING_THRESHOLD = 0.80; - var _this; + var _this; - function Bow() { - _this = this; - return; - } + function Bow() { + _this = this; + return; + } - Bow.prototype = { - isGrabbed: false, - stringDrawn: false, - stringData: { - top: {}, - bottom: {} - }, + // create bow + // on pickup, wait for other hand to start trigger pull + // then create strings that start at top and bottom of bow + // extend them to the other hand position + // on trigger release, + // create arrow + // shoot arrow with velocity relative to distance between hand position and bow + // delete lines - preload: function(entityID) { - this.entityID = entityID; - }, + Bow.prototype = { + isGrabbed: false, + stringDrawn: false, + hasArrow: false, + stringData: { + currentColor: { + red: 0, + green: 255, + blue: 0 + } - setLeftHand: function() { - this.hand = 'left'; - }, + }, - setRightHand: function() { - this.hand = 'right'; - }, + preload: function(entityID) { + this.entityID = entityID; + }, - drawStrings: function() { + setLeftHand: function() { + if (this.isGrabbed === true) { + return false; + } + this.hand = 'left'; + }, - Entities.editEntity(this.topString, { - linePoints: this.stringData.top.points, - normals: this.stringData.top.normals, - strokeWidths: this.stringData.top.strokeWidths, - color: stringData.currentColor - }); + setRightHand: function() { + if (this.isGrabbed === true) { + return false; + } + this.hand = 'right'; + }, - Entities.editEntity(this.bottomString, { - linePoints: this.stringData.bottom.points, - normals: this.stringData.bottom.normals, - strokeWidths: this.stringData.bottom.strokeWidths, - color: stringData.currentColor - }); + drawStrings: function() { + this.updateStringPositions(); + var lineVectors = this.getLocalLineVectors(); + + Entities.editEntity(this.topString, { + linePoints: [{ + x: 0, + y: 0, + z: 0 + }, lineVectors[0]], + lineWidth: 5, + color: this.stringData.currentColor + }); + + Entities.editEntity(this.bottomString, { + linePoints: [{ + x: 0, + y: 0, + z: 0 + }, lineVectors[1]], + lineWidth: 5, + color: this.stringData.currentColor + }); + + }, + + createStrings: function() { + this.createTopString(); + this.createBottomString(); + }, + + deleteStrings: function() { + Entities.deleteEntity(this.topString); + Entities.deleteEntity(this.bottomString); + }, + + createTopString: function() { + var stringProperties = { + type: 'Line', + position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), + dimensions: LINE_DIMENSIONS + }; + + this.topString = Entities.addEntity(stringProperties); + }, + + createBottomString: function() { + var stringProperties = { + type: 'Line', + position: Vec3.sum(this.bowProperties.position, BOTTOM_NOTCH_OFFSET), + dimensions: LINE_DIMENSIONS + }; + + this.bottomString = Entities.addEntity(stringProperties); + }, + + updateStringPositions: function() { + + var upVector = Quat.getFront(this.bowProperties.rotation); + var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); + var downVector = Vec3.multiply(-1, Quat.getFront(this.bowProperties.rotation)); + var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET); + + this.topStringPosition = Vec3.sum(this.bowProperties.position, upOffset); + this.bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); + + Entities.editEntity(this.topString, { + position: this.topStringPosition + }); + Entities.editEntity(this.bottomString, { + position: this.bottomStringPosition + }); + + + }, + + startNearGrab: function() { + if (this.isGrabbed === true) { + return false; + } + this.isGrabbed = true; + this.initialHand = this.hand; + }, + + continueNearGrab: function() { + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); + this.checkStringHand(); + }, + + releaseGrab: function() { + if (this.isGrabbed === true && this.hand === this.initialHand) { + this.isGrabbed = false; + this.stringDrawn = false; + this.deleteStrings(); + this.hasArrow = false; + } + }, + + checkStringHand: function() { + //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with + if (this.initialHand === 'left') { + this.getStringHandPosition = MyAvatar.getRightPalmPosition; + this.getStringHandRotation = MyAvatar.getRightPalmRotation; + this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); + } else if (this.initialHand === 'right') { + this.getStringHandPosition = MyAvatar.getLeftPalmPosition; + this.getStringHandRotation = MyAvatar.getLeftPalmRotation; + this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); + } + + this.triggerValue = Controller.getActionValue(this.stringTriggerAction); + + if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + print('TRIGGER 1') + //let it fly + this.stringDrawn = false; + this.deleteStrings(); + this.hasArrow = false; + //this.releaseArrow(); + + } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + print('TRIGGER 2') + //update + this.stringData.handPosition = this.getStringHandPosition(); + this.stringData.handRotation = this.getStringHandRotation(); + this.drawStrings(); + this.updateArrowPosition(); + + } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + print('TRIGGER 3') + //create this.stringDrawn = true; - }, - - createStrings: function() { - this.createTopString(); - this.createBottomString(); - }, - - deleteStrings: function() { - Entities.deleteEntity(this.topString); - Entities.deleteEntity(this.bottomString); - }, - - createTopString: function() { - var stringProperties = { - type: 'Polyline' - position: Vec3.sum(this.position, this.TOP_NOTCH_OFFSET); - }; - - this.topString = Entities.addEntity(stringProperties); - }, - - createBottomString: function() { - var stringProperties = { - type: 'Polyline' - position: Vec3.sum(this.position, this.TOP_NOTCH_OFFSET); - }; - - this.bottomString = Entities.addEntity(stringProperties); - }, - - startNearGrab: function() { - this.isGrabbed = true; - this.initialHand = this.hand; - }, - - continueNearGrab: function() { - this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); - this.checkStringHand(); - }, - - releaseGrab: function() { - if (this.isGrabbed === true && this.hand === this.initialHand) { - this.isGrabbed = false; + this.createStrings(); + this.stringData.handPosition = this.getStringHandPosition(); + this.stringData.handRotation = this.getStringHandRotation(); + if (this.hasArrow === false) { + this.createArrow(); + this.hasArrow = true; } - }, + this.drawStrings(); - checkStringHand: function() { - //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with - if (this.hand === 'left') { - this.getStringHandPosition = MyAvatar.getRightPalmPosition; - this.getStringHandRotation = MyAvatar.getRightPalmRotation; - this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); - } else if (this.hand === 'right') { - this.getStringHandPosition = MyAvatar.getLeftPalmPosition; - this.getStringHandRotation = MyAvatar.getLeftPalmRotation; - this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); - } + } - this.triggerValue = Controller.getActionValue(handClick); + }, - if (this.triggerValue < RELEASE_STRING_THRESHOLD && this.stringDrawn === true) { + getArrowPosition: function() { + + var arrowVector = Vec3.subtract(this.bowProperties.position,this.stringData.handPosition); + arrowVector = Vec3.normalize(arrowVector); + arrowVector = Vec3.multiply(arrowVector,ARROW_OFFSET); + var arrowPosition = Vec3.sum(this.stringData.handPosition,arrowVector); + return arrowPosition; + }, - this.releaseArrow(); - this.stringDrawn = false; + updateArrowPosition: function() { + var arrowPosition = this.getArrowPosition(); + print('ARROW POSITION:::' + JSON.stringify(arrowPosition)); + Entities.editEntity(this.arrow, { + position: arrowPosition, + rotation: this.bowProperties.rotation + }); + }, - this.deleteStrings(); + createArrow: function() { + print('CREATING ARROW'); + var arrowProperties = { + name: 'Hifi-Arrow', + type: 'Model', + modelURL: ARROW_MODEL_URL, + dimensions: ARROW_DIMENSIONS, + position: this.getArrowPosition(), + rotation: this.bowProperties.rotation, + collisionsWillMove: false, + ignoreForCollisions: true, + script: ARROW_SCRIPT_URL, + lifetime: 40 + }; - } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + this.arrow = Entities.addEntity(arrowProperties); + }, - this.stringData.handPosition = this.getStringHandPosition(); - this.stringData.handRotation = this.getStringHandRotation(); - this.drawStrings(); + releaseArrow: function() { - } + var forwardVec = Quat.getFront(Quat.multiply(this.bowProperties.rotation, Quat.fromPitchYawRollDegrees(0, 180, 0))); + forwardVec = Vec3.normalize(forwardVec); + var handDistanceAtRelease = Vec3.length(this.bowProperties.position, this.stringData.handPosition); - }, + forwardVec = Vec3.multiply(forwardVec, ARROW_FORCE); - releaseArrow: function() { + var arrowProperties = { + velocity: forwardVec, + collisionsWillMove: true, + gravity: ARROW_GRAVITY, + lifetime: 20 + }; - var handDistanceAtRelease = Vec3.length(this.bowProperties.position, this.stringData.handPosition); - var releaseDirection = Vec3.subtract(this.bowProperties.position, this.stringData.handPosition); - var releaseVector = Vec3.multiply(handDistanceAtRelease, releaseDirection); + Entities.editEntity(this.arrow, arrowProperties); + }, + getLocalLineVectors: function() { + var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); + var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); + return [topVector, bottomVector]; + } + }; - var arrowProperties = { - type: 'Model', - dimensions: ARROW_DIMENSIONS, - position: Vec3.sum(this.bowProperties.position, ARROW_OFFSET), - velocity: releaseVector, - rotation: this.bowProperties.rotation; - collisionsWillMove: true, - gravity: ARROW_GRAVITY, - script: ARROW_SCRIPT_URL, - lifetime: 30 - }; - - this.arrow = Entities.addEntity(arrowProperties); - }, - - - return new Bow; - }) \ No newline at end of file + return new Bow(); +}); \ No newline at end of file diff --git a/examples/toys/bow/createBow.js b/examples/toys/bow/createBow.js index 7fdab595e4..ae739f3f84 100644 --- a/examples/toys/bow/createBow.js +++ b/examples/toys/bow/createBow.js @@ -16,9 +16,9 @@ var SCRIPT_URL = Script.resolvePath('bow.js'); var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow.fbx"; var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_collision_hull.obj"; var BOW_DIMENSIONS = { - x: 0.08, - y: 0.30, - z: 0.08 + x: 0.1, + y: 0.02, + z: 1 }; var BOW_GRAVITY = { @@ -26,20 +26,35 @@ var BOW_GRAVITY = { y: 0, z: 0 } +var BOW_ROTATION = Quat.fromPitchYawRollDegrees(90, 180, 0); var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, - y: 0.5, + y: 1, z: 0 -}), Vec3.multiply(1, Quat.getFront(Camera.getOrientation()))); +}), Vec3.multiply(1.5, Quat.getFront(Camera.getOrientation()))); -var flashlight = Entities.addEntity({ +var bow = Entities.addEntity({ + name: 'Hifi-Bow', type: "Model", modelURL: MODEL_URL, position: center, + rotation: BOW_ROTATION, dimensions: BOW_DIMENSIONS, collisionsWillMove: true, gravity: BOW_GRAVITY, shapeType: 'compound', - script: SCRIPT_URL -}); \ No newline at end of file + compoundShapeURL: COLLISION_HULL_URL, + script: SCRIPT_URL, + // userData: JSON.stringify({ + // grabbableKey: { + // invertSolidWhileHeld: true + // } + // }) +}); + +function cleanup() { + Entities.deleteEntity(bow); +} + +Script.scriptEnding.connect(cleanup); \ No newline at end of file From a4166fd1c32cabdad1991549178f70f9feea8924 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 20 Oct 2015 10:54:24 -0700 Subject: [PATCH 05/79] bow stuff --- examples/controllers/handControllerGrab.js | 15 ++++++++ examples/toys/bow/bow.js | 44 +++++++++++++++------- examples/toys/bow/createBow.js | 10 ++--- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 44aa1146d4..9fb223737e 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -249,6 +249,9 @@ function MyController(hand, triggerAction) { } this.search = function() { + if(this.hasGrabbedEntity===true){ + return; + } if (this.triggerSmoothedReleased()) { this.setState(STATE_RELEASE); return; @@ -465,11 +468,21 @@ function MyController(hand, triggerAction) { this.nearGrabbing = function() { var now = Date.now(); + var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); + var turnOffOtherHand = grabbableData["turnOffOtherHand"]; + if (turnOffOtherHand === true) { + this.hasGrabbedEntity = true; + print('DONT ACTIVATE SECOND HAND GRAB BECAUSE THE SCRIPT HAS GOT IT') + return; + } + if (this.triggerSmoothedReleased()) { this.setState(STATE_RELEASE); return; } + + this.lineOff(); var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, @@ -690,6 +703,8 @@ function MyController(hand, triggerAction) { this.release = function() { + this.hasGrabbedEntity=false; + this.lineOff(); if (this.grabbedEntity !== null) { diff --git a/examples/toys/bow/bow.js b/examples/toys/bow/bow.js index b4de1bce59..e422706556 100644 --- a/examples/toys/bow/bow.js +++ b/examples/toys/bow/bow.js @@ -169,6 +169,13 @@ } this.isGrabbed = true; this.initialHand = this.hand; + Entities.editEntity(this.entityID, { + userData: JSON.stringify({ + grabbableKey: { + turnOffOtherHand: true + } + }) + }); }, continueNearGrab: function() { @@ -182,6 +189,13 @@ this.stringDrawn = false; this.deleteStrings(); this.hasArrow = false; + Entities.editEntity(this.entityID, { + userData: JSON.stringify({ + grabbableKey: { + turnOffOtherHand: false + } + }) + }); } }, @@ -200,24 +214,24 @@ this.triggerValue = Controller.getActionValue(this.stringTriggerAction); if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - print('TRIGGER 1') - //let it fly + // print('TRIGGER 1'); + //let it fly this.stringDrawn = false; this.deleteStrings(); this.hasArrow = false; //this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - print('TRIGGER 2') - //update + //print('TRIGGER 2'); + //update this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.drawStrings(); this.updateArrowPosition(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { - print('TRIGGER 3') - //create + //print('TRIGGER 3'); + //create this.stringDrawn = true; this.createStrings(); this.stringData.handPosition = this.getStringHandPosition(); @@ -233,25 +247,27 @@ }, getArrowPosition: function() { - - var arrowVector = Vec3.subtract(this.bowProperties.position,this.stringData.handPosition); + + var arrowVector = Vec3.subtract(this.bowProperties.position, this.stringData.handPosition); arrowVector = Vec3.normalize(arrowVector); - arrowVector = Vec3.multiply(arrowVector,ARROW_OFFSET); - var arrowPosition = Vec3.sum(this.stringData.handPosition,arrowVector); + arrowVector = Vec3.multiply(arrowVector, ARROW_OFFSET); + var arrowPosition = Vec3.sum(this.stringData.handPosition, arrowVector); return arrowPosition; }, updateArrowPosition: function() { - var arrowPosition = this.getArrowPosition(); - print('ARROW POSITION:::' + JSON.stringify(arrowPosition)); + var arrowPosition = this.getArrowPosition(); + + var arrowRotation = Quat.getFront(this.bowProperties.rotation); + // print('ARROW POSITION:::' + JSON.stringify(arrowPosition)); Entities.editEntity(this.arrow, { position: arrowPosition, - rotation: this.bowProperties.rotation + rotation: arrowRotation }); }, createArrow: function() { - print('CREATING ARROW'); + // print('CREATING ARROW'); var arrowProperties = { name: 'Hifi-Arrow', type: 'Model', diff --git a/examples/toys/bow/createBow.js b/examples/toys/bow/createBow.js index ae739f3f84..b5f959a5ea 100644 --- a/examples/toys/bow/createBow.js +++ b/examples/toys/bow/createBow.js @@ -46,11 +46,11 @@ var bow = Entities.addEntity({ shapeType: 'compound', compoundShapeURL: COLLISION_HULL_URL, script: SCRIPT_URL, - // userData: JSON.stringify({ - // grabbableKey: { - // invertSolidWhileHeld: true - // } - // }) + userData: JSON.stringify({ + grabbableKey: { + invertSolidWhileHeld: true + } + }) }); function cleanup() { From 8f0cc42859a0f5067fced4d5ba31da62a6e857f3 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 20 Oct 2015 12:22:12 -0700 Subject: [PATCH 06/79] normalize line endings --- examples/toys/bow/arrow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/toys/bow/arrow.js b/examples/toys/bow/arrow.js index 50c4ce3b73..778e993513 100644 --- a/examples/toys/bow/arrow.js +++ b/examples/toys/bow/arrow.js @@ -1,5 +1,5 @@ // -// arrow.js +// arrow.js // // This script attaches to an arrow to make it stop and stick when it hits something. Could use this to make it a fire arrow or really give any kind of property to itself or the entity it hits. // From 31f1dfe25c61b19ddb369e203c22b3f72f1ed43e Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 20 Oct 2015 13:35:28 -0700 Subject: [PATCH 07/79] good two handed stuff --- examples/controllers/handControllerGrab.js | 46 ++++++++++++++-------- examples/toys/bow/bow.js | 10 +++-- examples/toys/bow/createBow.js | 3 +- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index cf86d9b9de..974c6a8fe7 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -91,6 +91,8 @@ var DEFAULT_GRABBABLE_DATA = { invertSolidWhileHeld: false }; +var disabledHand ='none'; + function getTag() { return "grab-" + MyAvatar.sessionUUID; } @@ -249,14 +251,17 @@ function MyController(hand, triggerAction) { } this.search = function() { - if(this.hasGrabbedEntity===true){ + //if this hand is the one that's disabled, we don't want to search for anything at all + if (this.hand === disabledHand) { return; } + if (this.triggerSmoothedReleased()) { this.setState(STATE_RELEASE); return; } + // the trigger is being pressed, do a ray test var handPosition = this.getHandPosition(); var distantPickRay = { @@ -295,9 +300,20 @@ function MyController(hand, triggerAction) { var intersectionDistance = Vec3.distance(pickRay.origin, intersection.intersection); this.grabbedEntity = intersection.entityID; - var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, - intersection.entityID, - DEFAULT_GRABBABLE_DATA); + + //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) { + if (this.hand === RIGHT_HAND) { + disabledHand = LEFT_HAND; + } else { + disabledHand = RIGHT_HAND; + } + + } else { + disabledHand = 'none'; + } + if (grabbableData.grabbable === false) { this.grabbedEntity = null; continue; @@ -389,9 +405,7 @@ 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); @@ -441,9 +455,7 @@ 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); @@ -463,10 +475,10 @@ function MyController(hand, triggerAction) { var now = Date.now(); var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); + var turnOffOtherHand = grabbableData["turnOffOtherHand"]; if (turnOffOtherHand === true) { - this.hasGrabbedEntity = true; - print('DONT ACTIVATE SECOND HAND GRAB BECAUSE THE SCRIPT HAS GOT IT') + //don't activate the second hand grab because the script is handling the second hand logic return; } @@ -476,11 +488,9 @@ 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(); @@ -718,8 +728,10 @@ function MyController(hand, triggerAction) { this.release = function() { - this.hasGrabbedEntity=false; - + if(this.hand!==disabledHand){ + //release the disabled hand when we let go with the main one + disabledHand='none'; + } this.lineOff(); if (this.grabbedEntity !== null) { diff --git a/examples/toys/bow/bow.js b/examples/toys/bow/bow.js index e422706556..a4c36ec52f 100644 --- a/examples/toys/bow/bow.js +++ b/examples/toys/bow/bow.js @@ -172,7 +172,9 @@ Entities.editEntity(this.entityID, { userData: JSON.stringify({ grabbableKey: { - turnOffOtherHand: true + turnOffOtherHand: true, + turnOffOppositeBeam:true + } }) }); @@ -192,7 +194,9 @@ Entities.editEntity(this.entityID, { userData: JSON.stringify({ grabbableKey: { - turnOffOtherHand: false + turnOffOtherHand: false, + turnOffOppositeBeam:true + } }) }); @@ -219,7 +223,7 @@ this.stringDrawn = false; this.deleteStrings(); this.hasArrow = false; - //this.releaseArrow(); + this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { //print('TRIGGER 2'); diff --git a/examples/toys/bow/createBow.js b/examples/toys/bow/createBow.js index b5f959a5ea..6307fe086d 100644 --- a/examples/toys/bow/createBow.js +++ b/examples/toys/bow/createBow.js @@ -48,7 +48,8 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { - invertSolidWhileHeld: true + invertSolidWhileHeld: true, + turnOffOppositeBeam:true } }) }); From fd0dc3cd493fa005351a5df01ff8b17d209ab225 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 20 Oct 2015 16:22:30 -0700 Subject: [PATCH 08/79] bow and grab --- examples/controllers/handControllerGrab.js | 42 ++++++++++++++++++++-- examples/toys/bow/bow.js | 4 +-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 974c6a8fe7..63f70312c5 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -3,6 +3,7 @@ // // Created by Eric Levin on 9/2/15 // Additions by James B. Pollack @imgntn on 9/24/2015 +// Additions By Seth Alves on 10/20/2015 // Copyright 2015 High Fidelity, Inc. // // Grabs physically moveable entities with hydra-like controllers; it works for either near or far objects. @@ -13,6 +14,13 @@ Script.include("../libraries/utils.js"); + +//////////////////////////////////////////////////////////// +// +// add lines where the hand ray picking is happening +// +var DEBUG_HAND_RAY_PICKING = false; + ///////////////////////////////////////////////////////////////// // // these tune time-averaging and "on" value for analog trigger @@ -43,9 +51,9 @@ var PICK_MAX_DISTANCE = 500; // max length of pick-ray 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.2; // max length of pick-ray for close grabbing to be selected +var NEAR_PICK_MAX_DISTANCE = 0.3; // max length of pick-ray for close grabbing to be selected var RELEASE_VELOCITY_MULTIPLIER = 1.5; // affects throwing things -var PICK_BACKOFF_DISTANCE = 0.1; // helps when hand is intersecting the grabble object +var PICK_BACKOFF_DISTANCE = 0.2; // helps when hand is intersecting the grabble object ///////////////////////////////////////////////////////////////// // @@ -190,6 +198,19 @@ function MyController(hand, triggerAction) { } + 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 + }); + } + this.lineOn = function(closePoint, farPoint, color) { // draw a line if (this.pointer === null) { @@ -275,6 +296,13 @@ function MyController(hand, triggerAction) { 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. @@ -284,7 +312,7 @@ function MyController(hand, triggerAction) { } this.lastPickTime = now; - var pickRays = [distantPickRay, palmPickRay]; + var pickRays = [distantPickRay, palmPickRay, otherPickRay]; for (var index=0; index < pickRays.length; ++index) { var pickRay = pickRays[index]; var directionNormalized = Vec3.normalize(pickRay.direction); @@ -294,7 +322,15 @@ function MyController(hand, triggerAction) { direction: pickRay.direction }; + if (DEBUG_HAND_RAY_PICKING) + this.debugLine(pickRayBacked.origin, Vec3.multiply(pickRayBacked.direction, NEAR_PICK_MAX_DISTANCE), { + red: 0, + green: 255, + blue: 0 + }) + var intersection = Entities.findRayIntersection(pickRayBacked, true); + if (intersection.intersects && intersection.properties.locked === 0) { // the ray is intersecting something we can move. var intersectionDistance = Vec3.distance(pickRay.origin, intersection.intersection); diff --git a/examples/toys/bow/bow.js b/examples/toys/bow/bow.js index a4c36ec52f..d828f4197b 100644 --- a/examples/toys/bow/bow.js +++ b/examples/toys/bow/bow.js @@ -16,7 +16,7 @@ var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_collision_hull.obj"; var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); var ARROW_OFFSET = 0.25; - var ARROW_FORCE = 0.25; + var ARROW_FORCE = 1.25; var ARROW_DIMENSIONS = { x: 0.08, y: 0.02, @@ -290,7 +290,7 @@ releaseArrow: function() { - var forwardVec = Quat.getFront(Quat.multiply(this.bowProperties.rotation, Quat.fromPitchYawRollDegrees(0, 180, 0))); + var forwardVec = Quat.getRight(Quat.multiply(this.bowProperties.rotation, Quat.fromPitchYawRollDegrees(0, 180, 0))); forwardVec = Vec3.normalize(forwardVec); var handDistanceAtRelease = Vec3.length(this.bowProperties.position, this.stringData.handPosition); From 5b417a1ba5570044b09177f3a591800b585a9209 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 21 Oct 2015 13:47:17 -0700 Subject: [PATCH 09/79] use updated hand controller --- examples/controllers/handControllerGrab.js | 107 +++++++++++++-------- 1 file changed, 65 insertions(+), 42 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 63f70312c5..80fb4c8e40 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -1,4 +1,4 @@ -// hydraGrab.js +// handControllerGrab.js // examples // // Created by Eric Levin on 9/2/15 @@ -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; ///////////////////////////////////////////////////////////////// // @@ -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 @@ -193,22 +194,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 +228,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 +283,6 @@ function MyController(hand, triggerAction) { return; } - // the trigger is being pressed, do a ray test var handPosition = this.getHandPosition(); var distantPickRay = { @@ -290,29 +290,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 +310,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 +325,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 +333,6 @@ function MyController(hand, triggerAction) { } else { disabledHand = RIGHT_HAND; } - } else { disabledHand = 'none'; } @@ -380,6 +367,37 @@ 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, grabbableData; + for (i = 0; i < nearbyEntities.length; i++) { + var grabbableDataForCandidate = + getEntityCustomData(GRABBABLE_DATA_KEY, nearbyEntities[i], DEFAULT_GRABBABLE_DATA); + if (grabbableDataForCandidate.grabbable === false) { + continue; + } + var propsForCandidate = + Entities.getEntityProperties(nearbyEntities[i], ["position", "name", "collisionsWillMove", "locked"]); + distance = Vec3.distance(propsForCandidate.position, handPosition); + if (distance < minDistance && propsForCandidate.name !== "pointer") { + 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 && 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); + } + } }; this.distanceHolding = function() { @@ -441,7 +459,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 +510,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 +548,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 +787,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 b8f09722287c8b8ab5b47505d6cd2a398bbd94b6 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 21 Oct 2015 14:00:51 -0700 Subject: [PATCH 10/79] move bow into toybox --- examples/{toys => toybox}/bow/arrow.js | 0 examples/{toys => toybox}/bow/bow.js | 0 examples/{toys => toybox}/bow/createBow.js | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename examples/{toys => toybox}/bow/arrow.js (100%) rename examples/{toys => toybox}/bow/bow.js (100%) rename examples/{toys => toybox}/bow/createBow.js (100%) diff --git a/examples/toys/bow/arrow.js b/examples/toybox/bow/arrow.js similarity index 100% rename from examples/toys/bow/arrow.js rename to examples/toybox/bow/arrow.js diff --git a/examples/toys/bow/bow.js b/examples/toybox/bow/bow.js similarity index 100% rename from examples/toys/bow/bow.js rename to examples/toybox/bow/bow.js diff --git a/examples/toys/bow/createBow.js b/examples/toybox/bow/createBow.js similarity index 100% rename from examples/toys/bow/createBow.js rename to examples/toybox/bow/createBow.js From ca0aad7d3be8473fd4c2538f1e708f103787d0b4 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 22 Oct 2015 17:25:15 -0700 Subject: [PATCH 11/79] update after updating models --- examples/toybox/bow/bow.js | 649 ++++++++++++++++++------------- examples/toybox/bow/createBow.js | 14 +- 2 files changed, 380 insertions(+), 283 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index d828f4197b..f407a49c2b 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -12,305 +12,402 @@ (function() { - var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow.fbx"; - var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_collision_hull.obj"; - var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); - var ARROW_OFFSET = 0.25; - var ARROW_FORCE = 1.25; - var ARROW_DIMENSIONS = { - x: 0.08, - y: 0.02, - z: 0.08 - }; - var ARROW_GRAVITY = { - x: 0, - y: -9.8, - z: 0 - }; - - var TOP_NOTCH_OFFSET = 0.5; - var BOTTOM_NOTCH_OFFSET = 0.5; - - var LINE_DIMENSIONS = { - x: 5, - y: 5, - z: 5 - }; - - var DRAW_STRING_THRESHOLD = 0.80; - - var _this; - - function Bow() { - _this = this; - return; - } - - // create bow - // on pickup, wait for other hand to start trigger pull - // then create strings that start at top and bottom of bow - // extend them to the other hand position - // on trigger release, - // create arrow - // shoot arrow with velocity relative to distance between hand position and bow - // delete lines - - Bow.prototype = { - isGrabbed: false, - stringDrawn: false, - hasArrow: false, - stringData: { - currentColor: { - red: 0, - green: 255, - blue: 0 - } - - }, - - preload: function(entityID) { - this.entityID = entityID; - }, - - setLeftHand: function() { - if (this.isGrabbed === true) { - return false; - } - this.hand = 'left'; - }, - - setRightHand: function() { - if (this.isGrabbed === true) { - return false; - } - this.hand = 'right'; - }, - - drawStrings: function() { - - this.updateStringPositions(); - var lineVectors = this.getLocalLineVectors(); - - Entities.editEntity(this.topString, { - linePoints: [{ - x: 0, - y: 0, - z: 0 - }, lineVectors[0]], - lineWidth: 5, - color: this.stringData.currentColor - }); - - Entities.editEntity(this.bottomString, { - linePoints: [{ - x: 0, - y: 0, - z: 0 - }, lineVectors[1]], - lineWidth: 5, - color: this.stringData.currentColor - }); - - }, - - createStrings: function() { - this.createTopString(); - this.createBottomString(); - }, - - deleteStrings: function() { - Entities.deleteEntity(this.topString); - Entities.deleteEntity(this.bottomString); - }, - - createTopString: function() { - var stringProperties = { - type: 'Line', - position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), - dimensions: LINE_DIMENSIONS - }; - - this.topString = Entities.addEntity(stringProperties); - }, - - createBottomString: function() { - var stringProperties = { - type: 'Line', - position: Vec3.sum(this.bowProperties.position, BOTTOM_NOTCH_OFFSET), - dimensions: LINE_DIMENSIONS - }; - - this.bottomString = Entities.addEntity(stringProperties); - }, - - updateStringPositions: function() { - - var upVector = Quat.getFront(this.bowProperties.rotation); - var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); - var downVector = Vec3.multiply(-1, Quat.getFront(this.bowProperties.rotation)); - var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET); - - this.topStringPosition = Vec3.sum(this.bowProperties.position, upOffset); - this.bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); - - Entities.editEntity(this.topString, { - position: this.topStringPosition - }); - Entities.editEntity(this.bottomString, { - position: this.bottomStringPosition - }); + var ZERO_VEC = { + x: 0, + y: 0, + z: 0 + }; + var LINE_ENTITY_DIMENSIONS = { + x: 1000, + y: 1000, + z: 1000 + }; - }, + var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good.fbx"; + var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good_collision_hull.obj"; + var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); + var ARROW_OFFSET = 0.25; + var ARROW_FORCE = 1.25; + var ARROW_DIMENSIONS = { + x: 0.01, + y: 0.01, + z: 0.08 + }; + var ARROW_GRAVITY = { + x: 0, + y: -9.8, + z: 0 + }; - startNearGrab: function() { - if (this.isGrabbed === true) { - return false; - } - this.isGrabbed = true; - this.initialHand = this.hand; - Entities.editEntity(this.entityID, { - userData: JSON.stringify({ - grabbableKey: { - turnOffOtherHand: true, - turnOffOppositeBeam:true + var TOP_NOTCH_OFFSET = 0.5; + var BOTTOM_NOTCH_OFFSET = 0.5; + + var LINE_DIMENSIONS = { + x: 5, + y: 5, + z: 5 + }; + + var DRAW_STRING_THRESHOLD = 0.80; + + var TARGET_LINE_LENGTH = 0.3; + + var _this; + + function Bow() { + _this = this; + return; + } + + // create bow + // on pickup, wait for other hand to start trigger pull + // then create strings that start at top and bottom of bow + // extend them to the other hand position + // on trigger release, + // create arrow + // shoot arrow with velocity relative to distance between hand position and bow + // delete lines + + Bow.prototype = { + isGrabbed: false, + stringDrawn: false, + hasArrow: false, + stringData: { + currentColor: { + red: 0, + green: 255, + blue: 0 + } + + }, + + preload: function(entityID) { + this.entityID = entityID; + }, + + setLeftHand: function() { + if (this.isGrabbed === true) { + return false; + } + this.hand = 'left'; + }, + + setRightHand: function() { + if (this.isGrabbed === true) { + return false; + } + this.hand = 'right'; + }, + + drawStrings: function() { + + this.updateStringPositions(); + var lineVectors = this.getLocalLineVectors(); + + Entities.editEntity(this.topString, { + linePoints: [{ + x: 0, + y: 0, + z: 0 + }, lineVectors[0]], + lineWidth: 5, + color: this.stringData.currentColor + }); + + Entities.editEntity(this.bottomString, { + linePoints: [{ + x: 0, + y: 0, + z: 0 + }, lineVectors[1]], + lineWidth: 5, + color: this.stringData.currentColor + }); + + }, + + createStrings: function() { + this.createTopString(); + this.createBottomString(); + }, + + deleteStrings: function() { + Entities.deleteEntity(this.topString); + Entities.deleteEntity(this.bottomString); + }, + + createTopString: function() { + var stringProperties = { + type: 'Line', + position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), + dimensions: LINE_DIMENSIONS + }; + + this.topString = Entities.addEntity(stringProperties); + }, + + createBottomString: function() { + var stringProperties = { + type: 'Line', + position: Vec3.sum(this.bowProperties.position, BOTTOM_NOTCH_OFFSET), + dimensions: LINE_DIMENSIONS + }; + + this.bottomString = Entities.addEntity(stringProperties); + }, + + updateStringPositions: function() { + + var upVector = Quat.getUp(this.bowProperties.rotation); + var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); + var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); + var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET); + + this.topStringPosition = Vec3.sum(this.bowProperties.position, upOffset); + this.bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); + + Entities.editEntity(this.topString, { + position: this.topStringPosition + }); + Entities.editEntity(this.bottomString, { + position: this.bottomStringPosition + }); + + + }, + + startNearGrab: function() { + if (this.isGrabbed === true) { + return false; + } + this.isGrabbed = true; + this.initialHand = this.hand; + Entities.editEntity(this.entityID, { + userData: JSON.stringify({ + grabbableKey: { + turnOffOtherHand: true, + turnOffOppositeBeam: true + + } + }) + }); + }, + + continueNearGrab: function() { + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); + this.checkStringHand(); + this.createTargetLine(); + }, + + releaseGrab: function() { + if (this.isGrabbed === true && this.hand === this.initialHand) { + this.isGrabbed = false; + this.stringDrawn = false; + this.deleteStrings(); + this.hasArrow = false; + Entities.editEntity(this.entityID, { + userData: JSON.stringify({ + grabbableKey: { + turnOffOtherHand: false, + turnOffOppositeBeam: true + + } + }) + }); + } + }, + + checkStringHand: function() { + //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with + if (this.initialHand === 'left') { + this.getStringHandPosition = MyAvatar.getRightPalmPosition; + this.getStringHandRotation = MyAvatar.getRightPalmRotation; + this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); + } else if (this.initialHand === 'right') { + this.getStringHandPosition = MyAvatar.getLeftPalmPosition; + this.getStringHandRotation = MyAvatar.getLeftPalmRotation; + this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); + } + + this.triggerValue = Controller.getActionValue(this.stringTriggerAction); + + if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + // print('TRIGGER 1'); + //let it fly + this.stringDrawn = false; + this.deleteStrings(); + this.hasArrow = false; + this.releaseArrow(); + + } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + //print('TRIGGER 2'); + //update + this.stringData.handPosition = this.getStringHandPosition(); + this.stringData.handRotation = this.getStringHandRotation(); + this.drawStrings(); + this.updateArrowPosition(); + + } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + //print('TRIGGER 3'); + //create + this.stringDrawn = true; + this.createStrings(); + this.stringData.handPosition = this.getStringHandPosition(); + this.stringData.handRotation = this.getStringHandRotation(); + if (this.hasArrow === false) { + this.createArrow(); + this.hasArrow = true; + } + this.drawStrings(); } - }) - }); - }, - continueNearGrab: function() { - this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); - this.checkStringHand(); - }, + }, - releaseGrab: function() { - if (this.isGrabbed === true && this.hand === this.initialHand) { - this.isGrabbed = false; - this.stringDrawn = false; - this.deleteStrings(); - this.hasArrow = false; - Entities.editEntity(this.entityID, { - userData: JSON.stringify({ - grabbableKey: { - turnOffOtherHand: false, - turnOffOppositeBeam:true + getArrowPosition: function() { - } - }) - }); - } - }, + var arrowVector = Vec3.subtract(this.bowProperties.position, this.stringData.handPosition); + arrowVector = Vec3.normalize(arrowVector); + arrowVector = Vec3.multiply(arrowVector, ARROW_OFFSET); + var arrowPosition = Vec3.sum(this.stringData.handPosition, arrowVector); + return arrowPosition; + }, - checkStringHand: function() { - //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with - if (this.initialHand === 'left') { - this.getStringHandPosition = MyAvatar.getRightPalmPosition; - this.getStringHandRotation = MyAvatar.getRightPalmRotation; - this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); - } else if (this.initialHand === 'right') { - this.getStringHandPosition = MyAvatar.getLeftPalmPosition; - this.getStringHandRotation = MyAvatar.getLeftPalmRotation; - this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); - } + updateArrowPosition: function() { + var arrowPosition = this.getArrowPosition(); - this.triggerValue = Controller.getActionValue(this.stringTriggerAction); + var bowRotation = Quat.getFront(this.bowProperties.rotation); - if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - // print('TRIGGER 1'); - //let it fly - this.stringDrawn = false; - this.deleteStrings(); - this.hasArrow = false; - this.releaseArrow(); + Entities.editEntity(this.arrow, { + position: arrowPosition, + rotation: this.bowProperties.rotation + }); + }, + createArrow: function() { + // print('CREATING ARROW'); + var arrowProperties = { + name: 'Hifi-Arrow', + type: 'Model', + modelURL: ARROW_MODEL_URL, + dimensions: ARROW_DIMENSIONS, + position: this.getArrowPosition(), + rotation: this.bowProperties.rotation, + collisionsWillMove: false, + ignoreForCollisions: true, + script: ARROW_SCRIPT_URL, + lifetime: 40 + }; - } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - //print('TRIGGER 2'); - //update - this.stringData.handPosition = this.getStringHandPosition(); - this.stringData.handRotation = this.getStringHandRotation(); - this.drawStrings(); - this.updateArrowPosition(); + this.arrow = Entities.addEntity(arrowProperties); + }, - } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { - //print('TRIGGER 3'); - //create - this.stringDrawn = true; - this.createStrings(); - this.stringData.handPosition = this.getStringHandPosition(); - this.stringData.handRotation = this.getStringHandRotation(); - if (this.hasArrow === false) { - this.createArrow(); - this.hasArrow = true; - } - this.drawStrings(); + createTargetLine: function() { - } + var bowRotation = this.bowProperties.rotation; - }, - getArrowPosition: function() { + Entities.addEntity({ + type: "Line", + name: "Debug Line", + dimensions: LINE_ENTITY_DIMENSIONS, + visible: true, + rotation: bowRotation, + position: this.bowProperties.position, + linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, Quat.getFront(this.bowProperties.rotation))], + color: { + red: 255, + green: 0, + blue: 255 + }, + lifetime: 0.1 + }); - var arrowVector = Vec3.subtract(this.bowProperties.position, this.stringData.handPosition); - arrowVector = Vec3.normalize(arrowVector); - arrowVector = Vec3.multiply(arrowVector, ARROW_OFFSET); - var arrowPosition = Vec3.sum(this.stringData.handPosition, arrowVector); - return arrowPosition; - }, + Entities.addEntity({ + type: "Line", + name: "Debug Line", + dimensions: LINE_ENTITY_DIMENSIONS, + visible: true, + // rotation: bowRotation, + position: this.bowProperties.position, + linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { + x: 1, + y: 0, + z: 0 + })], + color: { + red: 255, + green: 0, + blue: 0 + }, + lifetime: 0.1 + }); - updateArrowPosition: function() { - var arrowPosition = this.getArrowPosition(); + Entities.addEntity({ + type: "Line", + name: "Debug Line", + dimensions: LINE_ENTITY_DIMENSIONS, + visible: true, + // rotation: bowRotation, + position: this.bowProperties.position, + linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { + x: 0, + y: 1, + z: 0 + })], + color: { + red: 0, + green: 255, + blue: 0 + }, + lifetime: 0.1 + }); - var arrowRotation = Quat.getFront(this.bowProperties.rotation); - // print('ARROW POSITION:::' + JSON.stringify(arrowPosition)); - Entities.editEntity(this.arrow, { - position: arrowPosition, - rotation: arrowRotation - }); - }, + Entities.addEntity({ + type: "Line", + name: "Debug Line", + dimensions: LINE_ENTITY_DIMENSIONS, + visible: true, + // rotation:bowRotation, + position: this.bowProperties.position, + linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { + x: 0, + y: 0, + z: 1 + })], + color: { + red: 0, + green: 0, + blue: 255 + }, + lifetime: 0.1 + }); + }, - createArrow: function() { - // print('CREATING ARROW'); - var arrowProperties = { - name: 'Hifi-Arrow', - type: 'Model', - modelURL: ARROW_MODEL_URL, - dimensions: ARROW_DIMENSIONS, - position: this.getArrowPosition(), - rotation: this.bowProperties.rotation, - collisionsWillMove: false, - ignoreForCollisions: true, - script: ARROW_SCRIPT_URL, - lifetime: 40 - }; - this.arrow = Entities.addEntity(arrowProperties); - }, + releaseArrow: function() { - releaseArrow: function() { + var forwardVec = Quat.getRight(Quat.multiply(this.bowProperties.rotation, Quat.fromPitchYawRollDegrees(0, 40, 0))); + forwardVec = Vec3.normalize(forwardVec); + var handDistanceAtRelease = Vec3.length(this.bowProperties.position, this.stringData.handPosition); - var forwardVec = Quat.getRight(Quat.multiply(this.bowProperties.rotation, Quat.fromPitchYawRollDegrees(0, 180, 0))); - forwardVec = Vec3.normalize(forwardVec); - var handDistanceAtRelease = Vec3.length(this.bowProperties.position, this.stringData.handPosition); + forwardVec = Vec3.multiply(forwardVec, ARROW_FORCE); - forwardVec = Vec3.multiply(forwardVec, ARROW_FORCE); + var arrowProperties = { + velocity: forwardVec, + collisionsWillMove: true, + gravity: ARROW_GRAVITY, + lifetime: 20 + }; - var arrowProperties = { - velocity: forwardVec, - collisionsWillMove: true, - gravity: ARROW_GRAVITY, - lifetime: 20 - }; + Entities.editEntity(this.arrow, arrowProperties); + }, + getLocalLineVectors: function() { + var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); + var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); + return [topVector, bottomVector]; + } + }; - Entities.editEntity(this.arrow, arrowProperties); - }, - getLocalLineVectors: function() { - var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); - var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); - return [topVector, bottomVector]; - } - }; - - return new Bow(); -}); \ No newline at end of file + return new Bow(); + }); \ No newline at end of file diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index 6307fe086d..b8a00f9e87 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -13,12 +13,12 @@ var SCRIPT_URL = Script.resolvePath('bow.js'); -var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow.fbx"; -var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_collision_hull.obj"; +var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_good.fbx"; +var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_good_collision_hull.obj"; var BOW_DIMENSIONS = { - x: 0.1, - y: 0.02, - z: 1 + x: 0.02, + y: 1, + z: 0.1 }; var BOW_GRAVITY = { @@ -26,7 +26,7 @@ var BOW_GRAVITY = { y: 0, z: 0 } -var BOW_ROTATION = Quat.fromPitchYawRollDegrees(90, 180, 0); +// var BOW_ROTATION = Quat.fromPitchYawRollDegrees(90, 180, 0); var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, @@ -39,7 +39,7 @@ var bow = Entities.addEntity({ type: "Model", modelURL: MODEL_URL, position: center, - rotation: BOW_ROTATION, + // rotation: BOW_ROTATION, dimensions: BOW_DIMENSIONS, collisionsWillMove: true, gravity: BOW_GRAVITY, From 63d18acf91e1614f9b2933994d79ff88c98fd785 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 23 Oct 2015 14:12:56 -0700 Subject: [PATCH 12/79] bow updates --- examples/toybox/bow/bow.js | 788 ++++++++++++++++--------------- examples/toybox/bow/createBow.js | 5 +- 2 files changed, 420 insertions(+), 373 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index f407a49c2b..351a7a03ce 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -12,402 +12,450 @@ (function() { - var ZERO_VEC = { - x: 0, - y: 0, - z: 0 - }; - var LINE_ENTITY_DIMENSIONS = { - x: 1000, - y: 1000, - z: 1000 - }; + var ZERO_VEC = { + x: 0, + y: 0, + z: 0 + }; + var LINE_ENTITY_DIMENSIONS = { + x: 1000, + y: 1000, + z: 1000 + }; - var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good.fbx"; - var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good_collision_hull.obj"; - var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); - var ARROW_OFFSET = 0.25; - var ARROW_FORCE = 1.25; - var ARROW_DIMENSIONS = { - x: 0.01, - y: 0.01, - z: 0.08 - }; - var ARROW_GRAVITY = { - x: 0, - y: -9.8, - z: 0 - }; + var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good.fbx"; + var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good_collision_hull.obj"; + var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); + var ARROW_OFFSET = 0.25; + var ARROW_FORCE = 1.25; + var ARROW_DIMENSIONS = { + x: 0.02, + y: 0.02, + z: 0.16 + }; - var TOP_NOTCH_OFFSET = 0.5; - var BOTTOM_NOTCH_OFFSET = 0.5; + var ARROW_GRAVITY = { + x: 0, + y: -9.8, + z: 0 + }; - var LINE_DIMENSIONS = { - x: 5, - y: 5, - z: 5 - }; + var TOP_NOTCH_OFFSET = 0.5; + var BOTTOM_NOTCH_OFFSET = 0.5; - var DRAW_STRING_THRESHOLD = 0.80; + var LINE_DIMENSIONS = { + x: 5, + y: 5, + z: 5 + }; - var TARGET_LINE_LENGTH = 0.3; + var DRAW_STRING_THRESHOLD = 0.80; - var _this; + var TARGET_LINE_LENGTH = 1; - function Bow() { - _this = this; - return; - } + var _this; - // create bow - // on pickup, wait for other hand to start trigger pull - // then create strings that start at top and bottom of bow - // extend them to the other hand position - // on trigger release, - // create arrow - // shoot arrow with velocity relative to distance between hand position and bow - // delete lines + function Bow() { + _this = this; + return; + } - Bow.prototype = { - isGrabbed: false, - stringDrawn: false, - hasArrow: false, - stringData: { - currentColor: { - red: 0, - green: 255, - blue: 0 + // create bow + // on pickup, wait for other hand to start trigger pull + // then create strings that start at top and bottom of bow + // extend them to the other hand position + // on trigger release, + // create arrow + // shoot arrow with velocity relative to distance between hand position and bow + // delete lines + + Bow.prototype = { + isGrabbed: false, + stringDrawn: false, + hasArrow: false, + stringData: { + currentColor: { + red: 0, + green: 255, + blue: 0 + } + + }, + + preload: function(entityID) { + this.entityID = entityID; + }, + + setLeftHand: function() { + if (this.isGrabbed === true) { + return false; + } + this.hand = 'left'; + }, + + setRightHand: function() { + if (this.isGrabbed === true) { + return false; + } + this.hand = 'right'; + }, + + drawStrings: function() { + + this.updateStringPositions(); + var lineVectors = this.getLocalLineVectors(); + + Entities.editEntity(this.topString, { + linePoints: [{ + x: 0, + y: 0, + z: 0 + }, lineVectors[0]], + lineWidth: 5, + color: this.stringData.currentColor + }); + + Entities.editEntity(this.bottomString, { + linePoints: [{ + x: 0, + y: 0, + z: 0 + }, lineVectors[1]], + lineWidth: 5, + color: this.stringData.currentColor + }); + + }, + + createStrings: function() { + this.createTopString(); + this.createBottomString(); + }, + + deleteStrings: function() { + Entities.deleteEntity(this.topString); + Entities.deleteEntity(this.bottomString); + }, + + createTopString: function() { + var stringProperties = { + type: 'Line', + position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), + dimensions: LINE_DIMENSIONS + }; + + this.topString = Entities.addEntity(stringProperties); + }, + + createBottomString: function() { + var stringProperties = { + type: 'Line', + position: Vec3.sum(this.bowProperties.position, BOTTOM_NOTCH_OFFSET), + dimensions: LINE_DIMENSIONS + }; + + this.bottomString = Entities.addEntity(stringProperties); + }, + + updateStringPositions: function() { + + var upVector = Quat.getUp(this.bowProperties.rotation); + var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); + var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); + var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET); + + this.topStringPosition = Vec3.sum(this.bowProperties.position, upOffset); + this.bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); + + Entities.editEntity(this.topString, { + position: this.topStringPosition + }); + Entities.editEntity(this.bottomString, { + position: this.bottomStringPosition + }); + + + }, + + startNearGrab: function() { + if (this.isGrabbed === true) { + return false; + } + this.isGrabbed = true; + this.initialHand = this.hand; + Entities.editEntity(this.entityID, { + userData: JSON.stringify({ + grabbableKey: { + turnOffOtherHand: true, + turnOffOppositeBeam: true, + invertSolidWhileHeld: true, } + }) + }); + }, - }, + continueNearGrab: function() { + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); + this.checkStringHand(); - preload: function(entityID) { - this.entityID = entityID; - }, + }, - setLeftHand: function() { - if (this.isGrabbed === true) { - return false; - } - this.hand = 'left'; - }, - - setRightHand: function() { - if (this.isGrabbed === true) { - return false; - } - this.hand = 'right'; - }, - - drawStrings: function() { - - this.updateStringPositions(); - var lineVectors = this.getLocalLineVectors(); - - Entities.editEntity(this.topString, { - linePoints: [{ - x: 0, - y: 0, - z: 0 - }, lineVectors[0]], - lineWidth: 5, - color: this.stringData.currentColor - }); - - Entities.editEntity(this.bottomString, { - linePoints: [{ - x: 0, - y: 0, - z: 0 - }, lineVectors[1]], - lineWidth: 5, - color: this.stringData.currentColor - }); - - }, - - createStrings: function() { - this.createTopString(); - this.createBottomString(); - }, - - deleteStrings: function() { - Entities.deleteEntity(this.topString); - Entities.deleteEntity(this.bottomString); - }, - - createTopString: function() { - var stringProperties = { - type: 'Line', - position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), - dimensions: LINE_DIMENSIONS - }; - - this.topString = Entities.addEntity(stringProperties); - }, - - createBottomString: function() { - var stringProperties = { - type: 'Line', - position: Vec3.sum(this.bowProperties.position, BOTTOM_NOTCH_OFFSET), - dimensions: LINE_DIMENSIONS - }; - - this.bottomString = Entities.addEntity(stringProperties); - }, - - updateStringPositions: function() { - - var upVector = Quat.getUp(this.bowProperties.rotation); - var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); - var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); - var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET); - - this.topStringPosition = Vec3.sum(this.bowProperties.position, upOffset); - this.bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); - - Entities.editEntity(this.topString, { - position: this.topStringPosition - }); - Entities.editEntity(this.bottomString, { - position: this.bottomStringPosition - }); - - - }, - - startNearGrab: function() { - if (this.isGrabbed === true) { - return false; - } - this.isGrabbed = true; - this.initialHand = this.hand; - Entities.editEntity(this.entityID, { - userData: JSON.stringify({ - grabbableKey: { - turnOffOtherHand: true, - turnOffOppositeBeam: true - - } - }) - }); - }, - - continueNearGrab: function() { - this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); - this.checkStringHand(); - this.createTargetLine(); - }, - - releaseGrab: function() { - if (this.isGrabbed === true && this.hand === this.initialHand) { - this.isGrabbed = false; - this.stringDrawn = false; - this.deleteStrings(); - this.hasArrow = false; - Entities.editEntity(this.entityID, { - userData: JSON.stringify({ - grabbableKey: { - turnOffOtherHand: false, - turnOffOppositeBeam: true - - } - }) - }); - } - }, - - checkStringHand: function() { - //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with - if (this.initialHand === 'left') { - this.getStringHandPosition = MyAvatar.getRightPalmPosition; - this.getStringHandRotation = MyAvatar.getRightPalmRotation; - this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); - } else if (this.initialHand === 'right') { - this.getStringHandPosition = MyAvatar.getLeftPalmPosition; - this.getStringHandRotation = MyAvatar.getLeftPalmRotation; - this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); - } - - this.triggerValue = Controller.getActionValue(this.stringTriggerAction); - - if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - // print('TRIGGER 1'); - //let it fly - this.stringDrawn = false; - this.deleteStrings(); - this.hasArrow = false; - this.releaseArrow(); - - } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - //print('TRIGGER 2'); - //update - this.stringData.handPosition = this.getStringHandPosition(); - this.stringData.handRotation = this.getStringHandRotation(); - this.drawStrings(); - this.updateArrowPosition(); - - } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { - //print('TRIGGER 3'); - //create - this.stringDrawn = true; - this.createStrings(); - this.stringData.handPosition = this.getStringHandPosition(); - this.stringData.handRotation = this.getStringHandRotation(); - if (this.hasArrow === false) { - this.createArrow(); - this.hasArrow = true; + releaseGrab: function() { + if (this.isGrabbed === true && this.hand === this.initialHand) { + this.isGrabbed = false; + this.stringDrawn = false; + this.deleteStrings(); + this.hasArrow = false; + Entities.editEntity(this.entityID, { + userData: JSON.stringify({ + grabbableKey: { + turnOffOtherHand: false, + turnOffOppositeBeam: true, + invertSolidWhileHeld: true, } - this.drawStrings(); + }) + }); + } + }, + + checkStringHand: function() { + //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with + if (this.initialHand === 'left') { + this.getStringHandPosition = MyAvatar.getRightPalmPosition; + this.getStringHandRotation = MyAvatar.getRightPalmRotation; + this.getGrabHandPosition = MyAvatar.getLeftPalmPosition; + this.getGrabHandRotation = MyAvatar.getLeftPalmRotation; + this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); + } else if (this.initialHand === 'right') { + this.getStringHandPosition = MyAvatar.getLeftPalmPosition; + this.getStringHandRotation = MyAvatar.getLeftPalmRotation; + this.getGrabHandPosition = MyAvatar.getRightPalmPosition; + this.getGrabHandRotation = MyAvatar.getRightPalmRotation; + this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); + } + + this.triggerValue = Controller.getActionValue(this.stringTriggerAction); + + if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + this.stringDrawn = false; + this.deleteStrings(); + this.hasArrow = false; + this.releaseArrow(); + + } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + this.stringData.handPosition = this.getStringHandPosition(); + this.stringData.handRotation = this.getStringHandRotation(); + this.stringData.grabHandPosition = this.getGrabHandPosition(); + this.stringData.grabHandRotation = this.getGrabHandRotation(); + + + this.drawStrings(); + this.updateArrowPosition(); + this.createTargetLine(); + + } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + this.stringDrawn = true; + this.createStrings(); + this.stringData.handPosition = this.getStringHandPosition(); + this.stringData.handRotation = this.getStringHandRotation(); + this.stringData.grabHandPosition = this.getGrabHandPosition(); + this.stringData.grabHandRotation = this.getGrabHandRotation(); + if (this.hasArrow === false) { + this.createArrow(); + this.hasArrow = true; + } + + this.drawStrings(); + this.updateArrowPosition(); + this.createTargetLine(); + + } + }, + + getArrowPosition: function() { + + var arrowVector = Vec3.subtract(this.bowProperties.position, this.stringData.handPosition); + arrowVector = Vec3.normalize(arrowVector); + arrowVector = Vec3.multiply(arrowVector, ARROW_OFFSET); + var arrowPosition = Vec3.sum(this.stringData.handPosition, arrowVector); + return arrowPosition; + }, + orientationOf: function(vector) { + var Y_AXIS = { + x: 0, + y: 1, + z: 0 + }; + var X_AXIS = { + x: 1, + y: 0, + z: 0 + }; + + var theta = 0.0; + + var RAD_TO_DEG = 180.0 / Math.PI; + var direction, yaw, pitch; + direction = Vec3.normalize(vector); + yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * RAD_TO_DEG, Y_AXIS); + pitch = Quat.angleAxis(Math.asin(-direction.y) * RAD_TO_DEG, X_AXIS); + return Quat.multiply(yaw, pitch); + }, + + updateArrowPosition: function() { + var arrowPosition = this.getArrowPosition(); + + var handToHand = Vec3.subtract(this.stringData.handPosition, this.stringData.grabHandPosition); + + var arrowRotation = this.orientationOf(handToHand); + + Entities.editEntity(this.arrow, { + position: arrowPosition, + rotation: arrowRotation + }); + }, + createArrow: function() { + // print('CREATING ARROW'); + var arrowProperties = { + name: 'Hifi-Arrow', + type: 'Model', + shapeType:'box', + modelURL: ARROW_MODEL_URL, + dimensions: ARROW_DIMENSIONS, + position: this.getArrowPosition(), + rotation: this.bowProperties.rotation, + collisionsWillMove: false, + ignoreForCollisions: true, + gravity: ARROW_GRAVITY, + // script: ARROW_SCRIPT_URL, + lifetime: 40, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false, } + }) + }; - }, + this.arrow = Entities.addEntity(arrowProperties); + }, - getArrowPosition: function() { + releaseArrow: function() { + var handToHand = Vec3.subtract(this.stringData.grabHandPosition, this.stringData.handPosition); - var arrowVector = Vec3.subtract(this.bowProperties.position, this.stringData.handPosition); - arrowVector = Vec3.normalize(arrowVector); - arrowVector = Vec3.multiply(arrowVector, ARROW_OFFSET); - var arrowPosition = Vec3.sum(this.stringData.handPosition, arrowVector); - return arrowPosition; - }, + var arrowRotation = this.orientationOf(handToHand); - updateArrowPosition: function() { - var arrowPosition = this.getArrowPosition(); + // var forwardVec = Quat.getFront(this.arrow.rotation); + // forwardVec = Vec3.normalize(forwardVec); + var handDistanceAtRelease = Vec3.distance(this.stringData.grabHandPosition, this.stringData.handPosition); + print('HAND DISTANCE:: ' + handDistanceAtRelease); + var arrowForce = this.scaleArrowShotStrength(handDistanceAtRelease, 0, 1.5, 0, 10); + print('ARROW FORCE::' + arrowForce); + var forwardVec = Vec3.multiply(handToHand, arrowForce); - var bowRotation = Quat.getFront(this.bowProperties.rotation); + var arrowProperties = { + // rotation:handToHand, + velocity: forwardVec, + ignoreForCollisions: false, + collisionsWillMove: true, + gravity: ARROW_GRAVITY, + lifetime: 20 + }; - Entities.editEntity(this.arrow, { - position: arrowPosition, - rotation: this.bowProperties.rotation - }); - }, - createArrow: function() { - // print('CREATING ARROW'); - var arrowProperties = { - name: 'Hifi-Arrow', - type: 'Model', - modelURL: ARROW_MODEL_URL, - dimensions: ARROW_DIMENSIONS, - position: this.getArrowPosition(), - rotation: this.bowProperties.rotation, - collisionsWillMove: false, - ignoreForCollisions: true, - script: ARROW_SCRIPT_URL, - lifetime: 40 - }; + Entities.editEntity(this.arrow, arrowProperties); + + }, + createTargetLine: function() { + var handToHand = Vec3.subtract(this.stringData.handPosition, this.stringData.grabHandPosition); - this.arrow = Entities.addEntity(arrowProperties); - }, + var arrowRotation = this.orientationOf(handToHand); - createTargetLine: function() { + // Entities.addEntity({ + // type: "Line", + // name: "Debug Line", + // dimensions: LINE_ENTITY_DIMENSIONS, + // visible: true, + // rotation: arrowRotation, + // position: this.stringData.handPosition, + // linePoints: [ZERO_VEC, Vec3.multiply(-TARGET_LINE_LENGTH, handToHand)], + // color: { + // red: 255, + // green: 0, + // blue: 255 + // }, + // lifetime: 0.1 + // }); - var bowRotation = this.bowProperties.rotation; + // Entities.addEntity({ + // type: "Line", + // name: "Debug Line", + // dimensions: LINE_ENTITY_DIMENSIONS, + // visible: true, + // // rotation: bowRotation, + // position: this.bowProperties.position, + // linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { + // x: 1, + // y: 0, + // z: 0 + // })], + // color: { + // red: 255, + // green: 0, + // blue: 0 + // }, + // lifetime: 0.1 + // }); + // Entities.addEntity({ + // type: "Line", + // name: "Debug Line", + // dimensions: LINE_ENTITY_DIMENSIONS, + // visible: true, + // // rotation: bowRotation, + // position: this.bowProperties.position, + // linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { + // x: 0, + // y: 1, + // z: 0 + // })], + // color: { + // red: 0, + // green: 255, + // blue: 0 + // }, + // lifetime: 0.1 + // }); - Entities.addEntity({ - type: "Line", - name: "Debug Line", - dimensions: LINE_ENTITY_DIMENSIONS, - visible: true, - rotation: bowRotation, - position: this.bowProperties.position, - linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, Quat.getFront(this.bowProperties.rotation))], - color: { - red: 255, - green: 0, - blue: 255 - }, - lifetime: 0.1 - }); + // Entities.addEntity({ + // type: "Line", + // name: "Debug Line", + // dimensions: LINE_ENTITY_DIMENSIONS, + // visible: true, + // // rotation:bowRotation, + // position: this.bowProperties.position, + // linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { + // x: 0, + // y: 0, + // z: 1 + // })], + // color: { + // red: 0, + // green: 0, + // blue: 255 + // }, + // lifetime: 0.1 + // }); + }, + getLocalLineVectors: function() { + var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); + var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); + return [topVector, bottomVector]; + }, + scaleArrowShotStrength: function(value, min1, max1, min2, max2) { + return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); + } + }; - Entities.addEntity({ - type: "Line", - name: "Debug Line", - dimensions: LINE_ENTITY_DIMENSIONS, - visible: true, - // rotation: bowRotation, - position: this.bowProperties.position, - linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { - x: 1, - y: 0, - z: 0 - })], - color: { - red: 255, - green: 0, - blue: 0 - }, - lifetime: 0.1 - }); - - Entities.addEntity({ - type: "Line", - name: "Debug Line", - dimensions: LINE_ENTITY_DIMENSIONS, - visible: true, - // rotation: bowRotation, - position: this.bowProperties.position, - linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { - x: 0, - y: 1, - z: 0 - })], - color: { - red: 0, - green: 255, - blue: 0 - }, - lifetime: 0.1 - }); - - Entities.addEntity({ - type: "Line", - name: "Debug Line", - dimensions: LINE_ENTITY_DIMENSIONS, - visible: true, - // rotation:bowRotation, - position: this.bowProperties.position, - linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { - x: 0, - y: 0, - z: 1 - })], - color: { - red: 0, - green: 0, - blue: 255 - }, - lifetime: 0.1 - }); - }, - - - releaseArrow: function() { - - var forwardVec = Quat.getRight(Quat.multiply(this.bowProperties.rotation, Quat.fromPitchYawRollDegrees(0, 40, 0))); - forwardVec = Vec3.normalize(forwardVec); - var handDistanceAtRelease = Vec3.length(this.bowProperties.position, this.stringData.handPosition); - - forwardVec = Vec3.multiply(forwardVec, ARROW_FORCE); - - var arrowProperties = { - velocity: forwardVec, - collisionsWillMove: true, - gravity: ARROW_GRAVITY, - lifetime: 20 - }; - - Entities.editEntity(this.arrow, arrowProperties); - }, - getLocalLineVectors: function() { - var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); - var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); - return [topVector, bottomVector]; - } - }; - - return new Bow(); - }); \ No newline at end of file + return new Bow(); +}); \ No newline at end of file diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index b8a00f9e87..f50d7403aa 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -13,8 +13,8 @@ var SCRIPT_URL = Script.resolvePath('bow.js'); -var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_good.fbx"; -var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_good_collision_hull.obj"; +var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_good_2.fbx"; +var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_good_2_collision_hull.obj"; var BOW_DIMENSIONS = { x: 0.02, y: 1, @@ -26,7 +26,6 @@ var BOW_GRAVITY = { y: 0, z: 0 } -// var BOW_ROTATION = Quat.fromPitchYawRollDegrees(90, 180, 0); var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, From 6e8d505726f50851a0013b03aed106f82d8c2c8f Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 23 Oct 2015 14:38:17 -0700 Subject: [PATCH 13/79] updates --- examples/toybox/bow/bow.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 351a7a03ce..d6251b2e30 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -320,7 +320,7 @@ var arrowProperties = { name: 'Hifi-Arrow', type: 'Model', - shapeType:'box', + shapeType: 'box', modelURL: ARROW_MODEL_URL, dimensions: ARROW_DIMENSIONS, position: this.getArrowPosition(), @@ -350,21 +350,28 @@ // forwardVec = Vec3.normalize(forwardVec); var handDistanceAtRelease = Vec3.distance(this.stringData.grabHandPosition, this.stringData.handPosition); print('HAND DISTANCE:: ' + handDistanceAtRelease); - var arrowForce = this.scaleArrowShotStrength(handDistanceAtRelease, 0, 1.5, 0, 10); + var arrowForce = this.scaleArrowShotStrength(handDistanceAtRelease, 0, 2, 20, 50); print('ARROW FORCE::' + arrowForce); var forwardVec = Vec3.multiply(handToHand, arrowForce); var arrowProperties = { // rotation:handToHand, - velocity: forwardVec, ignoreForCollisions: false, collisionsWillMove: true, gravity: ARROW_GRAVITY, + velocity: forwardVec, lifetime: 20 }; Entities.editEntity(this.arrow, arrowProperties); - + Script.setTimeout(function() { + Entities.editEntity(this.arrow, { + ignoreForCollisions: false, + collisionsWillMove: true, + gravity: ARROW_GRAVITY, + }); + }, 100) + }, createTargetLine: function() { var handToHand = Vec3.subtract(this.stringData.handPosition, this.stringData.grabHandPosition); From 29de3581dd1b822c47c1364e045f7c0c4eae1c18 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 23 Oct 2015 17:39:03 -0700 Subject: [PATCH 14/79] end of day --- examples/controllers/handControllerGrab.js | 61 ++++++++--- examples/toybox/bow/arrow.js | 37 ++++++- examples/toybox/bow/bow.js | 117 +++++---------------- 3 files changed, 105 insertions(+), 110 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 80fb4c8e40..e8b1b9cfbd 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -38,9 +38,21 @@ var TRIGGER_OFF_VALUE = 0.15; var DISTANCE_HOLDING_RADIUS_FACTOR = 5; // multiplied by distance between hand and object var DISTANCE_HOLDING_ACTION_TIMEFRAME = 0.1; // how quickly objects move to their new position var DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR = 2.0; // object rotates this much more than hand did -var NO_INTERSECT_COLOR = { red: 10, green: 10, blue: 255}; // line color when pick misses -var INTERSECT_COLOR = { red: 250, green: 10, blue: 10}; // line color when pick hits -var LINE_ENTITY_DIMENSIONS = { x: 1000, y: 1000,z: 1000}; +var NO_INTERSECT_COLOR = { + red: 10, + green: 10, + blue: 255 +}; // line color when pick misses +var INTERSECT_COLOR = { + red: 250, + green: 10, + blue: 10 +}; // line color when pick hits +var LINE_ENTITY_DIMENSIONS = { + x: 1000, + y: 1000, + z: 1000 +}; var LINE_LENGTH = 500; var PICK_MAX_DISTANCE = 500; // max length of pick-ray @@ -100,7 +112,7 @@ var DEFAULT_GRABBABLE_DATA = { invertSolidWhileHeld: false }; -var disabledHand ='none'; +var disabledHand = 'none'; function getTag() { return "grab-" + MyAvatar.sessionUUID; @@ -200,7 +212,7 @@ function MyController(hand, triggerAction) { this.state = newState; } - this.debugLine = function(closePoint, farPoint, color){ + this.debugLine = function(closePoint, farPoint, color) { Entities.addEntity({ type: "Line", name: "Debug Line", @@ -301,7 +313,7 @@ function MyController(hand, triggerAction) { this.lastPickTime = now; } - for (var index=0; index < pickRays.length; ++index) { + for (var index = 0; index < pickRays.length; ++index) { var pickRay = pickRays[index]; var directionNormalized = Vec3.normalize(pickRay.direction); var directionBacked = Vec3.multiply(directionNormalized, PICK_BACKOFF_DISTANCE); @@ -376,6 +388,15 @@ function MyController(hand, triggerAction) { for (i = 0; i < nearbyEntities.length; i++) { var grabbableDataForCandidate = getEntityCustomData(GRABBABLE_DATA_KEY, nearbyEntities[i], DEFAULT_GRABBABLE_DATA); + if (grabbableDataForCandidate["turnOffOppositeBeam"] === true) { + if (this.hand === RIGHT_HAND) { + disabledHand = LEFT_HAND; + } else { + disabledHand = RIGHT_HAND; + } + } else { + disabledHand = 'none'; + } if (grabbableDataForCandidate.grabbable === false) { continue; } @@ -405,7 +426,8 @@ function MyController(hand, triggerAction) { var handControllerPosition = Controller.getSpatialControlPosition(this.palm); var handRotation = Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(this.palm)); var grabbedProperties = Entities.getEntityProperties(this.grabbedEntity, ["position", "rotation", - "gravity", "ignoreForCollisions"]); + "gravity", "ignoreForCollisions" + ]); var now = Date.now(); // add the action and initialize some variables @@ -460,7 +482,7 @@ function MyController(hand, triggerAction) { // 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); + 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); @@ -511,9 +533,9 @@ function MyController(hand, triggerAction) { // this doubles hand rotation var handChange = Quat.multiply(Quat.slerp(this.handPreviousRotation, - handRotation, - DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR), - Quat.inverse(this.handPreviousRotation)); + handRotation, + DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR), + Quat.inverse(this.handPreviousRotation)); this.handPreviousRotation = handRotation; this.currentObjectRotation = Quat.multiply(handChange, this.currentObjectRotation); @@ -548,8 +570,7 @@ 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(); @@ -787,7 +808,7 @@ 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'; } @@ -828,9 +849,15 @@ function MyController(hand, triggerAction) { if (data["refCount"] == 1) { data["gravity"] = grabbedProperties.gravity; data["ignoreForCollisions"] = grabbedProperties.ignoreForCollisions; - var whileHeldProperties = {gravity: {x:0, y:0, z:0}}; + var whileHeldProperties = { + gravity: { + x: 0, + y: 0, + z: 0 + } + }; if (invertSolidWhileHeld) { - whileHeldProperties["ignoreForCollisions"] = ! grabbedProperties.ignoreForCollisions; + whileHeldProperties["ignoreForCollisions"] = !grabbedProperties.ignoreForCollisions; } Entities.editEntity(entityID, whileHeldProperties); } @@ -870,4 +897,4 @@ function cleanup() { } Script.scriptEnding.connect(cleanup); -Script.update.connect(update); +Script.update.connect(update); \ No newline at end of file diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 778e993513..fcb9ea2f20 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -2,7 +2,7 @@ // arrow.js // // This script attaches to an arrow to make it stop and stick when it hits something. Could use this to make it a fire arrow or really give any kind of property to itself or the entity it hits. -// +// // Created by James B. Pollack @imgntn on 10/19/2015 // Copyright 2015 High Fidelity, Inc. // @@ -20,9 +20,44 @@ } Arrow.prototype = { + glowBox: null, preload: function(entityID) { this.entityID = entityID; + if (this.glowBox === null) { + this.createGlowBox(); + Script.update.connect(this.updateGlowBoxPosition); + } }, + unload: function() { + Script.update.disconnect(this.updateGlowBoxPosition); + Entities.deleteEntity(this.glowBox); + }, + createGlowBox: function() { + var glowBowProperties = { + name: 'Arrow Glow Box', + type: 'Box', + dimensions: { + x: 0.02, + y: 0.02, + z: 0.64 + }, + color: { + red: 255, + green: 0, + blue: 255 + }, + } + _this.glowBow = Entities.addEntity(glowBowProperties); + }, + updateGlowBoxPosition: function() { + var arrowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation"]); + //once parenting is available, just attach the glowbow to the arrow + Entities.editEntity(_this.entityID, { + position: arrowProperties.position, + rotation: arrowProperties.rotation + }) + }, + collisionWithEntity: function(me, otherEntity, collision) { Vec3.print('penetration = ', collision.penetration); diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index d6251b2e30..d4a93eeed3 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -10,6 +10,16 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// TODO: +// make it so you can shoot without blocking your view with your hand +// make arrows more visible +// make arrow rotate toward ground as it flies +// different model? compound bow will look better in the HMD, and be easier to aim. this is what HTC uses in the longbow demo: http://www.turbosquid.com/3d-models/3d-model-bow-arrow/773106 +// add noise when you release arrow -> add the sound to the arrow and keep it with position so you hear it whizz by +// add noise when you draw string +// re-enable arrows sticking when they hit +// prepare for haptics + (function() { var ZERO_VEC = { @@ -17,6 +27,7 @@ y: 0, z: 0 }; + var LINE_ENTITY_DIMENSIONS = { x: 1000, y: 1000, @@ -27,12 +38,12 @@ var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good.fbx"; var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good_collision_hull.obj"; var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); - var ARROW_OFFSET = 0.25; + var ARROW_OFFSET = 0.32; var ARROW_FORCE = 1.25; var ARROW_DIMENSIONS = { x: 0.02, y: 0.02, - z: 0.16 + z: 0.64 }; var ARROW_GRAVITY = { @@ -54,6 +65,9 @@ var TARGET_LINE_LENGTH = 1; + var LEFT_TIP = 1; + var RIGHT_TIP = 3; + var _this; function Bow() { @@ -189,7 +203,7 @@ grabbableKey: { turnOffOtherHand: true, turnOffOppositeBeam: true, - invertSolidWhileHeld: true, + invertSolidWhileHeld: true } }) }); @@ -207,12 +221,13 @@ this.stringDrawn = false; this.deleteStrings(); this.hasArrow = false; + Entities.deleteEntity(this.arrow); Entities.editEntity(this.entityID, { userData: JSON.stringify({ grabbableKey: { turnOffOtherHand: false, turnOffOppositeBeam: true, - invertSolidWhileHeld: true, + invertSolidWhileHeld: true } }) }); @@ -246,20 +261,21 @@ } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); - this.stringData.grabHandPosition = this.getGrabHandPosition(); + this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); + // this.stringData.grabHandPosition = this.getGrabHandPosition(); this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); this.updateArrowPosition(); - this.createTargetLine(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { this.stringDrawn = true; this.createStrings(); this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); - this.stringData.grabHandPosition = this.getGrabHandPosition(); + // this.stringData.grabHandPosition = this.getGrabHandPosition(); + this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); this.stringData.grabHandRotation = this.getGrabHandRotation(); if (this.hasArrow === false) { this.createArrow(); @@ -268,17 +284,15 @@ this.drawStrings(); this.updateArrowPosition(); - this.createTargetLine(); } }, getArrowPosition: function() { - - var arrowVector = Vec3.subtract(this.bowProperties.position, this.stringData.handPosition); + var arrowVector = Vec3.subtract(this.stringData.handPosition, this.stringData.grabHandPosition); arrowVector = Vec3.normalize(arrowVector); arrowVector = Vec3.multiply(arrowVector, ARROW_OFFSET); - var arrowPosition = Vec3.sum(this.stringData.handPosition, arrowVector); + var arrowPosition = Vec3.sum(this.stringData.grabHandPosition, arrowVector); return arrowPosition; }, orientationOf: function(vector) { @@ -373,87 +387,6 @@ }, 100) }, - createTargetLine: function() { - var handToHand = Vec3.subtract(this.stringData.handPosition, this.stringData.grabHandPosition); - - var arrowRotation = this.orientationOf(handToHand); - - // Entities.addEntity({ - // type: "Line", - // name: "Debug Line", - // dimensions: LINE_ENTITY_DIMENSIONS, - // visible: true, - // rotation: arrowRotation, - // position: this.stringData.handPosition, - // linePoints: [ZERO_VEC, Vec3.multiply(-TARGET_LINE_LENGTH, handToHand)], - // color: { - // red: 255, - // green: 0, - // blue: 255 - // }, - // lifetime: 0.1 - // }); - - // Entities.addEntity({ - // type: "Line", - // name: "Debug Line", - // dimensions: LINE_ENTITY_DIMENSIONS, - // visible: true, - // // rotation: bowRotation, - // position: this.bowProperties.position, - // linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { - // x: 1, - // y: 0, - // z: 0 - // })], - // color: { - // red: 255, - // green: 0, - // blue: 0 - // }, - // lifetime: 0.1 - // }); - - // Entities.addEntity({ - // type: "Line", - // name: "Debug Line", - // dimensions: LINE_ENTITY_DIMENSIONS, - // visible: true, - // // rotation: bowRotation, - // position: this.bowProperties.position, - // linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { - // x: 0, - // y: 1, - // z: 0 - // })], - // color: { - // red: 0, - // green: 255, - // blue: 0 - // }, - // lifetime: 0.1 - // }); - - // Entities.addEntity({ - // type: "Line", - // name: "Debug Line", - // dimensions: LINE_ENTITY_DIMENSIONS, - // visible: true, - // // rotation:bowRotation, - // position: this.bowProperties.position, - // linePoints: [ZERO_VEC, Vec3.multiply(TARGET_LINE_LENGTH, { - // x: 0, - // y: 0, - // z: 1 - // })], - // color: { - // red: 0, - // green: 0, - // blue: 255 - // }, - // lifetime: 0.1 - // }); - }, getLocalLineVectors: function() { var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); From c0cb8b542e599abd7308a4dad1608ffdfe74de91 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Sun, 25 Oct 2015 22:47:02 -0700 Subject: [PATCH 15/79] add notching system, add fire --- examples/toybox/bow/arrow.js | 104 ++++++++++--- examples/toybox/bow/bow.js | 273 ++++++++++++++++++++++++++++++++++- 2 files changed, 352 insertions(+), 25 deletions(-) diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index fcb9ea2f20..f505c59dee 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -20,18 +20,56 @@ } Arrow.prototype = { + stickOnCollision: false, + glow: false, glowBox: null, + isBurning:false, preload: function(entityID) { this.entityID = entityID; - if (this.glowBox === null) { + this.isBurning = this.checkIfBurning(); + if (this.isBurning === true || this.glow === true) { + Script.update.connect(this.updateArrowProperties); + } + if (this.isBurning === true) { + Script.update.connect(this.updateFirePosition); + + } + if (this.glow === true && this.glowBow === null) { this.createGlowBox(); Script.update.connect(this.updateGlowBoxPosition); } }, + unload: function() { - Script.update.disconnect(this.updateGlowBoxPosition); - Entities.deleteEntity(this.glowBox); + + Script.update.disconnect(this.updateArrowProperties); + + if (this.isBurning) { + Script.update.disconnect(this.updateFirePosition); + + } + if (this.glowBox !== null) { + Script.update.disconnect(this.updateGlowBoxPosition); + } + }, + + checkIfBurning: function() { + var properties = Entities.getEntityProperties(this.entityID, "userData"); + var userData = JSON.parse(properties.userData); + var fire = false; + + if (userData.hasOwnProperty('hifiFireArrowKey')) { + this.fire = userData.hifiFireArrowKey.fire; + return true + + } else { + return false; + } + + + }, + createGlowBox: function() { var glowBowProperties = { name: 'Arrow Glow Box', @@ -46,39 +84,61 @@ green: 0, blue: 255 }, - } - _this.glowBow = Entities.addEntity(glowBowProperties); + }; + + _this.glowBox = Entities.addEntity(glowBowProperties); + }, + updateArrowProperties: function() { + _this.arrowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation"]); }, updateGlowBoxPosition: function() { - var arrowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation"]); //once parenting is available, just attach the glowbow to the arrow Entities.editEntity(_this.entityID, { - position: arrowProperties.position, - rotation: arrowProperties.rotation + position: _this.arrowProperties.position, + rotation: _this.arrowProperties.rotation + }) + }, + updateFirePosition: function() { + //once parenting is available, just attach the glowbow to the arrow + Entities.editEntity(_this.entityID, { + position: _this.arrowProperties.position }) }, collisionWithEntity: function(me, otherEntity, collision) { - Vec3.print('penetration = ', collision.penetration); Vec3.print('collision contact point = ', collision.contactPoint); - Entities.editEntity(this.entityID, { - velocity: { - x: 0, - y: 0, - z: 0 - }, - gravity: { - x: 0, - y: 0, - z: 0 - }, - collisionsWillMove: false + if (this.stickOnCollision === true) { + Entities.editEntity(this.entityID, { + velocity: { + x: 0, + y: 0, + z: 0 + }, + gravity: { + x: 0, + y: 0, + z: 0 + }, + collisionsWillMove: false + + }) + } - }) } } + function deleteEntity(entityID) { + if (entityID === this.entityID) { + if (_this.isBurning === true) { + _this.deleteEntity(_this.fire); + } + } + } + + Entities.deletingEntity.connect(deleteEntity); + + return new Arrow; }) \ No newline at end of file diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index d4a93eeed3..d40e0e7cb7 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -12,6 +12,7 @@ // TODO: // make it so you can shoot without blocking your view with your hand +// notching system // make arrows more visible // make arrow rotate toward ground as it flies // different model? compound bow will look better in the HMD, and be easier to aim. this is what HTC uses in the longbow demo: http://www.turbosquid.com/3d-models/3d-model-bow-arrow/773106 @@ -68,6 +69,33 @@ var LEFT_TIP = 1; var RIGHT_TIP = 3; + var NOTCH_DETECTOR_OFFSET = { + x: 0, + y: 0, + z: 0 + }; + + var NOTCH_DETECTOR_DIMENSIONS = { + x: 0, + y: 0, + z: 0 + }; + + var NOTCH_DETECTOR_DISTANCE = 0.1; + + var RELOAD_DETECTOR_OFFSET = { + x: 0, + y: 0, + z: 0 + }; + + var RELOAD_DETECTOR_DIMENSIONS = { + x: 0, + y: 0, + z: 0 + }; + var RELOAD_DETECTOR_DISTANCE = 0.1; + var _this; function Bow() { @@ -84,17 +112,29 @@ // shoot arrow with velocity relative to distance between hand position and bow // delete lines + // with notching system (for reloading): + // pick up bow + // move other hand near reload detector + // move arrow near notch detector + // pull back on the trigger to draw the string + // release to fire + Bow.prototype = { isGrabbed: false, stringDrawn: false, hasArrow: false, + arrowTipPosition: null, + arrowIsBurning: false, + hasArrowLoaded: false, + hasArrowNotched: false, + arrow: null, + fire: null, stringData: { currentColor: { red: 0, green: 255, blue: 0 } - }, preload: function(entityID) { @@ -211,10 +251,47 @@ continueNearGrab: function() { this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); + this.checkStringHand(); + if (this.useNotching === true) { + + + if (this.hasArrowLoaded === false) { + this.updateReloadDetectorPosition(); + + } + + if (this.hasArrowLoaded === true) { + this.updateNotchDetectorPosition(); + } + + this.checkArrowHand(); + + if (this.hasArrowNotched === true) { + //only test for strings now that an arrow is notched + this.checkStringHand(); + //should probably draw a string straight across the bow until its notched + } + } + }, + createStringPreNotch: function() { + var properties = {}; + this.preNotchString = Entities.addEntitiy(properties); + }, + + updatePreNotchStringPosition: function() { + var position; + Entities.editEntityProperties(this.preNotchString, { + position: position + }) + }, + + deletePreNotchString: function() { + Entities.deleteEntity(this.preNotchString); + }, releaseGrab: function() { if (this.isGrabbed === true && this.hand === this.initialHand) { this.isGrabbed = false; @@ -234,6 +311,43 @@ } }, + checkArrowHand: function() { + if (this.initialHand === 'left') { + this.getArrowHandPosition = MyAvatar.getRightPalmPosition; + this.getArrowHandRotation = MyAvatar.getRightPalmRotation; + } else if (this.initialHand === 'right') { + this.getArrowHandPosition = MyAvatar.getLeftPalmPosition; + this.getArrowHandRotation = MyAvatar.getLeftPalmRotation; + } + if (this.hasArrowLoaded === false) { + this.testForHandInReloadDetector(); + } + + if (this.hasArrowLoaded === true) { + this.testForHandInNotchDetector(); + } + + }, + + testForHandInReloadDetector: function() { + var arrowHandPosition = this.getArrowHandPosition(); + var reloadDetectorPosition = Entities.getEntityProperties(this.reloadDetector, "position"); + var fromArrowHandToReloadDetector = Vec3.distance(arrowHandPosition, reloadDetectorPosition); + if (fromArrowHandToReloadDetector < RELEOAD_DETECTOR_DISTANCE) { + this.hasArrowLoaded = true; + } + }, + + testForHandInNotchDetector: function() { + var arrowHandPosition = this.getArrowHandPosition(); + var notchDetectorPosition = Entities.getEntityProperties(this.notchDetector, "position"); + var fromArrowHandToNotchDetector = Vec3.distance(arrowHandPosition, notchDetectorPosition); + if (fromArrowHandToNotchDetector < NOTCH_DETECTOR_DISTANCE) { + this.hasArrowNotched = true; + this.notchArrow(); + } + }, + checkStringHand: function() { //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with if (this.initialHand === 'left') { @@ -295,6 +409,7 @@ var arrowPosition = Vec3.sum(this.stringData.grabHandPosition, arrowVector); return arrowPosition; }, + orientationOf: function(vector) { var Y_AXIS = { x: 0, @@ -328,7 +443,30 @@ position: arrowPosition, rotation: arrowRotation }); + + if (this.arrowIsBurning === true) { + Entities.editEntity(this.fire, { + position: this.arrow.position + }); + } }, + + updateArrowPositionPreNotch: function() { + + var arrowHandPosition = this.getArrowHandPosition(); + + + Entities.editEntity(this.arrow, { + position: arrowPosition + }); + + if (this.arrowIsBurning === true) { + Entities.editEntity(this.fire, { + position: this.arrow.position + }); + } + }, + createArrow: function() { // print('CREATING ARROW'); var arrowProperties = { @@ -353,6 +491,7 @@ }; this.arrow = Entities.addEntity(arrowProperties); + }, releaseArrow: function() { @@ -360,8 +499,6 @@ var arrowRotation = this.orientationOf(handToHand); - // var forwardVec = Quat.getFront(this.arrow.rotation); - // forwardVec = Vec3.normalize(forwardVec); var handDistanceAtRelease = Vec3.distance(this.stringData.grabHandPosition, this.stringData.handPosition); print('HAND DISTANCE:: ' + handDistanceAtRelease); var arrowForce = this.scaleArrowShotStrength(handDistanceAtRelease, 0, 2, 20, 50); @@ -386,15 +523,145 @@ }); }, 100) + this.arrow = null; + this.fire = null; + this.arrowIsBurnning = false; + }, + getLocalLineVectors: function() { var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); return [topVector, bottomVector]; }, + scaleArrowShotStrength: function(value, min1, max1, min2, max2) { return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); + }, + + createReloadDetector: function() { + var detectorProperties = { + type: 'Box', + shapeType: 'box', + visible: false, + dimensions: RELOAD_DETECTOR_DIMENSIONS, + position: detectorPosition + }; + this.reloadDetector = Entities.addEntity(detectorProperties); + }, + + createNotchDetector: function() { + var detectorPosition = Vec3.sum(this.bowProperties.position, NOTCH_DETECTOR_OFFSET); + + var detectorProperties = { + type: 'Box', + shapeType: 'box', + visible: false, + dimensions: NOTCH_DETECTOR_DIMENSIONS, + position: detectorPosition + }; + this.notchDetector = Entities.addEntity(detectorProperties); + }, + + updateReloadDetectorPosition: function() { + this.reloadDetectorPosition = Vec3.sum(MyAvatar.position, RELOAD_DETECTOR_OFFSET); + Entities.editEntity(this.reloadDetector, { + position: thi8s.reloadDetectorPosition + }); + }, + + updateNotchDetectorPosition: function() { + this.notchDetectorPosition = Vec3.sum(MyAvatar.position, NOTCH_DETECTOR_OFFSET); + Entities.editEntity(this.notchDetector, { + position: this.notchDetectorPosition + }); + }, + + updateArrowInHand: function() { + var arrowHandPosition = this.getArrowHandPosition(); + + }, + + updateArrowInNotch: function() { + var notchPosition = this.notchPosition + }, + + setArrowOnFire: function() { + + var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); + + var animationSettings = JSON.stringify({ + fps: 30, + running: true, + loop: true, + firstFrame: 1, + lastFrame: 10000 + }); + + + var fire = Entities.addEntity({ + type: "ParticleEffect", + name: "Hifi-Arrow-Fire", + animationSettings: animationSettings, + textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", + position: this.arrowTipPosition, + emitRate: 100, + colorStart: { + red: 70, + green: 70, + blue: 137 + }, + color: { + red: 200, + green: 99, + blue: 42 + }, + colorFinish: { + red: 255, + green: 99, + blue: 32 + }, + radiusSpread: 0.01, + radiusStart: 0.02, + radiusEnd: 0.001, + particleRadius: 0.05, + radiusFinish: 0.0, + emitOrientation: myOrientation, + emitSpeed: 0.3, + speedSpread: 0.1, + alphaStart: 0.05, + alpha: 0.1, + alphaFinish: 0.05, + emitDimensions: { + x: 1, + y: 1, + z: 0.1 + }, + polarFinish: 0.1, + emitAcceleration: { + x: 0.0, + y: 0.0, + z: 0.0 + }, + accelerationSpread: { + x: 0.1, + y: 0.01, + z: 0.1 + }, + lifespan: 1, + }); + + Entites.editEntityProperties(this.arrow, { + userData: JSON.stringify({ + hifiFireArrowKey: { + fire: this.fire + } + }) + }) + + this.arrowIsBurning = true; } + }; return new Bow(); From fb5b60021132f94e1038aeed925a28d86e09eba4 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 26 Oct 2015 12:25:01 -0700 Subject: [PATCH 16/79] bow work --- examples/toybox/bow/arrow.js | 2 - examples/toybox/bow/bow.js | 388 +++++++++++++++++++++-------------- 2 files changed, 233 insertions(+), 157 deletions(-) diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index f505c59dee..75c2a28316 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -66,8 +66,6 @@ } else { return false; } - - }, createGlowBox: function() { diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index d40e0e7cb7..3e13ece574 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -15,7 +15,7 @@ // notching system // make arrows more visible // make arrow rotate toward ground as it flies -// different model? compound bow will look better in the HMD, and be easier to aim. this is what HTC uses in the longbow demo: http://www.turbosquid.com/3d-models/3d-model-bow-arrow/773106 +// different model? compound bow will look better in the HMD, and be easier to aim. http://www.turbosquid.com/3d-models/3d-model-bow-arrow/773106 // add noise when you release arrow -> add the sound to the arrow and keep it with position so you hear it whizz by // add noise when you draw string // re-enable arrows sticking when they hit @@ -53,6 +53,8 @@ z: 0 }; + var ARROW_TIP_OFFSET = 0.32; + var TOP_NOTCH_OFFSET = 0.5; var BOTTOM_NOTCH_OFFSET = 0.5; @@ -76,24 +78,21 @@ }; var NOTCH_DETECTOR_DIMENSIONS = { - x: 0, - y: 0, - z: 0 + x: 0.25, + y: 0.25, + z: 0.25 }; var NOTCH_DETECTOR_DISTANCE = 0.1; - var RELOAD_DETECTOR_OFFSET = { - x: 0, - y: 0, - z: 0 - }; + var RELOAD_DETECTOR_OFFSET = 0.3; var RELOAD_DETECTOR_DIMENSIONS = { - x: 0, - y: 0, - z: 0 + x: 0.5, + y: 0.5, + z: 0.5 }; + var RELOAD_DETECTOR_DISTANCE = 0.1; var _this; @@ -120,14 +119,17 @@ // release to fire Bow.prototype = { + useNotching: true, isGrabbed: false, stringDrawn: false, hasArrow: false, arrowTipPosition: null, - arrowIsBurning: false, hasArrowLoaded: false, + reloadDetector: null, hasArrowNotched: false, + notchDetector: null, arrow: null, + arrowIsBurning: false, fire: null, stringData: { currentColor: { @@ -155,6 +157,139 @@ this.hand = 'right'; }, + startNearGrab: function() { + if (this.isGrabbed === true) { + return false; + } + this.isGrabbed = true; + this.initialHand = this.hand; + this.createReloadDetector(); + + Entities.editEntity(this.entityID, { + userData: JSON.stringify({ + grabbableKey: { + turnOffOtherHand: true, + turnOffOppositeBeam: true, + invertSolidWhileHeld: true + } + }) + }); + }, + + continueNearGrab: function() { + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); + if (this.notchDetector === null) { + this.createNotchDetector(); + } + + this.updateNotchDetectorPosition(); + this.updateReloadDetectorPosition(); + + this.checkStringHand(); + + if (this.useNotching === true) { + this.checkArrowHand(); + + if (this.hasArrowLoaded === false) { + this.updateArrowPositionPreNotch(); + + } + + if (this.hasArrowLoaded === true) { + this.updateNotchDetectorPosition(); + } + + + if (this.hasArrowNotched === true) { + //only test for strings now that an arrow is notched + // this.checkStringHand(); + //should probably draw a string straight across the bow until its notched + } + } + + }, + + releaseGrab: function() { + if (this.isGrabbed === true && this.hand === this.initialHand) { + this.isGrabbed = false; + this.stringDrawn = false; + this.deleteStrings(); + this.hasArrow = false; + Entities.deleteEntity(this.arrow); + Entities.editEntity(this.entityID, { + userData: JSON.stringify({ + grabbableKey: { + turnOffOtherHand: false, + turnOffOppositeBeam: true, + invertSolidWhileHeld: true + } + }) + }); + Entities.deleteEntity(this.reloadDetector); + Entities.deleteEntity(this.notchDetector); + this.notchDetector = null; + this.reloadDetector = null; + // if(this.useNotching===true){ + + // } + } + }, + + createStrings: function() { + this.createTopString(); + this.createBottomString(); + }, + + createTopString: function() { + var stringProperties = { + type: 'Line', + position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), + dimensions: LINE_DIMENSIONS + }; + + this.topString = Entities.addEntity(stringProperties); + }, + + createBottomString: function() { + var stringProperties = { + type: 'Line', + position: Vec3.sum(this.bowProperties.position, BOTTOM_NOTCH_OFFSET), + dimensions: LINE_DIMENSIONS + }; + + this.bottomString = Entities.addEntity(stringProperties); + }, + + deleteStrings: function() { + Entities.deleteEntity(this.topString); + Entities.deleteEntity(this.bottomString); + }, + + updateStringPositions: function() { + + var upVector = Quat.getUp(this.bowProperties.rotation); + var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); + var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); + var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET); + + this.topStringPosition = Vec3.sum(this.bowProperties.position, upOffset); + this.bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); + + Entities.editEntity(this.topString, { + position: this.topStringPosition + }); + + Entities.editEntity(this.bottomString, { + position: this.bottomStringPosition + }); + + Entities.editEntity(this.preNotchString, { + position: this.topStringPosition + }); + + + }, + drawStrings: function() { this.updateStringPositions(); @@ -182,134 +317,39 @@ }, - createStrings: function() { - this.createTopString(); - this.createBottomString(); - }, - deleteStrings: function() { - Entities.deleteEntity(this.topString); - Entities.deleteEntity(this.bottomString); - }, - - createTopString: function() { + createPreNotchString: function() { var stringProperties = { type: 'Line', position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), dimensions: LINE_DIMENSIONS }; - this.topString = Entities.addEntity(stringProperties); + this.preNotchString = Entities.addEntity(stringProperties); }, - createBottomString: function() { - var stringProperties = { - type: 'Line', - position: Vec3.sum(this.bowProperties.position, BOTTOM_NOTCH_OFFSET), - dimensions: LINE_DIMENSIONS - }; + drawPreNotchStrings: function() { - this.bottomString = Entities.addEntity(stringProperties); - }, + this.updateStringPositions(); - updateStringPositions: function() { - - var upVector = Quat.getUp(this.bowProperties.rotation); - var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); - var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); - var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET); - - this.topStringPosition = Vec3.sum(this.bowProperties.position, upOffset); - this.bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); - - Entities.editEntity(this.topString, { - position: this.topStringPosition + Entities.editEntity(this.preNotchString, { + linePoints: [{ + x: 0, + y: 0, + z: 0 + }, Vec3.sum({ + x: 0, + y: 0, + z: 0 + }, BOTTOM_NOTCH_OFFSET * 2)], + lineWidth: 5, + color: this.stringData.currentColor }); - Entities.editEntity(this.bottomString, { - position: this.bottomStringPosition - }); - - - }, - - startNearGrab: function() { - if (this.isGrabbed === true) { - return false; - } - this.isGrabbed = true; - this.initialHand = this.hand; - Entities.editEntity(this.entityID, { - userData: JSON.stringify({ - grabbableKey: { - turnOffOtherHand: true, - turnOffOppositeBeam: true, - invertSolidWhileHeld: true - } - }) - }); - }, - - continueNearGrab: function() { - this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); - - this.checkStringHand(); - - if (this.useNotching === true) { - - - if (this.hasArrowLoaded === false) { - this.updateReloadDetectorPosition(); - - } - - if (this.hasArrowLoaded === true) { - this.updateNotchDetectorPosition(); - } - - this.checkArrowHand(); - - if (this.hasArrowNotched === true) { - //only test for strings now that an arrow is notched - this.checkStringHand(); - //should probably draw a string straight across the bow until its notched - } - } - - }, - - createStringPreNotch: function() { - var properties = {}; - this.preNotchString = Entities.addEntitiy(properties); - }, - - updatePreNotchStringPosition: function() { - var position; - Entities.editEntityProperties(this.preNotchString, { - position: position - }) }, deletePreNotchString: function() { Entities.deleteEntity(this.preNotchString); }, - releaseGrab: function() { - if (this.isGrabbed === true && this.hand === this.initialHand) { - this.isGrabbed = false; - this.stringDrawn = false; - this.deleteStrings(); - this.hasArrow = false; - Entities.deleteEntity(this.arrow); - Entities.editEntity(this.entityID, { - userData: JSON.stringify({ - grabbableKey: { - turnOffOtherHand: false, - turnOffOppositeBeam: true, - invertSolidWhileHeld: true - } - }) - }); - } - }, checkArrowHand: function() { if (this.initialHand === 'left') { @@ -332,8 +372,11 @@ testForHandInReloadDetector: function() { var arrowHandPosition = this.getArrowHandPosition(); var reloadDetectorPosition = Entities.getEntityProperties(this.reloadDetector, "position"); - var fromArrowHandToReloadDetector = Vec3.distance(arrowHandPosition, reloadDetectorPosition); - if (fromArrowHandToReloadDetector < RELEOAD_DETECTOR_DISTANCE) { + var fromArrowHandToReloadDetector = Vec3.subtract(arrowHandPosition, reloadDetectorPosition); + var distance = Vec3.length(fromArrowHandToReloadDetector); + print('fromArrowHandToReloadDetector distance :: '+distance); + if (fromArrowHandToReloadDetector < RELOAD_DETECTOR_DISTANCE) { + print('ARROW LOADED'); this.hasArrowLoaded = true; } }, @@ -341,8 +384,10 @@ testForHandInNotchDetector: function() { var arrowHandPosition = this.getArrowHandPosition(); var notchDetectorPosition = Entities.getEntityProperties(this.notchDetector, "position"); - var fromArrowHandToNotchDetector = Vec3.distance(arrowHandPosition, notchDetectorPosition); - if (fromArrowHandToNotchDetector < NOTCH_DETECTOR_DISTANCE) { + var fromArrowHandToNotchDetector = Vec3.subtract(arrowHandPosition, notchDetectorPosition); + var distance = Vec3.length(fromArrowHandToNotchDetector); + if (distance < NOTCH_DETECTOR_DISTANCE) { + print('ARROW NOTCHED'); this.hasArrowNotched = true; this.notchArrow(); } @@ -452,21 +497,33 @@ }, updateArrowPositionPreNotch: function() { - - var arrowHandPosition = this.getArrowHandPosition(); - - Entities.editEntity(this.arrow, { - position: arrowPosition + position: this.getArrowHandPosition(), + rotatin: this.getArrowHandRotation() }); if (this.arrowIsBurning === true) { Entities.editEntity(this.fire, { - position: this.arrow.position + position: arrowHandPosition }); } }, + updateArrowPositionInNotch: function() { + var notchPosition = this.notchDetectorPosition; + + Entities.editEntityProperties(this.arrow, { + position: notchPosition + }) + + if (this.arrowIsBurning === true) { + Entities.editEntity(this.fire, { + position: arrowTipPosition + }); + } + + }, + createArrow: function() { // print('CREATING ARROW'); var arrowProperties = { @@ -494,6 +551,12 @@ }, + getArrowTipPosition: function() { + var arrowPosition = this.getArrowPosition(); + var arrowTipPosition = Vec3.sum(arrowPosition, ARROW_TIP_OFFSET); + return arrowTipPosition + }, + releaseArrow: function() { var handToHand = Vec3.subtract(this.stringData.grabHandPosition, this.stringData.handPosition); @@ -539,14 +602,33 @@ return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); }, + getReloadDetectorPosition: function() { + var avatarHeadPosition = MyAvatar.getHeadPosition(); + var offsetDirection; + this.initialHand === 'left' ? offsetDirection = Quat.getRight(MyAvatar.orientation) : offsetDirection = Vec3.multiply(-1, Quat.getRight(MyAvatar.orientation)); + var offset = Vec3.multiply(offsetDirection, RELOAD_DETECTOR_OFFSET); + var detectorPosition = Vec3.sum(avatarHeadPosition, offset); + + return detectorPosition + }, + createReloadDetector: function() { + + var detectorPosition = this.getReloadDetectorPosition(); + var detectorProperties = { type: 'Box', - shapeType: 'box', - visible: false, + visible: true, + ignoreForCollisions: true, dimensions: RELOAD_DETECTOR_DIMENSIONS, - position: detectorPosition + position: detectorPosition, + color: { + red: 255, + green: 0, + blue: 0 + } }; + this.reloadDetector = Entities.addEntity(detectorProperties); }, @@ -555,37 +637,34 @@ var detectorProperties = { type: 'Box', - shapeType: 'box', - visible: false, + visible: true, + ignoreForCollisions: true, dimensions: NOTCH_DETECTOR_DIMENSIONS, - position: detectorPosition + position: detectorPosition, + color: { + red: 0, + green: 255, + blue: 0 + } }; + this.notchDetector = Entities.addEntity(detectorProperties); }, - + updateReloadDetectorPosition: function() { - this.reloadDetectorPosition = Vec3.sum(MyAvatar.position, RELOAD_DETECTOR_OFFSET); + this.reloadDetectorPosition = this.getReloadDetectorPosition(); Entities.editEntity(this.reloadDetector, { - position: thi8s.reloadDetectorPosition + position: this.reloadDetectorPosition }); }, updateNotchDetectorPosition: function() { - this.notchDetectorPosition = Vec3.sum(MyAvatar.position, NOTCH_DETECTOR_OFFSET); + this.notchDetectorPosition = Vec3.sum(this.bowProperties.position, NOTCH_DETECTOR_OFFSET); Entities.editEntity(this.notchDetector, { - position: this.notchDetectorPosition + position: this.notchDetectorPosition }); }, - updateArrowInHand: function() { - var arrowHandPosition = this.getArrowHandPosition(); - - }, - - updateArrowInNotch: function() { - var notchPosition = this.notchPosition - }, - setArrowOnFire: function() { var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); @@ -598,7 +677,6 @@ lastFrame: 10000 }); - var fire = Entities.addEntity({ type: "ParticleEffect", name: "Hifi-Arrow-Fire", From 8b78278a79c63171138a1a40b3100193c92f1cfb Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 27 Oct 2015 18:26:12 -0700 Subject: [PATCH 17/79] end of day --- examples/toybox/bow/bow.js | 103 +++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 27 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 3e13ece574..a1fd32e836 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -11,16 +11,20 @@ // // TODO: -// make it so you can shoot without blocking your view with your hand -// notching system +// [x] make it so you can shoot without blocking your view with your hand +// [x] notching system // make arrows more visible // make arrow rotate toward ground as it flies -// different model? compound bow will look better in the HMD, and be easier to aim. http://www.turbosquid.com/3d-models/3d-model-bow-arrow/773106 +// [x] different model? compound bow will look better in the HMD, and be easier to aim. http://www.turbosquid.com/3d-models/3d-model-bow-arrow/773106 // add noise when you release arrow -> add the sound to the arrow and keep it with position so you hear it whizz by // add noise when you draw string // re-enable arrows sticking when they hit // prepare for haptics +// from chat w/ ryan +// 5 arrows on table +// pick up arrow entity +// notch it (function() { var ZERO_VEC = { @@ -163,7 +167,11 @@ } this.isGrabbed = true; this.initialHand = this.hand; - this.createReloadDetector(); + + if (this.useNotching === true) { + this.createReloadDetector(); + + } Entities.editEntity(this.entityID, { userData: JSON.stringify({ @@ -178,31 +186,31 @@ continueNearGrab: function() { this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); - if (this.notchDetector === null) { - this.createNotchDetector(); - } - - this.updateNotchDetectorPosition(); - this.updateReloadDetectorPosition(); this.checkStringHand(); if (this.useNotching === true) { - this.checkArrowHand(); + if (this.notchDetector === null) { + this.createNotchDetector(); + } - if (this.hasArrowLoaded === false) { - this.updateArrowPositionPreNotch(); + this.updateNotchDetectorPosition(); + this.updateReloadDetectorPosition(); + this.checkArrowHand(); + + if (this.hasArrowLoaded === true && this.hasArrowNotched === false) { + updateArrowPositionPreNotch(); } if (this.hasArrowLoaded === true) { this.updateNotchDetectorPosition(); } - + if (this.hasArrowNotched === true) { //only test for strings now that an arrow is notched - // this.checkStringHand(); + // this.checkStringHand(); //should probably draw a string straight across the bow until its notched } } @@ -287,7 +295,6 @@ position: this.topStringPosition }); - }, drawStrings: function() { @@ -318,6 +325,14 @@ }, + getLocalLineVectors: function() { + var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); + var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); + return [topVector, bottomVector]; + }, + + + createPreNotchString: function() { var stringProperties = { type: 'Line', @@ -332,6 +347,10 @@ this.updateStringPositions(); + var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); + var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET * 2); + + Entities.editEntity(this.preNotchString, { linePoints: [{ x: 0, @@ -341,7 +360,7 @@ x: 0, y: 0, z: 0 - }, BOTTOM_NOTCH_OFFSET * 2)], + }, downOffset)], lineWidth: 5, color: this.stringData.currentColor }); @@ -374,13 +393,41 @@ var reloadDetectorPosition = Entities.getEntityProperties(this.reloadDetector, "position"); var fromArrowHandToReloadDetector = Vec3.subtract(arrowHandPosition, reloadDetectorPosition); var distance = Vec3.length(fromArrowHandToReloadDetector); - print('fromArrowHandToReloadDetector distance :: '+distance); + print('fromArrowHandToReloadDetector distance :: ' + distance); if (fromArrowHandToReloadDetector < RELOAD_DETECTOR_DISTANCE) { print('ARROW LOADED'); - this.hasArrowLoaded = true; + if (this.hasArrowLoaded === false) { + this.hasArrowLoaded = true; + this.loadArrow() + } } }, + loadArrow: function() { + var arrowProperties = { + name: 'Hifi-Arrow', + type: 'Model', + shapeType: 'box', + modelURL: ARROW_MODEL_URL, + dimensions: ARROW_DIMENSIONS, + position: this.getArrowHandPosition(), + rotation: this.getArrowHandRotation(), + collisionsWillMove: false, + ignoreForCollisions: true, + gravity: ARROW_GRAVITY, + // script: ARROW_SCRIPT_URL, + lifetime: 40, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false, + + } + }) + }; + + this.arrow = Entities.addEntity(arrowProperties); + }, + testForHandInNotchDetector: function() { var arrowHandPosition = this.getArrowHandPosition(); var notchDetectorPosition = Entities.getEntityProperties(this.notchDetector, "position"); @@ -388,11 +435,19 @@ var distance = Vec3.length(fromArrowHandToNotchDetector); if (distance < NOTCH_DETECTOR_DISTANCE) { print('ARROW NOTCHED'); - this.hasArrowNotched = true; - this.notchArrow(); + if (this.hasArrowNotched === false) { + this.hasArrowNotched = true; + this.notchArrow(); + } + } }, + notchArrow: function() { + + //put the arrow in the notch + }, + checkStringHand: function() { //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with if (this.initialHand === 'left') { @@ -592,12 +647,6 @@ }, - getLocalLineVectors: function() { - var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); - var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); - return [topVector, bottomVector]; - }, - scaleArrowShotStrength: function(value, min1, max1, min2, max2) { return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); }, From 0003eecf088e65de7633bf9d4972d37111ce0612 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 28 Oct 2015 10:20:35 -0700 Subject: [PATCH 18/79] remove reload feature to prep for arrow netity --- examples/toybox/bow/bow.js | 147 ++----------------------------------- 1 file changed, 5 insertions(+), 142 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index a1fd32e836..b64353b080 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -11,11 +11,8 @@ // // TODO: -// [x] make it so you can shoot without blocking your view with your hand -// [x] notching system // make arrows more visible // make arrow rotate toward ground as it flies -// [x] different model? compound bow will look better in the HMD, and be easier to aim. http://www.turbosquid.com/3d-models/3d-model-bow-arrow/773106 // add noise when you release arrow -> add the sound to the arrow and keep it with position so you hear it whizz by // add noise when you draw string // re-enable arrows sticking when they hit @@ -89,16 +86,6 @@ var NOTCH_DETECTOR_DISTANCE = 0.1; - var RELOAD_DETECTOR_OFFSET = 0.3; - - var RELOAD_DETECTOR_DIMENSIONS = { - x: 0.5, - y: 0.5, - z: 0.5 - }; - - var RELOAD_DETECTOR_DISTANCE = 0.1; - var _this; function Bow() { @@ -117,19 +104,16 @@ // with notching system (for reloading): // pick up bow - // move other hand near reload detector + // pick up arrow // move arrow near notch detector // pull back on the trigger to draw the string // release to fire Bow.prototype = { - useNotching: true, isGrabbed: false, stringDrawn: false, hasArrow: false, arrowTipPosition: null, - hasArrowLoaded: false, - reloadDetector: null, hasArrowNotched: false, notchDetector: null, arrow: null, @@ -168,11 +152,6 @@ this.isGrabbed = true; this.initialHand = this.hand; - if (this.useNotching === true) { - this.createReloadDetector(); - - } - Entities.editEntity(this.entityID, { userData: JSON.stringify({ grabbableKey: { @@ -185,36 +164,21 @@ }, continueNearGrab: function() { - this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation"]); + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation","userData"]); - this.checkStringHand(); - - if (this.useNotching === true) { if (this.notchDetector === null) { this.createNotchDetector(); } this.updateNotchDetectorPosition(); - this.updateReloadDetectorPosition(); this.checkArrowHand(); - if (this.hasArrowLoaded === true && this.hasArrowNotched === false) { - updateArrowPositionPreNotch(); - } - - if (this.hasArrowLoaded === true) { - this.updateNotchDetectorPosition(); - } - - if (this.hasArrowNotched === true) { //only test for strings now that an arrow is notched // this.checkStringHand(); //should probably draw a string straight across the bow until its notched } - } - }, releaseGrab: function() { @@ -233,15 +197,10 @@ } }) }); - Entities.deleteEntity(this.reloadDetector); Entities.deleteEntity(this.notchDetector); this.notchDetector = null; - this.reloadDetector = null; - // if(this.useNotching===true){ - // } - } - }, + }}, createStrings: function() { this.createTopString(); @@ -369,63 +328,8 @@ deletePreNotchString: function() { Entities.deleteEntity(this.preNotchString); }, - - checkArrowHand: function() { - if (this.initialHand === 'left') { - this.getArrowHandPosition = MyAvatar.getRightPalmPosition; - this.getArrowHandRotation = MyAvatar.getRightPalmRotation; - } else if (this.initialHand === 'right') { - this.getArrowHandPosition = MyAvatar.getLeftPalmPosition; - this.getArrowHandRotation = MyAvatar.getLeftPalmRotation; - } - if (this.hasArrowLoaded === false) { - this.testForHandInReloadDetector(); - } - - if (this.hasArrowLoaded === true) { - this.testForHandInNotchDetector(); - } - - }, - - testForHandInReloadDetector: function() { - var arrowHandPosition = this.getArrowHandPosition(); - var reloadDetectorPosition = Entities.getEntityProperties(this.reloadDetector, "position"); - var fromArrowHandToReloadDetector = Vec3.subtract(arrowHandPosition, reloadDetectorPosition); - var distance = Vec3.length(fromArrowHandToReloadDetector); - print('fromArrowHandToReloadDetector distance :: ' + distance); - if (fromArrowHandToReloadDetector < RELOAD_DETECTOR_DISTANCE) { - print('ARROW LOADED'); - if (this.hasArrowLoaded === false) { - this.hasArrowLoaded = true; - this.loadArrow() - } - } - }, - - loadArrow: function() { - var arrowProperties = { - name: 'Hifi-Arrow', - type: 'Model', - shapeType: 'box', - modelURL: ARROW_MODEL_URL, - dimensions: ARROW_DIMENSIONS, - position: this.getArrowHandPosition(), - rotation: this.getArrowHandRotation(), - collisionsWillMove: false, - ignoreForCollisions: true, - gravity: ARROW_GRAVITY, - // script: ARROW_SCRIPT_URL, - lifetime: 40, - userData: JSON.stringify({ - grabbableKey: { - grabbable: false, - - } - }) - }; - - this.arrow = Entities.addEntity(arrowProperties); + getArrowHandPosition:function(){ + return }, testForHandInNotchDetector: function() { @@ -476,10 +380,7 @@ this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); - // this.stringData.grabHandPosition = this.getGrabHandPosition(); this.stringData.grabHandRotation = this.getGrabHandRotation(); - - this.drawStrings(); this.updateArrowPosition(); @@ -488,7 +389,6 @@ this.createStrings(); this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); - // this.stringData.grabHandPosition = this.getGrabHandPosition(); this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); this.stringData.grabHandRotation = this.getGrabHandRotation(); if (this.hasArrow === false) { @@ -651,36 +551,6 @@ return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); }, - getReloadDetectorPosition: function() { - var avatarHeadPosition = MyAvatar.getHeadPosition(); - var offsetDirection; - this.initialHand === 'left' ? offsetDirection = Quat.getRight(MyAvatar.orientation) : offsetDirection = Vec3.multiply(-1, Quat.getRight(MyAvatar.orientation)); - var offset = Vec3.multiply(offsetDirection, RELOAD_DETECTOR_OFFSET); - var detectorPosition = Vec3.sum(avatarHeadPosition, offset); - - return detectorPosition - }, - - createReloadDetector: function() { - - var detectorPosition = this.getReloadDetectorPosition(); - - var detectorProperties = { - type: 'Box', - visible: true, - ignoreForCollisions: true, - dimensions: RELOAD_DETECTOR_DIMENSIONS, - position: detectorPosition, - color: { - red: 255, - green: 0, - blue: 0 - } - }; - - this.reloadDetector = Entities.addEntity(detectorProperties); - }, - createNotchDetector: function() { var detectorPosition = Vec3.sum(this.bowProperties.position, NOTCH_DETECTOR_OFFSET); @@ -700,13 +570,6 @@ this.notchDetector = Entities.addEntity(detectorProperties); }, - updateReloadDetectorPosition: function() { - this.reloadDetectorPosition = this.getReloadDetectorPosition(); - Entities.editEntity(this.reloadDetector, { - position: this.reloadDetectorPosition - }); - }, - updateNotchDetectorPosition: function() { this.notchDetectorPosition = Vec3.sum(this.bowProperties.position, NOTCH_DETECTOR_OFFSET); Entities.editEntity(this.notchDetector, { From 369cf8e454909340b530ca9b7dc33882a5999e5d Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 28 Oct 2015 10:22:51 -0700 Subject: [PATCH 19/79] update model paths --- examples/toybox/bow/bow.js | 31 ++++++++++++++++--------------- examples/toybox/bow/createBow.js | 7 +++---- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index b64353b080..ef240cdf0c 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -37,8 +37,8 @@ }; - var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good.fbx"; - var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/arrow_good_collision_hull.obj"; + var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow.fbx"; + var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow_collision_hull.obj"; var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); var ARROW_OFFSET = 0.32; var ARROW_FORCE = 1.25; @@ -164,21 +164,21 @@ }, continueNearGrab: function() { - this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation","userData"]); + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); - if (this.notchDetector === null) { - this.createNotchDetector(); - } + if (this.notchDetector === null) { + this.createNotchDetector(); + } - this.updateNotchDetectorPosition(); + this.updateNotchDetectorPosition(); - this.checkArrowHand(); + this.checkArrowHand(); - if (this.hasArrowNotched === true) { - //only test for strings now that an arrow is notched - // this.checkStringHand(); - //should probably draw a string straight across the bow until its notched - } + if (this.hasArrowNotched === true) { + //only test for strings now that an arrow is notched + // this.checkStringHand(); + //should probably draw a string straight across the bow until its notched + } }, releaseGrab: function() { @@ -200,7 +200,8 @@ Entities.deleteEntity(this.notchDetector); this.notchDetector = null; - }}, + } + }, createStrings: function() { this.createTopString(); @@ -328,7 +329,7 @@ deletePreNotchString: function() { Entities.deleteEntity(this.preNotchString); }, - getArrowHandPosition:function(){ + getArrowHandPosition: function() { return }, diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index f50d7403aa..f52f72cc92 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -13,8 +13,8 @@ var SCRIPT_URL = Script.resolvePath('bow.js'); -var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_good_2.fbx"; -var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/bow_good_2_collision_hull.obj"; +var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow.fbx"; +var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow_collision_hull.obj"; var BOW_DIMENSIONS = { x: 0.02, y: 1, @@ -38,7 +38,6 @@ var bow = Entities.addEntity({ type: "Model", modelURL: MODEL_URL, position: center, - // rotation: BOW_ROTATION, dimensions: BOW_DIMENSIONS, collisionsWillMove: true, gravity: BOW_GRAVITY, @@ -48,7 +47,7 @@ var bow = Entities.addEntity({ userData: JSON.stringify({ grabbableKey: { invertSolidWhileHeld: true, - turnOffOppositeBeam:true + turnOffOppositeBeam: true } }) }); From 4e6234741aa103c3be9861681cbf828b7aee1bd6 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 28 Oct 2015 10:24:09 -0700 Subject: [PATCH 20/79] reenable both hands to pick up arrow --- examples/toybox/bow/bow.js | 6 +++--- examples/toybox/bow/createBow.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index ef240cdf0c..7ded7e82b8 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -155,8 +155,8 @@ Entities.editEntity(this.entityID, { userData: JSON.stringify({ grabbableKey: { - turnOffOtherHand: true, - turnOffOppositeBeam: true, + turnOffOtherHand: false, + turnOffOppositeBeam: false, invertSolidWhileHeld: true } }) @@ -192,7 +192,7 @@ userData: JSON.stringify({ grabbableKey: { turnOffOtherHand: false, - turnOffOppositeBeam: true, + turnOffOppositeBeam: false, invertSolidWhileHeld: true } }) diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index f52f72cc92..d76cee2841 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -47,7 +47,7 @@ var bow = Entities.addEntity({ userData: JSON.stringify({ grabbableKey: { invertSolidWhileHeld: true, - turnOffOppositeBeam: true + turnOffOppositeBeam: false } }) }); From 68a4365fa5186e33f87a049315098b60e6c7751f Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 28 Oct 2015 13:27:31 -0700 Subject: [PATCH 21/79] changes for arrow entity reload --- examples/toybox/bow/arrow.js | 81 ++++++++++--- examples/toybox/bow/bow.js | 198 ++++++++++++------------------- examples/toybox/bow/createBow.js | 6 +- 3 files changed, 140 insertions(+), 145 deletions(-) diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 75c2a28316..760a92ec72 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -12,6 +12,10 @@ (function() { + Script.include("../../libraries/utils.js"); + + var NOTCH_DETECTOR_SEARCH_RADIUS = 0.25; + var _this; function Arrow() { @@ -23,36 +27,77 @@ stickOnCollision: false, glow: false, glowBox: null, - isBurning:false, + isBurning: false, preload: function(entityID) { this.entityID = entityID; - this.isBurning = this.checkIfBurning(); - if (this.isBurning === true || this.glow === true) { - Script.update.connect(this.updateArrowProperties); - } - if (this.isBurning === true) { - Script.update.connect(this.updateFirePosition); - } - if (this.glow === true && this.glowBow === null) { - this.createGlowBox(); - Script.update.connect(this.updateGlowBoxPosition); - } + + // this.isBurning = this.checkIfBurning(); + // if (this.isBurning === true || this.glow === true) { + // Script.update.connect(this.updateArrowProperties); + // } + // if (this.isBurning === true) { + // Script.update.connect(this.updateFirePosition); + + // } + // if (this.glow === true && this.glowBow === null) { + // this.createGlowBox(); + // Script.update.connect(this.updateGlowBoxPosition); + // } }, unload: function() { - Script.update.disconnect(this.updateArrowProperties); + // Script.update.disconnect(this.updateArrowProperties); - if (this.isBurning) { - Script.update.disconnect(this.updateFirePosition); + // if (this.isBurning) { + // Script.update.disconnect(this.updateFirePosition); - } - if (this.glowBox !== null) { - Script.update.disconnect(this.updateGlowBoxPosition); + // } + // if (this.glowBox !== null) { + // Script.update.disconnect(this.updateGlowBoxPosition); + // } + + }, + + continueNearGrab: function() { + this.searchForNotchDetectors(); + }, + + searchForNotchDetectors: function() { + + var ids = Entities.findEntities(MyAvatar.position, NOTCH_DETECTOR_SEARCH_RADIUS); + var i, properties; + for (i = 0; i < ids.length; i++) { + id = ids[i]; + properties = Entities.getEntityProperties(id, 'name'); + if (properties.name == "Hifi-NotchDetector") { + this.tellBowArrowIsNotched(this.getBowID()); + this.disableGrab(); + } } }, + getBowID: function() { + var properties = Entities.getEntityProperties(notchDetector, "userData"); + var userData = JSON.parse(properties.userData); + if (userData.hasOwnProperty('hifiBowKey')) { + return userData.hifiBowKey.bowID; + } + }, + + tellBowArrowIsNotched: function(bowID) { + setEntityCustomData('hifiBowKey', bowID, { + arrowNotched: true, + arrowID: this.entityID + }); + }, + + disableGrab: function() { + setEntityCustomData('grabbableKey', this.entityID, { + grabbable: false + }); + }, checkIfBurning: function() { var properties = Entities.getEntityProperties(this.entityID, "userData"); diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 7ded7e82b8..7c261e8d5f 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -1,8 +1,7 @@ // // bow.js // -// This script attaches to a bow that you can pick up with a hand controller. Use your other hand and press the trigger to grab the line, and release the trigger to fire. -// +// This script attaches to a bow that you can pick up with a hand controller. Load an arrow and then shoot it. // Created by James B. Pollack @imgntn on 10/19/2015 // Copyright 2015 High Fidelity, Inc. // @@ -24,6 +23,8 @@ // notch it (function() { + Script.include("../../libraries/utils.js"); + var ZERO_VEC = { x: 0, y: 0, @@ -36,17 +37,7 @@ z: 1000 }; - - var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow.fbx"; - var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow_collision_hull.obj"; - var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); var ARROW_OFFSET = 0.32; - var ARROW_FORCE = 1.25; - var ARROW_DIMENSIONS = { - x: 0.02, - y: 0.02, - z: 0.64 - }; var ARROW_GRAVITY = { x: 0, @@ -54,10 +45,8 @@ z: 0 }; - var ARROW_TIP_OFFSET = 0.32; - - var TOP_NOTCH_OFFSET = 0.5; - var BOTTOM_NOTCH_OFFSET = 0.5; + var TOP_NOTCH_OFFSET = 0.6; + var BOTTOM_NOTCH_OFFSET = 0.6; var LINE_DIMENSIONS = { x: 5, @@ -93,16 +82,6 @@ return; } - // create bow - // on pickup, wait for other hand to start trigger pull - // then create strings that start at top and bottom of bow - // extend them to the other hand position - // on trigger release, - // create arrow - // shoot arrow with velocity relative to distance between hand position and bow - // delete lines - - // with notching system (for reloading): // pick up bow // pick up arrow // move arrow near notch detector @@ -114,6 +93,7 @@ stringDrawn: false, hasArrow: false, arrowTipPosition: null, + preNotchString: null, hasArrowNotched: false, notchDetector: null, arrow: null, @@ -152,32 +132,50 @@ this.isGrabbed = true; this.initialHand = this.hand; - Entities.editEntity(this.entityID, { - userData: JSON.stringify({ - grabbableKey: { - turnOffOtherHand: false, - turnOffOppositeBeam: false, - invertSolidWhileHeld: true - } - }) + setEntityCustomData('grabbableKey', this.entityID, { + turnOffOtherHand: false, + turnOffOppositeBeam: false, + invertSolidWhileHeld: true }); + }, continueNearGrab: function() { this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); + //check to see if an arrow has notched itself in our notch detector + var userData = JSON.parse(this.bowProperties.userData); + if (userData.hasOwnProperty('hifiBowKey')) { + if (this.hasArrowNotched === false) { + this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; + } + this.arrow = userData.hifiBowKey.arrowID; + } + + //create a string across the bow when we pick it up + if (this.preNotchString === null) { + this.createPreNotchString(); + } + + if (this.preNotchString !== null && this.hasArrowNotched === false) { + this.drawPreNotchStrings(); + } + + // create the notch detector that arrows will look for if (this.notchDetector === null) { this.createNotchDetector(); } - this.updateNotchDetectorPosition(); - - this.checkArrowHand(); - + //if we have an arrow notched, then draw some new strings if (this.hasArrowNotched === true) { + Entities.deleteEntity(this.preNotchString); //only test for strings now that an arrow is notched - // this.checkStringHand(); - //should probably draw a string straight across the bow until its notched + this.checkStringHand(); + + } else { + //otherwise, just update the notch detector position + this.updateNotchDetectorPosition(); + } }, @@ -188,17 +186,15 @@ this.deleteStrings(); this.hasArrow = false; Entities.deleteEntity(this.arrow); - Entities.editEntity(this.entityID, { - userData: JSON.stringify({ - grabbableKey: { - turnOffOtherHand: false, - turnOffOppositeBeam: false, - invertSolidWhileHeld: true - } - }) + setEntityCustomData('grabbableKey', this.entityID, { + turnOffOtherHand: false, + turnOffOppositeBeam: false, + invertSolidWhileHeld: true }); Entities.deleteEntity(this.notchDetector); this.notchDetector = null; + Entities.deleteEntity(this.preNotchString); + this.preNotchString = null; } }, @@ -239,9 +235,12 @@ var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET); + var backOffset = Vec3.multiply(-0.1, Quat.getFront(this.bowProperties.rotation)); - this.topStringPosition = Vec3.sum(this.bowProperties.position, upOffset); - this.bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); + var topStringPosition = Vec3.sum(this.bowProperties.position, upOffset); + this.topStringPosition = Vec3.sum(topStringPosition, backOffset); + var bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); + this.bottomStringPosition = Vec3.sum(bottomStringPosition, backOffset); Entities.editEntity(this.topString, { position: this.topStringPosition @@ -297,7 +296,12 @@ var stringProperties = { type: 'Line', position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), - dimensions: LINE_DIMENSIONS + dimensions: LINE_DIMENSIONS, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false + } + }) }; this.preNotchString = Entities.addEntity(stringProperties); @@ -326,33 +330,6 @@ }); }, - deletePreNotchString: function() { - Entities.deleteEntity(this.preNotchString); - }, - getArrowHandPosition: function() { - return - }, - - testForHandInNotchDetector: function() { - var arrowHandPosition = this.getArrowHandPosition(); - var notchDetectorPosition = Entities.getEntityProperties(this.notchDetector, "position"); - var fromArrowHandToNotchDetector = Vec3.subtract(arrowHandPosition, notchDetectorPosition); - var distance = Vec3.length(fromArrowHandToNotchDetector); - if (distance < NOTCH_DETECTOR_DISTANCE) { - print('ARROW NOTCHED'); - if (this.hasArrowNotched === false) { - this.hasArrowNotched = true; - this.notchArrow(); - } - - } - }, - - notchArrow: function() { - - //put the arrow in the notch - }, - checkStringHand: function() { //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with if (this.initialHand === 'left') { @@ -372,12 +349,16 @@ this.triggerValue = Controller.getActionValue(this.stringTriggerAction); if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + + //firing the arrow this.stringDrawn = false; this.deleteStrings(); this.hasArrow = false; + this.preNotchString = null; this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); @@ -386,16 +367,13 @@ this.updateArrowPosition(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); this.stringData.grabHandRotation = this.getGrabHandRotation(); - if (this.hasArrow === false) { - this.createArrow(); - this.hasArrow = true; - } this.drawStrings(); this.updateArrowPosition(); @@ -480,39 +458,6 @@ }, - createArrow: function() { - // print('CREATING ARROW'); - var arrowProperties = { - name: 'Hifi-Arrow', - type: 'Model', - shapeType: 'box', - modelURL: ARROW_MODEL_URL, - dimensions: ARROW_DIMENSIONS, - position: this.getArrowPosition(), - rotation: this.bowProperties.rotation, - collisionsWillMove: false, - ignoreForCollisions: true, - gravity: ARROW_GRAVITY, - // script: ARROW_SCRIPT_URL, - lifetime: 40, - userData: JSON.stringify({ - grabbableKey: { - grabbable: false, - - } - }) - }; - - this.arrow = Entities.addEntity(arrowProperties); - - }, - - getArrowTipPosition: function() { - var arrowPosition = this.getArrowPosition(); - var arrowTipPosition = Vec3.sum(arrowPosition, ARROW_TIP_OFFSET); - return arrowTipPosition - }, - releaseArrow: function() { var handToHand = Vec3.subtract(this.stringData.grabHandPosition, this.stringData.handPosition); @@ -556,8 +501,9 @@ var detectorPosition = Vec3.sum(this.bowProperties.position, NOTCH_DETECTOR_OFFSET); var detectorProperties = { + name: 'Hifi-NotchDetector', type: 'Box', - visible: true, + visible: false, ignoreForCollisions: true, dimensions: NOTCH_DETECTOR_DIMENSIONS, position: detectorPosition, @@ -565,9 +511,15 @@ red: 0, green: 255, blue: 0 - } + }, + userData: JSON.stringify({ + hifiBowKey: { + bowID: this.entityID + } + }) }; + this.notchDetector = Entities.addEntity(detectorProperties); }, @@ -642,13 +594,11 @@ lifespan: 1, }); - Entites.editEntityProperties(this.arrow, { - userData: JSON.stringify({ - hifiFireArrowKey: { - fire: this.fire - } - }) - }) + setEntityCustomData('hifiFireArrowKey', this.arrow, { + fire: this.fire + + }); + this.arrowIsBurning = true; } diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index d76cee2841..3960f0c271 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -16,9 +16,9 @@ var SCRIPT_URL = Script.resolvePath('bow.js'); var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow.fbx"; var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow_collision_hull.obj"; var BOW_DIMENSIONS = { - x: 0.02, - y: 1, - z: 0.1 + x: 0.04, + y: 1.3, + z: 0.21 }; var BOW_GRAVITY = { From 24a5cdc19e59b84d5225385e745bb989c7b9bdeb Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 28 Oct 2015 14:00:37 -0700 Subject: [PATCH 22/79] through notching the arrow --- examples/controllers/handControllerGrab.js | 3 ++ examples/toybox/bow/arrow.js | 30 +++++++++-- examples/toybox/bow/bow.js | 1 - examples/toybox/bow/createArrow.js | 60 ++++++++++++++++++++++ examples/toybox/bow/createBowAndArrow.js | 2 + 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 examples/toybox/bow/createArrow.js create mode 100644 examples/toybox/bow/createBowAndArrow.js diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index e8b1b9cfbd..9dfbabbbe2 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -613,6 +613,9 @@ function MyController(hand, triggerAction) { Entities.callEntityMethod(this.grabbedEntity, "setLeftHand"); } Entities.callEntityMethod(this.grabbedEntity, "startNearGrab"); + setEntityCustomData('hifiHoldActionKey', this.grabbedEntity, { + holdActionID: this.actionID + }) } diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 760a92ec72..2a77d83fde 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -59,32 +59,48 @@ // } }, - + startNearGrab: function() { + print('STARTING GRAB ON ARROW!!') + }, continueNearGrab: function() { + this.currentProperties = Entities.getEntityProperties(this.entityID, "position"); this.searchForNotchDetectors(); }, searchForNotchDetectors: function() { + if (this.notched === true) { + return + }; + print('SEARCHING FOR NOTCH DETECTOR'); - var ids = Entities.findEntities(MyAvatar.position, NOTCH_DETECTOR_SEARCH_RADIUS); + var ids = Entities.findEntities(this.currentProperties.position, NOTCH_DETECTOR_SEARCH_RADIUS); var i, properties; for (i = 0; i < ids.length; i++) { id = ids[i]; properties = Entities.getEntityProperties(id, 'name'); if (properties.name == "Hifi-NotchDetector") { - this.tellBowArrowIsNotched(this.getBowID()); + print('NEAR THE NOTCH!!!') + this.notched = true; + this.tellBowArrowIsNotched(this.getBowID(id)); this.disableGrab(); } } }, - getBowID: function() { - var properties = Entities.getEntityProperties(notchDetector, "userData"); + getBowID: function(notchDetectorID) { + var properties = Entities.getEntityProperties(notchDetectorID, "userData"); var userData = JSON.parse(properties.userData); if (userData.hasOwnProperty('hifiBowKey')) { return userData.hifiBowKey.bowID; } }, + getActionID: function() { + var properties = Entities.getEntityProperties(this.entityID, "userData"); + var userData = JSON.parse(properties.userData); + if (userData.hasOwnProperty('hifiHoldActionKey')) { + return userData.hifiHoldActionKey.holdActionID; + } + }, tellBowArrowIsNotched: function(bowID) { setEntityCustomData('hifiBowKey', bowID, { @@ -94,9 +110,13 @@ }, disableGrab: function() { + print('ACTION ID IS::: ' + this.getActionID()); + Entities.deleteAction(this.entityID, this.getActionID()); + setEntityCustomData('grabbableKey', this.entityID, { grabbable: false }); + }, checkIfBurning: function() { diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 7c261e8d5f..05a8da2a2d 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -519,7 +519,6 @@ }) }; - this.notchDetector = Entities.addEntity(detectorProperties); }, diff --git a/examples/toybox/bow/createArrow.js b/examples/toybox/bow/createArrow.js new file mode 100644 index 0000000000..19e7c4be12 --- /dev/null +++ b/examples/toybox/bow/createArrow.js @@ -0,0 +1,60 @@ +// +// createArrow.js +// +// Created byJames Pollack @imgntn on 10/19/2015 +// Copyright 2015 High Fidelity, Inc. +// +// This script creates a bow you can use to shoot an arrow. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow.fbx"; +var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow_collision_hull.obj"; +var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); + +var ARROW_DIMENSIONS = { + x: 0.02, + y: 0.02, + z: 0.64 +}; + +var ARROW_GRAVITY = { + x: 0, + y: 0, + z: 0 +}; + +var center = Vec3.sum(Vec3.sum(MyAvatar.position, { + x: 0, + y: 1, + z: 0 +}), Vec3.multiply(1.25, Quat.getFront(Camera.getOrientation()))); + +function cleanup() { + Entities.deleteEntity(arrow); +} + +var arrow; + +function createArrow() { + arrow = Entities.addEntity({ + name: 'Hifi-Arrow', + type: 'Model', + modelURL: ARROW_MODEL_URL, + shapeType: 'compound', + compoundShapeURL: ARROW_COLLISION_HULL_URL, + dimensions: ARROW_DIMENSIONS, + position: center, + script:ARROW_SCRIPT_URL, + collisionsWillMove: true, + ignoreForCollisions: false, + gravity: ARROW_GRAVITY + }); + +} + +createArrow(); + +Script.scriptEnding.connect(cleanup); \ No newline at end of file diff --git a/examples/toybox/bow/createBowAndArrow.js b/examples/toybox/bow/createBowAndArrow.js new file mode 100644 index 0000000000..a731891e6c --- /dev/null +++ b/examples/toybox/bow/createBowAndArrow.js @@ -0,0 +1,2 @@ + Script.include("createBow.js"); + Script.include("createArrow.js"); From 8695963dd1dd9fa1248240ecbb1e74d7c53dc421 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 28 Oct 2015 19:10:16 -0700 Subject: [PATCH 23/79] end of day --- examples/controllers/handControllerGrab.js | 16 +++- examples/toybox/bow/arrow.js | 27 +++---- examples/toybox/bow/bow.js | 94 +++++++++++----------- 3 files changed, 73 insertions(+), 64 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 9dfbabbbe2..ebc5f97ee1 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -613,9 +613,6 @@ function MyController(hand, triggerAction) { Entities.callEntityMethod(this.grabbedEntity, "setLeftHand"); } Entities.callEntityMethod(this.grabbedEntity, "startNearGrab"); - setEntityCustomData('hifiHoldActionKey', this.grabbedEntity, { - holdActionID: this.actionID - }) } @@ -625,6 +622,7 @@ function MyController(hand, triggerAction) { }; this.continueNearGrabbing = function() { + if (this.triggerSmoothedReleased()) { this.setState(STATE_RELEASE); return; @@ -647,6 +645,17 @@ function MyController(hand, triggerAction) { this.currentHandControllerTipPosition = handControllerPosition; this.currentObjectTime = now; Entities.callEntityMethod(this.grabbedEntity, "continueNearGrab"); + var properties = Entities.getEntityProperties(this.grabbedEntity, ["userData","name"]); + var userData = JSON.parse(properties.userData); + var shouldRelease; + if (userData.hasOwnProperty('hifiBowKey')) { + var shouldRelease; + if (userData.hifiBowKey.shouldRelease === true) { + this.setState(STATE_RELEASE); + shouldRelease=true; + return; + } + } if (this.actionTimeout - now < ACTION_LIFETIME_REFRESH * MSEC_PER_SEC) { // if less than a 5 seconds left, refresh the actions lifetime @@ -824,6 +833,7 @@ function MyController(hand, triggerAction) { Entities.callEntityMethod(this.grabbedEntity, "releaseGrab"); } + // the action will tend to quickly bring an object's velocity to zero. now that // the action is gone, set the objects velocity to something the holder might expect. Entities.editEntity(this.grabbedEntity, { diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 2a77d83fde..6fda8e471c 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -28,6 +28,7 @@ glow: false, glowBox: null, isBurning: false, + notched: false, preload: function(entityID) { this.entityID = entityID; @@ -59,19 +60,19 @@ // } }, - startNearGrab: function() { - print('STARTING GRAB ON ARROW!!') - }, + continueNearGrab: function() { this.currentProperties = Entities.getEntityProperties(this.entityID, "position"); - this.searchForNotchDetectors(); + if (this.notched !== true) { + this.searchForNotchDetectors(); + + } }, searchForNotchDetectors: function() { if (this.notched === true) { return }; - print('SEARCHING FOR NOTCH DETECTOR'); var ids = Entities.findEntities(this.currentProperties.position, NOTCH_DETECTOR_SEARCH_RADIUS); var i, properties; @@ -82,7 +83,6 @@ print('NEAR THE NOTCH!!!') this.notched = true; this.tellBowArrowIsNotched(this.getBowID(id)); - this.disableGrab(); } } @@ -103,20 +103,18 @@ }, tellBowArrowIsNotched: function(bowID) { - setEntityCustomData('hifiBowKey', bowID, { - arrowNotched: true, - arrowID: this.entityID + setEntityCustomData('hifiBowKey', this.entityID, { + shouldRelease: true }); - }, - - disableGrab: function() { - print('ACTION ID IS::: ' + this.getActionID()); - Entities.deleteAction(this.entityID, this.getActionID()); setEntityCustomData('grabbableKey', this.entityID, { grabbable: false }); + setEntityCustomData('hifiBowKey', bowID, { + hasArrowNotched: true, + arrowID: this.entityID + }); }, checkIfBurning: function() { @@ -202,6 +200,5 @@ Entities.deletingEntity.connect(deleteEntity); - return new Arrow; }) \ No newline at end of file diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 05a8da2a2d..6eff7c6f51 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -68,9 +68,9 @@ }; var NOTCH_DETECTOR_DIMENSIONS = { - x: 0.25, - y: 0.25, - z: 0.25 + x: 0.15, + y: 0.15, + z: 0.15 }; var NOTCH_DETECTOR_DISTANCE = 0.1; @@ -142,16 +142,27 @@ continueNearGrab: function() { this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); + this.updateNotchDetectorPosition(); //check to see if an arrow has notched itself in our notch detector var userData = JSON.parse(this.bowProperties.userData); if (userData.hasOwnProperty('hifiBowKey')) { if (this.hasArrowNotched === false) { this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; + + + this.arrow = userData.hifiBowKey.arrowID; + + setEntityCustomData('grabbableKey', this.entityID, { + turnOffOtherHand: true, + turnOffOppositeBeam: false, + invertSolidWhileHeld: true + }); } - this.arrow = userData.hifiBowKey.arrowID; + } + //create a string across the bow when we pick it up if (this.preNotchString === null) { this.createPreNotchString(); @@ -168,13 +179,15 @@ //if we have an arrow notched, then draw some new strings if (this.hasArrowNotched === true) { - Entities.deleteEntity(this.preNotchString); + if (this.preNotchString !== null) { + Entities.deleteEntity(this.preNotchString); + } + //only test for strings now that an arrow is notched this.checkStringHand(); } else { //otherwise, just update the notch detector position - this.updateNotchDetectorPosition(); } }, @@ -350,12 +363,12 @@ if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + //firing the arrow - this.stringDrawn = false; - this.deleteStrings(); - this.hasArrow = false; - this.preNotchString = null; - this.releaseArrow(); + // this.stringDrawn = false; + // this.deleteStrings(); + // this.hasArrow = false; + // this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { //continuing to aim the arrow @@ -364,7 +377,7 @@ this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); - this.updateArrowPosition(); + this.updateArrowPositionInNotch(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { //the first time aiming the arrow @@ -376,7 +389,7 @@ this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); - this.updateArrowPosition(); + this.updateArrowPositionInNotch(); } }, @@ -411,45 +424,32 @@ return Quat.multiply(yaw, pitch); }, - updateArrowPosition: function() { - var arrowPosition = this.getArrowPosition(); - var handToHand = Vec3.subtract(this.stringData.handPosition, this.stringData.grabHandPosition); - var arrowRotation = this.orientationOf(handToHand); + updateArrowPositionInNotch: function() { + var arrowRotation = { + x:0, + y:0, + z:0, + w:1 + } + + var arrowOffset = Vec3.multiply(Quat.getFront(this.bowProperties.rotation),ARROW_OFFSET); + var arrowPosition = Vec3.sum(this.notchDetectorPosition,arrowOffset) + + var handToNotch = Vec3.subtract(this.stringData.handPosition, this.notchDetectorPosition); + + var rotation1 = Entities.getEntityProperties(this.arrow,"rotation").rotation; + + //var arrowRotation = this.orientationOf(handToNotch); Entities.editEntity(this.arrow, { position: arrowPosition, rotation: arrowRotation - }); - - if (this.arrowIsBurning === true) { - Entities.editEntity(this.fire, { - position: this.arrow.position - }); - } - }, - - updateArrowPositionPreNotch: function() { - Entities.editEntity(this.arrow, { - position: this.getArrowHandPosition(), - rotatin: this.getArrowHandRotation() - }); - - if (this.arrowIsBurning === true) { - Entities.editEntity(this.fire, { - position: arrowHandPosition - }); - } - }, - - updateArrowPositionInNotch: function() { - var notchPosition = this.notchDetectorPosition; - - Entities.editEntityProperties(this.arrow, { - position: notchPosition }) + var rotation2 = Entities.getEntityProperties(this.arrow,"rotation").rotation; + if (this.arrowIsBurning === true) { Entities.editEntity(this.fire, { position: arrowTipPosition @@ -459,6 +459,8 @@ }, releaseArrow: function() { + + print('RELEASE ARROW!!!') var handToHand = Vec3.subtract(this.stringData.grabHandPosition, this.stringData.handPosition); var arrowRotation = this.orientationOf(handToHand); @@ -474,8 +476,7 @@ ignoreForCollisions: false, collisionsWillMove: true, gravity: ARROW_GRAVITY, - velocity: forwardVec, - lifetime: 20 + velocity: forwardVec }; Entities.editEntity(this.arrow, arrowProperties); @@ -504,6 +505,7 @@ name: 'Hifi-NotchDetector', type: 'Box', visible: false, + collisionsWillMove:false, ignoreForCollisions: true, dimensions: NOTCH_DETECTOR_DIMENSIONS, position: detectorPosition, From 028ea584b8cb1c8d372fc9e8977179588b513534 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 29 Oct 2015 11:23:03 -0700 Subject: [PATCH 24/79] updating bow --- examples/controllers/handControllerGrab.js | 13 ++-- examples/toybox/bow/arrow.js | 14 ++-- examples/toybox/bow/bow.js | 82 ++++++++++++---------- examples/toybox/bow/createBow.js | 4 +- 4 files changed, 58 insertions(+), 55 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index ebc5f97ee1..50d99fc7b4 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -647,16 +647,19 @@ function MyController(hand, triggerAction) { Entities.callEntityMethod(this.grabbedEntity, "continueNearGrab"); var properties = Entities.getEntityProperties(this.grabbedEntity, ["userData","name"]); var userData = JSON.parse(properties.userData); - var shouldRelease; + if (userData.hasOwnProperty('hifiBowKey')) { - var shouldRelease; - if (userData.hifiBowKey.shouldRelease === true) { + if (userData.hifiBowKey.shouldRelease === true && userData.hifiBowKey.didRelease===false) { this.setState(STATE_RELEASE); - shouldRelease=true; + + setEntityCustomData('hifiBowKey', this.grabbedEntity, { + shouldRelease:false, + didRelease: true + }); return; } } - + if (this.actionTimeout - now < ACTION_LIFETIME_REFRESH * MSEC_PER_SEC) { // if less than a 5 seconds left, refresh the actions lifetime Entities.updateAction(this.grabbedEntity, this.actionID, { diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 6fda8e471c..6aa1efc266 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -94,17 +94,11 @@ return userData.hifiBowKey.bowID; } }, - getActionID: function() { - var properties = Entities.getEntityProperties(this.entityID, "userData"); - var userData = JSON.parse(properties.userData); - if (userData.hasOwnProperty('hifiHoldActionKey')) { - return userData.hifiHoldActionKey.holdActionID; - } - }, tellBowArrowIsNotched: function(bowID) { setEntityCustomData('hifiBowKey', this.entityID, { - shouldRelease: true + shouldRelease: true, + didRelease: false }); setEntityCustomData('grabbableKey', this.entityID, { @@ -167,10 +161,10 @@ }, collisionWithEntity: function(me, otherEntity, collision) { - Vec3.print('penetration = ', collision.penetration); - Vec3.print('collision contact point = ', collision.contactPoint); if (this.stickOnCollision === true) { + Vec3.print('penetration = ', collision.penetration); + Vec3.print('collision contact point = ', collision.contactPoint); Entities.editEntity(this.entityID, { velocity: { x: 0, diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 6eff7c6f51..c2767c4b2f 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -61,16 +61,13 @@ var LEFT_TIP = 1; var RIGHT_TIP = 3; - var NOTCH_DETECTOR_OFFSET = { - x: 0, - y: 0, - z: 0 - }; + var NOTCH_DETECTOR_OFFSET_FORWARD = 0.08; + var NOTCH_DETECTOR_OFFSET_UP = 0.035; var NOTCH_DETECTOR_DIMENSIONS = { - x: 0.15, - y: 0.15, - z: 0.15 + x: 0.05, + y: 0.05, + z: 0.05 }; var NOTCH_DETECTOR_DISTANCE = 0.1; @@ -134,8 +131,7 @@ setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, - turnOffOppositeBeam: false, - invertSolidWhileHeld: true + turnOffOppositeBeam: false }); }, @@ -150,13 +146,15 @@ if (this.hasArrowNotched === false) { this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; - this.arrow = userData.hifiBowKey.arrowID; + Entities.editEntity(this.arrow, { + collisionsWillMove: false, + ignoreForCollisions: true + }); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: true, - turnOffOppositeBeam: false, - invertSolidWhileHeld: true + turnOffOppositeBeam: false }); } @@ -187,7 +185,7 @@ this.checkStringHand(); } else { - //otherwise, just update the notch detector position + //otherwise, don't do much of anything. } }, @@ -201,8 +199,7 @@ Entities.deleteEntity(this.arrow); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, - turnOffOppositeBeam: false, - invertSolidWhileHeld: true + turnOffOppositeBeam: false }); Entities.deleteEntity(this.notchDetector); this.notchDetector = null; @@ -363,12 +360,11 @@ if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - - //firing the arrow - // this.stringDrawn = false; - // this.deleteStrings(); - // this.hasArrow = false; - // this.releaseArrow(); + // firing the arrow + this.stringDrawn = false; + this.deleteStrings(); + this.hasArrow = false; + this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { //continuing to aim the arrow @@ -428,27 +424,19 @@ updateArrowPositionInNotch: function() { - var arrowRotation = { - x:0, - y:0, - z:0, - w:1 - } - - var arrowOffset = Vec3.multiply(Quat.getFront(this.bowProperties.rotation),ARROW_OFFSET); - var arrowPosition = Vec3.sum(this.notchDetectorPosition,arrowOffset) + + var arrowOffset = Vec3.multiply(Quat.getFront(this.bowProperties.rotation), ARROW_OFFSET); + var arrowPosition = Vec3.sum(this.notchDetectorPosition, arrowOffset) var handToNotch = Vec3.subtract(this.stringData.handPosition, this.notchDetectorPosition); - var rotation1 = Entities.getEntityProperties(this.arrow,"rotation").rotation; - //var arrowRotation = this.orientationOf(handToNotch); + var arrowRotation = this.orientationOf(handToNotch); Entities.editEntity(this.arrow, { - position: arrowPosition, + position: this.notchDetectorPosition, rotation: arrowRotation }) - var rotation2 = Entities.getEntityProperties(this.arrow,"rotation").rotation; if (this.arrowIsBurning === true) { Entities.editEntity(this.fire, { @@ -499,13 +487,20 @@ }, createNotchDetector: function() { - var detectorPosition = Vec3.sum(this.bowProperties.position, NOTCH_DETECTOR_OFFSET); + var detectorPosition; + var frontVector = Quat.getFront(this.bowProperties.rotation); + var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); + var upVector = Quat.getUp(this.bowProperties.rotation); + var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); + + detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); + detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); var detectorProperties = { name: 'Hifi-NotchDetector', type: 'Box', - visible: false, - collisionsWillMove:false, + visible: true, + collisionsWillMove: false, ignoreForCollisions: true, dimensions: NOTCH_DETECTOR_DIMENSIONS, position: detectorPosition, @@ -525,7 +520,16 @@ }, updateNotchDetectorPosition: function() { - this.notchDetectorPosition = Vec3.sum(this.bowProperties.position, NOTCH_DETECTOR_OFFSET); + var detectorPosition; + var frontVector = Quat.getFront(this.bowProperties.rotation); + var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); + var upVector = Quat.getUp(this.bowProperties.rotation); + var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); + + detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); + detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); + + this.notchDetectorPosition = detectorPosition; Entities.editEntity(this.notchDetector, { position: this.notchDetectorPosition }); diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index 3960f0c271..a78343dc35 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -46,8 +46,10 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { - invertSolidWhileHeld: true, turnOffOppositeBeam: false + }, + grabKey: { + invertSolidWhileHeld: true } }) }); From 7166c6ddd20041452a47d5a80facc447c5705108 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 29 Oct 2015 12:23:32 -0700 Subject: [PATCH 25/79] through arrow in notch with rotation --- examples/controllers/handControllerGrab.js | 20 ++++++++++-------- examples/toybox/bow/arrow.js | 2 +- examples/toybox/bow/bow.js | 24 +++++++++++++--------- examples/toybox/bow/createBow.js | 4 +--- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 50d99fc7b4..d7eba55664 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -645,21 +645,23 @@ function MyController(hand, triggerAction) { this.currentHandControllerTipPosition = handControllerPosition; this.currentObjectTime = now; Entities.callEntityMethod(this.grabbedEntity, "continueNearGrab"); - var properties = Entities.getEntityProperties(this.grabbedEntity, ["userData","name"]); + var properties = Entities.getEntityProperties(this.grabbedEntity, ["userData", "name"]); var userData = JSON.parse(properties.userData); - - if (userData.hasOwnProperty('hifiBowKey')) { - if (userData.hifiBowKey.shouldRelease === true && userData.hifiBowKey.didRelease===false) { - this.setState(STATE_RELEASE); - - setEntityCustomData('hifiBowKey', this.grabbedEntity, { - shouldRelease:false, + + if (userData.hasOwnProperty('releaseGrabKey')) { + if (userData.releaseGrabKey.shouldRelease === true && userData.releaseGrabKey.didRelease === false) { + + setEntityCustomData('releaseGrabKey', this.grabbedEntity, { + shouldRelease: false, didRelease: true }); + + this.setState(STATE_RELEASE); + return; } } - + if (this.actionTimeout - now < ACTION_LIFETIME_REFRESH * MSEC_PER_SEC) { // if less than a 5 seconds left, refresh the actions lifetime Entities.updateAction(this.grabbedEntity, this.actionID, { diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 6aa1efc266..40d32f5d43 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -96,7 +96,7 @@ }, tellBowArrowIsNotched: function(bowID) { - setEntityCustomData('hifiBowKey', this.entityID, { + setEntityCustomData('releaseGrabKey', this.entityID, { shouldRelease: true, didRelease: false }); diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index c2767c4b2f..ad41a2c72b 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -9,7 +9,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// TODO: +// to do: // make arrows more visible // make arrow rotate toward ground as it flies // add noise when you release arrow -> add the sound to the arrow and keep it with position so you hear it whizz by @@ -131,12 +131,14 @@ setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, - turnOffOppositeBeam: false + turnOffOppositeBeam: false, + invertSolidWhileHeld: true }); }, continueNearGrab: function() { + print('GRABBING BOW!!') this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); this.updateNotchDetectorPosition(); @@ -154,7 +156,8 @@ setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: true, - turnOffOppositeBeam: false + turnOffOppositeBeam: true, + invertSolidWhileHeld: true }); } @@ -199,7 +202,8 @@ Entities.deleteEntity(this.arrow); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, - turnOffOppositeBeam: false + turnOffOppositeBeam: false, + invertSolidWhileHeld: true }); Entities.deleteEntity(this.notchDetector); this.notchDetector = null; @@ -370,7 +374,7 @@ //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); - this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); + // this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); this.updateArrowPositionInNotch(); @@ -381,7 +385,7 @@ this.createStrings(); this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); - this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); + // this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); @@ -472,9 +476,9 @@ Entities.editEntity(this.arrow, { ignoreForCollisions: false, collisionsWillMove: true, - gravity: ARROW_GRAVITY, + gravity: ARROW_GRAVITY }); - }, 100) + }, 100); this.arrow = null; this.fire = null; @@ -499,7 +503,7 @@ var detectorProperties = { name: 'Hifi-NotchDetector', type: 'Box', - visible: true, + visible: false, collisionsWillMove: false, ignoreForCollisions: true, dimensions: NOTCH_DETECTOR_DIMENSIONS, @@ -596,7 +600,7 @@ y: 0.01, z: 0.1 }, - lifespan: 1, + lifespan: 1 }); setEntityCustomData('hifiFireArrowKey', this.arrow, { diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index a78343dc35..cd92c1280c 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -46,9 +46,7 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { - turnOffOppositeBeam: false - }, - grabKey: { + turnOffOppositeBeam: false, invertSolidWhileHeld: true } }) From 48a295f4bcf0c646c3901e0612024181d5cb57a0 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 29 Oct 2015 15:24:04 -0700 Subject: [PATCH 26/79] werk --- examples/controllers/handControllerGrab.js | 14 ++++--- examples/toybox/bow/arrow.js | 8 +++- examples/toybox/bow/bow.js | 45 ++++++++++++---------- examples/toybox/bow/createArrow.js | 9 ++++- examples/toybox/bow/createBow.js | 2 +- 5 files changed, 48 insertions(+), 30 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index d7eba55664..ed616d85d3 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -286,10 +286,7 @@ function MyController(hand, triggerAction) { this.search = function() { //if this hand is the one that's disabled, we don't want to search for anything at all - if (this.hand === disabledHand) { - return; - } - + if (this.triggerSmoothedReleased()) { this.setState(STATE_RELEASE); return; @@ -303,7 +300,14 @@ function MyController(hand, triggerAction) { length: PICK_MAX_DISTANCE }; - this.lineOn(distantPickRay.origin, Vec3.multiply(distantPickRay.direction, LINE_LENGTH), NO_INTERSECT_COLOR); + if (this.hand === disabledHand) { + + } + else{ + this.lineOn(distantPickRay.origin, Vec3.multiply(distantPickRay.direction, LINE_LENGTH), NO_INTERSECT_COLOR); + + } + // don't pick 60x per second. var pickRays = []; diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 40d32f5d43..723f68f5b9 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -102,8 +102,14 @@ }); setEntityCustomData('grabbableKey', this.entityID, { - grabbable: false + grabbable: false, + invertSolidWhileHeld:true }); + + Entities.editEntity(this.entityID,{ + collisionsWillMove:false, + ignoreForCollisions:true + }) setEntityCustomData('hifiBowKey', bowID, { hasArrowNotched: true, diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index ad41a2c72b..720ce5dc7a 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -37,7 +37,7 @@ z: 1000 }; - var ARROW_OFFSET = 0.32; + var ARROW_OFFSET = -0.36; var ARROW_GRAVITY = { x: 0, @@ -98,9 +98,9 @@ fire: null, stringData: { currentColor: { - red: 0, + red: 255, green: 255, - blue: 0 + blue: 255 } }, @@ -138,7 +138,6 @@ }, continueNearGrab: function() { - print('GRABBING BOW!!') this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); this.updateNotchDetectorPosition(); @@ -146,13 +145,10 @@ var userData = JSON.parse(this.bowProperties.userData); if (userData.hasOwnProperty('hifiBowKey')) { if (this.hasArrowNotched === false) { + //notch the arrow this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; this.arrow = userData.hifiBowKey.arrowID; - Entities.editEntity(this.arrow, { - collisionsWillMove: false, - ignoreForCollisions: true - }); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: true, @@ -365,16 +361,16 @@ if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { // firing the arrow - this.stringDrawn = false; - this.deleteStrings(); - this.hasArrow = false; - this.releaseArrow(); + // this.stringDrawn = false; + // this.deleteStrings(); + // this.hasArrow = false; + // this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); - // this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); + this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); this.updateArrowPositionInNotch(); @@ -385,7 +381,7 @@ this.createStrings(); this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); - // this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); + this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); @@ -425,23 +421,29 @@ }, - updateArrowPositionInNotch: function() { - - var arrowOffset = Vec3.multiply(Quat.getFront(this.bowProperties.rotation), ARROW_OFFSET); - var arrowPosition = Vec3.sum(this.notchDetectorPosition, arrowOffset) - + //move it backwards var handToNotch = Vec3.subtract(this.stringData.handPosition, this.notchDetectorPosition); + var pullBackDistance = Vec3.length(handToNotch); + if(pullBackDistance>=0.6){ + pullBackDistance= 0.6; + } + + var pullBackOffset = Vec3.multiply(handToNotch,pullBackDistance); + var arrowPosition = Vec3.sum(this.notchDetectorPosition,pullBackOffset); + + var pushForwardOffset = Vec3.multiply(handToNotch, ARROW_OFFSET); + var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); var arrowRotation = this.orientationOf(handToNotch); + Entities.editEntity(this.arrow, { - position: this.notchDetectorPosition, + position: finalArrowPosition, rotation: arrowRotation }) - if (this.arrowIsBurning === true) { Entities.editEntity(this.fire, { position: arrowTipPosition @@ -534,6 +536,7 @@ detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); this.notchDetectorPosition = detectorPosition; + Entities.editEntity(this.notchDetector, { position: this.notchDetectorPosition }); diff --git a/examples/toybox/bow/createArrow.js b/examples/toybox/bow/createArrow.js index 19e7c4be12..fd0bd7ae53 100644 --- a/examples/toybox/bow/createArrow.js +++ b/examples/toybox/bow/createArrow.js @@ -47,10 +47,15 @@ function createArrow() { compoundShapeURL: ARROW_COLLISION_HULL_URL, dimensions: ARROW_DIMENSIONS, position: center, - script:ARROW_SCRIPT_URL, + script: ARROW_SCRIPT_URL, collisionsWillMove: true, ignoreForCollisions: false, - gravity: ARROW_GRAVITY + gravity: ARROW_GRAVITY, + userData: JSON.stringify({ + grabbableKey: { + invertSolidWhileHeld: true + } + }) }); } diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index cd92c1280c..cc59dea9e9 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -46,7 +46,7 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { - turnOffOppositeBeam: false, + turnOffOppositeBeam: true, invertSolidWhileHeld: true } }) From ad9cfbab50d5e592b0778c6463cc6250c3f510ac Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 29 Oct 2015 17:24:43 -0700 Subject: [PATCH 27/79] end of day --- examples/controllers/handControllerGrab.js | 14 ++--- examples/toybox/bow/arrow.js | 13 ++-- examples/toybox/bow/bow.js | 72 ++++++++++++++-------- examples/toybox/bow/createBow.js | 3 +- 4 files changed, 60 insertions(+), 42 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index e651743f00..9a82c901c7 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -88,7 +88,7 @@ var MSEC_PER_SEC = 1000.0; var LIFETIME = 10; var ACTION_TTL = 15; // seconds var ACTION_TTL_REFRESH = 5; -var PICKS_PER_SECOND_PER_HAND = 5; +var PICKS_PER_SECOND_PER_HAND = 10; var MSECS_PER_SEC = 1000.0; // states for the state machine @@ -286,7 +286,7 @@ function MyController(hand, triggerAction) { this.search = function() { //if this hand is the one that's disabled, we don't want to search for anything at all - + if (this.triggerSmoothedReleased()) { this.setState(STATE_RELEASE); return; @@ -300,13 +300,8 @@ function MyController(hand, triggerAction) { length: PICK_MAX_DISTANCE }; - if (this.hand === disabledHand) { - - } - else{ - this.lineOn(distantPickRay.origin, Vec3.multiply(distantPickRay.direction, LINE_LENGTH), NO_INTERSECT_COLOR); - } + this.lineOn(distantPickRay.origin, Vec3.multiply(distantPickRay.direction, LINE_LENGTH), NO_INTERSECT_COLOR); // don't pick 60x per second. @@ -561,6 +556,7 @@ function MyController(hand, triggerAction) { var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); var turnOffOtherHand = grabbableData["turnOffOtherHand"]; + print('SECOND GRAB ON ALREADY HELD OBJEcT') if (turnOffOtherHand === true) { //don't activate the second hand grab because the script is handling the second hand logic return; @@ -919,4 +915,4 @@ function cleanup() { } Script.scriptEnding.connect(cleanup); -Script.update.connect(update); +Script.update.connect(update); \ No newline at end of file diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 723f68f5b9..de4355cb7f 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -103,12 +103,12 @@ setEntityCustomData('grabbableKey', this.entityID, { grabbable: false, - invertSolidWhileHeld:true + invertSolidWhileHeld: true }); - - Entities.editEntity(this.entityID,{ - collisionsWillMove:false, - ignoreForCollisions:true + + Entities.editEntity(this.entityID, { + collisionsWillMove: false, + ignoreForCollisions: true }) setEntityCustomData('hifiBowKey', bowID, { @@ -149,9 +149,11 @@ _this.glowBox = Entities.addEntity(glowBowProperties); }, + updateArrowProperties: function() { _this.arrowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation"]); }, + updateGlowBoxPosition: function() { //once parenting is available, just attach the glowbow to the arrow Entities.editEntity(_this.entityID, { @@ -159,6 +161,7 @@ rotation: _this.arrowProperties.rotation }) }, + updateFirePosition: function() { //once parenting is available, just attach the glowbow to the arrow Entities.editEntity(_this.entityID, { diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 720ce5dc7a..7ebe733fde 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -130,8 +130,7 @@ this.initialHand = this.hand; setEntityCustomData('grabbableKey', this.entityID, { - turnOffOtherHand: false, - turnOffOppositeBeam: false, + turnOffOtherHand: true, invertSolidWhileHeld: true }); @@ -152,7 +151,6 @@ setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: true, - turnOffOppositeBeam: true, invertSolidWhileHeld: true }); } @@ -198,7 +196,6 @@ Entities.deleteEntity(this.arrow); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, - turnOffOppositeBeam: false, invertSolidWhileHeld: true }); Entities.deleteEntity(this.notchDetector); @@ -218,7 +215,14 @@ var stringProperties = { type: 'Line', position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), - dimensions: LINE_DIMENSIONS + dimensions: LINE_DIMENSIONS, + collisionsWillMove: false, + ignoreForCollisions: true, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false + } + }) }; this.topString = Entities.addEntity(stringProperties); @@ -228,7 +232,14 @@ var stringProperties = { type: 'Line', position: Vec3.sum(this.bowProperties.position, BOTTOM_NOTCH_OFFSET), - dimensions: LINE_DIMENSIONS + dimensions: LINE_DIMENSIONS, + collisionsWillMove: false, + ignoreForCollisions: true, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false + } + }) }; this.bottomString = Entities.addEntity(stringProperties); @@ -361,13 +372,15 @@ if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { // firing the arrow - // this.stringDrawn = false; - // this.deleteStrings(); - // this.hasArrow = false; - // this.releaseArrow(); + print('HIT RELEASE LOOP IN CHECK') + // this.stringDrawn = false; + // this.deleteStrings(); + // this.hasArrow = false; + // this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - //continuing to aim the arrow + print('HIT CONTINUE LOOP IN CHECK') + //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); @@ -376,7 +389,8 @@ this.updateArrowPositionInNotch(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { - //the first time aiming the arrow + print('HIT START LOOP IN CHECK') + //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); this.stringData.handPosition = this.getStringHandPosition(); @@ -427,12 +441,12 @@ var handToNotch = Vec3.subtract(this.stringData.handPosition, this.notchDetectorPosition); var pullBackDistance = Vec3.length(handToNotch); - if(pullBackDistance>=0.6){ - pullBackDistance= 0.6; + if (pullBackDistance >= 0.6) { + pullBackDistance = 0.6; } - var pullBackOffset = Vec3.multiply(handToNotch,pullBackDistance); - var arrowPosition = Vec3.sum(this.notchDetectorPosition,pullBackOffset); + var pullBackOffset = Vec3.multiply(handToNotch, pullBackDistance); + var arrowPosition = Vec3.sum(this.notchDetectorPosition, pullBackOffset); var pushForwardOffset = Vec3.multiply(handToNotch, ARROW_OFFSET); var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); @@ -455,34 +469,40 @@ releaseArrow: function() { print('RELEASE ARROW!!!') - var handToHand = Vec3.subtract(this.stringData.grabHandPosition, this.stringData.handPosition); - var arrowRotation = this.orientationOf(handToHand); + var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); + var pullBackDistance = Vec3.length(handToNotch); - var handDistanceAtRelease = Vec3.distance(this.stringData.grabHandPosition, this.stringData.handPosition); - print('HAND DISTANCE:: ' + handDistanceAtRelease); - var arrowForce = this.scaleArrowShotStrength(handDistanceAtRelease, 0, 2, 20, 50); + var arrowRotation = this.orientationOf(handToNotch); + + print('HAND DISTANCE:: ' + pullBackDistance); + var arrowForce = this.scaleArrowShotStrength(pullBackDistance, 0, 2, 20, 50); print('ARROW FORCE::' + arrowForce); - var forwardVec = Vec3.multiply(handToHand, arrowForce); + var forwardVec = Vec3.multiply(handToNotch, arrowForce); + print('FWD VEC:::' + JSON.stringify(forwardVec)); var arrowProperties = { // rotation:handToHand, - ignoreForCollisions: false, - collisionsWillMove: true, - gravity: ARROW_GRAVITY, + velocity: forwardVec }; Entities.editEntity(this.arrow, arrowProperties); Script.setTimeout(function() { + Entities.editEntity(this.arrow, { ignoreForCollisions: false, collisionsWillMove: true, gravity: ARROW_GRAVITY }); - }, 100); + }, 100) + + print('ARROW collisions??:::' + Entities.getEntityProperties(this.arrow, "collisionsWillMove").collisionsWillMove); + + print('ARROW velocity:::' + JSON.stringify(Entities.getEntityProperties(this.arrow, "velocity").velocity)) this.arrow = null; + this.hasArrowNotched = false; this.fire = null; this.arrowIsBurnning = false; diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index cc59dea9e9..f8f0064459 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -13,7 +13,7 @@ var SCRIPT_URL = Script.resolvePath('bow.js'); -var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow.fbx"; +var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow-deadly.fbx"; var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow_collision_hull.obj"; var BOW_DIMENSIONS = { x: 0.04, @@ -46,7 +46,6 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { - turnOffOppositeBeam: true, invertSolidWhileHeld: true } }) From 31b540ca38b191fd195a44f9d0bdd3a953d1f844 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 5 Nov 2015 14:29:58 -0800 Subject: [PATCH 28/79] mid test --- examples/toybox/bow/arrow.js | 2 +- examples/toybox/bow/bow.js | 43 ++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index de4355cb7f..b891bb699e 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -170,7 +170,7 @@ }, collisionWithEntity: function(me, otherEntity, collision) { - +print('ARROW HAD COLLISION') if (this.stickOnCollision === true) { Vec3.print('penetration = ', collision.penetration); Vec3.print('collision contact point = ', collision.contactPoint); diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 7ebe733fde..856b7d2ba5 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -368,6 +368,7 @@ } this.triggerValue = Controller.getActionValue(this.stringTriggerAction); + // print('TRIGGER VALUE:::' + this.triggerValue) if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { @@ -379,11 +380,11 @@ // this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - print('HIT CONTINUE LOOP IN CHECK') + // print('HIT CONTINUE LOOP IN CHECK') //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); - this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); + this.stringData.grabHandPosition = this.getGrabHandPosition(); this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); this.updateArrowPositionInNotch(); @@ -395,7 +396,8 @@ this.createStrings(); this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); - this.initialHand === 'right' ? this.stringData.grabHandPosition = Controller.getSpatialControlPosition(RIGHT_TIP) : this.stringData.grabHandPosition = Controller.getSpatialControlPosition(LEFT_TIP); + this.stringData.grabHandPosition = this.getGrabHandPosition(); + this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); @@ -436,33 +438,36 @@ updateArrowPositionInNotch: function() { - //move it backwards var handToNotch = Vec3.subtract(this.stringData.handPosition, this.notchDetectorPosition); - var pullBackDistance = Vec3.length(handToNotch); + // var pullBackDistance = Vec3.length(handToNotch); - if (pullBackDistance >= 0.6) { - pullBackDistance = 0.6; - } + // if (pullBackDistance >= 0.6) { + // pullBackDistance = 0.6; + // } - var pullBackOffset = Vec3.multiply(handToNotch, pullBackDistance); - var arrowPosition = Vec3.sum(this.notchDetectorPosition, pullBackOffset); + // var pullBackOffset = Vec3.multiply(handToNotch, pullBackDistance); + // var arrowPosition = Vec3.sum(this.notchDetectorPosition, pullBackOffset); - var pushForwardOffset = Vec3.multiply(handToNotch, ARROW_OFFSET); - var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); + // var pushForwardOffset = Vec3.multiply(handToNotch, ARROW_OFFSET); + // var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); var arrowRotation = this.orientationOf(handToNotch); - + print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); Entities.editEntity(this.arrow, { - position: finalArrowPosition, + position: this.notchDetectorPosition, rotation: arrowRotation }) - if (this.arrowIsBurning === true) { - Entities.editEntity(this.fire, { - position: arrowTipPosition - }); - } + var currentRotation = Entities.getEntityProperties(this.arrow,"rotation").rotation + print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); + + print('DIFFERENCE::: '+(1-Quat.dot(arrowRotation,currentRotation))); + // if (this.arrowIsBurning === true) { + // Entities.editEntity(this.fire, { + // position: arrowTipPosition + // }); + // } }, From 8fc702485b5ca0f9d234b0672cc13432cf8b2850 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 9 Nov 2015 14:39:30 -0800 Subject: [PATCH 29/79] werk --- examples/controllers/handControllerGrab.js | 5 ++- examples/toybox/bow/arrow.js | 22 ++++++++++-- examples/toybox/bow/bow.js | 39 ++++++++++++++-------- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index a44c97fef5..66bc4e951c 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -704,7 +704,10 @@ function MyController(hand) { Entities.callEntityMethod(this.grabbedEntity, "setLeftHand"); } Entities.callEntityMethod(this.grabbedEntity, "startNearGrab"); - + + setEntityCustomData('hifiHoldActionKey', this.grabbedEntity, { + holdActionID: this.actionID + }) } this.currentHandControllerTipPosition = (this.hand === RIGHT_HAND) ? MyAvatar.rightHandTipPosition : MyAvatar.leftHandTipPosition;; diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index b891bb699e..afefece195 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -94,8 +94,25 @@ return userData.hifiBowKey.bowID; } }, + getActionID: function() { + var properties = Entities.getEntityProperties(this.entityID, "userData"); + var userData = JSON.parse(properties.userData); + if (userData.hasOwnProperty('hifiHoldActionKey')) { + return userData.hifiHoldActionKey.holdActionID; + } + }, + + disableGrab: function() { + + + var actionID = this.getActionID(); + + print('actionID'+actionID) + Entities.deleteAction(this.entityID, actionID); + }, tellBowArrowIsNotched: function(bowID) { + this.disableGrab(); setEntityCustomData('releaseGrabKey', this.entityID, { shouldRelease: true, didRelease: false @@ -115,6 +132,7 @@ hasArrowNotched: true, arrowID: this.entityID }); + }, checkIfBurning: function() { @@ -149,7 +167,7 @@ _this.glowBox = Entities.addEntity(glowBowProperties); }, - + updateArrowProperties: function() { _this.arrowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation"]); }, @@ -170,7 +188,7 @@ }, collisionWithEntity: function(me, otherEntity, collision) { -print('ARROW HAD COLLISION') + print('ARROW HAD COLLISION') if (this.stickOnCollision === true) { Vec3.print('penetration = ', collision.penetration); Vec3.print('collision contact point = ', collision.contactPoint); diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 856b7d2ba5..4a4ca632f0 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -145,6 +145,7 @@ if (userData.hasOwnProperty('hifiBowKey')) { if (this.hasArrowNotched === false) { //notch the arrow + print('NOTCHING IT!') this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; this.arrow = userData.hifiBowKey.arrowID; @@ -193,7 +194,7 @@ this.stringDrawn = false; this.deleteStrings(); this.hasArrow = false; - Entities.deleteEntity(this.arrow); + // Entities.deleteEntity(this.arrow); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, invertSolidWhileHeld: true @@ -440,29 +441,39 @@ updateArrowPositionInNotch: function() { //move it backwards var handToNotch = Vec3.subtract(this.stringData.handPosition, this.notchDetectorPosition); - // var pullBackDistance = Vec3.length(handToNotch); + var pullBackDistance = Vec3.length(handToNotch); - // if (pullBackDistance >= 0.6) { - // pullBackDistance = 0.6; - // } + if (pullBackDistance >= 0.6) { + pullBackDistance = 0.6; + } - // var pullBackOffset = Vec3.multiply(handToNotch, pullBackDistance); - // var arrowPosition = Vec3.sum(this.notchDetectorPosition, pullBackOffset); + var pullBackOffset = Vec3.multiply(handToNotch, pullBackDistance); + var arrowPosition = Vec3.sum(this.notchDetectorPosition, pullBackOffset); - // var pushForwardOffset = Vec3.multiply(handToNotch, ARROW_OFFSET); - // var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); + var pushForwardOffset = Vec3.multiply(handToNotch, ARROW_OFFSET); + var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); var arrowRotation = this.orientationOf(handToNotch); - print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); + // print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); Entities.editEntity(this.arrow, { - position: this.notchDetectorPosition, - rotation: arrowRotation + position:this.notchDetectorPosition, + rotation: arrowRotation, + velocity: { + x: 0, + y: 0, + z: 0 + }, + angularVelocity: { + x: 0, + y: 0, + z: 0 + } }) var currentRotation = Entities.getEntityProperties(this.arrow,"rotation").rotation - print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); + // print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); - print('DIFFERENCE::: '+(1-Quat.dot(arrowRotation,currentRotation))); + print('DIFFERENCE::: '+(1-Quat.dot(arrowRotation,currentRotation))); // if (this.arrowIsBurning === true) { // Entities.editEntity(this.fire, { // position: arrowTipPosition From 9681145a1f809bcf0393d064013f51f40a8dbe10 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 11 Nov 2015 14:28:19 -0800 Subject: [PATCH 30/79] storage --- examples/toybox/bow/arrow.js | 27 +++++++++++++++++++------ examples/toybox/bow/bow.js | 38 ++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index afefece195..a7c9d9b3f3 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -32,7 +32,7 @@ preload: function(entityID) { this.entityID = entityID; - + Script.update.connect(this.update); // this.isBurning = this.checkIfBurning(); // if (this.isBurning === true || this.glow === true) { // Script.update.connect(this.updateArrowProperties); @@ -48,7 +48,7 @@ }, unload: function() { - + Script.update.disconnect(this.update); // Script.update.disconnect(this.updateArrowProperties); // if (this.isBurning) { @@ -103,11 +103,11 @@ }, disableGrab: function() { - + var actionID = this.getActionID(); - - print('actionID'+actionID) + + print('actionID' + actionID) Entities.deleteAction(this.entityID, actionID); }, @@ -125,7 +125,17 @@ Entities.editEntity(this.entityID, { collisionsWillMove: false, - ignoreForCollisions: true + ignoreForCollisions: true, + velocity: { + x: 0, + y: 0, + z: 0 + }, + angularVelocity: { + x: 0, + y: 0, + z: 0 + } }) setEntityCustomData('hifiBowKey', bowID, { @@ -134,6 +144,11 @@ }); }, + update:function(){ + + // var actions = Entities.getActionIDs(_this.entityID); + // print('ACTIONS ON ME:::'+actions) + }, checkIfBurning: function() { var properties = Entities.getEntityProperties(this.entityID, "userData"); diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 4a4ca632f0..2f1248dcb3 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -194,7 +194,7 @@ this.stringDrawn = false; this.deleteStrings(); this.hasArrow = false; - // Entities.deleteEntity(this.arrow); + // Entities.deleteEntity(this.arrow); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, invertSolidWhileHeld: true @@ -369,7 +369,7 @@ } this.triggerValue = Controller.getActionValue(this.stringTriggerAction); - // print('TRIGGER VALUE:::' + this.triggerValue) + // print('TRIGGER VALUE:::' + this.triggerValue) if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { @@ -381,8 +381,8 @@ // this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - // print('HIT CONTINUE LOOP IN CHECK') - //continuing to aim the arrow + // print('HIT CONTINUE LOOP IN CHECK') + //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.stringData.grabHandPosition = this.getGrabHandPosition(); @@ -454,26 +454,26 @@ var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); var arrowRotation = this.orientationOf(handToNotch); - // print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); + // print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); Entities.editEntity(this.arrow, { - position:this.notchDetectorPosition, + position: this.notchDetectorPosition, rotation: arrowRotation, - velocity: { - x: 0, - y: 0, - z: 0 - }, - angularVelocity: { - x: 0, - y: 0, - z: 0 - } + velocity: { + x: 0, + y: 0, + z: 0 + }, + angularVelocity: { + x: 0, + y: 0, + z: 0 + } }) - var currentRotation = Entities.getEntityProperties(this.arrow,"rotation").rotation - // print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); + var currentRotation = Entities.getEntityProperties(this.arrow, "rotation").rotation + // print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); - print('DIFFERENCE::: '+(1-Quat.dot(arrowRotation,currentRotation))); + print('DIFFERENCE::: ' + (1 - Quat.dot(arrowRotation, currentRotation))); // if (this.arrowIsBurning === true) { // Entities.editEntity(this.fire, { // position: arrowTipPosition From ffe90367554d76413466e9d704a7a11d111347a0 Mon Sep 17 00:00:00 2001 From: James Pollack Date: Wed, 11 Nov 2015 14:51:20 -0800 Subject: [PATCH 31/79] debug --- examples/controllers/handControllerGrab.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index e0af8c2bd8..e06cdd51c5 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -758,7 +758,7 @@ function MyController(hand) { if (this.actionTimeout - now < ACTION_TTL_REFRESH * MSEC_PER_SEC) { // if less than a 5 seconds left, refresh the actions ttl - Entities.updateAction(this.grabbedEntity, this.actionID, { + var success = Entities.updateAction(this.grabbedEntity, this.actionID, { hand: this.hand === RIGHT_HAND ? "right" : "left", timeScale: NEAR_GRABBING_ACTION_TIMEFRAME, relativePosition: this.offsetPosition, @@ -767,6 +767,8 @@ function MyController(hand) { kinematic: NEAR_GRABBING_KINEMATIC, kinematicSetVelocity: true }); + + print('Action update success:::'+success); this.actionTimeout = now + (ACTION_TTL * MSEC_PER_SEC); } }; From 595c628c39ed3ea3f254357b42148ebe6677031c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 11 Nov 2015 17:32:03 -0800 Subject: [PATCH 32/79] it works! --- examples/controllers/handControllerGrab.js | 4 +-- examples/toybox/bow/arrow.js | 7 ++-- examples/toybox/bow/bow.js | 42 +++++++++++----------- examples/toybox/bow/createArrow.js | 2 +- examples/toybox/bow/createBowAndArrow.js | 4 +-- 5 files changed, 26 insertions(+), 33 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index e06cdd51c5..e0af8c2bd8 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -758,7 +758,7 @@ function MyController(hand) { if (this.actionTimeout - now < ACTION_TTL_REFRESH * MSEC_PER_SEC) { // if less than a 5 seconds left, refresh the actions ttl - var success = Entities.updateAction(this.grabbedEntity, this.actionID, { + Entities.updateAction(this.grabbedEntity, this.actionID, { hand: this.hand === RIGHT_HAND ? "right" : "left", timeScale: NEAR_GRABBING_ACTION_TIMEFRAME, relativePosition: this.offsetPosition, @@ -767,8 +767,6 @@ function MyController(hand) { kinematic: NEAR_GRABBING_KINEMATIC, kinematicSetVelocity: true }); - - print('Action update success:::'+success); this.actionTimeout = now + (ACTION_TTL * MSEC_PER_SEC); } }; diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index a7c9d9b3f3..101271760a 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -108,15 +108,12 @@ var actionID = this.getActionID(); print('actionID' + actionID) - Entities.deleteAction(this.entityID, actionID); + var success = Entities.deleteAction(this.entityID, actionID); + print('ACTION DELETE SUCCESS:::'+success) }, tellBowArrowIsNotched: function(bowID) { this.disableGrab(); - setEntityCustomData('releaseGrabKey', this.entityID, { - shouldRelease: true, - didRelease: false - }); setEntityCustomData('grabbableKey', this.entityID, { grabbable: false, diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 2f1248dcb3..31f385e095 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -106,6 +106,7 @@ preload: function(entityID) { this.entityID = entityID; + this.bowID = entityID; }, setLeftHand: function() { @@ -440,40 +441,36 @@ updateArrowPositionInNotch: function() { //move it backwards - var handToNotch = Vec3.subtract(this.stringData.handPosition, this.notchDetectorPosition); + + + var handToNotch = Vec3.subtract(this.notchDetectorPosition,this.stringData.handPosition); + print('h2n:::'+JSON.stringify(handToNotch)) var pullBackDistance = Vec3.length(handToNotch); if (pullBackDistance >= 0.6) { pullBackDistance = 0.6; } - var pullBackOffset = Vec3.multiply(handToNotch, pullBackDistance); + var pullBackOffset = Vec3.multiply(handToNotch, -pullBackDistance); var arrowPosition = Vec3.sum(this.notchDetectorPosition, pullBackOffset); - var pushForwardOffset = Vec3.multiply(handToNotch, ARROW_OFFSET); + var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); - var arrowRotation = this.orientationOf(handToNotch); - // print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); + + + var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); + + print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); Entities.editEntity(this.arrow, { - position: this.notchDetectorPosition, - rotation: arrowRotation, - velocity: { - x: 0, - y: 0, - z: 0 - }, - angularVelocity: { - x: 0, - y: 0, - z: 0 - } + position: finalArrowPosition, + rotation: arrowRotation }) - var currentRotation = Entities.getEntityProperties(this.arrow, "rotation").rotation - // print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); + var currentRotation = Entities.getEntityProperties(this.arrow, "rotation").rotation + // print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); - print('DIFFERENCE::: ' + (1 - Quat.dot(arrowRotation, currentRotation))); + // print('DIFFERENCE::: ' + (1 - Quat.dot(arrowRotation, currentRotation))); // if (this.arrowIsBurning === true) { // Entities.editEntity(this.fire, { // position: arrowTipPosition @@ -541,7 +538,7 @@ var detectorProperties = { name: 'Hifi-NotchDetector', type: 'Box', - visible: false, + visible: true, collisionsWillMove: false, ignoreForCollisions: true, dimensions: NOTCH_DETECTOR_DIMENSIONS, @@ -574,7 +571,8 @@ this.notchDetectorPosition = detectorPosition; Entities.editEntity(this.notchDetector, { - position: this.notchDetectorPosition + position: this.notchDetectorPosition, + rotation:this.bowProperties.rotation }); }, diff --git a/examples/toybox/bow/createArrow.js b/examples/toybox/bow/createArrow.js index fd0bd7ae53..0af73b2e0f 100644 --- a/examples/toybox/bow/createArrow.js +++ b/examples/toybox/bow/createArrow.js @@ -12,7 +12,7 @@ var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow.fbx"; var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow_collision_hull.obj"; -var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); +var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js?123'); var ARROW_DIMENSIONS = { x: 0.02, diff --git a/examples/toybox/bow/createBowAndArrow.js b/examples/toybox/bow/createBowAndArrow.js index a731891e6c..b24f0e8d32 100644 --- a/examples/toybox/bow/createBowAndArrow.js +++ b/examples/toybox/bow/createBowAndArrow.js @@ -1,2 +1,2 @@ - Script.include("createBow.js"); - Script.include("createArrow.js"); + Script.include("createBow.js?123"); + Script.include("createArrow.js?123"); From 2b8ad51b4169fc097c77c6399fb36438b09533d5 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 11 Nov 2015 18:07:05 -0800 Subject: [PATCH 33/79] cleanup orientation of --- examples/controllers/handControllerGrab.js | 109 +++++++++++++-------- examples/toybox/bow/arrow.js | 13 +-- examples/toybox/bow/bow.js | 43 ++------ examples/toybox/bow/createArrow.js | 2 +- examples/toybox/bow/createBowAndArrow.js | 4 +- 5 files changed, 84 insertions(+), 87 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index e0af8c2bd8..44b56f6666 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -87,12 +87,13 @@ var ACTION_TTL_REFRESH = 5; var PICKS_PER_SECOND_PER_HAND = 10; var MSECS_PER_SEC = 1000.0; var GRABBABLE_PROPERTIES = ["position", - "rotation", - "gravity", - "ignoreForCollisions", - "collisionsWillMove", - "locked", - "name"]; + "rotation", + "gravity", + "ignoreForCollisions", + "collisionsWillMove", + "locked", + "name" +]; var GRABBABLE_DATA_KEY = "grabbableKey"; // shared with grab.js @@ -103,7 +104,7 @@ var DEFAULT_GRABBABLE_DATA = { invertSolidWhileHeld: false }; -var disabledHand ='none'; +var disabledHand = 'none'; // states for the state machine @@ -122,28 +123,28 @@ var STATE_RELEASE = 10; function stateToName(state) { switch (state) { - case STATE_OFF: - return "off"; - case STATE_SEARCHING: - return "searching"; - case STATE_DISTANCE_HOLDING: - return "distance_holding"; - case STATE_CONTINUE_DISTANCE_HOLDING: - return "continue_distance_holding"; - case STATE_NEAR_GRABBING: - return "near_grabbing"; - case STATE_CONTINUE_NEAR_GRABBING: - return "continue_near_grabbing"; - case STATE_NEAR_TRIGGER: - return "near_trigger"; - case STATE_CONTINUE_NEAR_TRIGGER: - return "continue_near_trigger"; - case STATE_FAR_TRIGGER: - return "far_trigger"; - case STATE_CONTINUE_FAR_TRIGGER: - return "continue_far_trigger"; - case STATE_RELEASE: - return "release"; + case STATE_OFF: + return "off"; + case STATE_SEARCHING: + return "searching"; + case STATE_DISTANCE_HOLDING: + return "distance_holding"; + case STATE_CONTINUE_DISTANCE_HOLDING: + return "continue_distance_holding"; + case STATE_NEAR_GRABBING: + return "near_grabbing"; + case STATE_CONTINUE_NEAR_GRABBING: + return "continue_near_grabbing"; + case STATE_NEAR_TRIGGER: + return "near_trigger"; + case STATE_CONTINUE_NEAR_TRIGGER: + return "continue_near_trigger"; + case STATE_FAR_TRIGGER: + return "far_trigger"; + case STATE_CONTINUE_FAR_TRIGGER: + return "continue_far_trigger"; + case STATE_RELEASE: + return "release"; } return "unknown"; @@ -195,8 +196,17 @@ function MyController(hand) { this.triggerValue = 0; // rolling average of trigger value this.rawTriggerValue = 0; - this.offsetPosition = { x: 0.0, y: 0.0, z: 0.0 }; - this.offsetRotation = { x: 0.0, y: 0.0, z: 0.0, w: 1.0 }; + this.offsetPosition = { + x: 0.0, + y: 0.0, + z: 0.0 + }; + this.offsetRotation = { + x: 0.0, + y: 0.0, + z: 0.0, + w: 1.0 + }; var _this = this; @@ -293,11 +303,11 @@ function MyController(hand) { this.pointer = null; }; - this.eitherTrigger = function (value) { + this.eitherTrigger = function(value) { _this.rawTriggerValue = value; }; - this.updateSmoothedTrigger = function () { + this.updateSmoothedTrigger = function() { var triggerValue = this.rawTriggerValue; // smooth out trigger value this.triggerValue = (this.triggerValue * TRIGGER_SMOOTH_RATIO) + @@ -412,10 +422,9 @@ function MyController(hand) { this.setState(STATE_NEAR_GRABBING); return; } - } else if (! entityIsGrabbedByOther(intersection.entityID)) { + } else if (!entityIsGrabbedByOther(intersection.entityID)) { // don't allow two people to distance grab the same object - if (intersection.properties.collisionsWillMove - && !intersection.properties.locked) { + if (intersection.properties.collisionsWillMove && !intersection.properties.locked) { // the hand is far from the intersected object. go into distance-holding mode this.grabbedEntity = intersection.entityID; this.setState(STATE_DISTANCE_HOLDING); @@ -434,10 +443,18 @@ function MyController(hand) { Entities.addEntity({ type: "Sphere", name: "Grab Debug Entity", - dimensions: {x: GRAB_RADIUS, y: GRAB_RADIUS, z: GRAB_RADIUS}, + dimensions: { + x: GRAB_RADIUS, + y: GRAB_RADIUS, + z: GRAB_RADIUS + }, visible: true, position: handPosition, - color: { red: 0, green: 255, blue: 0}, + color: { + red: 0, + green: 255, + blue: 0 + }, lifetime: 0.1 }); } @@ -704,7 +721,7 @@ function MyController(hand) { Entities.callEntityMethod(this.grabbedEntity, "setLeftHand"); } Entities.callEntityMethod(this.grabbedEntity, "startNearGrab"); - + setEntityCustomData('hifiHoldActionKey', this.grabbedEntity, { holdActionID: this.actionID }) @@ -758,7 +775,7 @@ function MyController(hand) { if (this.actionTimeout - now < ACTION_TTL_REFRESH * MSEC_PER_SEC) { // if less than a 5 seconds left, refresh the actions ttl - Entities.updateAction(this.grabbedEntity, this.actionID, { + var success = Entities.updateAction(this.grabbedEntity, this.actionID, { hand: this.hand === RIGHT_HAND ? "right" : "left", timeScale: NEAR_GRABBING_ACTION_TIMEFRAME, relativePosition: this.offsetPosition, @@ -767,6 +784,8 @@ function MyController(hand) { kinematic: NEAR_GRABBING_KINEMATIC, kinematicSetVelocity: true }); + + print('Action update success:::' + success); this.actionTimeout = now + (ACTION_TTL * MSEC_PER_SEC); } }; @@ -944,7 +963,13 @@ function MyController(hand) { data["gravity"] = grabbedProperties.gravity; data["ignoreForCollisions"] = grabbedProperties.ignoreForCollisions; data["collisionsWillMove"] = grabbedProperties.collisionsWillMove; - var whileHeldProperties = {gravity: {x:0, y:0, z:0}}; + var whileHeldProperties = { + gravity: { + x: 0, + y: 0, + z: 0 + } + }; if (invertSolidWhileHeld) { whileHeldProperties["ignoreForCollisions"] = !grabbedProperties.ignoreForCollisions; } @@ -996,4 +1021,4 @@ function cleanup() { } Script.scriptEnding.connect(cleanup); -Script.update.connect(update); +Script.update.connect(update); \ No newline at end of file diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 101271760a..c8455cb817 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -103,13 +103,8 @@ }, disableGrab: function() { - - var actionID = this.getActionID(); - - print('actionID' + actionID) - var success = Entities.deleteAction(this.entityID, actionID); - print('ACTION DELETE SUCCESS:::'+success) + var success = Entities.deleteAction(this.entityID, actionID); }, tellBowArrowIsNotched: function(bowID) { @@ -141,10 +136,10 @@ }); }, - update:function(){ + update: function() { - // var actions = Entities.getActionIDs(_this.entityID); - // print('ACTIONS ON ME:::'+actions) + // var actions = Entities.getActionIDs(_this.entityID); + // print('ACTIONS ON ME:::'+actions) }, checkIfBurning: function() { diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 31f385e095..9c4ca46c3b 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -416,35 +416,12 @@ return arrowPosition; }, - orientationOf: function(vector) { - var Y_AXIS = { - x: 0, - y: 1, - z: 0 - }; - var X_AXIS = { - x: 1, - y: 0, - z: 0 - }; - - var theta = 0.0; - - var RAD_TO_DEG = 180.0 / Math.PI; - var direction, yaw, pitch; - direction = Vec3.normalize(vector); - yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * RAD_TO_DEG, Y_AXIS); - pitch = Quat.angleAxis(Math.asin(-direction.y) * RAD_TO_DEG, X_AXIS); - return Quat.multiply(yaw, pitch); - }, - - updateArrowPositionInNotch: function() { //move it backwards - - var handToNotch = Vec3.subtract(this.notchDetectorPosition,this.stringData.handPosition); - print('h2n:::'+JSON.stringify(handToNotch)) + + var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); + print('h2n:::' + JSON.stringify(handToNotch)) var pullBackDistance = Vec3.length(handToNotch); if (pullBackDistance >= 0.6) { @@ -461,16 +438,16 @@ var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); - print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); + print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); Entities.editEntity(this.arrow, { position: finalArrowPosition, rotation: arrowRotation }) - var currentRotation = Entities.getEntityProperties(this.arrow, "rotation").rotation - // print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); + var currentRotation = Entities.getEntityProperties(this.arrow, "rotation").rotation + // print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); - // print('DIFFERENCE::: ' + (1 - Quat.dot(arrowRotation, currentRotation))); + // print('DIFFERENCE::: ' + (1 - Quat.dot(arrowRotation, currentRotation))); // if (this.arrowIsBurning === true) { // Entities.editEntity(this.fire, { // position: arrowTipPosition @@ -486,7 +463,7 @@ var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); var pullBackDistance = Vec3.length(handToNotch); - var arrowRotation = this.orientationOf(handToNotch); + var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); print('HAND DISTANCE:: ' + pullBackDistance); var arrowForce = this.scaleArrowShotStrength(pullBackDistance, 0, 2, 20, 50); @@ -538,7 +515,7 @@ var detectorProperties = { name: 'Hifi-NotchDetector', type: 'Box', - visible: true, + visible: false, collisionsWillMove: false, ignoreForCollisions: true, dimensions: NOTCH_DETECTOR_DIMENSIONS, @@ -572,7 +549,7 @@ Entities.editEntity(this.notchDetector, { position: this.notchDetectorPosition, - rotation:this.bowProperties.rotation + rotation: this.bowProperties.rotation }); }, diff --git a/examples/toybox/bow/createArrow.js b/examples/toybox/bow/createArrow.js index 0af73b2e0f..fd0bd7ae53 100644 --- a/examples/toybox/bow/createArrow.js +++ b/examples/toybox/bow/createArrow.js @@ -12,7 +12,7 @@ var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow.fbx"; var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow_collision_hull.obj"; -var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js?123'); +var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); var ARROW_DIMENSIONS = { x: 0.02, diff --git a/examples/toybox/bow/createBowAndArrow.js b/examples/toybox/bow/createBowAndArrow.js index b24f0e8d32..a731891e6c 100644 --- a/examples/toybox/bow/createBowAndArrow.js +++ b/examples/toybox/bow/createBowAndArrow.js @@ -1,2 +1,2 @@ - Script.include("createBow.js?123"); - Script.include("createArrow.js?123"); + Script.include("createBow.js"); + Script.include("createArrow.js"); From 361a89ad463c4295ce4cc03dddf9764ce6a99bf1 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 12 Nov 2015 11:32:25 -0800 Subject: [PATCH 34/79] redraw pre notch strings after firing --- examples/toybox/bow/bow.js | 73 +++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 9c4ca46c3b..889a9911c7 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -88,7 +88,6 @@ Bow.prototype = { isGrabbed: false, stringDrawn: false, - hasArrow: false, arrowTipPosition: null, preNotchString: null, hasArrowNotched: false, @@ -144,7 +143,7 @@ //check to see if an arrow has notched itself in our notch detector var userData = JSON.parse(this.bowProperties.userData); if (userData.hasOwnProperty('hifiBowKey')) { - if (this.hasArrowNotched === false) { + if (this.hasArrowNotched === false && userData.hifiBowKey.arrowID!==null) { //notch the arrow print('NOTCHING IT!') this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; @@ -159,32 +158,42 @@ } + //this.arrow + //this.hasArrowNotched + //create a string across the bow when we pick it up if (this.preNotchString === null) { + print('CREATE PRE NOTCH STRING') this.createPreNotchString(); } if (this.preNotchString !== null && this.hasArrowNotched === false) { + print('DRAW PRE NOTCH STRING') this.drawPreNotchStrings(); } // create the notch detector that arrows will look for if (this.notchDetector === null) { + print('CREATE NOTCH DETECTOR') this.createNotchDetector(); } //if we have an arrow notched, then draw some new strings if (this.hasArrowNotched === true) { if (this.preNotchString !== null) { - Entities.deleteEntity(this.preNotchString); + print('MAKE PRE NOTCH INVISIBLE') + Entities.editEntity(this.preNotchString, { + visible: false + }); } - - //only test for strings now that an arrow is notched + print('CHECK STRING HAND') + //only test for strings now that an arrow is notched this.checkStringHand(); } else { - //otherwise, don't do much of anything. + print('DONT DO ANYTHING') + //otherwise, don't do much of anything. } }, @@ -195,7 +204,7 @@ this.stringDrawn = false; this.deleteStrings(); this.hasArrow = false; - // Entities.deleteEntity(this.arrow); + Entities.deleteEntity(this.arrow); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, invertSolidWhileHeld: true @@ -376,10 +385,9 @@ // firing the arrow print('HIT RELEASE LOOP IN CHECK') - // this.stringDrawn = false; - // this.deleteStrings(); - // this.hasArrow = false; - // this.releaseArrow(); + + + this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { // print('HIT CONTINUE LOOP IN CHECK') @@ -421,7 +429,6 @@ var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); - print('h2n:::' + JSON.stringify(handToNotch)) var pullBackDistance = Vec3.length(handToNotch); if (pullBackDistance >= 0.6) { @@ -434,20 +441,13 @@ var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); - - var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); - print('ARROW ROTATION:: ' + JSON.stringify(arrowRotation)); Entities.editEntity(this.arrow, { position: finalArrowPosition, rotation: arrowRotation }) - var currentRotation = Entities.getEntityProperties(this.arrow, "rotation").rotation - // print('ACTUAL ARROW ROTATION::' +JSON.stringify(currentRotation)); - - // print('DIFFERENCE::: ' + (1 - Quat.dot(arrowRotation, currentRotation))); // if (this.arrowIsBurning === true) { // Entities.editEntity(this.fire, { // position: arrowTipPosition @@ -463,38 +463,45 @@ var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); var pullBackDistance = Vec3.length(handToNotch); - var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); + var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); print('HAND DISTANCE:: ' + pullBackDistance); var arrowForce = this.scaleArrowShotStrength(pullBackDistance, 0, 2, 20, 50); print('ARROW FORCE::' + arrowForce); var forwardVec = Vec3.multiply(handToNotch, arrowForce); - print('FWD VEC:::' + JSON.stringify(forwardVec)); var arrowProperties = { - // rotation:handToHand, - + rotation: arrowRotation, velocity: forwardVec }; - Entities.editEntity(this.arrow, arrowProperties); - Script.setTimeout(function() { + setEntityCustomData('hifiBowKey', this.entityID, { + hasArrowNotched: false, + arrowID: null + }); + Entities.editEntity(this.arrow, arrowProperties); + + this.arrow = null; + this.hasArrowNotched = false; + + Entities.editEntity(this.preNotchString, { + visible: true + }); + + this.stringDrawn = false; + this.deleteStrings(); + + Script.setTimeout(function() { + print('making arrow physical') Entities.editEntity(this.arrow, { ignoreForCollisions: false, collisionsWillMove: true, gravity: ARROW_GRAVITY }); + }, 200) - }, 100) - print('ARROW collisions??:::' + Entities.getEntityProperties(this.arrow, "collisionsWillMove").collisionsWillMove); - - print('ARROW velocity:::' + JSON.stringify(Entities.getEntityProperties(this.arrow, "velocity").velocity)) - this.arrow = null; - this.hasArrowNotched = false; - this.fire = null; - this.arrowIsBurnning = false; }, From 7774f9096b499fe3fd196a4e0ce3d777ad581166 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 12 Nov 2015 12:31:53 -0800 Subject: [PATCH 35/79] work --- examples/toybox/bow/bow.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 889a9911c7..3d855bf728 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -169,7 +169,7 @@ } if (this.preNotchString !== null && this.hasArrowNotched === false) { - print('DRAW PRE NOTCH STRING') + // print('DRAW PRE NOTCH STRING') this.drawPreNotchStrings(); } @@ -192,7 +192,7 @@ this.checkStringHand(); } else { - print('DONT DO ANYTHING') + // print('DONT DO ANYTHING') //otherwise, don't do much of anything. } @@ -466,7 +466,7 @@ var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); print('HAND DISTANCE:: ' + pullBackDistance); - var arrowForce = this.scaleArrowShotStrength(pullBackDistance, 0, 2, 20, 50); + var arrowForce = this.scaleArrowShotStrength(pullBackDistance, 0, 2, 10, 25); print('ARROW FORCE::' + arrowForce); var forwardVec = Vec3.multiply(handToNotch, arrowForce); @@ -481,7 +481,7 @@ }); Entities.editEntity(this.arrow, arrowProperties); - + var arrowStore = this.arrow; this.arrow = null; this.hasArrowNotched = false; @@ -494,12 +494,12 @@ Script.setTimeout(function() { print('making arrow physical') - Entities.editEntity(this.arrow, { + Entities.editEntity(arrowStore, { ignoreForCollisions: false, collisionsWillMove: true, gravity: ARROW_GRAVITY }); - }, 200) + }, 100) From ff2b5fc8da3e128761cf7eb99e09b3e93f179c2c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 12 Nov 2015 19:49:52 -0800 Subject: [PATCH 36/79] with child glow box --- examples/toybox/bow/arrow.js | 140 ++-------- examples/toybox/bow/bow.js | 249 +++++++++++++++--- examples/toybox/bow/createArrow.js | 22 +- .../toybox/ping_pong_gun/createTargets.js | 2 + 4 files changed, 256 insertions(+), 157 deletions(-) diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index c8455cb817..8aad6c4f32 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -25,41 +25,13 @@ Arrow.prototype = { stickOnCollision: false, - glow: false, - glowBox: null, - isBurning: false, notched: false, preload: function(entityID) { this.entityID = entityID; - Script.update.connect(this.update); - // this.isBurning = this.checkIfBurning(); - // if (this.isBurning === true || this.glow === true) { - // Script.update.connect(this.updateArrowProperties); - // } - // if (this.isBurning === true) { - // Script.update.connect(this.updateFirePosition); - - // } - // if (this.glow === true && this.glowBow === null) { - // this.createGlowBox(); - // Script.update.connect(this.updateGlowBoxPosition); - // } }, - unload: function() { - Script.update.disconnect(this.update); - // Script.update.disconnect(this.updateArrowProperties); - - // if (this.isBurning) { - // Script.update.disconnect(this.updateFirePosition); - - // } - // if (this.glowBox !== null) { - // Script.update.disconnect(this.updateGlowBoxPosition); - // } - - }, + unload: function() {}, continueNearGrab: function() { this.currentProperties = Entities.getEntityProperties(this.entityID, "position"); @@ -117,17 +89,8 @@ Entities.editEntity(this.entityID, { collisionsWillMove: false, - ignoreForCollisions: true, - velocity: { - x: 0, - y: 0, - z: 0 - }, - angularVelocity: { - x: 0, - y: 0, - z: 0 - } + ignoreForCollisions: true + }) setEntityCustomData('hifiBowKey', bowID, { @@ -136,84 +99,31 @@ }); }, - update: function() { - // var actions = Entities.getActionIDs(_this.entityID); - // print('ACTIONS ON ME:::'+actions) - }, + // collisionWithEntity: function(me, otherEntity, collision) { + // print('ARROW HAD COLLISION') + // // if (this.stickOnCollision === true) { + // // print('ARROW SHOULD STICK') + // // Vec3.print('penetration = ', collision.penetration); + // // Vec3.print('collision contact point = ', collision.contactPoint); + // // Entities.editEntity(this.entityID, { + // // velocity: { + // // x: 0, + // // y: 0, + // // z: 0 + // // }, + // // gravity: { + // // x: 0, + // // y: 0, + // // z: 0 + // // }, + // // collisionsWillMove: false - checkIfBurning: function() { - var properties = Entities.getEntityProperties(this.entityID, "userData"); - var userData = JSON.parse(properties.userData); - var fire = false; + // // }) + // // } - if (userData.hasOwnProperty('hifiFireArrowKey')) { - this.fire = userData.hifiFireArrowKey.fire; - return true - - } else { - return false; - } - }, - - createGlowBox: function() { - var glowBowProperties = { - name: 'Arrow Glow Box', - type: 'Box', - dimensions: { - x: 0.02, - y: 0.02, - z: 0.64 - }, - color: { - red: 255, - green: 0, - blue: 255 - }, - }; - - _this.glowBox = Entities.addEntity(glowBowProperties); - }, - - updateArrowProperties: function() { - _this.arrowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation"]); - }, - - updateGlowBoxPosition: function() { - //once parenting is available, just attach the glowbow to the arrow - Entities.editEntity(_this.entityID, { - position: _this.arrowProperties.position, - rotation: _this.arrowProperties.rotation - }) - }, - - updateFirePosition: function() { - //once parenting is available, just attach the glowbow to the arrow - Entities.editEntity(_this.entityID, { - position: _this.arrowProperties.position - }) - }, - - collisionWithEntity: function(me, otherEntity, collision) { - print('ARROW HAD COLLISION') - if (this.stickOnCollision === true) { - Vec3.print('penetration = ', collision.penetration); - Vec3.print('collision contact point = ', collision.contactPoint); - Entities.editEntity(this.entityID, { - velocity: { - x: 0, - y: 0, - z: 0 - }, - gravity: { - x: 0, - y: 0, - z: 0 - }, - collisionsWillMove: false - - }) - } + // }, + playCollisionSound: function() { } } diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 3d855bf728..77b802086c 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -25,6 +25,13 @@ Script.include("../../libraries/utils.js"); + + var ARROW_DIMENSIONS = { + x: 0.02, + y: 0.02, + z: 0.64 + }; + var ZERO_VEC = { x: 0, y: 0, @@ -72,6 +79,14 @@ var NOTCH_DETECTOR_DISTANCE = 0.1; + var SHOT_SCALE = { + min1: 0, + max1: 0.6, + min2: 5, + max2: 30 + } + + var _this; function Bow() { @@ -93,8 +108,6 @@ hasArrowNotched: false, notchDetector: null, arrow: null, - arrowIsBurning: false, - fire: null, stringData: { currentColor: { red: 255, @@ -104,8 +117,15 @@ }, preload: function(entityID) { + print('preload bow') this.entityID = entityID; this.bowID = entityID; + Script.update.connect(this.updateArrowTrackers); + + }, + + unload: function() { + Script.update.disconnect(this.updateArrowTrackers); }, setLeftHand: function() { @@ -137,13 +157,21 @@ }, continueNearGrab: function() { + + // print('collidable bow' + Entities.getEntityProperties(this.entityID, "collisionsWillMove").collisionsWillMove) + // print('collidable arrow' + Entities.getEntityProperties(this.arrow, "collisionsWillMove").collisionsWillMove) + // print('collidable notch' + Entities.getEntityProperties(this.notchDetector, "collisionsWillMove").collisionsWillMove) + // print('collidable topstring' + Entities.getEntityProperties(this.topString, "collisionsWillMove").collisionsWillMove) + // print('collidable bottomstring' + Entities.getEntityProperties(this.bottomString, "collisionsWillMove").collisionsWillMove) + // print('collidable prenotchstring' + Entities.getEntityProperties(this.preNotchString, "collisionsWillMove").collisionsWillMove) + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); this.updateNotchDetectorPosition(); //check to see if an arrow has notched itself in our notch detector var userData = JSON.parse(this.bowProperties.userData); if (userData.hasOwnProperty('hifiBowKey')) { - if (this.hasArrowNotched === false && userData.hifiBowKey.arrowID!==null) { + if (this.hasArrowNotched === false && userData.hifiBowKey.arrowID !== null) { //notch the arrow print('NOTCHING IT!') this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; @@ -169,7 +197,7 @@ } if (this.preNotchString !== null && this.hasArrowNotched === false) { - // print('DRAW PRE NOTCH STRING') + // print('DRAW PRE NOTCH STRING') this.drawPreNotchStrings(); } @@ -182,18 +210,18 @@ //if we have an arrow notched, then draw some new strings if (this.hasArrowNotched === true) { if (this.preNotchString !== null) { - print('MAKE PRE NOTCH INVISIBLE') + // print('MAKE PRE NOTCH INVISIBLE') Entities.editEntity(this.preNotchString, { visible: false }); } - print('CHECK STRING HAND') - //only test for strings now that an arrow is notched + // print('CHECK STRING HAND') + //only test for strings now that an arrow is notched this.checkStringHand(); } else { - // print('DONT DO ANYTHING') - //otherwise, don't do much of anything. + // print('DONT DO ANYTHING') + //otherwise, don't do much of anything. } }, @@ -203,15 +231,14 @@ this.isGrabbed = false; this.stringDrawn = false; this.deleteStrings(); - this.hasArrow = false; - Entities.deleteEntity(this.arrow); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, invertSolidWhileHeld: true }); Entities.deleteEntity(this.notchDetector); - this.notchDetector = null; Entities.deleteEntity(this.preNotchString); + + this.notchDetector = null; this.preNotchString = null; } @@ -404,6 +431,9 @@ //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); + var arrowTracker = this.createArrowTracker(this.arrow); + print('ARROW TRACKER IS:::' + JSON.stringify(arrowTracker)); + this.arrowTrackers.push(arrowTracker) this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.stringData.grabHandPosition = this.getGrabHandPosition(); @@ -448,12 +478,6 @@ rotation: arrowRotation }) - // if (this.arrowIsBurning === true) { - // Entities.editEntity(this.fire, { - // position: arrowTipPosition - // }); - // } - }, releaseArrow: function() { @@ -462,11 +486,15 @@ var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); var pullBackDistance = Vec3.length(handToNotch); + if (pullBackDistance >= 0.6) { + pullBackDistance = 0.6; + } + var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); print('HAND DISTANCE:: ' + pullBackDistance); - var arrowForce = this.scaleArrowShotStrength(pullBackDistance, 0, 2, 10, 25); + var arrowForce = this.scaleArrowShotStrength(pullBackDistance); print('ARROW FORCE::' + arrowForce); var forwardVec = Vec3.multiply(handToNotch, arrowForce); @@ -475,12 +503,14 @@ velocity: forwardVec }; + + Entities.editEntity(this.arrow, arrowProperties); + setEntityCustomData('hifiBowKey', this.entityID, { hasArrowNotched: false, arrowID: null }); - Entities.editEntity(this.arrow, arrowProperties); var arrowStore = this.arrow; this.arrow = null; this.hasArrowNotched = false; @@ -492,24 +522,52 @@ this.stringDrawn = false; this.deleteStrings(); - Script.setTimeout(function() { - print('making arrow physical') - Entities.editEntity(arrowStore, { - ignoreForCollisions: false, - collisionsWillMove: true, - gravity: ARROW_GRAVITY - }); - }, 100) + //set an itnerval to heck how far the arrow is from the bow before adding gravity, etc. if we add this too soon, the arrow collides with the bow. hence, this function + this.physicalArrowInterval = Script.setInterval(function() { + print('in physical interval') + var arrowProps = Entities.getEntityProperties(arrowStore, "position"); + var bowProps = Entities.getEntityProperties(_this.entityID, "position"); + var arrowPosition = arrowProps.position; + var bowPosition = bowProps.position; + var length = Vec3.distance(arrowPosition, bowPosition); + print('LENGTH:::' + length); + if (length > 2) { + print('make arrow physical' + arrowStore) + Entities.editEntity(arrowStore, { + ignoreForCollisions: false, + collisionsWillMove: true, + gravity: ARROW_GRAVITY + }); + print('after make physical' + arrowStore) + var arrowProps = Entities.getEntityProperties(arrowStore) + print('arrowprops-collisions::' + arrowProps.collisionsWillMove); + print('arrowprops-graviy' + JSON.stringify(arrowProps.gravity)); + Script.setTimeout(function() { + print('in timeout :: ' + arrowStore) + var arrowProps = Entities.getEntityProperties(arrowStore) + print('arrowprops-gravity2' + JSON.stringify(arrowProps.gravity)); + print('ARROW USER DATA::' + arrowProps.userData) + + }, 1000) + + Script.clearInterval(_this.physicalArrowInterval) + } + }, 10) }, - scaleArrowShotStrength: function(value, min1, max1, min2, max2) { + scaleArrowShotStrength: function(value) { + var min1 = SHOT_SCALE.min1; + var max1 = SHOT_SCALE.max1; + var min2 = SHOT_SCALE.min2; + var max2 = SHOT_SCALE.max2; return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); }, createNotchDetector: function() { + print('CREATE NOTCH DETECTOR') var detectorPosition; var frontVector = Quat.getFront(this.bowProperties.rotation); var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); @@ -560,8 +618,8 @@ }); }, - setArrowOnFire: function() { - + createFireParticleSystem: function() { + //will probably need to dynamically update the orientation to match the arrow??? var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); var animationSettings = JSON.stringify({ @@ -624,13 +682,134 @@ lifespan: 1 }); - setEntityCustomData('hifiFireArrowKey', this.arrow, { - fire: this.fire + return fire - }); + }, + createArrowTracker: function(arrow, isBurning) { + print('in create arrow tracker:::' + arrow) + var _t = this; + var isBurning = isBurning || false; + var arrowTracker = { + arrowID: arrow, + whizzingSound: _t.createWhizzingSound(), + fireSound: _t.createFireSound(), + glowBox: _t.createGlowBox(), + fireParticleSystem: isBurning === true ? _t.createFireParticleSystem() : null, + childEntities: [], + childSounds: [], + init: function() { + print('init arrow tracker') + this.setChildren(); + + }, + setCollisionCallback: function() { + print('set arrow tracker callback' + this.arrowID) + + Script.addEventHandler(this.arrowID, "collisionWithEntity", function(entityA, entityB, collision) { + //how to get context here...? maybe reverse lookup the tracker by entityA (arrow) id, to get the glowbox etc. + print('ARROW COLLIDED WITH SOMETHING!' + this.glowBox) + //this is global + print('THIS IN COLLISION !' + this) + + Entities.deleteEntity(this.glowBox); + //we don't want to update this arrow tracker anymore + var index = _t.arrowTrackers.indexOf(entityA); + if (index > -1) { + _t.arrowTrackers.splice(index, 1); + } + }); + }, + setChildren: function() { + print('set arrow tracker children') + // this.children.push(whizzingSound); + // this.children.push(fireSound); + this.childEntities.push(this.glowBox); + // this.children.push(fireParticleSystem); + }, + updateChildEntities: function(arrowID) { + // print('ARROWID??'+arrowID) + // print('UPDATING CHILDREN OF TRACKER:::' + this.childEntities.length+" ARROW::"+arrowID); + var arrowProperties = Entities.getEntityProperties(this.arrowID, ["position", "rotation"]) + this.childEntities.forEach(function(child) { + Entities.editEntity(child, { + position: arrowProperties.position, + rotation: arrowProperties.rotation + }) + }) + } + }; + arrowTracker.init(); + arrowTracker.setCollisionCallback(); + print('after create arrow tracker') + + return arrowTracker + }, + createWhizzingSound: function() { + var sound; + return sound + }, + createFireSound: function() { + var sound; + return sound + }, + createGlowBox: function() { + print('creating glow box') + var shaderUrl = Script.resolvePath('../../shaders/exampleV2.fs'); + var properties = { + name: 'Hifi-Arrow-Glow', + type: 'Box', + dimensions: ARROW_DIMENSIONS, + collisionsWillMove: false, + ignoreForCollisions: true, + color: { + red: 255, + green: 0, + blue: 255 + }, + //i want to use shader but for some reason its white... need to fix; - this.arrowIsBurning = true; + // userData: JSON.stringify({ + // "grabbableKey": { + // grabbable: false + // }, + // "ProceduralEntity": { + // "shaderUrl": shaderUrl, + // // V2 and onwards, shaders must include a version identifier, or they will default + // // to V1 behavior + // "version": 2, + // // Any values specified here will be passed on to uniforms with matching names in + // // the shader. Only numbers and arrays of length 1-4 of numbers are supported. + // // + // // The size of the data must match the size of the uniform: + // // a number or 1 value array = 'uniform float' + // // 2 value array = 'uniform vec2' + // // 3 value array = 'uniform vec3' + // // 4 value array = 'uniform vec4' + // // + // // Uniforms should always be declared in the shader with a default value + // // or failure to specify the value here will result in undefined behavior. + // "uniforms": { + // // uniform float iSpeed = 1.0; + // "iSpeed": 2.0, + // // uniform vec3 iSize = vec3(1.0); + // "iSize": [1.0, 2.0, 4.0] + // } + // } + // }) + } + + var glowBox = Entities.addEntity(properties); + return glowBox + }, + arrowTrackers: [], + updateArrowTrackers: function() { + // print('updating arrow trackers:::' + _this.arrowTrackers.length); + _this.arrowTrackers.forEach(function(tracker) { + var arrowID = tracker.arrowID; + // print('TRACKER ARROW ID'+tracker.arrowID) + tracker.updateChildEntities(arrowID); + }) } }; diff --git a/examples/toybox/bow/createArrow.js b/examples/toybox/bow/createArrow.js index fd0bd7ae53..2278a637a8 100644 --- a/examples/toybox/bow/createArrow.js +++ b/examples/toybox/bow/createArrow.js @@ -26,11 +26,6 @@ var ARROW_GRAVITY = { z: 0 }; -var center = Vec3.sum(Vec3.sum(MyAvatar.position, { - x: 0, - y: 1, - z: 0 -}), Vec3.multiply(1.25, Quat.getFront(Camera.getOrientation()))); function cleanup() { Entities.deleteEntity(arrow); @@ -38,7 +33,13 @@ function cleanup() { var arrow; -function createArrow() { +function createArrow(i) { + var center = Vec3.sum(Vec3.sum(MyAvatar.position, { + x: 0, + y: 1, + z: 0 +}), Vec3.multiply(1.25*i, Quat.getFront(Camera.getOrientation()))); + arrow = Entities.addEntity({ name: 'Hifi-Arrow', type: 'Model', @@ -51,6 +52,7 @@ function createArrow() { collisionsWillMove: true, ignoreForCollisions: false, gravity: ARROW_GRAVITY, + // linearDamping:0.1, userData: JSON.stringify({ grabbableKey: { invertSolidWhileHeld: true @@ -60,6 +62,12 @@ function createArrow() { } -createArrow(); +var i; +for(i=1;i<4;i++){ + createArrow(i); + +} + + Script.scriptEnding.connect(cleanup); \ No newline at end of file diff --git a/examples/toybox/ping_pong_gun/createTargets.js b/examples/toybox/ping_pong_gun/createTargets.js index fde0d6f54a..fd2a5d004e 100644 --- a/examples/toybox/ping_pong_gun/createTargets.js +++ b/examples/toybox/ping_pong_gun/createTargets.js @@ -39,6 +39,8 @@ var startPosition = { z: 509.74 }; +startPosition=MyAvatar.position; + var rotation = Quat.fromPitchYawRollDegrees(0, -55.25, 0); var targetIntervalClearer = Entities.addEntity({ From f3d602a6f3d3d0bc055e172bb4b41ce14e62106c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 13 Nov 2015 10:30:17 -0800 Subject: [PATCH 37/79] things --- examples/toybox/bow/bow.js | 46 +++++++++++++++++++++--------- examples/toybox/bow/createArrow.js | 2 +- examples/toybox/bow/createBow.js | 4 +-- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 77b802086c..fa94dd5332 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -619,7 +619,8 @@ }, createFireParticleSystem: function() { - //will probably need to dynamically update the orientation to match the arrow??? + print('MAKING A FIRE') + //will probably need to dynamically update the orientation to match the arrow??? var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); var animationSettings = JSON.stringify({ @@ -635,8 +636,8 @@ name: "Hifi-Arrow-Fire", animationSettings: animationSettings, textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", - position: this.arrowTipPosition, emitRate: 100, + position: MyAvatar.position, colorStart: { red: 70, green: 70, @@ -688,13 +689,13 @@ createArrowTracker: function(arrow, isBurning) { print('in create arrow tracker:::' + arrow) var _t = this; - var isBurning = isBurning || false; + var isBurning = isBurning || true; var arrowTracker = { arrowID: arrow, whizzingSound: _t.createWhizzingSound(), fireSound: _t.createFireSound(), glowBox: _t.createGlowBox(), - fireParticleSystem: isBurning === true ? _t.createFireParticleSystem() : null, + fireParticleSystem: _t.createFireParticleSystem(), childEntities: [], childSounds: [], init: function() { @@ -706,12 +707,12 @@ print('set arrow tracker callback' + this.arrowID) Script.addEventHandler(this.arrowID, "collisionWithEntity", function(entityA, entityB, collision) { - //how to get context here...? maybe reverse lookup the tracker by entityA (arrow) id, to get the glowbox etc. - print('ARROW COLLIDED WITH SOMETHING!' + this.glowBox) - //this is global - print('THIS IN COLLISION !' + this) + //have to reverse lookup the tracker by the arrow id to get access to the children + var tracker = getArrowTrackerByArrowID(entityA); + print('ARROW COLLIDED WITH SOMETHING!' + tracker.glowBox) + print('TRACKER IN COLLISION !' + tracker) - Entities.deleteEntity(this.glowBox); + Entities.deleteEntity(tracker.glowBox); //we don't want to update this arrow tracker anymore var index = _t.arrowTrackers.indexOf(entityA); if (index > -1) { @@ -721,15 +722,15 @@ }, setChildren: function() { print('set arrow tracker children') - // this.children.push(whizzingSound); - // this.children.push(fireSound); + // this.childSounds.push(this.whizzingSound); + // this.childSounds.push(this.fireSound); this.childEntities.push(this.glowBox); - // this.children.push(fireParticleSystem); + this.childEntities.push(this.fireParticleSystem); }, updateChildEntities: function(arrowID) { // print('ARROWID??'+arrowID) // print('UPDATING CHILDREN OF TRACKER:::' + this.childEntities.length+" ARROW::"+arrowID); - var arrowProperties = Entities.getEntityProperties(this.arrowID, ["position", "rotation"]) + var arrowProperties = Entities.getEntityProperties(this.arrowID, ["position", "rotation"]); this.childEntities.forEach(function(child) { Entities.editEntity(child, { position: arrowProperties.position, @@ -752,6 +753,17 @@ var sound; return sound }, + createGlowBoxAsModel: function() { + var modelURL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/glowBox.fbx'; + var properties = { + name: 'Hifi-Arrow-Glow', + type: 'Model', + modelURL: modelURL, + dimensions: ARROW_DIMENSIONS, + collisionsWillMove: false, + ignoreForCollisions: true, + } + }, createGlowBox: function() { print('creating glow box') var shaderUrl = Script.resolvePath('../../shaders/exampleV2.fs'); @@ -814,5 +826,13 @@ }; + getArrowTrackerByArrowID = function(arrowID) { + var result = _this.arrowTrackers.filter(function(tracker) { + return tracker.arrowID === arrowID; + }); + + var tracker = result[0] + return tracker + } return new Bow(); }); \ No newline at end of file diff --git a/examples/toybox/bow/createArrow.js b/examples/toybox/bow/createArrow.js index 2278a637a8..965205d9e8 100644 --- a/examples/toybox/bow/createArrow.js +++ b/examples/toybox/bow/createArrow.js @@ -36,7 +36,7 @@ var arrow; function createArrow(i) { var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, - y: 1, + y: 0, z: 0 }), Vec3.multiply(1.25*i, Quat.getFront(Camera.getOrientation()))); diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index f8f0064459..7e3643599c 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -29,9 +29,9 @@ var BOW_GRAVITY = { var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, - y: 1, + y: 0, z: 0 -}), Vec3.multiply(1.5, Quat.getFront(Camera.getOrientation()))); +}), Vec3.multiply(1, Quat.getFront(Camera.getOrientation()))); var bow = Entities.addEntity({ name: 'Hifi-Bow', From 6129d3757ec009c0ff1728cf78bd6fc55802a11c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 13 Nov 2015 11:28:09 -0800 Subject: [PATCH 38/79] fire code and sound code --- examples/toybox/bow/arrow.js | 107 ++++++++++++++++++++++++++--- examples/toybox/bow/bow.js | 71 ++++++++++++++----- examples/toybox/bow/createArrow.js | 4 +- examples/toybox/bow/createBow.js | 2 +- 4 files changed, 156 insertions(+), 28 deletions(-) diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 8aad6c4f32..027319dd6f 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -15,6 +15,7 @@ Script.include("../../libraries/utils.js"); var NOTCH_DETECTOR_SEARCH_RADIUS = 0.25; + var FIRE_DETECTOR_SEARCH_RADIUS = 0.25; var _this; @@ -26,18 +27,23 @@ Arrow.prototype = { stickOnCollision: false, notched: false, + burning: false, preload: function(entityID) { this.entityID = entityID; }, - unload: function() {}, + unload: function() { + + }, continueNearGrab: function() { this.currentProperties = Entities.getEntityProperties(this.entityID, "position"); + if (this.isBurning === true) { + updateFirePosition(); + } if (this.notched !== true) { this.searchForNotchDetectors(); - } }, @@ -59,6 +65,93 @@ } }, + searchForFires: function() { + if (this.notched === true) { + return + }; + + var ids = Entities.findEntities(this.currentProperties.position, FIRE_DETECTOR_SEARCH_RADIUS); + var i, properties; + for (i = 0; i < ids.length; i++) { + id = ids[i]; + properties = Entities.getEntityProperties(id, 'name'); + if (properties.name == "Hifi-Arrow-Fire-Source") { + print('NEAR A FIRE SOURCE!!!') + this.burning = true; + this.fire = this.createFireParticleSystem(); + } + } + + }, + updateFirePosition: function() { + Entities.editEntity(this.fire, { + position: this.currentProperties.position + }) + }, + createFireParticleSystem: function() { + var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); + + var animationSettings = JSON.stringify({ + fps: 30, + running: true, + loop: true, + firstFrame: 1, + lastFrame: 10000 + }); + + var fire = Entities.addEntity({ + type: "ParticleEffect", + name: "Hifi-Arrow-Fire-Source", + animationSettings: animationSettings, + textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", + emitRate: 100, + position: this.bowProperties.position, + colorStart: { + red: 70, + green: 70, + blue: 137 + }, + color: { + red: 200, + green: 99, + blue: 42 + }, + colorFinish: { + red: 255, + green: 99, + blue: 32 + }, + radiusSpread: 0.01, + radiusStart: 0.02, + radiusEnd: 0.001, + particleRadius: 0.5, + radiusFinish: 0.0, + emitOrientation: myOrientation, + emitSpeed: 0.3, + speedSpread: 0.1, + alphaStart: 0.05, + alpha: 0.1, + alphaFinish: 0.05, + emitDimensions: { + x: 1, + y: 1, + z: 0.1 + }, + polarFinish: 0.1, + emitAcceleration: { + x: 0.0, + y: 0.0, + z: 0.0 + }, + accelerationSpread: { + x: 0.1, + y: 0.01, + z: 0.1 + }, + lifespan: 1 + }); + return fire + }, getBowID: function(notchDetectorID) { var properties = Entities.getEntityProperties(notchDetectorID, "userData"); var userData = JSON.parse(properties.userData); @@ -95,6 +188,7 @@ setEntityCustomData('hifiBowKey', bowID, { hasArrowNotched: true, + arrowIsBurning: this.isBurning, arrowID: this.entityID }); @@ -122,16 +216,13 @@ // // }) // // } - // }, - playCollisionSound: function() { - - } + // } } function deleteEntity(entityID) { - if (entityID === this.entityID) { + if (entityID === _this.entityID) { if (_this.isBurning === true) { - _this.deleteEntity(_this.fire); + Entities.deleteEntity(_this.fire); } } } diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index fa94dd5332..7a75c51941 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -177,6 +177,7 @@ this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; this.arrow = userData.hifiBowKey.arrowID; + this.arrowIsBurning = userData.hifiBowKey.isBurning; setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: true, @@ -432,7 +433,6 @@ this.stringDrawn = true; this.createStrings(); var arrowTracker = this.createArrowTracker(this.arrow); - print('ARROW TRACKER IS:::' + JSON.stringify(arrowTracker)); this.arrowTrackers.push(arrowTracker) this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); @@ -456,8 +456,6 @@ updateArrowPositionInNotch: function() { //move it backwards - - var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); var pullBackDistance = Vec3.length(handToNotch); @@ -468,6 +466,7 @@ var pullBackOffset = Vec3.multiply(handToNotch, -pullBackDistance); var arrowPosition = Vec3.sum(this.notchDetectorPosition, pullBackOffset); + //move it forward a bit var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); @@ -481,7 +480,6 @@ }, releaseArrow: function() { - print('RELEASE ARROW!!!') var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); @@ -490,7 +488,6 @@ pullBackDistance = 0.6; } - var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); print('HAND DISTANCE:: ' + pullBackDistance); @@ -503,7 +500,6 @@ velocity: forwardVec }; - Entities.editEntity(this.arrow, arrowProperties); setEntityCustomData('hifiBowKey', this.entityID, { @@ -637,7 +633,7 @@ animationSettings: animationSettings, textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", emitRate: 100, - position: MyAvatar.position, + position: this.bowProperties.position, colorStart: { red: 70, green: 70, @@ -656,7 +652,7 @@ radiusSpread: 0.01, radiusStart: 0.02, radiusEnd: 0.001, - particleRadius: 0.05, + particleRadius: 0.5, radiusFinish: 0.0, emitOrientation: myOrientation, emitSpeed: 0.3, @@ -689,6 +685,9 @@ createArrowTracker: function(arrow, isBurning) { print('in create arrow tracker:::' + arrow) var _t = this; + + var isBurning = this.arrowIsBurning; + //delete this line below once debugging is done var isBurning = isBurning || true; var arrowTracker = { arrowID: arrow, @@ -728,15 +727,26 @@ this.childEntities.push(this.fireParticleSystem); }, updateChildEntities: function(arrowID) { - // print('ARROWID??'+arrowID) - // print('UPDATING CHILDREN OF TRACKER:::' + this.childEntities.length+" ARROW::"+arrowID); + + print('UPDATING CHILDREN OF TRACKER:::' + this.childEntities.length); var arrowProperties = Entities.getEntityProperties(this.arrowID, ["position", "rotation"]); + + //update the positions + this.soundEntities.forEach(function(injector) { + var audioProperties = { + volume: 0.25, + position: arrowProperties.position + }; + injector.options = audioProperties; + }) + this.childEntities.forEach(function(child) { Entities.editEntity(child, { position: arrowProperties.position, rotation: arrowProperties.rotation }) }) + } }; arrowTracker.init(); @@ -746,12 +756,25 @@ return arrowTracker }, createWhizzingSound: function() { - var sound; - return sound + var audioProperties = { + volume: 0.25, + position: this.bowProperties.position, + loop: true + }; + var injector = Audio.playSound(this.shootArrowSound, audioProperties); + + return injector }, - createFireSound: function() { - var sound; - return sound + createFireBurningSound: function() { + var audioProperties = { + volume: 0.25, + position: this.bowProperties.position, + loop: true + }; + + var injector = Audio.playSound(this.fireBurningSound, audioProperties); + + return injector }, createGlowBoxAsModel: function() { var modelURL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/glowBox.fbx'; @@ -810,7 +833,6 @@ // } // }) } - var glowBox = Entities.addEntity(properties); return glowBox }, @@ -822,7 +844,22 @@ // print('TRACKER ARROW ID'+tracker.arrowID) tracker.updateChildEntities(arrowID); }) - } + }, + playStringPullSound: function() { + var audioProperties = { + volume: 0.25, + position: this.bowProperties.position + }; + this.stringPullInjector = Audio.playSound(this.stringPullSound, audioProperties); + }, + playShootArrowSound: function(sound) { + var audioProperties = { + volume: 0.25, + position: this.bowProperties.position + }; + Audio.playSound(this.shootArrowSound, audioProperties); + }, + }; diff --git a/examples/toybox/bow/createArrow.js b/examples/toybox/bow/createArrow.js index 965205d9e8..803a1ee9e4 100644 --- a/examples/toybox/bow/createArrow.js +++ b/examples/toybox/bow/createArrow.js @@ -36,7 +36,7 @@ var arrow; function createArrow(i) { var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, - y: 0, + y: 0.5, z: 0 }), Vec3.multiply(1.25*i, Quat.getFront(Camera.getOrientation()))); @@ -63,7 +63,7 @@ function createArrow(i) { } var i; -for(i=1;i<4;i++){ +for(i=1;i<6;i++){ createArrow(i); } diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index 7e3643599c..8d84cd6d16 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -29,7 +29,7 @@ var BOW_GRAVITY = { var center = Vec3.sum(Vec3.sum(MyAvatar.position, { x: 0, - y: 0, + y: 0.5, z: 0 }), Vec3.multiply(1, Quat.getFront(Camera.getOrientation()))); From cdc6394a709622c56934ca0ff824025d0e6f994f Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 13 Nov 2015 18:22:36 -0800 Subject: [PATCH 39/79] moar bowwww --- examples/toybox/bow/arrow.js | 36 ++--- examples/toybox/bow/arrowFieldSpawner.js | 144 +++++++++++++++++ examples/toybox/bow/bow.js | 148 ++++++++++++------ examples/toybox/bow/createArrow.js | 3 +- examples/toybox/bow/createFireSource.js | 79 ++++++++++ .../toybox/ping_pong_gun/createTargets.js | 2 +- 6 files changed, 346 insertions(+), 66 deletions(-) create mode 100644 examples/toybox/bow/arrowFieldSpawner.js create mode 100644 examples/toybox/bow/createFireSource.js diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 027319dd6f..7bee4ee883 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -196,25 +196,25 @@ // collisionWithEntity: function(me, otherEntity, collision) { // print('ARROW HAD COLLISION') - // // if (this.stickOnCollision === true) { - // // print('ARROW SHOULD STICK') - // // Vec3.print('penetration = ', collision.penetration); - // // Vec3.print('collision contact point = ', collision.contactPoint); - // // Entities.editEntity(this.entityID, { - // // velocity: { - // // x: 0, - // // y: 0, - // // z: 0 - // // }, - // // gravity: { - // // x: 0, - // // y: 0, - // // z: 0 - // // }, - // // collisionsWillMove: false + // if (this.stickOnCollision === true) { + // print('ARROW SHOULD STICK') + // Vec3.print('penetration = ', collision.penetration); + // Vec3.print('collision contact point = ', collision.contactPoint); + // Entities.editEntity(this.entityID, { + // velocity: { + // x: 0, + // y: 0, + // z: 0 + // }, + // gravity: { + // x: 0, + // y: 0, + // z: 0 + // }, + // collisionsWillMove: false - // // }) - // // } + // }) + // } // } } diff --git a/examples/toybox/bow/arrowFieldSpawner.js b/examples/toybox/bow/arrowFieldSpawner.js new file mode 100644 index 0000000000..b08789cf2b --- /dev/null +++ b/examples/toybox/bow/arrowFieldSpawner.js @@ -0,0 +1,144 @@ +// dustSetSpawner.js +// examples +// +// Created by Eric Levin on 9/2/15 +// Copyright 2015 High Fidelity, Inc. +// +// Spawns a set with blocks and a desert-y ground. When blocks (or anything else is thrown), dust particles will kick up at the point the object hits the ground +// +// Distributed under the Apache License, Version 2.0. +// 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 */ + + +map = function(value, min1, max1, min2, max2) { + return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); +} + +orientationOf = function(vector) { + var Y_AXIS = { + x: 0, + y: 1, + z: 0 + }; + var X_AXIS = { + x: 1, + y: 0, + z: 0 + }; + + var theta = 0.0; + + var RAD_TO_DEG = 180.0 / Math.PI; + var direction, yaw, pitch; + direction = Vec3.normalize(vector); + yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * RAD_TO_DEG, Y_AXIS); + pitch = Quat.angleAxis(Math.asin(-direction.y) * RAD_TO_DEG, X_AXIS); + return Quat.multiply(yaw, pitch); +} + + +var ground, wall; +var boxes = []; +var dustSystems = []; +var ZERO_VEC = {x: 0, y: 0, z: 0}; + +Script.include("../libraries/utils.js"); + +function spawnGround() { + var groundModelURL = "https://hifi-public.s3.amazonaws.com/alan/Playa/Ground.fbx"; + var groundPosition = Vec3.sum(MyAvatar.position, {x: 0, y: -2, z: 0}); + ground = Entities.addEntity({ + type: "Model", + modelURL: groundModelURL, + shapeType: "box", + position: groundPosition, + dimensions: {x: 900, y: 0.82, z: 900}, + }); + // Script.addEventHandler(ground, "collisionWithEntity", entityCollisionWithGround); + +} + +/*function entityCollisionWithGround(ground, entity, collision) { + var dVelocityMagnitude = Vec3.length(collision.velocityChange); + var position = Entities.getEntityProperties(entity, "position").position; + var particleRadius = map(dVelocityMagnitude, 0.05, 3, 0.5, 2); + var speed = map(dVelocityMagnitude, 0.05, 3, 0.02, 0.09); + var displayTime = 400; + var orientationChange = orientationOf(collision.velocityChange); + var dustEffect = Entities.addEntity({ + type: "ParticleEffect", + name: "Dust-Puff", + position: position, + color: {red: 195, green: 170, blue: 185}, + lifespan: 3, + lifetime: 7,//displayTime/1000 * 2, //So we can fade particle system out gracefully + emitRate: 5, + emitSpeed: speed, + emitAcceleration: ZERO_VEC, + accelerationSpread: ZERO_VEC, + isEmitting: true, + polarStart: Math.PI/2, + polarFinish: Math.PI/2, + emitOrientation: orientationChange, + radiusSpread: 0.1, + radiusStart: particleRadius, + radiusFinish: particleRadius + particleRadius/2, + particleRadius: particleRadius, + alpha: 0.45, + alphaFinish: 0.001, + textures: "https://hifi-public.s3.amazonaws.com/alan/Playa/Particles/Particle-Sprite-Gen.png" + }); + + dustSystems.push(dustEffect); + + Script.setTimeout(function() { + var newRadius = 0.05; + Entities.editEntity(dustEffect, { + alpha: 0.0 + }); + }, displayTime); +}*/ + +function spawnBoxes() { + //why does this crash interface + var boxModelURL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/hay_bale_rectangle.fbx"; + + var collisionSoundURL = "https://hifi-public.s3.amazonaws.com/sounds/Collisions-otherorganic/ToyWoodBlock.L.wav"; + var numBoxes = 200; + var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(Camera.getOrientation()))); + for (var i = 0; i < numBoxes; i++) { + var position = Vec3.sum(center, {x: Math.random() * numBoxes, y: Math.random() * 3, z: Math.random() * numBoxes }) + var box = Entities.addEntity({ + type: "Model", + modelURL: boxModelURL, + collisionSoundURL: collisionSoundURL, + shapeType: "box", + position: position, + collisionsWillMove: true, + dimensions: {x: 1, y: 2, z: 3}, + velocity: {x: 0, y: -.01, z: 0}, + gravity: {x: 0, y: -2.5 - Math.random() * 6, z: 0} + }); + + boxes.push(box); + } +} + +spawnGround(); +spawnBoxes(); + + +function cleanup() { + Entities.deleteEntity(ground); + boxes.forEach(function(box){ + Entities.deleteEntity(box); + }); + dustSystems.forEach(function(dustEffect) { + Entities.deleteEntity(dustEffect); + }) +} + +Script.scriptEnding.connect(cleanup); + + diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 7a75c51941..676b6cdca7 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -10,22 +10,24 @@ // // to do: -// make arrows more visible +// create field with haybales and targets +// create torches to start arrow on fire +// play start fire sound +// parent the arrow fire to the arrow +// delete the arrow fire when the arrow is notched (the bow will now create and track fire) // make arrow rotate toward ground as it flies -// add noise when you release arrow -> add the sound to the arrow and keep it with position so you hear it whizz by -// add noise when you draw string -// re-enable arrows sticking when they hit -// prepare for haptics +// working shader or transparent model for glow box -// from chat w/ ryan -// 5 arrows on table -// pick up arrow entity -// notch it (function() { Script.include("../../libraries/utils.js"); - + var NOTCH_ARROW_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/notch.wav?123'; + var SHOOT_ARROW_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/String_release2.L.wav'; + var STRING_PULL_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Bow_draw.1.L.wav'; + var ARROW_WHIZZ_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/whizz.wav'; + //todo : multiple impact sounds + var ARROW_HIT_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/bow_and_arrow/sounds/Arrow_impact1.L.wav' var ARROW_DIMENSIONS = { x: 0.02, y: 0.02, @@ -45,10 +47,10 @@ }; var ARROW_OFFSET = -0.36; - + var ARROW_TIP_OFFSET = 0.32; var ARROW_GRAVITY = { x: 0, - y: -9.8, + y: -5.8, z: 0 }; @@ -83,7 +85,7 @@ min1: 0, max1: 0.6, min2: 5, - max2: 30 + max2: 20 } @@ -120,6 +122,11 @@ print('preload bow') this.entityID = entityID; this.bowID = entityID; + this.stringPullSound = SoundCache.getSound(STRING_PULL_SOUND_URL); + this.shootArrowSound = SoundCache.getSound(SHOOT_ARROW_SOUND_URL); + this.arrowHitSound = SoundCache.getSound(ARROW_HIT_SOUND_URL); + this.arrowNotchSound = SoundCache.getSound(NOTCH_ARROW_SOUND_URL); + this.arrowWhizzSound = SoundCache.getSound(ARROW_WHIZZ_SOUND_URL); Script.update.connect(this.updateArrowTrackers); }, @@ -166,6 +173,7 @@ // print('collidable prenotchstring' + Entities.getEntityProperties(this.preNotchString, "collisionsWillMove").collisionsWillMove) this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); + this.updateNotchDetectorPosition(); //check to see if an arrow has notched itself in our notch detector @@ -174,6 +182,8 @@ if (this.hasArrowNotched === false && userData.hifiBowKey.arrowID !== null) { //notch the arrow print('NOTCHING IT!') + this.playArrowNotchSound(); + this.playStringPullSound(); this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; this.arrow = userData.hifiBowKey.arrowID; @@ -218,6 +228,8 @@ } // print('CHECK STRING HAND') //only test for strings now that an arrow is notched + + this.checkStringHand(); } else { @@ -446,6 +458,14 @@ } }, + setArrowTipPosition: function(arrowPosition, arrowRotation) { + var frontVector = Quat.getFront(arrowRotation); + var frontOffset = Vec3.multiply(frontVector, ARROW_TIP_OFFSET); + var arrowTipPosition = Vec3.sum(arrowPosition, frontOffset); + this.arrowTipPosition = arrowTipPosition; + return arrowTipPosition; + + }, getArrowPosition: function() { var arrowVector = Vec3.subtract(this.stringData.handPosition, this.stringData.grabHandPosition); arrowVector = Vec3.normalize(arrowVector); @@ -465,13 +485,13 @@ var pullBackOffset = Vec3.multiply(handToNotch, -pullBackDistance); var arrowPosition = Vec3.sum(this.notchDetectorPosition, pullBackOffset); - + this.changeStringPullSoundVolume(pullBackDistance); //move it forward a bit var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); - + // this.setArrowTipPosition(finalArrowPosition, arrowRotation); Entities.editEntity(this.arrow, { position: finalArrowPosition, rotation: arrowRotation @@ -500,6 +520,7 @@ velocity: forwardVec }; + this.playShootArrowSound(); Entities.editEntity(this.arrow, arrowProperties); setEntityCustomData('hifiBowKey', this.entityID, { @@ -633,7 +654,7 @@ animationSettings: animationSettings, textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", emitRate: 100, - position: this.bowProperties.position, + position: MyAvatar.position, colorStart: { red: 70, green: 70, @@ -652,7 +673,7 @@ radiusSpread: 0.01, radiusStart: 0.02, radiusEnd: 0.001, - particleRadius: 0.5, + particleRadius: 0.15, radiusFinish: 0.0, emitOrientation: myOrientation, emitSpeed: 0.3, @@ -661,8 +682,8 @@ alpha: 0.1, alphaFinish: 0.05, emitDimensions: { - x: 1, - y: 1, + x: 0.5, + y: 0.5, z: 0.1 }, polarFinish: 0.1, @@ -676,27 +697,29 @@ y: 0.01, z: 0.1 }, - lifespan: 1 + lifespan: 0.5 }); - return fire + return fire; }, createArrowTracker: function(arrow, isBurning) { print('in create arrow tracker:::' + arrow) var _t = this; - var isBurning = this.arrowIsBurning; + // var isBurning = this.arrowIsBurning; //delete this line below once debugging is done var isBurning = isBurning || true; var arrowTracker = { arrowID: arrow, - whizzingSound: _t.createWhizzingSound(), - fireSound: _t.createFireSound(), + // whizzingSound: _t.playWhizzSound(), + //fireSound: _t.createFireSound(), + hasPlayedCollisionSound: false, glowBox: _t.createGlowBox(), fireParticleSystem: _t.createFireParticleSystem(), childEntities: [], childSounds: [], + childParticleSystems: [], init: function() { print('init arrow tracker') this.setChildren(); @@ -708,14 +731,18 @@ Script.addEventHandler(this.arrowID, "collisionWithEntity", function(entityA, entityB, collision) { //have to reverse lookup the tracker by the arrow id to get access to the children var tracker = getArrowTrackerByArrowID(entityA); + print('ARROW COLLIDED WITH SOMETHING!' + tracker.glowBox) print('TRACKER IN COLLISION !' + tracker) - + // _t.playArrowHitSound(collision.contactPoint); + //Vec3.print('penetration = ', collision.penetration); + //Vec3.print('collision contact point = ', collision.contactPoint); + // var orientationChange = orientationOf(collision.velocityChange); Entities.deleteEntity(tracker.glowBox); //we don't want to update this arrow tracker anymore var index = _t.arrowTrackers.indexOf(entityA); if (index > -1) { - _t.arrowTrackers.splice(index, 1); + // _t.arrowTrackers.splice(index, 1); } }); }, @@ -724,21 +751,21 @@ // this.childSounds.push(this.whizzingSound); // this.childSounds.push(this.fireSound); this.childEntities.push(this.glowBox); - this.childEntities.push(this.fireParticleSystem); + this.childParticleSystems.push(this.fireParticleSystem); }, updateChildEntities: function(arrowID) { - print('UPDATING CHILDREN OF TRACKER:::' + this.childEntities.length); + // print('UPDATING CHILDREN OF TRACKER:::' + this.childEntities.length); var arrowProperties = Entities.getEntityProperties(this.arrowID, ["position", "rotation"]); //update the positions - this.soundEntities.forEach(function(injector) { - var audioProperties = { - volume: 0.25, - position: arrowProperties.position - }; - injector.options = audioProperties; - }) + // this.soundEntities.forEach(function(injector) { + // var audioProperties = { + // volume: 0.25, + // position: arrowProperties.position + // }; + // injector.options = audioProperties; + // }) this.childEntities.forEach(function(child) { Entities.editEntity(child, { @@ -746,7 +773,13 @@ rotation: arrowProperties.rotation }) }) - + + this.childParticleSystems.forEach(function(child) { + Entities.editEntity(child, { + position: arrowProperties.position + }) + }) + } }; arrowTracker.init(); @@ -755,16 +788,6 @@ return arrowTracker }, - createWhizzingSound: function() { - var audioProperties = { - volume: 0.25, - position: this.bowProperties.position, - loop: true - }; - var injector = Audio.playSound(this.shootArrowSound, audioProperties); - - return injector - }, createFireBurningSound: function() { var audioProperties = { volume: 0.25, @@ -859,6 +882,39 @@ }; Audio.playSound(this.shootArrowSound, audioProperties); }, + playArrowHitSound: function(position) { + var audioProperties = { + volume: 0.25, + position: position + }; + Audio.playSound(this.arrowHitSound, audioProperties); + }, + playArrowNotchSound: function() { + print('play arrow notch sound') + var audioProperties = { + volume: 0.25, + position: this.bowProperties.position + }; + Audio.playSound(this.arrowNotchSound, audioProperties); + }, + playArrowWhizzSound: function() { + + var audioProperties = { + volume: 0.0, + position: this.bowProperties.position + }; + var injector = Audio.playSound(this.arrowWhizzSound, audioProperties); + + return injector + }, + changeStringPullSoundVolume: function(pullBackDistance) { + var audioProperties = { + volume: 0.25, + position: this.bowProperties.position + } + + this.stringPullInjector.options = audioProperties + } }; diff --git a/examples/toybox/bow/createArrow.js b/examples/toybox/bow/createArrow.js index 803a1ee9e4..e4db24cfd1 100644 --- a/examples/toybox/bow/createArrow.js +++ b/examples/toybox/bow/createArrow.js @@ -51,8 +51,9 @@ function createArrow(i) { script: ARROW_SCRIPT_URL, collisionsWillMove: true, ignoreForCollisions: false, + collisionSoundURL:'http://hifi-content.s3.amazonaws.com/bow_and_arrow/sounds/Arrow_impact1.L.wav', gravity: ARROW_GRAVITY, - // linearDamping:0.1, + linearDamping:0.01, userData: JSON.stringify({ grabbableKey: { invertSolidWhileHeld: true diff --git a/examples/toybox/bow/createFireSource.js b/examples/toybox/bow/createFireSource.js new file mode 100644 index 0000000000..9d974cd04e --- /dev/null +++ b/examples/toybox/bow/createFireSource.js @@ -0,0 +1,79 @@ +// +// createFireSource.js +// +// Created byJames Pollack @imgntn on 10/19/2015 +// Copyright 2015 High Fidelity, Inc. +// +// This script creates a fire that you can use to light arrows on fire. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +function createFireSource() { + + var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); + + var animationSettings = JSON.stringify({ + fps: 30, + running: true, + loop: true, + firstFrame: 1, + lastFrame: 10000 + }); + + var fire = Entities.addEntity({ + type: "ParticleEffect", + name: "Hifi-Arrow-Fire-Source", + animationSettings: animationSettings, + textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", + emitRate: 100, + position: this.bowProperties.position, + colorStart: { + red: 70, + green: 70, + blue: 137 + }, + color: { + red: 200, + green: 99, + blue: 42 + }, + colorFinish: { + red: 255, + green: 99, + blue: 32 + }, + radiusSpread: 0.01, + radiusStart: 0.02, + radiusEnd: 0.001, + particleRadius: 0.5, + radiusFinish: 0.0, + emitOrientation: myOrientation, + emitSpeed: 0.3, + speedSpread: 0.1, + alphaStart: 0.05, + alpha: 0.1, + alphaFinish: 0.05, + emitDimensions: { + x: 1, + y: 1, + z: 0.1 + }, + polarFinish: 0.1, + emitAcceleration: { + x: 0.0, + y: 0.0, + z: 0.0 + }, + accelerationSpread: { + x: 0.1, + y: 0.01, + z: 0.1 + }, + lifespan: 1 + }); + +} + +createFireSource(); diff --git a/examples/toybox/ping_pong_gun/createTargets.js b/examples/toybox/ping_pong_gun/createTargets.js index fd2a5d004e..c421352a9d 100644 --- a/examples/toybox/ping_pong_gun/createTargets.js +++ b/examples/toybox/ping_pong_gun/createTargets.js @@ -39,7 +39,7 @@ var startPosition = { z: 509.74 }; -startPosition=MyAvatar.position; +startPosition = MyAvatar.position; var rotation = Quat.fromPitchYawRollDegrees(0, -55.25, 0); From 2d7dd2fcf25e31afc6c7183e705d6e0059a10893 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 16 Nov 2015 11:35:31 -0800 Subject: [PATCH 40/79] fires --- examples/playa/playaSpawner.js | 52 +++++-- examples/toybox/bow/arrow.js | 76 ++++----- examples/toybox/bow/arrowFieldSpawner.js | 146 +++++++----------- examples/toybox/bow/bow.js | 7 +- ...eateFireSource.js => createFireSources.js} | 67 +++++++- 5 files changed, 199 insertions(+), 149 deletions(-) rename examples/toybox/bow/{createFireSource.js => createFireSources.js} (55%) diff --git a/examples/playa/playaSpawner.js b/examples/playa/playaSpawner.js index 61101a0e62..4982d70f2b 100644 --- a/examples/playa/playaSpawner.js +++ b/examples/playa/playaSpawner.js @@ -41,21 +41,33 @@ orientationOf = function(vector) { var ground, wall; var boxes = []; var dustSystems = []; -var ZERO_VEC = {x: 0, y: 0, z: 0}; +var ZERO_VEC = { + x: 0, + y: 0, + z: 0 +}; Script.include("../libraries/utils.js"); function spawnGround() { var groundModelURL = "https://hifi-public.s3.amazonaws.com/alan/Playa/Ground.fbx"; - var groundPosition = Vec3.sum(MyAvatar.position, {x: 0, y: -2, z: 0}); + var groundPosition = Vec3.sum(MyAvatar.position, { + x: 0, + y: -2, + z: 0 + }); ground = Entities.addEntity({ type: "Model", modelURL: groundModelURL, shapeType: "box", position: groundPosition, - dimensions: {x: 900, y: 0.82, z: 900}, + dimensions: { + x: 900, + y: 0.82, + z: 900 + }, }); - // Script.addEventHandler(ground, "collisionWithEntity", entityCollisionWithGround); + // Script.addEventHandler(ground, "collisionWithEntity", entityCollisionWithGround); } @@ -80,7 +92,7 @@ function spawnGround() { isEmitting: true, polarStart: Math.PI/2, polarFinish: Math.PI/2, - emitOrientation: orientationChange, + emitOrientation: orientationChange, radiusSpread: 0.1, radiusStart: particleRadius, radiusFinish: particleRadius + particleRadius/2, @@ -106,7 +118,11 @@ function spawnBoxes() { var numBoxes = 200; var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(Camera.getOrientation()))); for (var i = 0; i < numBoxes; i++) { - var position = Vec3.sum(center, {x: Math.random() * numBoxes, y: Math.random() * 3, z: Math.random() * numBoxes }) + var position = Vec3.sum(center, { + x: Math.random() * numBoxes, + y: Math.random() * 3, + z: Math.random() * numBoxes + }) var box = Entities.addEntity({ type: "Model", modelURL: boxModelURL, @@ -114,9 +130,21 @@ function spawnBoxes() { shapeType: "box", position: position, collisionsWillMove: true, - dimensions: {x: 1, y: 2, z: 3}, - velocity: {x: 0, y: -.01, z: 0}, - gravity: {x: 0, y: -2.5 - Math.random() * 6, z: 0} + dimensions: { + x: 1, + y: 2, + z: 3 + }, + velocity: { + x: 0, + y: -.01, + z: 0 + }, + gravity: { + x: 0, + y: -2.5 - Math.random() * 6, + z: 0 + } }); boxes.push(box); @@ -129,7 +157,7 @@ spawnBoxes(); function cleanup() { Entities.deleteEntity(ground); - boxes.forEach(function(box){ + boxes.forEach(function(box) { Entities.deleteEntity(box); }); dustSystems.forEach(function(dustEffect) { @@ -137,6 +165,4 @@ function cleanup() { }) } -Script.scriptEnding.connect(cleanup); - - +Script.scriptEnding.connect(cleanup); \ No newline at end of file diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 7bee4ee883..2994ce15c0 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -27,21 +27,29 @@ Arrow.prototype = { stickOnCollision: false, notched: false, - burning: false, + isBurning: false, + fire: null, preload: function(entityID) { this.entityID = entityID; - }, - unload: function() { - + releaseGrab: function() { + if (this.fire !== null) { + this.fire = null; + this.isBurning = false; + Entities.deleteEntity(this.fire); + } }, continueNearGrab: function() { this.currentProperties = Entities.getEntityProperties(this.entityID, "position"); - if (this.isBurning === true) { - updateFirePosition(); + + if (this.isBurning !== true) { + this.searchForFires(); + } else { + this.updateFirePosition(); } + if (this.notched !== true) { this.searchForNotchDetectors(); } @@ -57,7 +65,7 @@ for (i = 0; i < ids.length; i++) { id = ids[i]; properties = Entities.getEntityProperties(id, 'name'); - if (properties.name == "Hifi-NotchDetector") { + if (properties.name === "Hifi-NotchDetector") { print('NEAR THE NOTCH!!!') this.notched = true; this.tellBowArrowIsNotched(this.getBowID(id)); @@ -65,30 +73,40 @@ } }, + searchForFires: function() { + print('SEARCHING FOR FIRES!') if (this.notched === true) { return }; + if (this.isBurning === true) { + return + }; var ids = Entities.findEntities(this.currentProperties.position, FIRE_DETECTOR_SEARCH_RADIUS); var i, properties; for (i = 0; i < ids.length; i++) { id = ids[i]; properties = Entities.getEntityProperties(id, 'name'); - if (properties.name == "Hifi-Arrow-Fire-Source") { - print('NEAR A FIRE SOURCE!!!') - this.burning = true; - this.fire = this.createFireParticleSystem(); + if (properties.name === "Hifi-Arrow-Fire-Source") { + print('NEAR A FIRE SOURCE!!!'); + this.isBurning = true; + this.createFireParticleSystem(); + + } } }, updateFirePosition: function() { + print('updating fire position' + this.fire) Entities.editEntity(this.fire, { position: this.currentProperties.position }) }, createFireParticleSystem: function() { + print('CREATING FIRE PARTICLE SYSTEM') + var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); var animationSettings = JSON.stringify({ @@ -99,13 +117,13 @@ lastFrame: 10000 }); - var fire = Entities.addEntity({ + this.fire = Entities.addEntity({ type: "ParticleEffect", name: "Hifi-Arrow-Fire-Source", animationSettings: animationSettings, textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", emitRate: 100, - position: this.bowProperties.position, + position: this.currentProperties.position, colorStart: { red: 70, green: 70, @@ -124,7 +142,7 @@ radiusSpread: 0.01, radiusStart: 0.02, radiusEnd: 0.001, - particleRadius: 0.5, + particleRadius: 0.15, radiusFinish: 0.0, emitOrientation: myOrientation, emitSpeed: 0.3, @@ -150,7 +168,6 @@ }, lifespan: 1 }); - return fire }, getBowID: function(notchDetectorID) { var properties = Entities.getEntityProperties(notchDetectorID, "userData"); @@ -192,31 +209,14 @@ arrowID: this.entityID }); + if (this.isBurning === true) { + this.isBurning = false; + this.fire = null; + Entities.deleteEntity(this.fire); + } + }, - // collisionWithEntity: function(me, otherEntity, collision) { - // print('ARROW HAD COLLISION') - // if (this.stickOnCollision === true) { - // print('ARROW SHOULD STICK') - // Vec3.print('penetration = ', collision.penetration); - // Vec3.print('collision contact point = ', collision.contactPoint); - // Entities.editEntity(this.entityID, { - // velocity: { - // x: 0, - // y: 0, - // z: 0 - // }, - // gravity: { - // x: 0, - // y: 0, - // z: 0 - // }, - // collisionsWillMove: false - - // }) - // } - - // } } function deleteEntity(entityID) { diff --git a/examples/toybox/bow/arrowFieldSpawner.js b/examples/toybox/bow/arrowFieldSpawner.js index b08789cf2b..08bb3be283 100644 --- a/examples/toybox/bow/arrowFieldSpawner.js +++ b/examples/toybox/bow/arrowFieldSpawner.js @@ -1,144 +1,106 @@ -// dustSetSpawner.js +// arrowFieldSpawner.js // examples // -// Created by Eric Levin on 9/2/15 +// Created by James B. Pollack @imgntn on 11/16/2015 // Copyright 2015 High Fidelity, Inc. // -// Spawns a set with blocks and a desert-y ground. When blocks (or anything else is thrown), dust particles will kick up at the point the object hits the ground +// Spawns ground, targets, and fire sources. // // Distributed under the Apache License, Version 2.0. // 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 */ - - -map = function(value, min1, max1, min2, max2) { - return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); -} - -orientationOf = function(vector) { - var Y_AXIS = { - x: 0, - y: 1, - z: 0 - }; - var X_AXIS = { - x: 1, - y: 0, - z: 0 - }; - - var theta = 0.0; - - var RAD_TO_DEG = 180.0 / Math.PI; - var direction, yaw, pitch; - direction = Vec3.normalize(vector); - yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * RAD_TO_DEG, Y_AXIS); - pitch = Quat.angleAxis(Math.asin(-direction.y) * RAD_TO_DEG, X_AXIS); - return Quat.multiply(yaw, pitch); -} - var ground, wall; var boxes = []; var dustSystems = []; -var ZERO_VEC = {x: 0, y: 0, z: 0}; +var ZERO_VEC = { + x: 0, + y: 0, + z: 0 +}; Script.include("../libraries/utils.js"); +var startPosition = { + x: 0, + y: 0, + z: 0 +} + function spawnGround() { var groundModelURL = "https://hifi-public.s3.amazonaws.com/alan/Playa/Ground.fbx"; - var groundPosition = Vec3.sum(MyAvatar.position, {x: 0, y: -2, z: 0}); + var groundPosition = Vec3.sum(startPosition, { + x: 0, + y: -2, + z: 0 + }); ground = Entities.addEntity({ type: "Model", modelURL: groundModelURL, shapeType: "box", position: groundPosition, - dimensions: {x: 900, y: 0.82, z: 900}, + dimensions: { + x: 900, + y: 0.82, + z: 900 + }, }); - // Script.addEventHandler(ground, "collisionWithEntity", entityCollisionWithGround); } -/*function entityCollisionWithGround(ground, entity, collision) { - var dVelocityMagnitude = Vec3.length(collision.velocityChange); - var position = Entities.getEntityProperties(entity, "position").position; - var particleRadius = map(dVelocityMagnitude, 0.05, 3, 0.5, 2); - var speed = map(dVelocityMagnitude, 0.05, 3, 0.02, 0.09); - var displayTime = 400; - var orientationChange = orientationOf(collision.velocityChange); - var dustEffect = Entities.addEntity({ - type: "ParticleEffect", - name: "Dust-Puff", - position: position, - color: {red: 195, green: 170, blue: 185}, - lifespan: 3, - lifetime: 7,//displayTime/1000 * 2, //So we can fade particle system out gracefully - emitRate: 5, - emitSpeed: speed, - emitAcceleration: ZERO_VEC, - accelerationSpread: ZERO_VEC, - isEmitting: true, - polarStart: Math.PI/2, - polarFinish: Math.PI/2, - emitOrientation: orientationChange, - radiusSpread: 0.1, - radiusStart: particleRadius, - radiusFinish: particleRadius + particleRadius/2, - particleRadius: particleRadius, - alpha: 0.45, - alphaFinish: 0.001, - textures: "https://hifi-public.s3.amazonaws.com/alan/Playa/Particles/Particle-Sprite-Gen.png" - }); - - dustSystems.push(dustEffect); - - Script.setTimeout(function() { - var newRadius = 0.05; - Entities.editEntity(dustEffect, { - alpha: 0.0 - }); - }, displayTime); -}*/ - function spawnBoxes() { - //why does this crash interface - var boxModelURL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/hay_bale_rectangle.fbx"; + var boxModelURL = "http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/target.fbx"; + var COLLISION_HULL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/target_collision_hull.obj'; + var TARGET_DIMENSIONS = { + x: 0.12, + y: 0.84, + z: 0.84 + }; var collisionSoundURL = "https://hifi-public.s3.amazonaws.com/sounds/Collisions-otherorganic/ToyWoodBlock.L.wav"; var numBoxes = 200; - var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(Camera.getOrientation()))); for (var i = 0; i < numBoxes; i++) { - var position = Vec3.sum(center, {x: Math.random() * numBoxes, y: Math.random() * 3, z: Math.random() * numBoxes }) + var position = Vec3.sum(startPosition, { + x: Math.random() * numBoxes, + y: Math.random() * 2, + z: Math.random() * numBoxes + }) var box = Entities.addEntity({ type: "Model", modelURL: boxModelURL, collisionSoundURL: collisionSoundURL, - shapeType: "box", + shapeType: "compound", + compoundShapeURL: COLLISION_HULL_URL, position: position, collisionsWillMove: true, - dimensions: {x: 1, y: 2, z: 3}, - velocity: {x: 0, y: -.01, z: 0}, - gravity: {x: 0, y: -2.5 - Math.random() * 6, z: 0} + dimensions: TARGET_DIMENSIONS, + }); + Script.addEventHandler(box, "collisionWithEntity", boxCollision); boxes.push(box); } } +function boxCollision(me, other, collision) { + Entities.editEntity(me, { + gravity: { + x: 0, + y: -9.8, + z: 0 + } + }) +} + spawnGround(); spawnBoxes(); function cleanup() { Entities.deleteEntity(ground); - boxes.forEach(function(box){ + boxes.forEach(function(box) { Entities.deleteEntity(box); }); - dustSystems.forEach(function(dustEffect) { - Entities.deleteEntity(dustEffect); - }) + } -Script.scriptEnding.connect(cleanup); - - +Script.scriptEnding.connect(cleanup); \ No newline at end of file diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 676b6cdca7..df71885448 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -84,8 +84,8 @@ var SHOT_SCALE = { min1: 0, max1: 0.6, - min2: 5, - max2: 20 + min2: 3, + max2: 15 } @@ -110,6 +110,7 @@ hasArrowNotched: false, notchDetector: null, arrow: null, + arrowIsBurning:false, stringData: { currentColor: { red: 255, @@ -187,7 +188,7 @@ this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; this.arrow = userData.hifiBowKey.arrowID; - this.arrowIsBurning = userData.hifiBowKey.isBurning; + this.arrowIsBurning = userData.hifiBowKey.arrowIsBurning; setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: true, diff --git a/examples/toybox/bow/createFireSource.js b/examples/toybox/bow/createFireSources.js similarity index 55% rename from examples/toybox/bow/createFireSource.js rename to examples/toybox/bow/createFireSources.js index 9d974cd04e..1149626614 100644 --- a/examples/toybox/bow/createFireSource.js +++ b/examples/toybox/bow/createFireSources.js @@ -10,7 +10,35 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -function createFireSource() { +var TORCH_MODEL_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/torch.fbx'; +var TORCH_DIMENSIONS = { + x: 0.07, + y: 1.6, + z: 0.08 +}; + +var FIRE_VERTICAL_OFFSET = 0.9; + +function createFireSource(position) { + + var torchProperties = { + type: 'Model', + name: 'Hifi-Fire-Torch', + modelURL: TORCH_MODEL_URL, + shapeType: 'box', + collisionsWillMove: false, + ignoreForCollisions: true, + dimensions: TORCH_DIMENSIONS, + position: position + }; + + var torch = Entities.addEntity(torchProperties); + torches.push(torch); + var torchProperties = Entities.getEntityProperties(torch); + + var upVector = Quat.getUp(torchProperties.rotation); + var upOffset = Vec3.multiply(upVector, FIRE_VERTICAL_OFFSET); + var fireTipPosition = Vec3.sum(torchProperties.position, upOffset); var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); @@ -28,7 +56,7 @@ function createFireSource() { animationSettings: animationSettings, textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", emitRate: 100, - position: this.bowProperties.position, + position: fireTipPosition, colorStart: { red: 70, green: 70, @@ -74,6 +102,39 @@ function createFireSource() { lifespan: 1 }); + fires.push(fire) } -createFireSource(); +var fireSourcePositions = [{ + x: 100, + y: -1, + z: 100 + }, { + x: 100, + y: -1, + z: 102 + }, { + x: 100, + y: -1, + z: 104 + } + +]; + +var fires = []; +var torches = []; + +fireSourcePositions.forEach(function(position) { + createFireSource(position); +}) + +function cleanup() { + while (fires.length > 0) { + Entities.deleteEntity(fires.pop()); + } + while (torches.length > 0) { + Entities.deleteEntity(torches.pop()); + } +} + +Script.scriptEnding.connect(cleanup); \ No newline at end of file From da2f5c7848535474b82e69f1919490bcf18b9848 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 16 Nov 2015 12:57:43 -0800 Subject: [PATCH 41/79] work --- examples/toybox/bow/arrow.js | 12 +++++-- examples/toybox/bow/bow.js | 68 +++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js index 2994ce15c0..7ec846b599 100644 --- a/examples/toybox/bow/arrow.js +++ b/examples/toybox/bow/arrow.js @@ -44,6 +44,9 @@ continueNearGrab: function() { this.currentProperties = Entities.getEntityProperties(this.entityID, "position"); + if(this.notched===true){ + return; + } if (this.isBurning !== true) { this.searchForFires(); } else { @@ -198,8 +201,13 @@ }); Entities.editEntity(this.entityID, { - collisionsWillMove: false, - ignoreForCollisions: true + collisionsWillMove: true, + ignoreForCollisions: false, + gravity:{ + x:0, + y:-5.8, + z:0 + } }) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index df71885448..0ed5a1e555 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -215,7 +215,6 @@ // create the notch detector that arrows will look for if (this.notchDetector === null) { - print('CREATE NOTCH DETECTOR') this.createNotchDetector(); } @@ -431,7 +430,7 @@ this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - // print('HIT CONTINUE LOOP IN CHECK') + print('HIT CONTINUE LOOP IN CHECK') //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); @@ -514,10 +513,12 @@ print('HAND DISTANCE:: ' + pullBackDistance); var arrowForce = this.scaleArrowShotStrength(pullBackDistance); print('ARROW FORCE::' + arrowForce); - var forwardVec = Vec3.multiply(handToNotch, arrowForce); + handToNotch = Vec3.normalize(handToNotch) + var forwardVec=handToNotch; + // var forwardVec = Vec3.multiply(handToNotch, arrowForce); var arrowProperties = { - rotation: arrowRotation, + // rotation: arrowRotation, velocity: forwardVec }; @@ -542,37 +543,37 @@ //set an itnerval to heck how far the arrow is from the bow before adding gravity, etc. if we add this too soon, the arrow collides with the bow. hence, this function - this.physicalArrowInterval = Script.setInterval(function() { - print('in physical interval') - var arrowProps = Entities.getEntityProperties(arrowStore, "position"); - var bowProps = Entities.getEntityProperties(_this.entityID, "position"); - var arrowPosition = arrowProps.position; - var bowPosition = bowProps.position; + // this.physicalArrowInterval = Script.setInterval(function() { + // print('in physical interval') + // var arrowProps = Entities.getEntityProperties(arrowStore, "position"); + // var bowProps = Entities.getEntityProperties(_this.entityID, "position"); + // var arrowPosition = arrowProps.position; + // var bowPosition = bowProps.position; - var length = Vec3.distance(arrowPosition, bowPosition); - print('LENGTH:::' + length); - if (length > 2) { - print('make arrow physical' + arrowStore) - Entities.editEntity(arrowStore, { - ignoreForCollisions: false, - collisionsWillMove: true, - gravity: ARROW_GRAVITY - }); - print('after make physical' + arrowStore) - var arrowProps = Entities.getEntityProperties(arrowStore) - print('arrowprops-collisions::' + arrowProps.collisionsWillMove); - print('arrowprops-graviy' + JSON.stringify(arrowProps.gravity)); - Script.setTimeout(function() { - print('in timeout :: ' + arrowStore) - var arrowProps = Entities.getEntityProperties(arrowStore) - print('arrowprops-gravity2' + JSON.stringify(arrowProps.gravity)); - print('ARROW USER DATA::' + arrowProps.userData) + // var length = Vec3.distance(arrowPosition, bowPosition); + // print('LENGTH:::' + length); + // if (length > 2) { + // print('make arrow physical' + arrowStore) + // Entities.editEntity(arrowStore, { + // ignoreForCollisions: false, + // collisionsWillMove: true, + // gravity: ARROW_GRAVITY + // }); + // print('after make physical' + arrowStore) + // var arrowProps = Entities.getEntityProperties(arrowStore) + // print('arrowprops-collisions::' + arrowProps.collisionsWillMove); + // print('arrowprops-graviy' + JSON.stringify(arrowProps.gravity)); + // Script.setTimeout(function() { + // print('in timeout :: ' + arrowStore) + // var arrowProps = Entities.getEntityProperties(arrowStore) + // print('arrowprops-gravity2' + JSON.stringify(arrowProps.gravity)); + // print('ARROW USER DATA::' + arrowProps.userData) - }, 1000) + // }, 1000) - Script.clearInterval(_this.physicalArrowInterval) - } - }, 10) + // Script.clearInterval(_this.physicalArrowInterval) + // } + // }, 10) }, @@ -733,7 +734,8 @@ //have to reverse lookup the tracker by the arrow id to get access to the children var tracker = getArrowTrackerByArrowID(entityA); - print('ARROW COLLIDED WITH SOMETHING!' + tracker.glowBox) + var bProps = Entities.getEntityProperties(entityB,"name") + print('ARROW COLLIDED WITH SOMETHING!'+bProps.name) print('TRACKER IN COLLISION !' + tracker) // _t.playArrowHitSound(collision.contactPoint); //Vec3.print('penetration = ', collision.penetration); From ac336af9b558f66cb6bfd7c7700f776b83f4f4e2 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 18 Nov 2015 11:33:52 -0800 Subject: [PATCH 42/79] start spatial key --- examples/toybox/bow/createBow.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index 8d84cd6d16..abb3c74c1b 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -46,7 +46,15 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { - invertSolidWhileHeld: true + invertSolidWhileHeld: true, + spatialKey: { + relativePosition: { + x: 0, + y: 0.1, + z: 0 + }, + relativeRotation: Quat.fromPitchYawRollDegrees(0, -90, 90) + } } }) }); From 328950065f015739b613bc868a1a02843df74f7c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 18 Nov 2015 15:33:47 -0800 Subject: [PATCH 43/79] make bow equippable and change it so you dont have to load arrows --- examples/playa/playaSpawner.js | 20 +- examples/toybox/bow/arrow.js | 241 ---------------- examples/toybox/bow/bow.js | 268 +++++++++++------- examples/toybox/bow/createArrow.js | 74 ----- examples/toybox/bow/createBow.js | 4 +- examples/toybox/bow/createBowAndArrow.js | 2 - .../{arrowFieldSpawner.js => fieldSpawner.js} | 16 +- 7 files changed, 182 insertions(+), 443 deletions(-) delete mode 100644 examples/toybox/bow/arrow.js delete mode 100644 examples/toybox/bow/createArrow.js delete mode 100644 examples/toybox/bow/createBowAndArrow.js rename examples/toybox/bow/{arrowFieldSpawner.js => fieldSpawner.js} (91%) diff --git a/examples/playa/playaSpawner.js b/examples/playa/playaSpawner.js index 4982d70f2b..b929a219b9 100644 --- a/examples/playa/playaSpawner.js +++ b/examples/playa/playaSpawner.js @@ -155,14 +155,14 @@ spawnGround(); spawnBoxes(); -function cleanup() { - Entities.deleteEntity(ground); - boxes.forEach(function(box) { - Entities.deleteEntity(box); - }); - dustSystems.forEach(function(dustEffect) { - Entities.deleteEntity(dustEffect); - }) -} +// function cleanup() { +// Entities.deleteEntity(ground); +// boxes.forEach(function(box) { +// Entities.deleteEntity(box); +// }); +// dustSystems.forEach(function(dustEffect) { +// Entities.deleteEntity(dustEffect); +// }) +// } -Script.scriptEnding.connect(cleanup); \ No newline at end of file +// Script.scriptEnding.connect(cleanup); \ No newline at end of file diff --git a/examples/toybox/bow/arrow.js b/examples/toybox/bow/arrow.js deleted file mode 100644 index 7ec846b599..0000000000 --- a/examples/toybox/bow/arrow.js +++ /dev/null @@ -1,241 +0,0 @@ -// -// arrow.js -// -// This script attaches to an arrow to make it stop and stick when it hits something. Could use this to make it a fire arrow or really give any kind of property to itself or the entity it hits. -// -// Created by James B. Pollack @imgntn on 10/19/2015 -// Copyright 2015 High Fidelity, Inc. -// -// 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"); - - var NOTCH_DETECTOR_SEARCH_RADIUS = 0.25; - var FIRE_DETECTOR_SEARCH_RADIUS = 0.25; - - var _this; - - function Arrow() { - _this = this; - return; - } - - Arrow.prototype = { - stickOnCollision: false, - notched: false, - isBurning: false, - fire: null, - preload: function(entityID) { - this.entityID = entityID; - }, - - releaseGrab: function() { - if (this.fire !== null) { - this.fire = null; - this.isBurning = false; - Entities.deleteEntity(this.fire); - } - }, - - continueNearGrab: function() { - this.currentProperties = Entities.getEntityProperties(this.entityID, "position"); - - if(this.notched===true){ - return; - } - if (this.isBurning !== true) { - this.searchForFires(); - } else { - this.updateFirePosition(); - } - - if (this.notched !== true) { - this.searchForNotchDetectors(); - } - }, - - searchForNotchDetectors: function() { - if (this.notched === true) { - return - }; - - var ids = Entities.findEntities(this.currentProperties.position, NOTCH_DETECTOR_SEARCH_RADIUS); - var i, properties; - for (i = 0; i < ids.length; i++) { - id = ids[i]; - properties = Entities.getEntityProperties(id, 'name'); - if (properties.name === "Hifi-NotchDetector") { - print('NEAR THE NOTCH!!!') - this.notched = true; - this.tellBowArrowIsNotched(this.getBowID(id)); - } - } - - }, - - searchForFires: function() { - print('SEARCHING FOR FIRES!') - if (this.notched === true) { - return - }; - if (this.isBurning === true) { - return - }; - - var ids = Entities.findEntities(this.currentProperties.position, FIRE_DETECTOR_SEARCH_RADIUS); - var i, properties; - for (i = 0; i < ids.length; i++) { - id = ids[i]; - properties = Entities.getEntityProperties(id, 'name'); - if (properties.name === "Hifi-Arrow-Fire-Source") { - print('NEAR A FIRE SOURCE!!!'); - this.isBurning = true; - this.createFireParticleSystem(); - - - } - } - - }, - updateFirePosition: function() { - print('updating fire position' + this.fire) - Entities.editEntity(this.fire, { - position: this.currentProperties.position - }) - }, - createFireParticleSystem: function() { - print('CREATING FIRE PARTICLE SYSTEM') - - var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); - - var animationSettings = JSON.stringify({ - fps: 30, - running: true, - loop: true, - firstFrame: 1, - lastFrame: 10000 - }); - - this.fire = Entities.addEntity({ - type: "ParticleEffect", - name: "Hifi-Arrow-Fire-Source", - animationSettings: animationSettings, - textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", - emitRate: 100, - position: this.currentProperties.position, - colorStart: { - red: 70, - green: 70, - blue: 137 - }, - color: { - red: 200, - green: 99, - blue: 42 - }, - colorFinish: { - red: 255, - green: 99, - blue: 32 - }, - radiusSpread: 0.01, - radiusStart: 0.02, - radiusEnd: 0.001, - particleRadius: 0.15, - radiusFinish: 0.0, - emitOrientation: myOrientation, - emitSpeed: 0.3, - speedSpread: 0.1, - alphaStart: 0.05, - alpha: 0.1, - alphaFinish: 0.05, - emitDimensions: { - x: 1, - y: 1, - z: 0.1 - }, - polarFinish: 0.1, - emitAcceleration: { - x: 0.0, - y: 0.0, - z: 0.0 - }, - accelerationSpread: { - x: 0.1, - y: 0.01, - z: 0.1 - }, - lifespan: 1 - }); - }, - getBowID: function(notchDetectorID) { - var properties = Entities.getEntityProperties(notchDetectorID, "userData"); - var userData = JSON.parse(properties.userData); - if (userData.hasOwnProperty('hifiBowKey')) { - return userData.hifiBowKey.bowID; - } - }, - getActionID: function() { - var properties = Entities.getEntityProperties(this.entityID, "userData"); - var userData = JSON.parse(properties.userData); - if (userData.hasOwnProperty('hifiHoldActionKey')) { - return userData.hifiHoldActionKey.holdActionID; - } - }, - - disableGrab: function() { - var actionID = this.getActionID(); - var success = Entities.deleteAction(this.entityID, actionID); - }, - - tellBowArrowIsNotched: function(bowID) { - this.disableGrab(); - - setEntityCustomData('grabbableKey', this.entityID, { - grabbable: false, - invertSolidWhileHeld: true - }); - - Entities.editEntity(this.entityID, { - collisionsWillMove: true, - ignoreForCollisions: false, - gravity:{ - x:0, - y:-5.8, - z:0 - } - - }) - - setEntityCustomData('hifiBowKey', bowID, { - hasArrowNotched: true, - arrowIsBurning: this.isBurning, - arrowID: this.entityID - }); - - if (this.isBurning === true) { - this.isBurning = false; - this.fire = null; - Entities.deleteEntity(this.fire); - } - - }, - - } - - function deleteEntity(entityID) { - if (entityID === _this.entityID) { - if (_this.isBurning === true) { - Entities.deleteEntity(_this.fire); - } - } - } - - Entities.deletingEntity.connect(deleteEntity); - - return new Arrow; -}) \ No newline at end of file diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 0ed5a1e555..7564bec084 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -50,10 +50,21 @@ var ARROW_TIP_OFFSET = 0.32; var ARROW_GRAVITY = { x: 0, - y: -5.8, + y: -4.8, z: 0 }; + var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow.fbx"; + var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow_collision_hull.obj"; + var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); + + var ARROW_DIMENSIONS = { + x: 0.02, + y: 0.02, + z: 0.64 + }; + + var TOP_NOTCH_OFFSET = 0.6; var BOTTOM_NOTCH_OFFSET = 0.6; @@ -84,10 +95,18 @@ var SHOT_SCALE = { min1: 0, max1: 0.6, - min2: 3, - max2: 15 + min2: 1, + max2: 10 } + var BOW_SPATIAL_KEY = { + relativePosition: { + x: 0, + y: 0.06, + z: 0.11 + }, + relativeRotation: Quat.fromPitchYawRollDegrees(0, -90, 90) + } var _this; @@ -105,12 +124,13 @@ Bow.prototype = { isGrabbed: false, stringDrawn: false, + aiming: false, arrowTipPosition: null, preNotchString: null, hasArrowNotched: false, notchDetector: null, arrow: null, - arrowIsBurning:false, + arrowIsBurning: false, stringData: { currentColor: { red: 255, @@ -134,6 +154,22 @@ unload: function() { Script.update.disconnect(this.updateArrowTrackers); + + while (this.arrowTrackers.length > 0) { + var tracker = this.arrowTrackers.pop(); + tracker.childEntities.forEach(function(child) { + Entities.deleteEntity(child); + }) + tracker.childParticleSystems.forEach(function(child) { + Entities.deleteEntity(child); + }) + Entities.deleteEntity(tracker.arrowID); + } + this.deleteStrings(); + Entities.deleteEntity(this.notchDetector); + Entities.deleteEntity(this.preNotchString); + Entities.deleteEntity(this.arrow); + Script.clearInterval(physicalArrowInterval); }, setLeftHand: function() { @@ -159,7 +195,8 @@ setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: true, - invertSolidWhileHeld: true + invertSolidWhileHeld: true, + spatialKey: BOW_SPATIAL_KEY }); }, @@ -177,40 +214,30 @@ this.updateNotchDetectorPosition(); - //check to see if an arrow has notched itself in our notch detector - var userData = JSON.parse(this.bowProperties.userData); - if (userData.hasOwnProperty('hifiBowKey')) { - if (this.hasArrowNotched === false && userData.hifiBowKey.arrowID !== null) { - //notch the arrow - print('NOTCHING IT!') - this.playArrowNotchSound(); - this.playStringPullSound(); - this.hasArrowNotched = userData.hifiBowKey.hasArrowNotched; - - this.arrow = userData.hifiBowKey.arrowID; - this.arrowIsBurning = userData.hifiBowKey.arrowIsBurning; - - setEntityCustomData('grabbableKey', this.entityID, { - turnOffOtherHand: true, - invertSolidWhileHeld: true - }); - } - + if (this.hasArrowNotched === false) { + this.playArrowNotchSound(); + this.playStringPullSound(); + this.hasArrowNotched = true + this.arrow = this.createArrow(); + print('NOTCH ARROW' + this.arrow); + this.arrowIsBurning = false + setEntityCustomData('grabbableKey', this.entityID, { + turnOffOtherHand: true, + invertSolidWhileHeld: true, + spatialKey: BOW_SPATIAL_KEY + }); } - //this.arrow - //this.hasArrowNotched - - //create a string across the bow when we pick it up if (this.preNotchString === null) { print('CREATE PRE NOTCH STRING') this.createPreNotchString(); } - if (this.preNotchString !== null && this.hasArrowNotched === false) { - // print('DRAW PRE NOTCH STRING') + if (this.preNotchString !== null && this.aiming === false) { + // print('DRAW PRE NOTCH STRING') this.drawPreNotchStrings(); + this.updateArrowAttachedToBow(); } // create the notch detector that arrows will look for @@ -220,16 +247,17 @@ //if we have an arrow notched, then draw some new strings if (this.hasArrowNotched === true) { - if (this.preNotchString !== null) { - // print('MAKE PRE NOTCH INVISIBLE') + if (this.aiming === true) { Entities.editEntity(this.preNotchString, { visible: false - }); + }) + } else { + Entities.editEntity(this.preNotchString, { + visible: true + }) } - // print('CHECK STRING HAND') //only test for strings now that an arrow is notched - this.checkStringHand(); } else { @@ -240,23 +268,48 @@ }, releaseGrab: function() { + print('RELEASE GRAB EVENT') if (this.isGrabbed === true && this.hand === this.initialHand) { this.isGrabbed = false; this.stringDrawn = false; this.deleteStrings(); setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, - invertSolidWhileHeld: true + invertSolidWhileHeld: true, + spatialKey: BOW_SPATIAL_KEY }); Entities.deleteEntity(this.notchDetector); Entities.deleteEntity(this.preNotchString); - + Entities.deleteEntity(this.arrow); + this.aiming = false; this.notchDetector = null; + this.hasArrowNotched = false; this.preNotchString = null; } }, + createArrow: function() { + + var arrow = Entities.addEntity({ + name: 'Hifi-Arrow', + type: 'Model', + modelURL: ARROW_MODEL_URL, + shapeType: 'compound', + compoundShapeURL: ARROW_COLLISION_HULL_URL, + dimensions: ARROW_DIMENSIONS, + position: this.bowProperties.position, + script: ARROW_SCRIPT_URL, + collisionsWillMove: false, + ignoreForCollisions: true, + collisionSoundURL: 'http://hifi-content.s3.amazonaws.com/bow_and_arrow/sounds/Arrow_impact1.L.wav', + gravity: ARROW_GRAVITY, + linearDamping: 0.01 + + }); + return arrow + }, + createStrings: function() { this.createTopString(); this.createBottomString(); @@ -264,6 +317,7 @@ createTopString: function() { var stringProperties = { + name: 'Hifi-Bow-Top-String', type: 'Line', position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), dimensions: LINE_DIMENSIONS, @@ -281,6 +335,7 @@ createBottomString: function() { var stringProperties = { + name: 'Hifi-Bow-Bottom-String', type: 'Line', position: Vec3.sum(this.bowProperties.position, BOTTOM_NOTCH_OFFSET), dimensions: LINE_DIMENSIONS, @@ -302,7 +357,6 @@ }, updateStringPositions: function() { - var upVector = Quat.getUp(this.bowProperties.rotation); var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); @@ -313,7 +367,6 @@ this.topStringPosition = Vec3.sum(topStringPosition, backOffset); var bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); this.bottomStringPosition = Vec3.sum(bottomStringPosition, backOffset); - Entities.editEntity(this.topString, { position: this.topStringPosition }); @@ -321,7 +374,6 @@ Entities.editEntity(this.bottomString, { position: this.bottomStringPosition }); - Entities.editEntity(this.preNotchString, { position: this.topStringPosition }); @@ -355,20 +407,18 @@ }, - getLocalLineVectors: function() { - var topVector = Vec3.subtract(this.stringData.handPosition, this.topStringPosition); - var bottomVector = Vec3.subtract(this.stringData.handPosition, this.bottomStringPosition); + var topVector = Vec3.subtract(this.arrowRearPosition, this.topStringPosition); + var bottomVector = Vec3.subtract(this.arrowRearPosition, this.bottomStringPosition); return [topVector, bottomVector]; }, - - createPreNotchString: function() { var stringProperties = { type: 'Line', position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), dimensions: LINE_DIMENSIONS, + visible: true, userData: JSON.stringify({ grabbableKey: { grabbable: false @@ -380,13 +430,11 @@ }, drawPreNotchStrings: function() { - this.updateStringPositions(); var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET * 2); - Entities.editEntity(this.preNotchString, { linePoints: [{ x: 0, @@ -424,13 +472,14 @@ if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { // firing the arrow - print('HIT RELEASE LOOP IN CHECK') + // print('HIT RELEASE LOOP IN CHECK') this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - print('HIT CONTINUE LOOP IN CHECK') + // print('HIT CONTINUE LOOP IN CHECK') + this.aiming = true; //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); @@ -440,8 +489,8 @@ this.updateArrowPositionInNotch(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { - print('HIT START LOOP IN CHECK') - //the first time aiming the arrow + // print('HIT START LOOP IN CHECK') + //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); var arrowTracker = this.createArrowTracker(this.arrow); @@ -465,6 +514,14 @@ this.arrowTipPosition = arrowTipPosition; return arrowTipPosition; + }, + setArrowRearPosition: function(arrowPosition, arrowRotation) { + var frontVector = Quat.getFront(arrowRotation); + var frontOffset = Vec3.multiply(frontVector, -ARROW_TIP_OFFSET); + var arrowTipPosition = Vec3.sum(arrowPosition, frontOffset); + this.arrowRearPosition = arrowTipPosition; + return arrowTipPosition; + }, getArrowPosition: function() { var arrowVector = Vec3.subtract(this.stringData.handPosition, this.stringData.grabHandPosition); @@ -474,6 +531,13 @@ return arrowPosition; }, + updateArrowAttachedToBow: function() { + Entities.editEntity(this.arrow, { + position: this.notchDetectorPosition, + rotation: this.bowProperties.rotation + }) + }, + updateArrowPositionInNotch: function() { //move it backwards var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); @@ -491,7 +555,8 @@ var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); - // this.setArrowTipPosition(finalArrowPosition, arrowRotation); + this.setArrowTipPosition(finalArrowPosition, arrowRotation); + this.setArrowRearPosition(finalArrowPosition, arrowRotation); Entities.editEntity(this.arrow, { position: finalArrowPosition, rotation: arrowRotation @@ -514,25 +579,23 @@ var arrowForce = this.scaleArrowShotStrength(pullBackDistance); print('ARROW FORCE::' + arrowForce); handToNotch = Vec3.normalize(handToNotch) - var forwardVec=handToNotch; - // var forwardVec = Vec3.multiply(handToNotch, arrowForce); + // var forwardVec = handToNotch; + var forwardVec = Vec3.multiply(handToNotch, arrowForce); var arrowProperties = { - // rotation: arrowRotation, - velocity: forwardVec + // rotation: arrowRotation, + velocity: forwardVec, }; this.playShootArrowSound(); - Entities.editEntity(this.arrow, arrowProperties); - setEntityCustomData('hifiBowKey', this.entityID, { - hasArrowNotched: false, - arrowID: null - }); + Entities.editEntity(this.arrow, arrowProperties); var arrowStore = this.arrow; this.arrow = null; this.hasArrowNotched = false; + this.aiming = false; + Entities.editEntity(this.preNotchString, { visible: true @@ -543,37 +606,25 @@ //set an itnerval to heck how far the arrow is from the bow before adding gravity, etc. if we add this too soon, the arrow collides with the bow. hence, this function - // this.physicalArrowInterval = Script.setInterval(function() { - // print('in physical interval') - // var arrowProps = Entities.getEntityProperties(arrowStore, "position"); - // var bowProps = Entities.getEntityProperties(_this.entityID, "position"); - // var arrowPosition = arrowProps.position; - // var bowPosition = bowProps.position; + var physicalArrowInterval = Script.setInterval(function() { + print('in physical interval') + var arrowProps = Entities.getEntityProperties(arrowStore, "position"); + var bowProps = Entities.getEntityProperties(_this.entityID, "position"); + var arrowPosition = arrowProps.position; + var bowPosition = bowProps.position; - // var length = Vec3.distance(arrowPosition, bowPosition); - // print('LENGTH:::' + length); - // if (length > 2) { - // print('make arrow physical' + arrowStore) - // Entities.editEntity(arrowStore, { - // ignoreForCollisions: false, - // collisionsWillMove: true, - // gravity: ARROW_GRAVITY - // }); - // print('after make physical' + arrowStore) - // var arrowProps = Entities.getEntityProperties(arrowStore) - // print('arrowprops-collisions::' + arrowProps.collisionsWillMove); - // print('arrowprops-graviy' + JSON.stringify(arrowProps.gravity)); - // Script.setTimeout(function() { - // print('in timeout :: ' + arrowStore) - // var arrowProps = Entities.getEntityProperties(arrowStore) - // print('arrowprops-gravity2' + JSON.stringify(arrowProps.gravity)); - // print('ARROW USER DATA::' + arrowProps.userData) - - // }, 1000) - - // Script.clearInterval(_this.physicalArrowInterval) - // } - // }, 10) + var length = Vec3.distance(arrowPosition, bowPosition); + print('LENGTH:::' + length); + if (length > 2) { + print('make arrow physical' + arrowStore) + Entities.editEntity(arrowStore, { + ignoreForCollisions: false, + collisionsWillMove: true + }); + var arrowProps = Entities.getEntityProperties(arrowStore) + Script.clearInterval(physicalArrowInterval); + } + }, 5) }, @@ -608,12 +659,7 @@ red: 0, green: 255, blue: 0 - }, - userData: JSON.stringify({ - hifiBowKey: { - bowID: this.entityID - } - }) + } }; this.notchDetector = Entities.addEntity(detectorProperties); @@ -699,7 +745,9 @@ y: 0.01, z: 0.1 }, - lifespan: 0.5 + lifespan: 0.5, + ignoreForCollisions: true, + collisionsWillMove: false, }); return fire; @@ -717,7 +765,7 @@ // whizzingSound: _t.playWhizzSound(), //fireSound: _t.createFireSound(), hasPlayedCollisionSound: false, - glowBox: _t.createGlowBox(), + glowBox: _t.createGlowBoxAsModel(), fireParticleSystem: _t.createFireParticleSystem(), childEntities: [], childSounds: [], @@ -734,8 +782,8 @@ //have to reverse lookup the tracker by the arrow id to get access to the children var tracker = getArrowTrackerByArrowID(entityA); - var bProps = Entities.getEntityProperties(entityB,"name") - print('ARROW COLLIDED WITH SOMETHING!'+bProps.name) + var bProps = Entities.getEntityProperties(entityB, "name") + print('ARROW COLLIDED WITH SOMETHING!' + bProps.name) print('TRACKER IN COLLISION !' + tracker) // _t.playArrowHitSound(collision.contactPoint); //Vec3.print('penetration = ', collision.penetration); @@ -760,7 +808,8 @@ // print('UPDATING CHILDREN OF TRACKER:::' + this.childEntities.length); var arrowProperties = Entities.getEntityProperties(this.arrowID, ["position", "rotation"]); - + _t.setArrowTipPosition(arrowProperties.position, arrowProperties.rotation); + _t.setArrowRearPosition(arrowProperties.position, arrowProperties.rotation); //update the positions // this.soundEntities.forEach(function(injector) { // var audioProperties = { @@ -779,7 +828,7 @@ this.childParticleSystems.forEach(function(child) { Entities.editEntity(child, { - position: arrowProperties.position + position: _t.arrowTipPosition }) }) @@ -803,15 +852,22 @@ return injector }, createGlowBoxAsModel: function() { - var modelURL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/glowBox.fbx'; + var GLOW_MODEL_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/glow.fbx'; var properties = { - name: 'Hifi-Arrow-Glow', + name: 'Hifi-Arrow-Glow-Model', type: 'Model', - modelURL: modelURL, + modelURL: GLOW_MODEL_URL, dimensions: ARROW_DIMENSIONS, collisionsWillMove: false, ignoreForCollisions: true, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false + } + }) } + var glowBox = Entities.addEntity(properties); + return glowBox }, createGlowBox: function() { print('creating glow box') diff --git a/examples/toybox/bow/createArrow.js b/examples/toybox/bow/createArrow.js deleted file mode 100644 index e4db24cfd1..0000000000 --- a/examples/toybox/bow/createArrow.js +++ /dev/null @@ -1,74 +0,0 @@ -// -// createArrow.js -// -// Created byJames Pollack @imgntn on 10/19/2015 -// Copyright 2015 High Fidelity, Inc. -// -// This script creates a bow you can use to shoot an arrow. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow.fbx"; -var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow_collision_hull.obj"; -var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); - -var ARROW_DIMENSIONS = { - x: 0.02, - y: 0.02, - z: 0.64 -}; - -var ARROW_GRAVITY = { - x: 0, - y: 0, - z: 0 -}; - - -function cleanup() { - Entities.deleteEntity(arrow); -} - -var arrow; - -function createArrow(i) { - var center = Vec3.sum(Vec3.sum(MyAvatar.position, { - x: 0, - y: 0.5, - z: 0 -}), Vec3.multiply(1.25*i, Quat.getFront(Camera.getOrientation()))); - - arrow = Entities.addEntity({ - name: 'Hifi-Arrow', - type: 'Model', - modelURL: ARROW_MODEL_URL, - shapeType: 'compound', - compoundShapeURL: ARROW_COLLISION_HULL_URL, - dimensions: ARROW_DIMENSIONS, - position: center, - script: ARROW_SCRIPT_URL, - collisionsWillMove: true, - ignoreForCollisions: false, - collisionSoundURL:'http://hifi-content.s3.amazonaws.com/bow_and_arrow/sounds/Arrow_impact1.L.wav', - gravity: ARROW_GRAVITY, - linearDamping:0.01, - userData: JSON.stringify({ - grabbableKey: { - invertSolidWhileHeld: true - } - }) - }); - -} - -var i; -for(i=1;i<6;i++){ - createArrow(i); - -} - - - -Script.scriptEnding.connect(cleanup); \ No newline at end of file diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index abb3c74c1b..880b0920e8 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -50,8 +50,8 @@ var bow = Entities.addEntity({ spatialKey: { relativePosition: { x: 0, - y: 0.1, - z: 0 + y: 0.06, + z: 0.11 }, relativeRotation: Quat.fromPitchYawRollDegrees(0, -90, 90) } diff --git a/examples/toybox/bow/createBowAndArrow.js b/examples/toybox/bow/createBowAndArrow.js deleted file mode 100644 index a731891e6c..0000000000 --- a/examples/toybox/bow/createBowAndArrow.js +++ /dev/null @@ -1,2 +0,0 @@ - Script.include("createBow.js"); - Script.include("createArrow.js"); diff --git a/examples/toybox/bow/arrowFieldSpawner.js b/examples/toybox/bow/fieldSpawner.js similarity index 91% rename from examples/toybox/bow/arrowFieldSpawner.js rename to examples/toybox/bow/fieldSpawner.js index 08bb3be283..96fb56b6b7 100644 --- a/examples/toybox/bow/arrowFieldSpawner.js +++ b/examples/toybox/bow/fieldSpawner.js @@ -1,4 +1,4 @@ -// arrowFieldSpawner.js +// fieldSpawner.js // examples // // Created by James B. Pollack @imgntn on 11/16/2015 @@ -95,12 +95,12 @@ spawnGround(); spawnBoxes(); -function cleanup() { - Entities.deleteEntity(ground); - boxes.forEach(function(box) { - Entities.deleteEntity(box); - }); +// function cleanup() { +// Entities.deleteEntity(ground); +// boxes.forEach(function(box) { +// Entities.deleteEntity(box); +// }); -} +// } -Script.scriptEnding.connect(cleanup); \ No newline at end of file +// Script.scriptEnding.connect(cleanup); \ No newline at end of file From b9aa9cf98035f6115f69550ba0cf0fb0bbc0e79c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 18 Nov 2015 15:53:10 -0800 Subject: [PATCH 44/79] cleanup --- examples/toybox/bow/bow.js | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 7564bec084..7063960382 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -108,6 +108,8 @@ relativeRotation: Quat.fromPitchYawRollDegrees(0, -90, 90) } + var arrowTrackers = []; + var _this; function Bow() { @@ -155,8 +157,8 @@ unload: function() { Script.update.disconnect(this.updateArrowTrackers); - while (this.arrowTrackers.length > 0) { - var tracker = this.arrowTrackers.pop(); + while (arrowTrackers.length > 0) { + var tracker = arrowTrackers.pop(); tracker.childEntities.forEach(function(child) { Entities.deleteEntity(child); }) @@ -494,7 +496,8 @@ this.stringDrawn = true; this.createStrings(); var arrowTracker = this.createArrowTracker(this.arrow); - this.arrowTrackers.push(arrowTracker) + arrowTrackers.push(arrowTracker) + this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.stringData.grabHandPosition = this.getGrabHandPosition(); @@ -781,20 +784,8 @@ Script.addEventHandler(this.arrowID, "collisionWithEntity", function(entityA, entityB, collision) { //have to reverse lookup the tracker by the arrow id to get access to the children var tracker = getArrowTrackerByArrowID(entityA); - - var bProps = Entities.getEntityProperties(entityB, "name") - print('ARROW COLLIDED WITH SOMETHING!' + bProps.name) - print('TRACKER IN COLLISION !' + tracker) - // _t.playArrowHitSound(collision.contactPoint); - //Vec3.print('penetration = ', collision.penetration); - //Vec3.print('collision contact point = ', collision.contactPoint); - // var orientationChange = orientationOf(collision.velocityChange); Entities.deleteEntity(tracker.glowBox); - //we don't want to update this arrow tracker anymore - var index = _t.arrowTrackers.indexOf(entityA); - if (index > -1) { - // _t.arrowTrackers.splice(index, 1); - } + }); }, setChildren: function() { @@ -805,8 +796,6 @@ this.childParticleSystems.push(this.fireParticleSystem); }, updateChildEntities: function(arrowID) { - - // print('UPDATING CHILDREN OF TRACKER:::' + this.childEntities.length); var arrowProperties = Entities.getEntityProperties(this.arrowID, ["position", "rotation"]); _t.setArrowTipPosition(arrowProperties.position, arrowProperties.rotation); _t.setArrowRearPosition(arrowProperties.position, arrowProperties.rotation); @@ -836,7 +825,6 @@ }; arrowTracker.init(); arrowTracker.setCollisionCallback(); - print('after create arrow tracker') return arrowTracker }, @@ -918,12 +906,10 @@ var glowBox = Entities.addEntity(properties); return glowBox }, - arrowTrackers: [], updateArrowTrackers: function() { - // print('updating arrow trackers:::' + _this.arrowTrackers.length); - _this.arrowTrackers.forEach(function(tracker) { + // print('updating arrow trackers:::' + arrowTrackers.length); + arrowTrackers.forEach(function(tracker) { var arrowID = tracker.arrowID; - // print('TRACKER ARROW ID'+tracker.arrowID) tracker.updateChildEntities(arrowID); }) }, @@ -979,10 +965,9 @@ }; getArrowTrackerByArrowID = function(arrowID) { - var result = _this.arrowTrackers.filter(function(tracker) { + var result = arrowTrackers.filter(function(tracker) { return tracker.arrowID === arrowID; }); - var tracker = result[0] return tracker } From 52582e130a313999200b895244d31f9dad16ae6d Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 18 Nov 2015 16:32:47 -0800 Subject: [PATCH 45/79] add lifetime to arrows, disable arrow tracker --- examples/toybox/bow/bow.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 7063960382..49a616535a 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -150,23 +150,23 @@ this.arrowHitSound = SoundCache.getSound(ARROW_HIT_SOUND_URL); this.arrowNotchSound = SoundCache.getSound(NOTCH_ARROW_SOUND_URL); this.arrowWhizzSound = SoundCache.getSound(ARROW_WHIZZ_SOUND_URL); - Script.update.connect(this.updateArrowTrackers); + //Script.update.connect(this.updateArrowTrackers); }, unload: function() { - Script.update.disconnect(this.updateArrowTrackers); + // Script.update.disconnect(this.updateArrowTrackers); - while (arrowTrackers.length > 0) { - var tracker = arrowTrackers.pop(); - tracker.childEntities.forEach(function(child) { - Entities.deleteEntity(child); - }) - tracker.childParticleSystems.forEach(function(child) { - Entities.deleteEntity(child); - }) - Entities.deleteEntity(tracker.arrowID); - } + // while (arrowTrackers.length > 0) { + // var tracker = arrowTrackers.pop(); + // tracker.childEntities.forEach(function(child) { + // Entities.deleteEntity(child); + // }) + // tracker.childParticleSystems.forEach(function(child) { + // Entities.deleteEntity(child); + // }) + // Entities.deleteEntity(tracker.arrowID); + // } this.deleteStrings(); Entities.deleteEntity(this.notchDetector); Entities.deleteEntity(this.preNotchString); @@ -495,8 +495,8 @@ //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); - var arrowTracker = this.createArrowTracker(this.arrow); - arrowTrackers.push(arrowTracker) + // var arrowTracker = this.createArrowTracker(this.arrow); + // arrowTrackers.push(arrowTracker) this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); @@ -588,6 +588,7 @@ var arrowProperties = { // rotation: arrowRotation, velocity: forwardVec, + lifetime:10 }; this.playShootArrowSound(); From 670cca4eb78baf9fe96bb5c6655c391f278a41b6 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Wed, 18 Nov 2015 16:35:28 -0800 Subject: [PATCH 46/79] fix sounds --- examples/toybox/bow/bow.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 49a616535a..ed48b4a22f 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -218,7 +218,6 @@ if (this.hasArrowNotched === false) { this.playArrowNotchSound(); - this.playStringPullSound(); this.hasArrowNotched = true this.arrow = this.createArrow(); print('NOTCH ARROW' + this.arrow); @@ -492,6 +491,8 @@ } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { // print('HIT START LOOP IN CHECK') + this.playStringPullSound(); + //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); From ea59e0887d52b2a48308d9d9c8e1faed271dac88 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 19 Nov 2015 19:22:54 -0800 Subject: [PATCH 47/79] more bow features and debug prep --- examples/toybox/bow/bow.js | 41 ++++++++++++++++---------------- examples/toybox/bow/createBow.js | 1 + 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index ed48b4a22f..b49a39906f 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -54,8 +54,8 @@ z: 0 }; - var ARROW_MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow.fbx"; - var ARROW_COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/arrow_collision_hull.obj"; + var ARROW_MODEL_URL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/newarrow_textured.fbx"; + var ARROW_COLLISION_HULL_URL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/newarrow_collision_hull.obj"; var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); var ARROW_DIMENSIONS = { @@ -140,7 +140,6 @@ blue: 255 } }, - preload: function(entityID) { print('preload bow') this.entityID = entityID; @@ -171,7 +170,7 @@ Entities.deleteEntity(this.notchDetector); Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); - Script.clearInterval(physicalArrowInterval); + }, setLeftHand: function() { @@ -219,8 +218,7 @@ if (this.hasArrowNotched === false) { this.playArrowNotchSound(); this.hasArrowNotched = true - this.arrow = this.createArrow(); - print('NOTCH ARROW' + this.arrow); + this.arrowIsBurning = false setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: true, @@ -303,7 +301,7 @@ script: ARROW_SCRIPT_URL, collisionsWillMove: false, ignoreForCollisions: true, - collisionSoundURL: 'http://hifi-content.s3.amazonaws.com/bow_and_arrow/sounds/Arrow_impact1.L.wav', + collisionSoundURL: 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Arrow_impact1.L.wav', gravity: ARROW_GRAVITY, linearDamping: 0.01 @@ -490,15 +488,17 @@ this.updateArrowPositionInNotch(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + this.arrow = this.createArrow(); + print('CREATE ARROW' + this.arrow); // print('HIT START LOOP IN CHECK') - this.playStringPullSound(); + this.playStringPullSound(); //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); // var arrowTracker = this.createArrowTracker(this.arrow); // arrowTrackers.push(arrowTracker) - + this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.stringData.grabHandPosition = this.getGrabHandPosition(); @@ -573,23 +573,23 @@ var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); var pullBackDistance = Vec3.length(handToNotch); - if (pullBackDistance >= 0.6) { - pullBackDistance = 0.6; - } + // if (pullBackDistance >= 0.6) { + // pullBackDistance = 0.6; + // } var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); print('HAND DISTANCE:: ' + pullBackDistance); var arrowForce = this.scaleArrowShotStrength(pullBackDistance); print('ARROW FORCE::' + arrowForce); - handToNotch = Vec3.normalize(handToNotch) - // var forwardVec = handToNotch; - var forwardVec = Vec3.multiply(handToNotch, arrowForce); - + // handToNotch = Vec3.normalize(handToNotch) + var forwardVec = handToNotch; + // var forwardVec = Vec3.multiply(handToNotch, arrowForce); + var forwardVec = Vec3.multiply(handToNotch, 2); var arrowProperties = { // rotation: arrowRotation, velocity: forwardVec, - lifetime:10 + lifetime: 10 }; this.playShootArrowSound(); @@ -612,21 +612,20 @@ //set an itnerval to heck how far the arrow is from the bow before adding gravity, etc. if we add this too soon, the arrow collides with the bow. hence, this function var physicalArrowInterval = Script.setInterval(function() { - print('in physical interval') + // print('in physical interval') var arrowProps = Entities.getEntityProperties(arrowStore, "position"); var bowProps = Entities.getEntityProperties(_this.entityID, "position"); var arrowPosition = arrowProps.position; var bowPosition = bowProps.position; var length = Vec3.distance(arrowPosition, bowPosition); - print('LENGTH:::' + length); + // print('LENGTH:::' + length); if (length > 2) { - print('make arrow physical' + arrowStore) + print('make arrow physical' + arrowStore) Entities.editEntity(arrowStore, { ignoreForCollisions: false, collisionsWillMove: true }); - var arrowProps = Entities.getEntityProperties(arrowStore) Script.clearInterval(physicalArrowInterval); } }, 5) diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index 880b0920e8..33ddd58147 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -46,6 +46,7 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { + // turnOffOtherHand:true, invertSolidWhileHeld: true, spatialKey: { relativePosition: { From 225909f8a129a8c47620b595c3e8db0d395c6ee6 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Thu, 19 Nov 2015 19:25:51 -0800 Subject: [PATCH 48/79] play notch sound at right time --- examples/toybox/bow/bow.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index b49a39906f..da69469235 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -216,7 +216,6 @@ this.updateNotchDetectorPosition(); if (this.hasArrowNotched === false) { - this.playArrowNotchSound(); this.hasArrowNotched = true this.arrowIsBurning = false @@ -289,6 +288,7 @@ }, createArrow: function() { + this.playArrowNotchSound(); var arrow = Entities.addEntity({ name: 'Hifi-Arrow', @@ -582,10 +582,10 @@ print('HAND DISTANCE:: ' + pullBackDistance); var arrowForce = this.scaleArrowShotStrength(pullBackDistance); print('ARROW FORCE::' + arrowForce); - // handToNotch = Vec3.normalize(handToNotch) - var forwardVec = handToNotch; - // var forwardVec = Vec3.multiply(handToNotch, arrowForce); - var forwardVec = Vec3.multiply(handToNotch, 2); + // handToNotch = Vec3.normalize(handToNotch) + var forwardVec = handToNotch; + // var forwardVec = Vec3.multiply(handToNotch, arrowForce); + var forwardVec = Vec3.multiply(handToNotch, 2); var arrowProperties = { // rotation: arrowRotation, velocity: forwardVec, @@ -612,16 +612,16 @@ //set an itnerval to heck how far the arrow is from the bow before adding gravity, etc. if we add this too soon, the arrow collides with the bow. hence, this function var physicalArrowInterval = Script.setInterval(function() { - // print('in physical interval') + // print('in physical interval') var arrowProps = Entities.getEntityProperties(arrowStore, "position"); var bowProps = Entities.getEntityProperties(_this.entityID, "position"); var arrowPosition = arrowProps.position; var bowPosition = bowProps.position; var length = Vec3.distance(arrowPosition, bowPosition); - // print('LENGTH:::' + length); + // print('LENGTH:::' + length); if (length > 2) { - print('make arrow physical' + arrowStore) + print('make arrow physical' + arrowStore) Entities.editEntity(arrowStore, { ignoreForCollisions: false, collisionsWillMove: true From 8a65c7a48f8a8e8f48e4c584ea4e0ba20e53d98d Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 20 Nov 2015 10:26:41 -0800 Subject: [PATCH 49/79] draw strings sooner, arrow force squared, disable grab on arrow --- examples/toybox/bow/bow.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index da69469235..b6c6e562b1 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -133,6 +133,7 @@ notchDetector: null, arrow: null, arrowIsBurning: false, + prePickupString: null, stringData: { currentColor: { red: 255, @@ -149,7 +150,7 @@ this.arrowHitSound = SoundCache.getSound(ARROW_HIT_SOUND_URL); this.arrowNotchSound = SoundCache.getSound(NOTCH_ARROW_SOUND_URL); this.arrowWhizzSound = SoundCache.getSound(ARROW_WHIZZ_SOUND_URL); - //Script.update.connect(this.updateArrowTrackers); + Script.update.connect(this.drawStringsBeforePickup); }, @@ -170,6 +171,8 @@ Entities.deleteEntity(this.notchDetector); Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); + Entities.deleteEntity(this.preNotchString); + Script.update.disconnect(this.drawStringsBeforePickup); }, @@ -234,8 +237,8 @@ if (this.preNotchString !== null && this.aiming === false) { // print('DRAW PRE NOTCH STRING') - this.drawPreNotchStrings(); - this.updateArrowAttachedToBow(); + // this.drawPreNotchStrings(); + // this.updateArrowAttachedToBow(); } // create the notch detector that arrows will look for @@ -277,12 +280,12 @@ spatialKey: BOW_SPATIAL_KEY }); Entities.deleteEntity(this.notchDetector); - Entities.deleteEntity(this.preNotchString); + // Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); this.aiming = false; this.notchDetector = null; this.hasArrowNotched = false; - this.preNotchString = null; + // this.preNotchString = null; } }, @@ -303,7 +306,12 @@ ignoreForCollisions: true, collisionSoundURL: 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Arrow_impact1.L.wav', gravity: ARROW_GRAVITY, - linearDamping: 0.01 + linearDamping: 0.01, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false + } + }) }); return arrow @@ -412,6 +420,17 @@ return [topVector, bottomVector]; }, + drawStringsBeforePickup: function() { + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); + + if (this.prePickupString === null) { + this.createPreNotchString(); + + } else { + this.drawPreNotchStrings(); + } + }, + createPreNotchString: function() { var stringProperties = { type: 'Line', @@ -585,7 +604,7 @@ // handToNotch = Vec3.normalize(handToNotch) var forwardVec = handToNotch; // var forwardVec = Vec3.multiply(handToNotch, arrowForce); - var forwardVec = Vec3.multiply(handToNotch, 2); + var forwardVec = Vec3.multiply(handToNotch,handToNotch); var arrowProperties = { // rotation: arrowRotation, velocity: forwardVec, @@ -609,7 +628,7 @@ this.stringDrawn = false; this.deleteStrings(); - //set an itnerval to heck how far the arrow is from the bow before adding gravity, etc. if we add this too soon, the arrow collides with the bow. hence, this function + //set an itnerval to check how far the arrow is from the bow before adding gravity, etc. if we add this too soon, the arrow collides with the bow. hence, this function var physicalArrowInterval = Script.setInterval(function() { // print('in physical interval') From 4efe31f15809585e3ecae7ba820ce50a9b90913c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 20 Nov 2015 15:15:57 -0800 Subject: [PATCH 50/79] work --- examples/controllers/handControllerGrab.js | 27 +++- examples/toybox/bow/bow.js | 179 ++++++++++----------- 2 files changed, 110 insertions(+), 96 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index f8a2eeefa5..d7a3d05416 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -307,7 +307,14 @@ function MyController(hand) { position: closePoint, linePoints: [ZERO_VEC, farPoint], color: color, - lifetime: 0.1 + lifetime: 0.1, + collisionsWillMove: false, + ignoreForCollisions: true, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false + } + }) }); } @@ -322,7 +329,14 @@ function MyController(hand) { position: closePoint, linePoints: [ZERO_VEC, farPoint], color: color, - lifetime: LIFETIME + lifetime: LIFETIME, + collisionsWillMove: false, + ignoreForCollisions: true, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false + } + }) }); } else { var age = Entities.getEntityProperties(this.pointer, "age").age; @@ -526,7 +540,14 @@ function MyController(hand) { green: 255, blue: 0 }, - lifetime: 0.1 + lifetime: 0.1, + collisionsWillMove: false, + ignoreForCollisions: true, + userData: JSON.stringify({ + grabbableKey: { + grabbable: false + } + }) }); } diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index b6c6e562b1..d8da57412f 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -9,15 +9,6 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// to do: -// create field with haybales and targets -// create torches to start arrow on fire -// play start fire sound -// parent the arrow fire to the arrow -// delete the arrow fire when the arrow is notched (the bow will now create and track fire) -// make arrow rotate toward ground as it flies -// working shader or transparent model for glow box - (function() { Script.include("../../libraries/utils.js"); @@ -27,7 +18,7 @@ var STRING_PULL_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Bow_draw.1.L.wav'; var ARROW_WHIZZ_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/whizz.wav'; //todo : multiple impact sounds - var ARROW_HIT_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/bow_and_arrow/sounds/Arrow_impact1.L.wav' + var ARROW_HIT_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Arrow_impact1.L.wav' var ARROW_DIMENSIONS = { x: 0.02, y: 0.02, @@ -50,13 +41,12 @@ var ARROW_TIP_OFFSET = 0.32; var ARROW_GRAVITY = { x: 0, - y: -4.8, + y: 0, z: 0 }; var ARROW_MODEL_URL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/newarrow_textured.fbx"; var ARROW_COLLISION_HULL_URL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/newarrow_collision_hull.obj"; - var ARROW_SCRIPT_URL = Script.resolvePath('arrow.js'); var ARROW_DIMENSIONS = { x: 0.02, @@ -117,12 +107,6 @@ return; } - // pick up bow - // pick up arrow - // move arrow near notch detector - // pull back on the trigger to draw the string - // release to fire - Bow.prototype = { isGrabbed: false, stringDrawn: false, @@ -150,6 +134,7 @@ this.arrowHitSound = SoundCache.getSound(ARROW_HIT_SOUND_URL); this.arrowNotchSound = SoundCache.getSound(NOTCH_ARROW_SOUND_URL); this.arrowWhizzSound = SoundCache.getSound(ARROW_WHIZZ_SOUND_URL); + this.createPreNotchString(); Script.update.connect(this.drawStringsBeforePickup); }, @@ -171,7 +156,6 @@ Entities.deleteEntity(this.notchDetector); Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); - Entities.deleteEntity(this.preNotchString); Script.update.disconnect(this.drawStringsBeforePickup); }, @@ -218,22 +202,21 @@ this.updateNotchDetectorPosition(); - if (this.hasArrowNotched === false) { - this.hasArrowNotched = true + // if (this.hasArrowNotched === false) { + // this.hasArrowNotched = true - this.arrowIsBurning = false - setEntityCustomData('grabbableKey', this.entityID, { - turnOffOtherHand: true, - invertSolidWhileHeld: true, - spatialKey: BOW_SPATIAL_KEY - }); - } + // this.arrowIsBurning = false + // setEntityCustomData('grabbableKey', this.entityID, { + // turnOffOtherHand: true, + // invertSolidWhileHeld: true, + // spatialKey: BOW_SPATIAL_KEY + // }); + // } //create a string across the bow when we pick it up - if (this.preNotchString === null) { - print('CREATE PRE NOTCH STRING') - this.createPreNotchString(); - } + // if (this.preNotchString === null) { + // this.createPreNotchString(); + // } if (this.preNotchString !== null && this.aiming === false) { // print('DRAW PRE NOTCH STRING') @@ -247,25 +230,25 @@ } //if we have an arrow notched, then draw some new strings - if (this.hasArrowNotched === true) { - if (this.aiming === true) { - Entities.editEntity(this.preNotchString, { - visible: false - }) - } else { - Entities.editEntity(this.preNotchString, { - visible: true - }) - } - //only test for strings now that an arrow is notched - - this.checkStringHand(); - + // if (this.hasArrowNotched === true) { + if (this.aiming === true) { + Entities.editEntity(this.preNotchString, { + visible: false + }) } else { - // print('DONT DO ANYTHING') - //otherwise, don't do much of anything. - + Entities.editEntity(this.preNotchString, { + visible: true + }) } + //only test for strings now that an arrow is notched + + this.checkStringHand(); + + // } else { + // // print('DONT DO ANYTHING') + // //otherwise, don't do much of anything. + + // } }, releaseGrab: function() { @@ -301,12 +284,11 @@ compoundShapeURL: ARROW_COLLISION_HULL_URL, dimensions: ARROW_DIMENSIONS, position: this.bowProperties.position, - script: ARROW_SCRIPT_URL, collisionsWillMove: false, - ignoreForCollisions: true, - collisionSoundURL: 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Arrow_impact1.L.wav', + ignoreForCollisions: false, + collisionSoundURL:ARROW_HIT_SOUND_URL, gravity: ARROW_GRAVITY, - linearDamping: 0.01, + damping: 0.01, userData: JSON.stringify({ grabbableKey: { grabbable: false @@ -314,6 +296,15 @@ }) }); + var arrowProps = Entities.getEntityProperties(arrow) + Script.addEventHandler(arrow, "collisionWithEntity", function(entityA, entityB, collision) { + //have to reverse lookup the tracker by the arrow id to get access to the children + + print('ARROW COLLIDED WITH::' + entityB); + print('NAME OF ENTITY:::' + Entities.getEntityProperties(entityB, "name").name) + + }); + return arrow }, @@ -364,6 +355,7 @@ }, updateStringPositions: function() { + // print('update string positions!!!') var upVector = Quat.getUp(this.bowProperties.rotation); var upOffset = Vec3.multiply(upVector, TOP_NOTCH_OFFSET); var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); @@ -374,6 +366,11 @@ this.topStringPosition = Vec3.sum(topStringPosition, backOffset); var bottomStringPosition = Vec3.sum(this.bowProperties.position, downOffset); this.bottomStringPosition = Vec3.sum(bottomStringPosition, backOffset); + + Entities.editEntity(this.preNotchString, { + position: this.topStringPosition + }); + Entities.editEntity(this.topString, { position: this.topStringPosition }); @@ -381,9 +378,6 @@ Entities.editEntity(this.bottomString, { position: this.bottomStringPosition }); - Entities.editEntity(this.preNotchString, { - position: this.topStringPosition - }); }, @@ -421,17 +415,12 @@ }, drawStringsBeforePickup: function() { - this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); - - if (this.prePickupString === null) { - this.createPreNotchString(); - - } else { - this.drawPreNotchStrings(); - } + _this.drawPreNotchStrings(); }, createPreNotchString: function() { + this.bowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation", "userData"]); + var stringProperties = { type: 'Line', position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), @@ -448,12 +437,16 @@ }, drawPreNotchStrings: function() { + this.bowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation", "userData"]); + + this.updateStringPositions(); var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); var downOffset = Vec3.multiply(downVector, BOTTOM_NOTCH_OFFSET * 2); Entities.editEntity(this.preNotchString, { + name: 'Hifi-Pre-Notch-String', linePoints: [{ x: 0, y: 0, @@ -464,7 +457,8 @@ z: 0 }, downOffset)], lineWidth: 5, - color: this.stringData.currentColor + color: this.stringData.currentColor, + ignoreForCollisions: true }); }, @@ -490,13 +484,15 @@ if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { // firing the arrow - // print('HIT RELEASE LOOP IN CHECK') - + print('HIT RELEASE LOOP IN CHECK') + this.hasArrowNotched = false; + this.aiming = false; + this.stringDrawn = false; this.releaseArrow(); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - // print('HIT CONTINUE LOOP IN CHECK') + // print('HIT CONTINUE LOOP IN CHECK') this.aiming = true; //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); @@ -602,13 +598,14 @@ var arrowForce = this.scaleArrowShotStrength(pullBackDistance); print('ARROW FORCE::' + arrowForce); // handToNotch = Vec3.normalize(handToNotch) - var forwardVec = handToNotch; - // var forwardVec = Vec3.multiply(handToNotch, arrowForce); - var forwardVec = Vec3.multiply(handToNotch,handToNotch); + //var forwardVec = handToNotch; + var forwardVec = Vec3.multiply(handToNotch, arrowForce); + //var forwardVec = Vec3.multiply(handToNotch, handToNotch); var arrowProperties = { // rotation: arrowRotation, + collisionsWillMove: true, velocity: forwardVec, - lifetime: 10 + // lifetime: 10 }; this.playShootArrowSound(); @@ -617,37 +614,33 @@ var arrowStore = this.arrow; this.arrow = null; - this.hasArrowNotched = false; - this.aiming = false; - Entities.editEntity(this.preNotchString, { visible: true }); - this.stringDrawn = false; this.deleteStrings(); //set an itnerval to check how far the arrow is from the bow before adding gravity, etc. if we add this too soon, the arrow collides with the bow. hence, this function - var physicalArrowInterval = Script.setInterval(function() { - // print('in physical interval') - var arrowProps = Entities.getEntityProperties(arrowStore, "position"); - var bowProps = Entities.getEntityProperties(_this.entityID, "position"); - var arrowPosition = arrowProps.position; - var bowPosition = bowProps.position; + // var physicalArrowInterval = Script.setInterval(function() { + // // print('in physical interval') + // var arrowProps = Entities.getEntityProperties(arrowStore, "position"); + // var bowProps = Entities.getEntityProperties(_this.entityID, "position"); + // var arrowPosition = arrowProps.position; + // var bowPosition = bowProps.position; - var length = Vec3.distance(arrowPosition, bowPosition); - // print('LENGTH:::' + length); - if (length > 2) { - print('make arrow physical' + arrowStore) - Entities.editEntity(arrowStore, { - ignoreForCollisions: false, - collisionsWillMove: true - }); - Script.clearInterval(physicalArrowInterval); - } - }, 5) + // var length = Vec3.distance(arrowPosition, bowPosition); + // // print('LENGTH:::' + length); + // if (length > 2) { + // print('make arrow physical' + arrowStore) + // Entities.editEntity(arrowStore, { + // ignoreForCollisions: false, + // collisionsWillMove: true + // }); + // Script.clearInterval(physicalArrowInterval); + // } + // }, 5) }, From c98bfca359f2359bf77212d40a34ca4192f3b0da Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 20 Nov 2015 15:41:50 -0800 Subject: [PATCH 51/79] collisions --- examples/toybox/bow/bow.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index d8da57412f..3a1c13c531 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -41,7 +41,7 @@ var ARROW_TIP_OFFSET = 0.32; var ARROW_GRAVITY = { x: 0, - y: 0, + y: -4.8, z: 0 }; @@ -285,21 +285,22 @@ dimensions: ARROW_DIMENSIONS, position: this.bowProperties.position, collisionsWillMove: false, - ignoreForCollisions: false, - collisionSoundURL:ARROW_HIT_SOUND_URL, + ignoreForCollisions: true, + collisionSoundURL: ARROW_HIT_SOUND_URL, gravity: ARROW_GRAVITY, damping: 0.01, userData: JSON.stringify({ grabbableKey: { + invertSolidWhileHeld: true, grabbable: false } }) }); var arrowProps = Entities.getEntityProperties(arrow) - Script.addEventHandler(arrow, "collisionWithEntity", function(entityA, entityB, collision) { + Script.addEventHandler(arrow, "collisionWithEntity", function(entityA, entityB, collision) { //have to reverse lookup the tracker by the arrow id to get access to the children - + print('ARROW COLLIDED WITH::' + entityB); print('NAME OF ENTITY:::' + Entities.getEntityProperties(entityB, "name").name) @@ -426,6 +427,8 @@ position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), dimensions: LINE_DIMENSIONS, visible: true, + ignoreForCollisions: true, + collisionsWillMove: false, userData: JSON.stringify({ grabbableKey: { grabbable: false @@ -603,6 +606,7 @@ //var forwardVec = Vec3.multiply(handToNotch, handToNotch); var arrowProperties = { // rotation: arrowRotation, + ignoreForCollisions: false, collisionsWillMove: true, velocity: forwardVec, // lifetime: 10 From c6be37f63f03abddbadcb38c1c6539450142db3e Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 20 Nov 2015 16:10:39 -0800 Subject: [PATCH 52/79] lifetime --- examples/toybox/bow/bow.js | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 3a1c13c531..1947672084 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -461,7 +461,6 @@ }, downOffset)], lineWidth: 5, color: this.stringData.currentColor, - ignoreForCollisions: true }); }, @@ -609,7 +608,7 @@ ignoreForCollisions: false, collisionsWillMove: true, velocity: forwardVec, - // lifetime: 10 + lifetime: 10 }; this.playShootArrowSound(); @@ -625,27 +624,6 @@ this.deleteStrings(); - //set an itnerval to check how far the arrow is from the bow before adding gravity, etc. if we add this too soon, the arrow collides with the bow. hence, this function - - // var physicalArrowInterval = Script.setInterval(function() { - // // print('in physical interval') - // var arrowProps = Entities.getEntityProperties(arrowStore, "position"); - // var bowProps = Entities.getEntityProperties(_this.entityID, "position"); - // var arrowPosition = arrowProps.position; - // var bowPosition = bowProps.position; - - // var length = Vec3.distance(arrowPosition, bowPosition); - // // print('LENGTH:::' + length); - // if (length > 2) { - // print('make arrow physical' + arrowStore) - // Entities.editEntity(arrowStore, { - // ignoreForCollisions: false, - // collisionsWillMove: true - // }); - // Script.clearInterval(physicalArrowInterval); - // } - // }, 5) - }, scaleArrowShotStrength: function(value) { From 06322b1d6b025768f9a721f2290764202898b6ea Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 20 Nov 2015 16:30:05 -0800 Subject: [PATCH 53/79] dont update prenotch for everyone --- examples/toybox/bow/bow.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 1947672084..d1a1ab53b5 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -134,8 +134,7 @@ this.arrowHitSound = SoundCache.getSound(ARROW_HIT_SOUND_URL); this.arrowNotchSound = SoundCache.getSound(NOTCH_ARROW_SOUND_URL); this.arrowWhizzSound = SoundCache.getSound(ARROW_WHIZZ_SOUND_URL); - this.createPreNotchString(); - Script.update.connect(this.drawStringsBeforePickup); + }, @@ -156,7 +155,6 @@ Entities.deleteEntity(this.notchDetector); Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); - Script.update.disconnect(this.drawStringsBeforePickup); }, @@ -214,13 +212,13 @@ // } //create a string across the bow when we pick it up - // if (this.preNotchString === null) { - // this.createPreNotchString(); - // } + if (this.preNotchString === null) { + this.createPreNotchString(); + } if (this.preNotchString !== null && this.aiming === false) { // print('DRAW PRE NOTCH STRING') - // this.drawPreNotchStrings(); + this.drawPreNotchStrings(); // this.updateArrowAttachedToBow(); } @@ -263,12 +261,12 @@ spatialKey: BOW_SPATIAL_KEY }); Entities.deleteEntity(this.notchDetector); - // Entities.deleteEntity(this.preNotchString); + Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); this.aiming = false; this.notchDetector = null; this.hasArrowNotched = false; - // this.preNotchString = null; + this.preNotchString = null; } }, From a65b3b9aaad48008bc9a6da24575aca4ee384b3d Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 20 Nov 2015 17:12:54 -0800 Subject: [PATCH 54/79] collisions --- examples/toybox/bow/bow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index d1a1ab53b5..329373cbc8 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -603,7 +603,7 @@ //var forwardVec = Vec3.multiply(handToNotch, handToNotch); var arrowProperties = { // rotation: arrowRotation, - ignoreForCollisions: false, + ignoreForCollisions: true, collisionsWillMove: true, velocity: forwardVec, lifetime: 10 From b2dd900cb2a275f78bb8e8d1909e7b902e5b1c6e Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 20 Nov 2015 18:12:06 -0800 Subject: [PATCH 55/79] rotate correctly --- examples/toybox/bow/bow.js | 407 ++++++++----------------------------- 1 file changed, 83 insertions(+), 324 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 329373cbc8..8aba2aecb0 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -86,7 +86,7 @@ min1: 0, max1: 0.6, min2: 1, - max2: 10 + max2: 5 } var BOW_SPATIAL_KEY = { @@ -98,7 +98,18 @@ relativeRotation: Quat.fromPitchYawRollDegrees(0, -90, 90) } - var arrowTrackers = []; + function interval() { + var lastTime = new Date().getTime() / 1000; + + return function getInterval() { + var newTime = new Date().getTime() / 1000; + var delta = newTime - lastTime; + lastTime = newTime; + return delta; + }; + } + + var checkInterval = interval(); var _this; @@ -116,7 +127,6 @@ hasArrowNotched: false, notchDetector: null, arrow: null, - arrowIsBurning: false, prePickupString: null, stringData: { currentColor: { @@ -128,29 +138,15 @@ preload: function(entityID) { print('preload bow') this.entityID = entityID; - this.bowID = entityID; this.stringPullSound = SoundCache.getSound(STRING_PULL_SOUND_URL); this.shootArrowSound = SoundCache.getSound(SHOOT_ARROW_SOUND_URL); this.arrowHitSound = SoundCache.getSound(ARROW_HIT_SOUND_URL); this.arrowNotchSound = SoundCache.getSound(NOTCH_ARROW_SOUND_URL); this.arrowWhizzSound = SoundCache.getSound(ARROW_WHIZZ_SOUND_URL); - }, unload: function() { - // Script.update.disconnect(this.updateArrowTrackers); - - // while (arrowTrackers.length > 0) { - // var tracker = arrowTrackers.pop(); - // tracker.childEntities.forEach(function(child) { - // Entities.deleteEntity(child); - // }) - // tracker.childParticleSystems.forEach(function(child) { - // Entities.deleteEntity(child); - // }) - // Entities.deleteEntity(tracker.arrowID); - // } this.deleteStrings(); Entities.deleteEntity(this.notchDetector); Entities.deleteEntity(this.preNotchString); @@ -186,30 +182,23 @@ }); }, - continueNearGrab: function() { + var deltaTime = checkInterval(); + // print('DT:::'+deltaTime) + + // print('collidable bow' + Entities.getEntityProperties(this.entityID, "collisionsWillMove").collisionsWillMove) // print('collidable arrow' + Entities.getEntityProperties(this.arrow, "collisionsWillMove").collisionsWillMove) // print('collidable notch' + Entities.getEntityProperties(this.notchDetector, "collisionsWillMove").collisionsWillMove) // print('collidable topstring' + Entities.getEntityProperties(this.topString, "collisionsWillMove").collisionsWillMove) // print('collidable bottomstring' + Entities.getEntityProperties(this.bottomString, "collisionsWillMove").collisionsWillMove) // print('collidable prenotchstring' + Entities.getEntityProperties(this.preNotchString, "collisionsWillMove").collisionsWillMove) + Script.setTimeout(function() { + _this.bowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation", "userData"]); + }, 0) - this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); - - this.updateNotchDetectorPosition(); - - // if (this.hasArrowNotched === false) { - // this.hasArrowNotched = true - - // this.arrowIsBurning = false - // setEntityCustomData('grabbableKey', this.entityID, { - // turnOffOtherHand: true, - // invertSolidWhileHeld: true, - // spatialKey: BOW_SPATIAL_KEY - // }); - // } + //this.updateNotchDetectorPosition(); //create a string across the bow when we pick it up if (this.preNotchString === null) { @@ -218,7 +207,7 @@ if (this.preNotchString !== null && this.aiming === false) { // print('DRAW PRE NOTCH STRING') - this.drawPreNotchStrings(); + this.drawPreNotchStrings(); // this.updateArrowAttachedToBow(); } @@ -227,8 +216,6 @@ this.createNotchDetector(); } - //if we have an arrow notched, then draw some new strings - // if (this.hasArrowNotched === true) { if (this.aiming === true) { Entities.editEntity(this.preNotchString, { visible: false @@ -238,15 +225,9 @@ visible: true }) } - //only test for strings now that an arrow is notched - this.checkStringHand(); + this.checkStringHand(deltaTime); - // } else { - // // print('DONT DO ANYTHING') - // //otherwise, don't do much of anything. - - // } }, releaseGrab: function() { @@ -261,12 +242,12 @@ spatialKey: BOW_SPATIAL_KEY }); Entities.deleteEntity(this.notchDetector); - Entities.deleteEntity(this.preNotchString); + Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); this.aiming = false; this.notchDetector = null; this.hasArrowNotched = false; - this.preNotchString = null; + this.preNotchString = null; } }, @@ -462,7 +443,7 @@ }); }, - checkStringHand: function() { + checkStringHand: function(deltaTime) { //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with if (this.initialHand === 'left') { this.getStringHandPosition = MyAvatar.getRightPalmPosition; @@ -489,10 +470,10 @@ this.hasArrowNotched = false; this.aiming = false; this.stringDrawn = false; - this.releaseArrow(); + this.releaseArrow(deltaTime); } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - // print('HIT CONTINUE LOOP IN CHECK') + print('HIT CONTINUE LOOP IN CHECK') this.aiming = true; //continuing to aim the arrow this.stringData.handPosition = this.getStringHandPosition(); @@ -505,21 +486,17 @@ } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { this.arrow = this.createArrow(); print('CREATE ARROW' + this.arrow); - // print('HIT START LOOP IN CHECK') + print('HIT START LOOP IN CHECK') this.playStringPullSound(); //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); - // var arrowTracker = this.createArrowTracker(this.arrow); - // arrowTrackers.push(arrowTracker) this.stringData.handPosition = this.getStringHandPosition(); this.stringData.handRotation = this.getStringHandRotation(); this.stringData.grabHandPosition = this.getGrabHandPosition(); - this.stringData.grabHandRotation = this.getGrabHandRotation(); - this.drawStrings(); this.updateArrowPositionInNotch(); @@ -550,16 +527,19 @@ return arrowPosition; }, - updateArrowAttachedToBow: function() { - Entities.editEntity(this.arrow, { - position: this.notchDetectorPosition, - rotation: this.bowProperties.rotation - }) - }, - updateArrowPositionInNotch: function() { - //move it backwards - var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); + print('UPDATE ARROW POS') + //move it backwards + var detectorPosition; + var frontVector = Quat.getFront(this.bowProperties.rotation); + var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); + var upVector = Quat.getUp(this.bowProperties.rotation); + var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); + + detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); + detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); + + var handToNotch = Vec3.subtract(detectorPosition, this.stringData.handPosition); var pullBackDistance = Vec3.length(handToNotch); if (pullBackDistance >= 0.6) { @@ -567,7 +547,7 @@ } var pullBackOffset = Vec3.multiply(handToNotch, -pullBackDistance); - var arrowPosition = Vec3.sum(this.notchDetectorPosition, pullBackOffset); + var arrowPosition = Vec3.sum(detectorPosition, pullBackOffset); this.changeStringPullSoundVolume(pullBackDistance); //move it forward a bit var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); @@ -581,28 +561,31 @@ rotation: arrowRotation }) + this.localArrowProperties = { + handToNotch: handToNotch, + pullBackDistance: pullBackDistance, + position: finalArrowPosition, + rotation: arrowRotation + } }, releaseArrow: function() { print('RELEASE ARROW!!!') - var handToNotch = Vec3.subtract(this.notchDetectorPosition, this.stringData.handPosition); - var pullBackDistance = Vec3.length(handToNotch); - // if (pullBackDistance >= 0.6) { - // pullBackDistance = 0.6; - // } + var arrowProps = Entities.getEntityProperties(this.arrow); + var handToNotch = this.localArrowProperties.handToNotch; + var pullBackDistance = this.localArrowProperties.pullBackDistance; + var arrowRotation = this.localArrowProperties.arrowRotation; - var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); - - print('HAND DISTANCE:: ' + pullBackDistance); var arrowForce = this.scaleArrowShotStrength(pullBackDistance); - print('ARROW FORCE::' + arrowForce); - // handToNotch = Vec3.normalize(handToNotch) + + handToNotch = Vec3.normalize(handToNotch) //var forwardVec = handToNotch; var forwardVec = Vec3.multiply(handToNotch, arrowForce); //var forwardVec = Vec3.multiply(handToNotch, handToNotch); + var arrowProperties = { - // rotation: arrowRotation, + rotation: arrowRotation, ignoreForCollisions: true, collisionsWillMove: true, velocity: forwardVec, @@ -632,6 +615,18 @@ return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); }, + returnNotchPosition: function() { + var detectorPosition; + var frontVector = Quat.getFront(this.bowProperties.rotation); + var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); + var upVector = Quat.getUp(this.bowProperties.rotation); + var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); + + detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); + detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); + return detectorPosition + }, + createNotchDetector: function() { print('CREATE NOTCH DETECTOR') var detectorPosition; @@ -661,251 +656,25 @@ this.notchDetector = Entities.addEntity(detectorProperties); }, - updateNotchDetectorPosition: function() { - var detectorPosition; - var frontVector = Quat.getFront(this.bowProperties.rotation); - var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); - var upVector = Quat.getUp(this.bowProperties.rotation); - var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); + // updateNotchDetectorPosition: function() { + // print('UPDATE NOTCH POS') + // var detectorPosition; + // var frontVector = Quat.getFront(this.bowProperties.rotation); + // var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); + // var upVector = Quat.getUp(this.bowProperties.rotation); + // var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); - detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); - detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); + // detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); + // detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); - this.notchDetectorPosition = detectorPosition; + // this.notchDetectorPosition = detectorPosition; - Entities.editEntity(this.notchDetector, { - position: this.notchDetectorPosition, - rotation: this.bowProperties.rotation - }); - }, + // Entities.editEntity(this.notchDetector, { + // position: this.notchDetectorPosition, + // rotation: this.bowProperties.rotation + // }); + // }, - createFireParticleSystem: function() { - print('MAKING A FIRE') - //will probably need to dynamically update the orientation to match the arrow??? - var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); - - var animationSettings = JSON.stringify({ - fps: 30, - running: true, - loop: true, - firstFrame: 1, - lastFrame: 10000 - }); - - var fire = Entities.addEntity({ - type: "ParticleEffect", - name: "Hifi-Arrow-Fire", - animationSettings: animationSettings, - textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", - emitRate: 100, - position: MyAvatar.position, - colorStart: { - red: 70, - green: 70, - blue: 137 - }, - color: { - red: 200, - green: 99, - blue: 42 - }, - colorFinish: { - red: 255, - green: 99, - blue: 32 - }, - radiusSpread: 0.01, - radiusStart: 0.02, - radiusEnd: 0.001, - particleRadius: 0.15, - radiusFinish: 0.0, - emitOrientation: myOrientation, - emitSpeed: 0.3, - speedSpread: 0.1, - alphaStart: 0.05, - alpha: 0.1, - alphaFinish: 0.05, - emitDimensions: { - x: 0.5, - y: 0.5, - z: 0.1 - }, - polarFinish: 0.1, - emitAcceleration: { - x: 0.0, - y: 0.0, - z: 0.0 - }, - accelerationSpread: { - x: 0.1, - y: 0.01, - z: 0.1 - }, - lifespan: 0.5, - ignoreForCollisions: true, - collisionsWillMove: false, - }); - - return fire; - - }, - createArrowTracker: function(arrow, isBurning) { - print('in create arrow tracker:::' + arrow) - var _t = this; - - // var isBurning = this.arrowIsBurning; - //delete this line below once debugging is done - var isBurning = isBurning || true; - var arrowTracker = { - arrowID: arrow, - // whizzingSound: _t.playWhizzSound(), - //fireSound: _t.createFireSound(), - hasPlayedCollisionSound: false, - glowBox: _t.createGlowBoxAsModel(), - fireParticleSystem: _t.createFireParticleSystem(), - childEntities: [], - childSounds: [], - childParticleSystems: [], - init: function() { - print('init arrow tracker') - this.setChildren(); - - }, - setCollisionCallback: function() { - print('set arrow tracker callback' + this.arrowID) - - Script.addEventHandler(this.arrowID, "collisionWithEntity", function(entityA, entityB, collision) { - //have to reverse lookup the tracker by the arrow id to get access to the children - var tracker = getArrowTrackerByArrowID(entityA); - Entities.deleteEntity(tracker.glowBox); - - }); - }, - setChildren: function() { - print('set arrow tracker children') - // this.childSounds.push(this.whizzingSound); - // this.childSounds.push(this.fireSound); - this.childEntities.push(this.glowBox); - this.childParticleSystems.push(this.fireParticleSystem); - }, - updateChildEntities: function(arrowID) { - var arrowProperties = Entities.getEntityProperties(this.arrowID, ["position", "rotation"]); - _t.setArrowTipPosition(arrowProperties.position, arrowProperties.rotation); - _t.setArrowRearPosition(arrowProperties.position, arrowProperties.rotation); - //update the positions - // this.soundEntities.forEach(function(injector) { - // var audioProperties = { - // volume: 0.25, - // position: arrowProperties.position - // }; - // injector.options = audioProperties; - // }) - - this.childEntities.forEach(function(child) { - Entities.editEntity(child, { - position: arrowProperties.position, - rotation: arrowProperties.rotation - }) - }) - - this.childParticleSystems.forEach(function(child) { - Entities.editEntity(child, { - position: _t.arrowTipPosition - }) - }) - - } - }; - arrowTracker.init(); - arrowTracker.setCollisionCallback(); - - return arrowTracker - }, - createFireBurningSound: function() { - var audioProperties = { - volume: 0.25, - position: this.bowProperties.position, - loop: true - }; - - var injector = Audio.playSound(this.fireBurningSound, audioProperties); - - return injector - }, - createGlowBoxAsModel: function() { - var GLOW_MODEL_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/glow.fbx'; - var properties = { - name: 'Hifi-Arrow-Glow-Model', - type: 'Model', - modelURL: GLOW_MODEL_URL, - dimensions: ARROW_DIMENSIONS, - collisionsWillMove: false, - ignoreForCollisions: true, - userData: JSON.stringify({ - grabbableKey: { - grabbable: false - } - }) - } - var glowBox = Entities.addEntity(properties); - return glowBox - }, - createGlowBox: function() { - print('creating glow box') - var shaderUrl = Script.resolvePath('../../shaders/exampleV2.fs'); - var properties = { - name: 'Hifi-Arrow-Glow', - type: 'Box', - dimensions: ARROW_DIMENSIONS, - collisionsWillMove: false, - ignoreForCollisions: true, - color: { - red: 255, - green: 0, - blue: 255 - }, - //i want to use shader but for some reason its white... need to fix; - - - // userData: JSON.stringify({ - // "grabbableKey": { - // grabbable: false - // }, - // "ProceduralEntity": { - // "shaderUrl": shaderUrl, - // // V2 and onwards, shaders must include a version identifier, or they will default - // // to V1 behavior - // "version": 2, - // // Any values specified here will be passed on to uniforms with matching names in - // // the shader. Only numbers and arrays of length 1-4 of numbers are supported. - // // - // // The size of the data must match the size of the uniform: - // // a number or 1 value array = 'uniform float' - // // 2 value array = 'uniform vec2' - // // 3 value array = 'uniform vec3' - // // 4 value array = 'uniform vec4' - // // - // // Uniforms should always be declared in the shader with a default value - // // or failure to specify the value here will result in undefined behavior. - // "uniforms": { - // // uniform float iSpeed = 1.0; - // "iSpeed": 2.0, - // // uniform vec3 iSize = vec3(1.0); - // "iSize": [1.0, 2.0, 4.0] - // } - // } - // }) - } - var glowBox = Entities.addEntity(properties); - return glowBox - }, - updateArrowTrackers: function() { - // print('updating arrow trackers:::' + arrowTrackers.length); - arrowTrackers.forEach(function(tracker) { - var arrowID = tracker.arrowID; - tracker.updateChildEntities(arrowID); - }) - }, playStringPullSound: function() { var audioProperties = { volume: 0.25, @@ -935,16 +704,6 @@ }; Audio.playSound(this.arrowNotchSound, audioProperties); }, - playArrowWhizzSound: function() { - - var audioProperties = { - volume: 0.0, - position: this.bowProperties.position - }; - var injector = Audio.playSound(this.arrowWhizzSound, audioProperties); - - return injector - }, changeStringPullSoundVolume: function(pullBackDistance) { var audioProperties = { volume: 0.25, From f56c4ff575b57a2c51eba9e5c32efd3b66d6707a Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 20 Nov 2015 21:14:28 -0800 Subject: [PATCH 56/79] still debugging --- examples/controllers/handControllerGrab.js | 14 +- examples/toybox/bow/bow.js | 237 ++++++--------------- examples/toybox/bow/createBow.js | 2 +- 3 files changed, 81 insertions(+), 172 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index d7a3d05416..61368ce349 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -758,14 +758,20 @@ function MyController(hand) { this.nearGrabbing = function() { var now = Date.now(); - + print('HAND IN NEAR GRAB:::'+this.hand) var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); var turnOffOtherHand = grabbableData["turnOffOtherHand"]; - if (turnOffOtherHand) { - //don't activate the second hand grab because the script is handling the second hand logic - return; + print('TURN OFF OTHER HAND??'+turnOffOtherHand); + if(turnOffOtherHand==='left' && this.hand ===1){ + print('IGNORE RIGHT') + return } + if(turnOffOtherHand==='right'&&this.hand===0){ + print('IGNORE LEFT') + return + } + if (this.state == STATE_NEAR_GRABBING && this.triggerSmoothedReleased()) { this.setState(STATE_RELEASE); diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 8aba2aecb0..2db5b5ab04 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -86,7 +86,7 @@ min1: 0, max1: 0.6, min2: 1, - max2: 5 + max2: 15 } var BOW_SPATIAL_KEY = { @@ -98,11 +98,12 @@ relativeRotation: Quat.fromPitchYawRollDegrees(0, -90, 90) } + function interval() { - var lastTime = new Date().getTime() / 1000; + var lastTime = new Date().getTime(); return function getInterval() { - var newTime = new Date().getTime() / 1000; + var newTime = new Date().getTime(); var delta = newTime - lastTime; lastTime = newTime; return delta; @@ -125,7 +126,6 @@ arrowTipPosition: null, preNotchString: null, hasArrowNotched: false, - notchDetector: null, arrow: null, prePickupString: null, stringData: { @@ -148,7 +148,6 @@ unload: function() { this.deleteStrings(); - Entities.deleteEntity(this.notchDetector); Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); @@ -176,29 +175,22 @@ this.initialHand = this.hand; setEntityCustomData('grabbableKey', this.entityID, { - turnOffOtherHand: true, + turnOffOtherHand: this.initialHand, invertSolidWhileHeld: true, spatialKey: BOW_SPATIAL_KEY }); }, continueNearGrab: function() { - - var deltaTime = checkInterval(); - // print('DT:::'+deltaTime) - + this.deltaTime = checkInterval(); // print('collidable bow' + Entities.getEntityProperties(this.entityID, "collisionsWillMove").collisionsWillMove) // print('collidable arrow' + Entities.getEntityProperties(this.arrow, "collisionsWillMove").collisionsWillMove) - // print('collidable notch' + Entities.getEntityProperties(this.notchDetector, "collisionsWillMove").collisionsWillMove) // print('collidable topstring' + Entities.getEntityProperties(this.topString, "collisionsWillMove").collisionsWillMove) // print('collidable bottomstring' + Entities.getEntityProperties(this.bottomString, "collisionsWillMove").collisionsWillMove) // print('collidable prenotchstring' + Entities.getEntityProperties(this.preNotchString, "collisionsWillMove").collisionsWillMove) - Script.setTimeout(function() { - _this.bowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation", "userData"]); - }, 0) - //this.updateNotchDetectorPosition(); + this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); //create a string across the bow when we pick it up if (this.preNotchString === null) { @@ -212,9 +204,6 @@ } // create the notch detector that arrows will look for - if (this.notchDetector === null) { - this.createNotchDetector(); - } if (this.aiming === true) { Entities.editEntity(this.preNotchString, { @@ -226,7 +215,7 @@ }) } - this.checkStringHand(deltaTime); + this.checkStringHand(); }, @@ -241,11 +230,9 @@ invertSolidWhileHeld: true, spatialKey: BOW_SPATIAL_KEY }); - Entities.deleteEntity(this.notchDetector); Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); this.aiming = false; - this.notchDetector = null; this.hasArrowNotched = false; this.preNotchString = null; @@ -443,19 +430,13 @@ }); }, - checkStringHand: function(deltaTime) { + checkStringHand: function() { //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with if (this.initialHand === 'left') { this.getStringHandPosition = MyAvatar.getRightPalmPosition; - this.getStringHandRotation = MyAvatar.getRightPalmRotation; - this.getGrabHandPosition = MyAvatar.getLeftPalmPosition; - this.getGrabHandRotation = MyAvatar.getLeftPalmRotation; this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); } else if (this.initialHand === 'right') { this.getStringHandPosition = MyAvatar.getLeftPalmPosition; - this.getStringHandRotation = MyAvatar.getLeftPalmRotation; - this.getGrabHandPosition = MyAvatar.getRightPalmPosition; - this.getGrabHandRotation = MyAvatar.getRightPalmRotation; this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); } @@ -466,39 +447,28 @@ // firing the arrow print('HIT RELEASE LOOP IN CHECK') - + this.updateArrowPositionInNotch(true); this.hasArrowNotched = false; this.aiming = false; this.stringDrawn = false; - this.releaseArrow(deltaTime); - } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - print('HIT CONTINUE LOOP IN CHECK') + } else if (this.triggerValue > DRAW_STRING_THRESHOLD && this.stringDrawn === true) { + // print('HIT CONTINUE LOOP IN CHECK') this.aiming = true; //continuing to aim the arrow - this.stringData.handPosition = this.getStringHandPosition(); - this.stringData.handRotation = this.getStringHandRotation(); - this.stringData.grabHandPosition = this.getGrabHandPosition(); - this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); this.updateArrowPositionInNotch(); - } else if (this.triggerValue >= DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + } else if (this.triggerValue > DRAW_STRING_THRESHOLD && this.stringDrawn === false) { this.arrow = this.createArrow(); - print('CREATE ARROW' + this.arrow); print('HIT START LOOP IN CHECK') this.playStringPullSound(); //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); - - this.stringData.handPosition = this.getStringHandPosition(); - this.stringData.handRotation = this.getStringHandRotation(); - this.stringData.grabHandPosition = this.getGrabHandPosition(); - this.stringData.grabHandRotation = this.getGrabHandRotation(); this.drawStrings(); - this.updateArrowPositionInNotch(); + // this.updateArrowPositionInNotch(); } }, @@ -519,91 +489,84 @@ return arrowTipPosition; }, - getArrowPosition: function() { - var arrowVector = Vec3.subtract(this.stringData.handPosition, this.stringData.grabHandPosition); - arrowVector = Vec3.normalize(arrowVector); - arrowVector = Vec3.multiply(arrowVector, ARROW_OFFSET); - var arrowPosition = Vec3.sum(this.stringData.grabHandPosition, arrowVector); - return arrowPosition; - }, - updateArrowPositionInNotch: function() { - print('UPDATE ARROW POS') - //move it backwards - var detectorPosition; - var frontVector = Quat.getFront(this.bowProperties.rotation); + updateArrowPositionInNotch: function(shouldReleaseArrow) { + var stringHandPosition = this.getStringHandPosition(); + var bowProperties = Entities.getEntityProperties(this.entityID); + + var notchPosition; + var frontVector = Quat.getFront(bowProperties.rotation); var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); - var upVector = Quat.getUp(this.bowProperties.rotation); + var upVector = Quat.getUp(bowProperties.rotation); var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); - detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); - detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); + notchPosition = Vec3.sum(bowProperties.position, notchVectorForward); + notchPosition = Vec3.sum(notchPosition, notchVectorUp); - var handToNotch = Vec3.subtract(detectorPosition, this.stringData.handPosition); - var pullBackDistance = Vec3.length(handToNotch); + var handToNotch = Vec3.subtract(notchPosition, stringHandPosition); - if (pullBackDistance >= 0.6) { - pullBackDistance = 0.6; - } - var pullBackOffset = Vec3.multiply(handToNotch, -pullBackDistance); - var arrowPosition = Vec3.sum(detectorPosition, pullBackOffset); - this.changeStringPullSoundVolume(pullBackDistance); + // var pullBackDistance = Vec3.length(handToNotch); + + // if (pullBackDistance >= 0.6) { + // pullBackDistance = 0.6; + // } + + pullBackDistance = 0.5; + + // var pullBackOffset = Vec3.multiply(handToNotch, -pullBackDistance); + // var arrowPosition = Vec3.sum(detectorPosition, pullBackOffset); + //move it forward a bit - var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); - var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); + // var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); + // var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); - this.setArrowTipPosition(finalArrowPosition, arrowRotation); - this.setArrowRearPosition(finalArrowPosition, arrowRotation); - Entities.editEntity(this.arrow, { - position: finalArrowPosition, - rotation: arrowRotation - }) + var handToNotch = Vec3.normalize(handToNotch); + this.setArrowTipPosition(notchPosition, arrowRotation); + this.setArrowRearPosition(notchPosition, arrowRotation); - this.localArrowProperties = { - handToNotch: handToNotch, - pullBackDistance: pullBackDistance, - position: finalArrowPosition, - rotation: arrowRotation + // this.pullBackDistance = pullBackDistance; + if (shouldReleaseArrow !== true) { + Entities.editEntity(this.arrow, { + position: notchPosition, + rotation: arrowRotation + }) } - }, - releaseArrow: function() { - print('RELEASE ARROW!!!') + if (shouldReleaseArrow === true) { - var arrowProps = Entities.getEntityProperties(this.arrow); - var handToNotch = this.localArrowProperties.handToNotch; - var pullBackDistance = this.localArrowProperties.pullBackDistance; - var arrowRotation = this.localArrowProperties.arrowRotation; + var forwardVec = Vec3.multiply(handToNotch, 1 / this.deltaTime); + var arrowForce = this.scaleArrowShotStrength(0.5) + var forwardVec = Vec3.multiply(forwardVec, arrowForce) - var arrowForce = this.scaleArrowShotStrength(pullBackDistance); + // var velocity = Quat.getFront(bowProperties.rotation) - handToNotch = Vec3.normalize(handToNotch) - //var forwardVec = handToNotch; - var forwardVec = Vec3.multiply(handToNotch, arrowForce); - //var forwardVec = Vec3.multiply(handToNotch, handToNotch); + var arrowProperties = { + ignoreForCollisions: true, + // collisionsWillMove: true, + velocity: forwardVec, + lifetime: 10 + }; - var arrowProperties = { - rotation: arrowRotation, - ignoreForCollisions: true, - collisionsWillMove: true, - velocity: forwardVec, - lifetime: 10 - }; + this.playShootArrowSound(); - this.playShootArrowSound(); + Entities.editEntity(this.arrow, arrowProperties); - Entities.editEntity(this.arrow, arrowProperties); - var arrowStore = this.arrow; - this.arrow = null; - Entities.editEntity(this.preNotchString, { - visible: true - }); + Entities.editEntity(this.preNotchString, { + visible: true + }); + + this.deleteStrings(); + + var afterVelocity = Entities.getEntityProperties(this.arrow).velocity; + print('VELOCITY AT RELEASE:::' + JSON.stringify(afterVelocity)) + + // this.arrow = null; + } - this.deleteStrings(); }, @@ -615,66 +578,6 @@ return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); }, - returnNotchPosition: function() { - var detectorPosition; - var frontVector = Quat.getFront(this.bowProperties.rotation); - var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); - var upVector = Quat.getUp(this.bowProperties.rotation); - var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); - - detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); - detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); - return detectorPosition - }, - - createNotchDetector: function() { - print('CREATE NOTCH DETECTOR') - var detectorPosition; - var frontVector = Quat.getFront(this.bowProperties.rotation); - var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); - var upVector = Quat.getUp(this.bowProperties.rotation); - var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); - - detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); - detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); - - var detectorProperties = { - name: 'Hifi-NotchDetector', - type: 'Box', - visible: false, - collisionsWillMove: false, - ignoreForCollisions: true, - dimensions: NOTCH_DETECTOR_DIMENSIONS, - position: detectorPosition, - color: { - red: 0, - green: 255, - blue: 0 - } - }; - - this.notchDetector = Entities.addEntity(detectorProperties); - }, - - // updateNotchDetectorPosition: function() { - // print('UPDATE NOTCH POS') - // var detectorPosition; - // var frontVector = Quat.getFront(this.bowProperties.rotation); - // var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); - // var upVector = Quat.getUp(this.bowProperties.rotation); - // var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); - - // detectorPosition = Vec3.sum(this.bowProperties.position, notchVectorForward); - // detectorPosition = Vec3.sum(detectorPosition, notchVectorUp); - - // this.notchDetectorPosition = detectorPosition; - - // Entities.editEntity(this.notchDetector, { - // position: this.notchDetectorPosition, - // rotation: this.bowProperties.rotation - // }); - // }, - playStringPullSound: function() { var audioProperties = { volume: 0.25, diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index 33ddd58147..562384bee3 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -11,7 +11,7 @@ // -var SCRIPT_URL = Script.resolvePath('bow.js'); +var SCRIPT_URL = Script.resolvePath('bow.js?'+Math.random()); var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow-deadly.fbx"; var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow_collision_hull.obj"; From 945243306b47d1bf8f1bd15fa48e0aec3963d4fa Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Sat, 21 Nov 2015 01:47:20 -0800 Subject: [PATCH 57/79] cleanup --- examples/controllers/handControllerGrab.js | 17 ++- examples/toybox/bow/bow.js | 169 ++++++++------------- examples/toybox/bow/createBow.js | 3 +- examples/toybox/bow/createFireSources.js | 140 ----------------- examples/toybox/bow/fieldSpawner.js | 106 ------------- 5 files changed, 74 insertions(+), 361 deletions(-) delete mode 100644 examples/toybox/bow/createFireSources.js delete mode 100644 examples/toybox/bow/fieldSpawner.js diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 61368ce349..198e059ad2 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -37,6 +37,7 @@ var BUMPER_ON_VALUE = 0.5; var DISTANCE_HOLDING_RADIUS_FACTOR = 5; // multiplied by distance between hand and object var DISTANCE_HOLDING_ACTION_TIMEFRAME = 0.1; // how quickly objects move to their new position var DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR = 2.0; // object rotates this much more than hand did + var NO_INTERSECT_COLOR = { red: 10, green: 10, @@ -86,6 +87,7 @@ var ZERO_VEC = { y: 0, z: 0 }; + var NULL_ACTION_ID = "{00000000-0000-0000-000000000000}"; var MSEC_PER_SEC = 1000.0; @@ -95,7 +97,8 @@ var ACTION_TTL = 15; // seconds var ACTION_TTL_REFRESH = 5; var PICKS_PER_SECOND_PER_HAND = 5; var MSECS_PER_SEC = 1000.0; -var GRABBABLE_PROPERTIES = ["position", +var GRABBABLE_PROPERTIES = [ + "position", "rotation", "gravity", "ignoreForCollisions", @@ -104,7 +107,6 @@ var GRABBABLE_PROPERTIES = ["position", "name" ]; - var GRABBABLE_DATA_KEY = "grabbableKey"; // shared with grab.js var GRAB_USER_DATA_KEY = "grabKey"; // shared with grab.js @@ -115,7 +117,6 @@ var DEFAULT_GRABBABLE_DATA = { var disabledHand = 'none'; - // states for the state machine var STATE_OFF = 0; var STATE_SEARCHING = 1; @@ -461,7 +462,7 @@ function MyController(hand) { //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"]) { + if (grabbableData["turnOffOppositeBeam"]===true) { if (this.hand === RIGHT_HAND) { disabledHand = LEFT_HAND; } else { @@ -758,16 +759,16 @@ function MyController(hand) { this.nearGrabbing = function() { var now = Date.now(); - print('HAND IN NEAR GRAB:::'+this.hand) + print('HAND IN NEAR GRAB:::' + this.hand) var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); var turnOffOtherHand = grabbableData["turnOffOtherHand"]; - print('TURN OFF OTHER HAND??'+turnOffOtherHand); - if(turnOffOtherHand==='left' && this.hand ===1){ + print('TURN OFF OTHER HAND??' + turnOffOtherHand); + if (turnOffOtherHand === 'left' && this.hand === 1) { print('IGNORE RIGHT') return } - if(turnOffOtherHand==='right'&&this.hand===0){ + if (turnOffOtherHand === 'right' && this.hand === 0) { print('IGNORE LEFT') return } diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 2db5b5ab04..7938eb6951 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -32,9 +32,9 @@ }; var LINE_ENTITY_DIMENSIONS = { - x: 1000, - y: 1000, - z: 1000 + x: 10, + y: 10, + z: 10 }; var ARROW_OFFSET = -0.36; @@ -66,21 +66,11 @@ var DRAW_STRING_THRESHOLD = 0.80; - var TARGET_LINE_LENGTH = 1; - var LEFT_TIP = 1; var RIGHT_TIP = 3; - var NOTCH_DETECTOR_OFFSET_FORWARD = 0.08; - var NOTCH_DETECTOR_OFFSET_UP = 0.035; - - var NOTCH_DETECTOR_DIMENSIONS = { - x: 0.05, - y: 0.05, - z: 0.05 - }; - - var NOTCH_DETECTOR_DISTANCE = 0.1; + var NOTCH_OFFSET_FORWARD = 0.08; + var NOTCH_OFFSET_UP = 0.035; var SHOT_SCALE = { min1: 0, @@ -136,7 +126,7 @@ } }, preload: function(entityID) { - print('preload bow') + print('preload bow'); this.entityID = entityID; this.stringPullSound = SoundCache.getSound(STRING_PULL_SOUND_URL); this.shootArrowSound = SoundCache.getSound(SHOOT_ARROW_SOUND_URL); @@ -150,7 +140,6 @@ this.deleteStrings(); Entities.deleteEntity(this.preNotchString); Entities.deleteEntity(this.arrow); - }, setLeftHand: function() { @@ -177,20 +166,14 @@ setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: this.initialHand, invertSolidWhileHeld: true, + turnOffOppositebeam: true, spatialKey: BOW_SPATIAL_KEY }); }, continueNearGrab: function() { this.deltaTime = checkInterval(); - - // print('collidable bow' + Entities.getEntityProperties(this.entityID, "collisionsWillMove").collisionsWillMove) - // print('collidable arrow' + Entities.getEntityProperties(this.arrow, "collisionsWillMove").collisionsWillMove) - // print('collidable topstring' + Entities.getEntityProperties(this.topString, "collisionsWillMove").collisionsWillMove) - // print('collidable bottomstring' + Entities.getEntityProperties(this.bottomString, "collisionsWillMove").collisionsWillMove) - // print('collidable prenotchstring' + Entities.getEntityProperties(this.preNotchString, "collisionsWillMove").collisionsWillMove) - - this.bowProperties = Entities.getEntityProperties(this.entityID, ["position", "rotation", "userData"]); + this.bowProperties = Entities.getEntityProperties(this.entityID); //create a string across the bow when we pick it up if (this.preNotchString === null) { @@ -228,6 +211,7 @@ setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, invertSolidWhileHeld: true, + turnOffOppositebeam: true, spatialKey: BOW_SPATIAL_KEY }); Entities.deleteEntity(this.preNotchString); @@ -253,12 +237,12 @@ collisionsWillMove: false, ignoreForCollisions: true, collisionSoundURL: ARROW_HIT_SOUND_URL, - gravity: ARROW_GRAVITY, damping: 0.01, userData: JSON.stringify({ grabbableKey: { - invertSolidWhileHeld: true, grabbable: false + //shouldnbt need this but just in case + invertSolidWhileHeld: true, } }) @@ -381,10 +365,6 @@ return [topVector, bottomVector]; }, - drawStringsBeforePickup: function() { - _this.drawPreNotchStrings(); - }, - createPreNotchString: function() { this.bowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation", "userData"]); @@ -393,8 +373,8 @@ position: Vec3.sum(this.bowProperties.position, TOP_NOTCH_OFFSET), dimensions: LINE_DIMENSIONS, visible: true, - ignoreForCollisions: true, collisionsWillMove: false, + ignoreForCollisions: true, userData: JSON.stringify({ grabbableKey: { grabbable: false @@ -408,7 +388,6 @@ drawPreNotchStrings: function() { this.bowProperties = Entities.getEntityProperties(_this.entityID, ["position", "rotation", "userData"]); - this.updateStringPositions(); var downVector = Vec3.multiply(-1, Quat.getUp(this.bowProperties.rotation)); @@ -441,12 +420,11 @@ } this.triggerValue = Controller.getActionValue(this.stringTriggerAction); - // print('TRIGGER VALUE:::' + this.triggerValue) + // print('TRIGGER VALUE:::' + this.triggerValue); if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - // firing the arrow - print('HIT RELEASE LOOP IN CHECK') + print('HIT RELEASE LOOP IN CHECK'); this.updateArrowPositionInNotch(true); this.hasArrowNotched = false; this.aiming = false; @@ -454,79 +432,68 @@ } else if (this.triggerValue > DRAW_STRING_THRESHOLD && this.stringDrawn === true) { // print('HIT CONTINUE LOOP IN CHECK') - this.aiming = true; //continuing to aim the arrow + + this.aiming = true; this.drawStrings(); this.updateArrowPositionInNotch(); } else if (this.triggerValue > DRAW_STRING_THRESHOLD && this.stringDrawn === false) { + print('HIT START LOOP IN CHECK'); this.arrow = this.createArrow(); - print('HIT START LOOP IN CHECK') this.playStringPullSound(); //the first time aiming the arrow this.stringDrawn = true; this.createStrings(); this.drawStrings(); - // this.updateArrowPositionInNotch(); + this.updateArrowPositionInNotch(); } }, - setArrowTipPosition: function(arrowPosition, arrowRotation) { - var frontVector = Quat.getFront(arrowRotation); - var frontOffset = Vec3.multiply(frontVector, ARROW_TIP_OFFSET); - var arrowTipPosition = Vec3.sum(arrowPosition, frontOffset); - this.arrowTipPosition = arrowTipPosition; - return arrowTipPosition; - - }, setArrowRearPosition: function(arrowPosition, arrowRotation) { var frontVector = Quat.getFront(arrowRotation); var frontOffset = Vec3.multiply(frontVector, -ARROW_TIP_OFFSET); - var arrowTipPosition = Vec3.sum(arrowPosition, frontOffset); - this.arrowRearPosition = arrowTipPosition; - return arrowTipPosition; + var arrorRearPosition = Vec3.sum(arrowPosition, frontOffset); + this.arrowRearPosition = arrorRearPosition; + return arrorRearPosition; }, updateArrowPositionInNotch: function(shouldReleaseArrow) { - var stringHandPosition = this.getStringHandPosition(); var bowProperties = Entities.getEntityProperties(this.entityID); - var notchPosition; + //set the notch that the arrow should go through var frontVector = Quat.getFront(bowProperties.rotation); - var notchVectorForward = Vec3.multiply(frontVector, NOTCH_DETECTOR_OFFSET_FORWARD); + var notchVectorForward = Vec3.multiply(frontVector, NOTCH_OFFSET_FORWARD); var upVector = Quat.getUp(bowProperties.rotation); - var notchVectorUp = Vec3.multiply(upVector, NOTCH_DETECTOR_OFFSET_UP); - + var notchVectorUp = Vec3.multiply(upVector, NOTCH_OFFSET_UP); + var notchPosition; notchPosition = Vec3.sum(bowProperties.position, notchVectorForward); notchPosition = Vec3.sum(notchPosition, notchVectorUp); + //set the arrow rotation to be between the notch and other hand + var stringHandPosition = this.getStringHandPosition(); var handToNotch = Vec3.subtract(notchPosition, stringHandPosition); + var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); + //we draw strings to the rear of the arrow + this.setArrowRearPosition(notchPosition, arrowRotation); - // var pullBackDistance = Vec3.length(handToNotch); - - // if (pullBackDistance >= 0.6) { - // pullBackDistance = 0.6; - // } - - pullBackDistance = 0.5; + //modulate the sound by the + var pullBackDistance = Vec3.length(handToNotch); + // this.changeStringPullSoundVolume(pullBackDistance); + // //pull the arrow back a bit // var pullBackOffset = Vec3.multiply(handToNotch, -pullBackDistance); // var arrowPosition = Vec3.sum(detectorPosition, pullBackOffset); - //move it forward a bit + // // move it forward a bit // var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); // var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); - var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); - var handToNotch = Vec3.normalize(handToNotch); - this.setArrowTipPosition(notchPosition, arrowRotation); - this.setArrowRearPosition(notchPosition, arrowRotation); - - // this.pullBackDistance = pullBackDistance; + //if we're not shooting, we're updating the arrow's orientation if (shouldReleaseArrow !== true) { Entities.editEntity(this.arrow, { position: notchPosition, @@ -534,40 +501,36 @@ }) } + //shoot the arrow if (shouldReleaseArrow === true) { - var forwardVec = Vec3.multiply(handToNotch, 1 / this.deltaTime); - var arrowForce = this.scaleArrowShotStrength(0.5) - var forwardVec = Vec3.multiply(forwardVec, arrowForce) - - // var velocity = Quat.getFront(bowProperties.rotation) + //scale the shot strength by the distance you've pulled the arrow back and set its release velocity to be in the direction of the v + var arrowForce = this.scaleArrowShotStrength(pullBackDistance); + var releaseVelocity = Vec3.multiply(handToNotch, arrowForce); + //make the arrow physical, give it gravity, a lifetime, and set our velocity var arrowProperties = { - ignoreForCollisions: true, - // collisionsWillMove: true, - velocity: forwardVec, + collisionsWillMove: true, + velocity: releaseVelocity, + gravity: ARROW_GRAVITY, lifetime: 10 }; + //actually shoot the arrow and play its sound + Entities.editEntity(this.arrow, arrowProperties); this.playShootArrowSound(); - Entities.editEntity(this.arrow, arrowProperties); - - - + //clear the strings back to only the single straight one + this.deleteStrings(); Entities.editEntity(this.preNotchString, { visible: true }); - this.deleteStrings(); - var afterVelocity = Entities.getEntityProperties(this.arrow).velocity; - print('VELOCITY AT RELEASE:::' + JSON.stringify(afterVelocity)) + print('VELOCITY AFTER RELEASE:::' + JSON.stringify(afterVelocity)) - // this.arrow = null; } - }, scaleArrowShotStrength: function(value) { @@ -580,51 +543,45 @@ playStringPullSound: function() { var audioProperties = { - volume: 0.25, + volume: 0.15, position: this.bowProperties.position }; this.stringPullInjector = Audio.playSound(this.stringPullSound, audioProperties); }, + playShootArrowSound: function(sound) { var audioProperties = { - volume: 0.25, + volume: 0.20, position: this.bowProperties.position }; Audio.playSound(this.shootArrowSound, audioProperties); }, - playArrowHitSound: function(position) { - var audioProperties = { - volume: 0.25, - position: position - }; - Audio.playSound(this.arrowHitSound, audioProperties); - }, + playArrowNotchSound: function() { - print('play arrow notch sound') var audioProperties = { volume: 0.25, position: this.bowProperties.position }; Audio.playSound(this.arrowNotchSound, audioProperties); }, + changeStringPullSoundVolume: function(pullBackDistance) { var audioProperties = { - volume: 0.25, + volume: this.scaleSoundVolume(pullBackDistance), position: this.bowProperties.position } - this.stringPullInjector.options = audioProperties + this.stringPullInjector.options = audioProperties; + }, + scaleSoundVolume: function(value) { + var min1 = SHOT_SCALE.min1; + var max1 = SHOT_SCALE.max1; + var min2 = 0; + var max2 = 0.2; + return min2 + (max2 - min2) * ((value - min1) / (max1 - min1)); } - }; - getArrowTrackerByArrowID = function(arrowID) { - var result = arrowTrackers.filter(function(tracker) { - return tracker.arrowID === arrowID; - }); - var tracker = result[0] - return tracker - } return new Bow(); }); \ No newline at end of file diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index 562384bee3..db363a2428 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -46,7 +46,8 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { - // turnOffOtherHand:true, + turnOffOtherHand:false, + turnOffOppositebeam:true, invertSolidWhileHeld: true, spatialKey: { relativePosition: { diff --git a/examples/toybox/bow/createFireSources.js b/examples/toybox/bow/createFireSources.js deleted file mode 100644 index 1149626614..0000000000 --- a/examples/toybox/bow/createFireSources.js +++ /dev/null @@ -1,140 +0,0 @@ -// -// createFireSource.js -// -// Created byJames Pollack @imgntn on 10/19/2015 -// Copyright 2015 High Fidelity, Inc. -// -// This script creates a fire that you can use to light arrows on fire. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -var TORCH_MODEL_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/torch.fbx'; -var TORCH_DIMENSIONS = { - x: 0.07, - y: 1.6, - z: 0.08 -}; - -var FIRE_VERTICAL_OFFSET = 0.9; - -function createFireSource(position) { - - var torchProperties = { - type: 'Model', - name: 'Hifi-Fire-Torch', - modelURL: TORCH_MODEL_URL, - shapeType: 'box', - collisionsWillMove: false, - ignoreForCollisions: true, - dimensions: TORCH_DIMENSIONS, - position: position - }; - - var torch = Entities.addEntity(torchProperties); - torches.push(torch); - var torchProperties = Entities.getEntityProperties(torch); - - var upVector = Quat.getUp(torchProperties.rotation); - var upOffset = Vec3.multiply(upVector, FIRE_VERTICAL_OFFSET); - var fireTipPosition = Vec3.sum(torchProperties.position, upOffset); - - var myOrientation = Quat.fromPitchYawRollDegrees(-90, 0, 0.0); - - var animationSettings = JSON.stringify({ - fps: 30, - running: true, - loop: true, - firstFrame: 1, - lastFrame: 10000 - }); - - var fire = Entities.addEntity({ - type: "ParticleEffect", - name: "Hifi-Arrow-Fire-Source", - animationSettings: animationSettings, - textures: "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png", - emitRate: 100, - position: fireTipPosition, - colorStart: { - red: 70, - green: 70, - blue: 137 - }, - color: { - red: 200, - green: 99, - blue: 42 - }, - colorFinish: { - red: 255, - green: 99, - blue: 32 - }, - radiusSpread: 0.01, - radiusStart: 0.02, - radiusEnd: 0.001, - particleRadius: 0.5, - radiusFinish: 0.0, - emitOrientation: myOrientation, - emitSpeed: 0.3, - speedSpread: 0.1, - alphaStart: 0.05, - alpha: 0.1, - alphaFinish: 0.05, - emitDimensions: { - x: 1, - y: 1, - z: 0.1 - }, - polarFinish: 0.1, - emitAcceleration: { - x: 0.0, - y: 0.0, - z: 0.0 - }, - accelerationSpread: { - x: 0.1, - y: 0.01, - z: 0.1 - }, - lifespan: 1 - }); - - fires.push(fire) -} - -var fireSourcePositions = [{ - x: 100, - y: -1, - z: 100 - }, { - x: 100, - y: -1, - z: 102 - }, { - x: 100, - y: -1, - z: 104 - } - -]; - -var fires = []; -var torches = []; - -fireSourcePositions.forEach(function(position) { - createFireSource(position); -}) - -function cleanup() { - while (fires.length > 0) { - Entities.deleteEntity(fires.pop()); - } - while (torches.length > 0) { - Entities.deleteEntity(torches.pop()); - } -} - -Script.scriptEnding.connect(cleanup); \ No newline at end of file diff --git a/examples/toybox/bow/fieldSpawner.js b/examples/toybox/bow/fieldSpawner.js deleted file mode 100644 index 96fb56b6b7..0000000000 --- a/examples/toybox/bow/fieldSpawner.js +++ /dev/null @@ -1,106 +0,0 @@ -// fieldSpawner.js -// examples -// -// Created by James B. Pollack @imgntn on 11/16/2015 -// Copyright 2015 High Fidelity, Inc. -// -// Spawns ground, targets, and fire sources. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html - -var ground, wall; -var boxes = []; -var dustSystems = []; -var ZERO_VEC = { - x: 0, - y: 0, - z: 0 -}; - -Script.include("../libraries/utils.js"); - -var startPosition = { - x: 0, - y: 0, - z: 0 -} - -function spawnGround() { - var groundModelURL = "https://hifi-public.s3.amazonaws.com/alan/Playa/Ground.fbx"; - var groundPosition = Vec3.sum(startPosition, { - x: 0, - y: -2, - z: 0 - }); - ground = Entities.addEntity({ - type: "Model", - modelURL: groundModelURL, - shapeType: "box", - position: groundPosition, - dimensions: { - x: 900, - y: 0.82, - z: 900 - }, - }); - -} - -function spawnBoxes() { - var boxModelURL = "http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/target.fbx"; - var COLLISION_HULL_URL = 'http://hifi-public.s3.amazonaws.com/models/ping_pong_gun/target_collision_hull.obj'; - var TARGET_DIMENSIONS = { - x: 0.12, - y: 0.84, - z: 0.84 - }; - - var collisionSoundURL = "https://hifi-public.s3.amazonaws.com/sounds/Collisions-otherorganic/ToyWoodBlock.L.wav"; - var numBoxes = 200; - for (var i = 0; i < numBoxes; i++) { - var position = Vec3.sum(startPosition, { - x: Math.random() * numBoxes, - y: Math.random() * 2, - z: Math.random() * numBoxes - }) - var box = Entities.addEntity({ - type: "Model", - modelURL: boxModelURL, - collisionSoundURL: collisionSoundURL, - shapeType: "compound", - compoundShapeURL: COLLISION_HULL_URL, - position: position, - collisionsWillMove: true, - dimensions: TARGET_DIMENSIONS, - - }); - - Script.addEventHandler(box, "collisionWithEntity", boxCollision); - boxes.push(box); - } -} - -function boxCollision(me, other, collision) { - Entities.editEntity(me, { - gravity: { - x: 0, - y: -9.8, - z: 0 - } - }) -} - -spawnGround(); -spawnBoxes(); - - -// function cleanup() { -// Entities.deleteEntity(ground); -// boxes.forEach(function(box) { -// Entities.deleteEntity(box); -// }); - -// } - -// Script.scriptEnding.connect(cleanup); \ No newline at end of file From 3f4dee80d95c1bf44542d804c055a7eb3bfd1f0d Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Sat, 21 Nov 2015 02:32:54 -0800 Subject: [PATCH 58/79] more cleanup --- examples/controllers/handControllerGrab.js | 3 +- examples/toybox/bow/bow.js | 47 +++++++++------------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 198e059ad2..810bcdc3c0 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -562,8 +562,7 @@ function MyController(hand) { if (typeof grabbableDataForCandidate.grabbable !== 'undefined' && !grabbableDataForCandidate.grabbable) { continue; } - var propsForCandidate = - Entities.getEntityProperties(nearbyEntities[i], GRABBABLE_PROPERTIES); + var propsForCandidate = Entities.getEntityProperties(nearbyEntities[i], GRABBABLE_PROPERTIES); if (propsForCandidate.type == 'Unknown') { continue; diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 7938eb6951..c259a1279f 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -1,7 +1,7 @@ // // bow.js // -// This script attaches to a bow that you can pick up with a hand controller. Load an arrow and then shoot it. +// This script attaches to a bow that you can pick up with a hand controller. // Created by James B. Pollack @imgntn on 10/19/2015 // Copyright 2015 High Fidelity, Inc. // @@ -13,30 +13,17 @@ Script.include("../../libraries/utils.js"); - var NOTCH_ARROW_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/notch.wav?123'; + var NOTCH_ARROW_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/notch.wav'; var SHOOT_ARROW_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/String_release2.L.wav'; var STRING_PULL_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Bow_draw.1.L.wav'; - var ARROW_WHIZZ_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/whizz.wav'; - //todo : multiple impact sounds var ARROW_HIT_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Arrow_impact1.L.wav' + var ARROW_DIMENSIONS = { x: 0.02, y: 0.02, z: 0.64 }; - var ZERO_VEC = { - x: 0, - y: 0, - z: 0 - }; - - var LINE_ENTITY_DIMENSIONS = { - x: 10, - y: 10, - z: 10 - }; - var ARROW_OFFSET = -0.36; var ARROW_TIP_OFFSET = 0.32; var ARROW_GRAVITY = { @@ -117,7 +104,6 @@ preNotchString: null, hasArrowNotched: false, arrow: null, - prePickupString: null, stringData: { currentColor: { red: 255, @@ -126,7 +112,6 @@ } }, preload: function(entityID) { - print('preload bow'); this.entityID = entityID; this.stringPullSound = SoundCache.getSound(STRING_PULL_SOUND_URL); this.shootArrowSound = SoundCache.getSound(SHOOT_ARROW_SOUND_URL); @@ -166,7 +151,7 @@ setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: this.initialHand, invertSolidWhileHeld: true, - turnOffOppositebeam: true, + turnOffOppositeBeam: true, spatialKey: BOW_SPATIAL_KEY }); @@ -183,7 +168,6 @@ if (this.preNotchString !== null && this.aiming === false) { // print('DRAW PRE NOTCH STRING') this.drawPreNotchStrings(); - // this.updateArrowAttachedToBow(); } // create the notch detector that arrows will look for @@ -211,7 +195,7 @@ setEntityCustomData('grabbableKey', this.entityID, { turnOffOtherHand: false, invertSolidWhileHeld: true, - turnOffOppositebeam: true, + turnOffOppositebBam: true, spatialKey: BOW_SPATIAL_KEY }); Entities.deleteEntity(this.preNotchString); @@ -241,19 +225,26 @@ userData: JSON.stringify({ grabbableKey: { grabbable: false - //shouldnbt need this but just in case - invertSolidWhileHeld: true, } }) }); - var arrowProps = Entities.getEntityProperties(arrow) + Script.addEventHandler(arrow, "collisionWithEntity", function(entityA, entityB, collision) { - //have to reverse lookup the tracker by the arrow id to get access to the children - + Entities.editEntity(entityA, { + velocity: { + x: 0, + y: 0, + z: 0 + }, + gravity: { + x: 0, + y: 0, + z: 0 + }, + collisionsWillMove: false + }) print('ARROW COLLIDED WITH::' + entityB); - print('NAME OF ENTITY:::' + Entities.getEntityProperties(entityB, "name").name) - }); return arrow From aa832311f54a3c1f68678ae8855a13f5648910c8 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Sun, 22 Nov 2015 22:12:43 -0800 Subject: [PATCH 59/79] add beam disabler --- examples/controllers/handControllerGrab.js | 88 +++++++++++++--------- examples/toybox/bow/bow.js | 67 +++++++++++++--- examples/toybox/bow/createBow.js | 3 +- 3 files changed, 111 insertions(+), 47 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 810bcdc3c0..7edbb01b32 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -109,13 +109,13 @@ var GRABBABLE_PROPERTIES = [ var GRABBABLE_DATA_KEY = "grabbableKey"; // shared with grab.js var GRAB_USER_DATA_KEY = "grabKey"; // shared with grab.js +var BEAM_DISABLER_KEY = 'beamDisablerKey' var DEFAULT_GRABBABLE_DATA = { grabbable: true, invertSolidWhileHeld: false }; -var disabledHand = 'none'; // states for the state machine var STATE_OFF = 0; @@ -411,11 +411,6 @@ function MyController(hand) { this.search = function() { this.grabbedEntity = null; - // if this hand is the one that's disabled, we don't want to search for anything at all - if (this.hand === disabledHand) { - return; - } - if (this.state == STATE_SEARCHING ? this.triggerSmoothedReleased() : this.bumperReleased()) { this.setState(STATE_RELEASE); return; @@ -460,17 +455,7 @@ function MyController(hand) { // the ray is intersecting something we can move. var intersectionDistance = Vec3.distance(pickRay.origin, intersection.intersection); - //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) { - if (this.hand === RIGHT_HAND) { - disabledHand = LEFT_HAND; - } else { - disabledHand = RIGHT_HAND; - } - } else { - disabledHand = 'none'; - } if (intersection.properties.name == "Grab Debug Entity") { continue; @@ -758,21 +743,8 @@ function MyController(hand) { this.nearGrabbing = function() { var now = Date.now(); - print('HAND IN NEAR GRAB:::' + this.hand) var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); - var turnOffOtherHand = grabbableData["turnOffOtherHand"]; - print('TURN OFF OTHER HAND??' + turnOffOtherHand); - if (turnOffOtherHand === 'left' && this.hand === 1) { - print('IGNORE RIGHT') - return - } - if (turnOffOtherHand === 'right' && this.hand === 0) { - print('IGNORE LEFT') - return - } - - if (this.state == STATE_NEAR_GRABBING && this.triggerSmoothedReleased()) { this.setState(STATE_RELEASE); Entities.callEntityMethod(this.grabbedEntity, "releaseGrab"); @@ -1121,10 +1093,6 @@ function MyController(hand) { this.release = function() { - if (this.hand !== disabledHand) { - //release the disabled hand when we let go with the main one - disabledHand = 'none'; - } this.lineOff(); if (this.grabbedEntity !== null) { @@ -1246,14 +1214,64 @@ mapping.from([Controller.Standard.LB]).peek().to(leftController.bumperPress); Controller.enableMapping(MAPPING_NAME); +var beamDisabler; + +function createBeamDisabler() { + print('CREATING DISABLER') + var disablerProps = { + name: 'Hifi-Beam-Disabler', + type: 'Sphere', + dimensions: { + x: 0.1, + y: 0.1, + z: 0.1 + }, + color: { + red: 255, + green: 0, + blue: 0 + }, + visible: true, + position: MyAvatar.position, + ignoreForCollisions: true, + collisionsWillMove: false, + userData: JSON.stringify({ + beamDisablerKey: { + handToDisable: 'none' + }, + grabbableKey: { + grabbable: false + } + }) + } + beamDisabler = Entities.addEntity(disablerProps) +} + +function updateBeamDisablerPosition() { + Entities.editEntity(beamDisabler, { + position: MyAvatar.position + }) +} + +createBeamDisabler(); + function update() { - rightController.update(); - leftController.update(); + updateBeamDisablerPosition(); + var beamDisablerData = getEntityCustomData(BEAM_DISABLER_KEY, beamDisabler, { + handToDisable: 'none' + }); + if (beamDisablerData.handToDisable !== 0) { + leftController.update(); + } + if (beamDisablerData.handToDisable !== 1) { + rightController.update(); + } } function cleanup() { rightController.cleanup(); leftController.cleanup(); + Entities.deleteEntity(beamDisabler); Controller.disableMapping(MAPPING_NAME); } diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index c259a1279f..e1bbcb62fa 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -76,6 +76,7 @@ } + var USE_DEBOUNCE = false; function interval() { var lastTime = new Date().getTime(); @@ -111,13 +112,13 @@ blue: 255 } }, + sinceLastUpdate:0, preload: function(entityID) { this.entityID = entityID; this.stringPullSound = SoundCache.getSound(STRING_PULL_SOUND_URL); this.shootArrowSound = SoundCache.getSound(SHOOT_ARROW_SOUND_URL); this.arrowHitSound = SoundCache.getSound(ARROW_HIT_SOUND_URL); this.arrowNotchSound = SoundCache.getSound(NOTCH_ARROW_SOUND_URL); - this.arrowWhizzSound = SoundCache.getSound(ARROW_WHIZZ_SOUND_URL); }, @@ -145,11 +146,25 @@ if (this.isGrabbed === true) { return false; } + this.isGrabbed = true; this.initialHand = this.hand; + var ids = Entities.findEntities(MyAvatar.position, 1); + + for (var i in ids) { + var entityId = ids[i]; + var foundProps = Entities.getEntityProperties(entityId); + if (foundProps.name == "Hifi-Beam-Disabler") { + print('FOUND THE BEAM DISABLER') + setEntityCustomData('beamDisablerKey',entityId,{ + handToDisable:this.initialHand==='left'?1:0 + }) + } + } + setEntityCustomData('grabbableKey', this.entityID, { - turnOffOtherHand: this.initialHand, + grabbable: false, invertSolidWhileHeld: true, turnOffOppositeBeam: true, spatialKey: BOW_SPATIAL_KEY @@ -157,7 +172,19 @@ }, continueNearGrab: function() { - this.deltaTime = checkInterval(); + + //debounce during debugging -- maybe we're updating too fast? + if (USE_DEBOUNCE === true) { + this.deltaTime = checkInterval(); + this.sinceLastUpdate = this.sinceLastUpdate + this.deltaTime; + + if (this.sinceLastUpdate > 60) { + this.sinceLastUpdate = 0; + } else { + return; + } + } + this.bowProperties = Entities.getEntityProperties(this.entityID); //create a string across the bow when we pick it up @@ -189,13 +216,27 @@ releaseGrab: function() { print('RELEASE GRAB EVENT') if (this.isGrabbed === true && this.hand === this.initialHand) { + var ids = Entities.findEntities(MyAvatar.position, 1); + + for (var i in ids) { + var entityId = ids[i]; + var foundProps = Entities.getEntityProperties(entityId); + if (foundProps.name == "Hifi-Beam-Disabler") { + print('FOUND THE BEAM DISABLER') + setEntityCustomData('beamDisablerKey',entityId,{ + handToDisable:'none' + }) + } + } + + this.isGrabbed = false; this.stringDrawn = false; this.deleteStrings(); setEntityCustomData('grabbableKey', this.entityID, { - turnOffOtherHand: false, + grabbable: true, + turnOffOppositeBeam: true, invertSolidWhileHeld: true, - turnOffOppositebBam: true, spatialKey: BOW_SPATIAL_KEY }); Entities.deleteEntity(this.preNotchString); @@ -414,15 +455,19 @@ // print('TRIGGER VALUE:::' + this.triggerValue); if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - // firing the arrow + print('TRIGGER VALUE??' + this.triggerValue) + // firing the arrow print('HIT RELEASE LOOP IN CHECK'); - this.updateArrowPositionInNotch(true); + + this.drawStrings(); this.hasArrowNotched = false; this.aiming = false; this.stringDrawn = false; + this.updateArrowPositionInNotch(true); + } else if (this.triggerValue > DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - // print('HIT CONTINUE LOOP IN CHECK') + print('HIT CONTINUE LOOP IN CHECK') //continuing to aim the arrow this.aiming = true; @@ -454,7 +499,6 @@ updateArrowPositionInNotch: function(shouldReleaseArrow) { var bowProperties = Entities.getEntityProperties(this.entityID); - //set the notch that the arrow should go through var frontVector = Quat.getFront(bowProperties.rotation); var notchVectorForward = Vec3.multiply(frontVector, NOTCH_OFFSET_FORWARD); @@ -494,6 +538,7 @@ //shoot the arrow if (shouldReleaseArrow === true) { + var arrowProperties = Entities.getEntityProperties(this.arrow); //scale the shot strength by the distance you've pulled the arrow back and set its release velocity to be in the direction of the v var arrowForce = this.scaleArrowShotStrength(pullBackDistance); @@ -504,7 +549,9 @@ collisionsWillMove: true, velocity: releaseVelocity, gravity: ARROW_GRAVITY, - lifetime: 10 + lifetime: 10, + position:notchPosition, + rotation:arrowRotation }; //actually shoot the arrow and play its sound diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index db363a2428..10824c8f59 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -46,8 +46,7 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { - turnOffOtherHand:false, - turnOffOppositebeam:true, + turnOffOppositeBeam:true, invertSolidWhileHeld: true, spatialKey: { relativePosition: { From 8e81c3252a42f192e9f3984e7bf2b8f8ce3037ef Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Sun, 22 Nov 2015 22:24:42 -0800 Subject: [PATCH 60/79] cleanup and mostly? working --- examples/controllers/handControllerGrab.js | 3 +- examples/toybox/bow/bow.js | 42 +++++++++++----------- examples/toybox/bow/createBow.js | 1 - 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 7edbb01b32..ceb2b0d803 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -1217,7 +1217,6 @@ Controller.enableMapping(MAPPING_NAME); var beamDisabler; function createBeamDisabler() { - print('CREATING DISABLER') var disablerProps = { name: 'Hifi-Beam-Disabler', type: 'Sphere', @@ -1231,7 +1230,7 @@ function createBeamDisabler() { green: 0, blue: 0 }, - visible: true, + visible: false, position: MyAvatar.position, ignoreForCollisions: true, collisionsWillMove: false, diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index e1bbcb62fa..9783dafb6e 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -77,6 +77,7 @@ var USE_DEBOUNCE = false; + function interval() { var lastTime = new Date().getTime(); @@ -112,7 +113,7 @@ blue: 255 } }, - sinceLastUpdate:0, + sinceLastUpdate: 0, preload: function(entityID) { this.entityID = entityID; this.stringPullSound = SoundCache.getSound(STRING_PULL_SOUND_URL); @@ -157,8 +158,8 @@ var foundProps = Entities.getEntityProperties(entityId); if (foundProps.name == "Hifi-Beam-Disabler") { print('FOUND THE BEAM DISABLER') - setEntityCustomData('beamDisablerKey',entityId,{ - handToDisable:this.initialHand==='left'?1:0 + setEntityCustomData('beamDisablerKey', entityId, { + handToDisable: this.initialHand === 'left' ? 1 : 0 }) } } @@ -166,16 +167,15 @@ setEntityCustomData('grabbableKey', this.entityID, { grabbable: false, invertSolidWhileHeld: true, - turnOffOppositeBeam: true, spatialKey: BOW_SPATIAL_KEY }); }, continueNearGrab: function() { + this.deltaTime = checkInterval(); //debounce during debugging -- maybe we're updating too fast? if (USE_DEBOUNCE === true) { - this.deltaTime = checkInterval(); this.sinceLastUpdate = this.sinceLastUpdate + this.deltaTime; if (this.sinceLastUpdate > 60) { @@ -216,18 +216,18 @@ releaseGrab: function() { print('RELEASE GRAB EVENT') if (this.isGrabbed === true && this.hand === this.initialHand) { - var ids = Entities.findEntities(MyAvatar.position, 1); + var ids = Entities.findEntities(MyAvatar.position, 1); - for (var i in ids) { - var entityId = ids[i]; - var foundProps = Entities.getEntityProperties(entityId); - if (foundProps.name == "Hifi-Beam-Disabler") { - print('FOUND THE BEAM DISABLER') - setEntityCustomData('beamDisablerKey',entityId,{ - handToDisable:'none' - }) + for (var i in ids) { + var entityId = ids[i]; + var foundProps = Entities.getEntityProperties(entityId); + if (foundProps.name == "Hifi-Beam-Disabler") { + print('FOUND THE BEAM DISABLER') + setEntityCustomData('beamDisablerKey', entityId, { + handToDisable: 'none' + }) + } } - } this.isGrabbed = false; @@ -235,7 +235,6 @@ this.deleteStrings(); setEntityCustomData('grabbableKey', this.entityID, { grabbable: true, - turnOffOppositeBeam: true, invertSolidWhileHeld: true, spatialKey: BOW_SPATIAL_KEY }); @@ -458,7 +457,7 @@ print('TRIGGER VALUE??' + this.triggerValue) // firing the arrow print('HIT RELEASE LOOP IN CHECK'); - + this.drawStrings(); this.hasArrowNotched = false; this.aiming = false; @@ -467,7 +466,7 @@ } else if (this.triggerValue > DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - print('HIT CONTINUE LOOP IN CHECK') + // print('HIT CONTINUE LOOP IN CHECK') //continuing to aim the arrow this.aiming = true; @@ -542,7 +541,10 @@ //scale the shot strength by the distance you've pulled the arrow back and set its release velocity to be in the direction of the v var arrowForce = this.scaleArrowShotStrength(pullBackDistance); + var handToNotch = Vec3.normalize(handToNotch); + var releaseVelocity = Vec3.multiply(handToNotch, arrowForce); + // var releaseVelocity2 = Vec3.multiply() //make the arrow physical, give it gravity, a lifetime, and set our velocity var arrowProperties = { @@ -550,8 +552,8 @@ velocity: releaseVelocity, gravity: ARROW_GRAVITY, lifetime: 10, - position:notchPosition, - rotation:arrowRotation + position: arrowProperties.position, + rotation: arrowProperties.rotation }; //actually shoot the arrow and play its sound diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index 10824c8f59..53798c0fc3 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -46,7 +46,6 @@ var bow = Entities.addEntity({ script: SCRIPT_URL, userData: JSON.stringify({ grabbableKey: { - turnOffOppositeBeam:true, invertSolidWhileHeld: true, spatialKey: { relativePosition: { From aa2450d755c9dcb8fd05e4ddedf3545c281297fb Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 23 Nov 2015 11:15:36 -0800 Subject: [PATCH 61/79] cleanup, debugging script --- examples/toybox/bow/arrowTest.js | 74 ++++++++++++++++++++++++++++++++ examples/toybox/bow/bow.js | 6 +-- 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 examples/toybox/bow/arrowTest.js diff --git a/examples/toybox/bow/arrowTest.js b/examples/toybox/bow/arrowTest.js new file mode 100644 index 0000000000..98b30e9572 --- /dev/null +++ b/examples/toybox/bow/arrowTest.js @@ -0,0 +1,74 @@ + var ARROW_HIT_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Arrow_impact1.L.wav' + + var ARROW_DIMENSIONS = { + x: 0.02, + y: 0.02, + z: 0.64 + }; + + var ARROW_OFFSET = -0.36; + var ARROW_TIP_OFFSET = 0.32; + var ARROW_GRAVITY = { + x: 0, + y: -4.8, + z: 0 + }; + + var ARROW_MODEL_URL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/newarrow_textured.fbx"; + var ARROW_COLLISION_HULL_URL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/newarrow_collision_hull.obj"; + + var ARROW_DIMENSIONS = { + x: 0.02, + y: 0.02, + z: 0.64 + }; + + var center = Vec3.sum(Vec3.sum(MyAvatar.position, { + x: 0, + y: 0.5, + z: 0 + }), Vec3.multiply(1.5, Quat.getFront(Camera.getOrientation()))); + + var arrow; + createArrow = function() { + + arrow = Entities.addEntity({ + name: 'Hifi-Arrow', + type: 'Model', + modelURL: ARROW_MODEL_URL, + shapeType: 'compound', + compoundShapeURL: ARROW_COLLISION_HULL_URL, + dimensions: ARROW_DIMENSIONS, + position: center, + collisionsWillMove: false, + ignoreForCollisions: true, + + }); + + return arrow + } + + + createArrow(); + var yRotation = 0; + var rotateInterval = Script.setInterval(function() { + Entities.editEntity(arrow, { + rotation: Quat.fromPitchYawRollDegrees(0, yRotation, 0) + }) + yRotation += 1 + print('changing rotation') + }, 17) + + Script.setTimeout(function() { + Script.clearInterval(rotateInterval) + print('interval cleared') + Entities.editEntity(arrow, { + velocity: { + x: 0.5, + y: 0.25, + z: 1 + }, + collisionsWillMove: true, + }) + print('entity edited') + }, 2000) \ No newline at end of file diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 9783dafb6e..7971b27466 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -537,7 +537,7 @@ //shoot the arrow if (shouldReleaseArrow === true) { - var arrowProperties = Entities.getEntityProperties(this.arrow); + var arrowProps = Entities.getEntityProperties(this.arrow); //scale the shot strength by the distance you've pulled the arrow back and set its release velocity to be in the direction of the v var arrowForce = this.scaleArrowShotStrength(pullBackDistance); @@ -552,8 +552,8 @@ velocity: releaseVelocity, gravity: ARROW_GRAVITY, lifetime: 10, - position: arrowProperties.position, - rotation: arrowProperties.rotation + // position: arrowProps.position, + // rotation: arrowProps.rotation }; //actually shoot the arrow and play its sound From c22a07bba7be404299b053f0235965ee936ffa2c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 23 Nov 2015 12:36:39 -0800 Subject: [PATCH 62/79] bow collisions, etc --- examples/toybox/bow/bow.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 7971b27466..cc851f47f6 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -270,8 +270,13 @@ }); - Script.addEventHandler(arrow, "collisionWithEntity", function(entityA, entityB, collision) { + var makeArrowStick = function(entityA, entityB, collision) { Entities.editEntity(entityA, { + angularVelocity: { + x: 0, + y: 0, + z: 0 + }, velocity: { x: 0, y: 0, @@ -282,10 +287,14 @@ y: 0, z: 0 }, + position: collision.contactPoint, collisionsWillMove: false }) print('ARROW COLLIDED WITH::' + entityB); - }); + Script.removeEventHandler(arrow, "collisionWithEntity", makeArrowStick) + } + + Script.addEventHandler(arrow, "collisionWithEntity", makeArrowStick); return arrow }, @@ -549,6 +558,7 @@ //make the arrow physical, give it gravity, a lifetime, and set our velocity var arrowProperties = { collisionsWillMove: true, + ignoreForCollisions:false, velocity: releaseVelocity, gravity: ARROW_GRAVITY, lifetime: 10, From 0fd389b02fdf015e9bf8d9610d7f2f88c4a7a4f8 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Mon, 23 Nov 2015 12:53:58 -0800 Subject: [PATCH 63/79] remove arrowTest --- examples/toybox/bow/arrowTest.js | 74 -------------------------------- 1 file changed, 74 deletions(-) delete mode 100644 examples/toybox/bow/arrowTest.js diff --git a/examples/toybox/bow/arrowTest.js b/examples/toybox/bow/arrowTest.js deleted file mode 100644 index 98b30e9572..0000000000 --- a/examples/toybox/bow/arrowTest.js +++ /dev/null @@ -1,74 +0,0 @@ - var ARROW_HIT_SOUND_URL = 'http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/sounds/Arrow_impact1.L.wav' - - var ARROW_DIMENSIONS = { - x: 0.02, - y: 0.02, - z: 0.64 - }; - - var ARROW_OFFSET = -0.36; - var ARROW_TIP_OFFSET = 0.32; - var ARROW_GRAVITY = { - x: 0, - y: -4.8, - z: 0 - }; - - var ARROW_MODEL_URL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/newarrow_textured.fbx"; - var ARROW_COLLISION_HULL_URL = "http://hifi-content.s3.amazonaws.com/james/bow_and_arrow/models/newarrow_collision_hull.obj"; - - var ARROW_DIMENSIONS = { - x: 0.02, - y: 0.02, - z: 0.64 - }; - - var center = Vec3.sum(Vec3.sum(MyAvatar.position, { - x: 0, - y: 0.5, - z: 0 - }), Vec3.multiply(1.5, Quat.getFront(Camera.getOrientation()))); - - var arrow; - createArrow = function() { - - arrow = Entities.addEntity({ - name: 'Hifi-Arrow', - type: 'Model', - modelURL: ARROW_MODEL_URL, - shapeType: 'compound', - compoundShapeURL: ARROW_COLLISION_HULL_URL, - dimensions: ARROW_DIMENSIONS, - position: center, - collisionsWillMove: false, - ignoreForCollisions: true, - - }); - - return arrow - } - - - createArrow(); - var yRotation = 0; - var rotateInterval = Script.setInterval(function() { - Entities.editEntity(arrow, { - rotation: Quat.fromPitchYawRollDegrees(0, yRotation, 0) - }) - yRotation += 1 - print('changing rotation') - }, 17) - - Script.setTimeout(function() { - Script.clearInterval(rotateInterval) - print('interval cleared') - Entities.editEntity(arrow, { - velocity: { - x: 0.5, - y: 0.25, - z: 1 - }, - collisionsWillMove: true, - }) - print('entity edited') - }, 2000) \ No newline at end of file From fd26427390fbeda9753920032b6ad83dd5e88d9a Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 24 Nov 2015 10:23:29 -0800 Subject: [PATCH 64/79] add pullback / pushfwd --- examples/toybox/bow/bow.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index cc851f47f6..80922bd703 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -21,10 +21,10 @@ var ARROW_DIMENSIONS = { x: 0.02, y: 0.02, - z: 0.64 + z: 0.72 }; - var ARROW_OFFSET = -0.36; + var ARROW_OFFSET = -0.44; var ARROW_TIP_OFFSET = 0.32; var ARROW_GRAVITY = { x: 0, @@ -521,25 +521,31 @@ var handToNotch = Vec3.subtract(notchPosition, stringHandPosition); var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); - //we draw strings to the rear of the arrow - this.setArrowRearPosition(notchPosition, arrowRotation); + - //modulate the sound by the + var pullBackDistance = Vec3.length(handToNotch); // this.changeStringPullSoundVolume(pullBackDistance); + if(pullBackDistance>0.6){ + pullBackDistance = 0.6; + } + // //pull the arrow back a bit - // var pullBackOffset = Vec3.multiply(handToNotch, -pullBackDistance); - // var arrowPosition = Vec3.sum(detectorPosition, pullBackOffset); + var pullBackOffset = Vec3.multiply(handToNotch, -pullBackDistance); + var arrowPosition = Vec3.sum(notchPosition, pullBackOffset); // // move it forward a bit - // var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); - // var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); + var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); + var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); + + //we draw strings to the rear of the arrow + this.setArrowRearPosition(finalArrowPosition, arrowRotation); //if we're not shooting, we're updating the arrow's orientation if (shouldReleaseArrow !== true) { Entities.editEntity(this.arrow, { - position: notchPosition, + position: finalArrowPosition, rotation: arrowRotation }) } @@ -593,7 +599,7 @@ playStringPullSound: function() { var audioProperties = { - volume: 0.15, + volume: 0.10, position: this.bowProperties.position }; this.stringPullInjector = Audio.playSound(this.stringPullSound, audioProperties); @@ -601,7 +607,7 @@ playShootArrowSound: function(sound) { var audioProperties = { - volume: 0.20, + volume: 0.15, position: this.bowProperties.position }; Audio.playSound(this.shootArrowSound, audioProperties); @@ -609,7 +615,7 @@ playArrowNotchSound: function() { var audioProperties = { - volume: 0.25, + volume: 0.15, position: this.bowProperties.position }; Audio.playSound(this.arrowNotchSound, audioProperties); From 13e1042bfa8965eb84569aaabdb4aa4539e7903c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 24 Nov 2015 10:37:08 -0800 Subject: [PATCH 65/79] use messages instead of hidden entities to disable beams --- examples/controllers/handControllerGrab.js | 68 ++++++----------- examples/toybox/bow/bow.js | 87 ++++++++-------------- 2 files changed, 51 insertions(+), 104 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index ceb2b0d803..05e834577f 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -1213,60 +1213,34 @@ mapping.from([Controller.Standard.LB]).peek().to(leftController.bumperPress); Controller.enableMapping(MAPPING_NAME); - -var beamDisabler; - -function createBeamDisabler() { - var disablerProps = { - name: 'Hifi-Beam-Disabler', - type: 'Sphere', - dimensions: { - x: 0.1, - y: 0.1, - z: 0.1 - }, - color: { - red: 255, - green: 0, - blue: 0 - }, - visible: false, - position: MyAvatar.position, - ignoreForCollisions: true, - collisionsWillMove: false, - userData: JSON.stringify({ - beamDisablerKey: { - handToDisable: 'none' - }, - grabbableKey: { - grabbable: false - } - }) - } - beamDisabler = Entities.addEntity(disablerProps) -} - -function updateBeamDisablerPosition() { - Entities.editEntity(beamDisabler, { - position: MyAvatar.position - }) -} - -createBeamDisabler(); - +var handToDisable = 'none'; function update() { - updateBeamDisablerPosition(); - var beamDisablerData = getEntityCustomData(BEAM_DISABLER_KEY, beamDisabler, { - handToDisable: 'none' - }); - if (beamDisablerData.handToDisable !== 0) { + if (handToDisable !== 0) { leftController.update(); } - if (beamDisablerData.handToDisable !== 1) { + if (handToDisable !== 1) { rightController.update(); } } +Messages.subscribe('Hifi-Beam-Disabler'); + +handleBeamDisablerMessages = function(channel, message,sender) { + print("got message:"+message+" on channel:" + channel + " from sender:"+sender); + handToDisable = message; + if(message==='left'){ + handToDisable = 1; + } + if(message==='right'){ + handToDisable = 0; + } + +} + +Messages.messageReceived.connect(handleBeamDisablerMessages); + + + function cleanup() { rightController.cleanup(); leftController.cleanup(); diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 80922bd703..b238480829 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -149,20 +149,9 @@ } this.isGrabbed = true; + this.initialHand = this.hand; - - var ids = Entities.findEntities(MyAvatar.position, 1); - - for (var i in ids) { - var entityId = ids[i]; - var foundProps = Entities.getEntityProperties(entityId); - if (foundProps.name == "Hifi-Beam-Disabler") { - print('FOUND THE BEAM DISABLER') - setEntityCustomData('beamDisablerKey', entityId, { - handToDisable: this.initialHand === 'left' ? 1 : 0 - }) - } - } + Messages.sendMessage('Hifi-Beam-Disabler', this.initialHand); setEntityCustomData('grabbableKey', this.entityID, { grabbable: false, @@ -214,21 +203,10 @@ }, releaseGrab: function() { - print('RELEASE GRAB EVENT') + // print('RELEASE GRAB EVENT') if (this.isGrabbed === true && this.hand === this.initialHand) { - var ids = Entities.findEntities(MyAvatar.position, 1); - - for (var i in ids) { - var entityId = ids[i]; - var foundProps = Entities.getEntityProperties(entityId); - if (foundProps.name == "Hifi-Beam-Disabler") { - print('FOUND THE BEAM DISABLER') - setEntityCustomData('beamDisablerKey', entityId, { - handToDisable: 'none' - }) - } - } + Messages.sendMessage('Hifi-Beam-Disabler', "none") this.isGrabbed = false; this.stringDrawn = false; @@ -272,25 +250,25 @@ var makeArrowStick = function(entityA, entityB, collision) { Entities.editEntity(entityA, { - angularVelocity: { - x: 0, - y: 0, - z: 0 - }, - velocity: { - x: 0, - y: 0, - z: 0 - }, - gravity: { - x: 0, - y: 0, - z: 0 - }, - position: collision.contactPoint, - collisionsWillMove: false - }) - print('ARROW COLLIDED WITH::' + entityB); + angularVelocity: { + x: 0, + y: 0, + z: 0 + }, + velocity: { + x: 0, + y: 0, + z: 0 + }, + gravity: { + x: 0, + y: 0, + z: 0 + }, + position: collision.contactPoint, + collisionsWillMove: false + }) + // print('ARROW COLLIDED WITH::' + entityB); Script.removeEventHandler(arrow, "collisionWithEntity", makeArrowStick) } @@ -463,9 +441,8 @@ // print('TRIGGER VALUE:::' + this.triggerValue); if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { - print('TRIGGER VALUE??' + this.triggerValue) - // firing the arrow - print('HIT RELEASE LOOP IN CHECK'); + // firing the arrow + // print('HIT RELEASE LOOP IN CHECK'); this.drawStrings(); this.hasArrowNotched = false; @@ -483,7 +460,7 @@ this.updateArrowPositionInNotch(); } else if (this.triggerValue > DRAW_STRING_THRESHOLD && this.stringDrawn === false) { - print('HIT START LOOP IN CHECK'); + // print('HIT START LOOP IN CHECK'); this.arrow = this.createArrow(); this.playStringPullSound(); @@ -521,13 +498,12 @@ var handToNotch = Vec3.subtract(notchPosition, stringHandPosition); var arrowRotation = Quat.rotationBetween(Vec3.FRONT, handToNotch); - - + var pullBackDistance = Vec3.length(handToNotch); // this.changeStringPullSoundVolume(pullBackDistance); - if(pullBackDistance>0.6){ + if (pullBackDistance > 0.6) { pullBackDistance = 0.6; } @@ -539,7 +515,7 @@ var pushForwardOffset = Vec3.multiply(handToNotch, -ARROW_OFFSET); var finalArrowPosition = Vec3.sum(arrowPosition, pushForwardOffset); - //we draw strings to the rear of the arrow + //we draw strings to the rear of the arrow this.setArrowRearPosition(finalArrowPosition, arrowRotation); //if we're not shooting, we're updating the arrow's orientation @@ -564,7 +540,7 @@ //make the arrow physical, give it gravity, a lifetime, and set our velocity var arrowProperties = { collisionsWillMove: true, - ignoreForCollisions:false, + ignoreForCollisions: false, velocity: releaseVelocity, gravity: ARROW_GRAVITY, lifetime: 10, @@ -582,9 +558,6 @@ visible: true }); - var afterVelocity = Entities.getEntityProperties(this.arrow).velocity; - print('VELOCITY AFTER RELEASE:::' + JSON.stringify(afterVelocity)) - } }, From 52edc3b7017486751fd2021f0be5ccaddadc01af Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 24 Nov 2015 10:43:33 -0800 Subject: [PATCH 66/79] cleanup beamdisabler --- examples/controllers/handControllerGrab.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 05e834577f..af42d8c2eb 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -1214,6 +1214,7 @@ mapping.from([Controller.Standard.LB]).peek().to(leftController.bumperPress); Controller.enableMapping(MAPPING_NAME); var handToDisable = 'none'; + function update() { if (handToDisable !== 0) { leftController.update(); @@ -1225,26 +1226,21 @@ function update() { Messages.subscribe('Hifi-Beam-Disabler'); -handleBeamDisablerMessages = function(channel, message,sender) { - print("got message:"+message+" on channel:" + channel + " from sender:"+sender); +handleBeamDisablerMessages = function(channel, message, sender) { handToDisable = message; - if(message==='left'){ + if (message === 'left') { handToDisable = 1; } - if(message==='right'){ + if (message === 'right') { handToDisable = 0; } - } Messages.messageReceived.connect(handleBeamDisablerMessages); - - function cleanup() { rightController.cleanup(); leftController.cleanup(); - Entities.deleteEntity(beamDisabler); Controller.disableMapping(MAPPING_NAME); } From 2add609387deb49e78bfb0ed82dfffce969e462f Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 24 Nov 2015 12:12:01 -0800 Subject: [PATCH 67/79] use new thingo --- examples/controllers/handControllerGrab.js | 1 + examples/toybox/bow/bow.js | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index af42d8c2eb..732f2f3513 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -1216,6 +1216,7 @@ Controller.enableMapping(MAPPING_NAME); var handToDisable = 'none'; function update() { + // print('disabled in update:::'+handToDisable) if (handToDisable !== 0) { leftController.update(); } diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index b238480829..4d8b7ae0d7 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -78,6 +78,11 @@ var USE_DEBOUNCE = false; + var TRIGGER_CONTROLS = [ + Controller.Standard.LT, + Controller.Standard.RT, + ]; + function interval() { var lastTime = new Date().getTime(); @@ -144,6 +149,8 @@ }, startNearGrab: function() { + + print('START BOW GRAB') if (this.isGrabbed === true) { return false; } @@ -226,6 +233,7 @@ }, createArrow: function() { + print('create arrow') this.playArrowNotchSound(); var arrow = Entities.addEntity({ @@ -429,16 +437,21 @@ checkStringHand: function() { //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with + var triggerLookup; if (this.initialHand === 'left') { + triggerLookup=1; this.getStringHandPosition = MyAvatar.getRightPalmPosition; - this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); + // this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); } else if (this.initialHand === 'right') { this.getStringHandPosition = MyAvatar.getLeftPalmPosition; - this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); + triggerLookup=0; + // this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); } - this.triggerValue = Controller.getActionValue(this.stringTriggerAction); - // print('TRIGGER VALUE:::' + this.triggerValue); + this.triggerValue = Controller.getValue(TRIGGER_CONTROLS[triggerLookup]); + + // this.triggerValue = Controller.getActionValue(this.stringTriggerAction); + print('TRIGGER VALUE:::' + this.triggerValue); if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { // firing the arrow From 50c1786e9bc21c4f0e33ab4eceeded599964746f Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 24 Nov 2015 12:46:01 -0800 Subject: [PATCH 68/79] trigger changes and message debugging --- examples/controllers/handControllerGrab.js | 1 + examples/toybox/bow/bow.js | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 732f2f3513..7f363c47ae 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -1228,6 +1228,7 @@ function update() { Messages.subscribe('Hifi-Beam-Disabler'); handleBeamDisablerMessages = function(channel, message, sender) { + print('GOT MESSAGE:::'+message) handToDisable = message; if (message === 'left') { handToDisable = 1; diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index 4d8b7ae0d7..fad5148992 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -441,17 +441,13 @@ if (this.initialHand === 'left') { triggerLookup=1; this.getStringHandPosition = MyAvatar.getRightPalmPosition; - // this.stringTriggerAction = Controller.findAction("RIGHT_HAND_CLICK"); } else if (this.initialHand === 'right') { this.getStringHandPosition = MyAvatar.getLeftPalmPosition; triggerLookup=0; - // this.stringTriggerAction = Controller.findAction("LEFT_HAND_CLICK"); } this.triggerValue = Controller.getValue(TRIGGER_CONTROLS[triggerLookup]); - // this.triggerValue = Controller.getActionValue(this.stringTriggerAction); - print('TRIGGER VALUE:::' + this.triggerValue); if (this.triggerValue < DRAW_STRING_THRESHOLD && this.stringDrawn === true) { // firing the arrow From be7d52a773d4df32176bab92560fcef9312eecb9 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 24 Nov 2015 13:35:53 -0800 Subject: [PATCH 69/79] protect beam disabler from others --- examples/controllers/handControllerGrab.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 7f363c47ae..0a6aec6e7d 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -1216,7 +1216,6 @@ Controller.enableMapping(MAPPING_NAME); var handToDisable = 'none'; function update() { - // print('disabled in update:::'+handToDisable) if (handToDisable !== 0) { leftController.update(); } @@ -1228,14 +1227,17 @@ function update() { Messages.subscribe('Hifi-Beam-Disabler'); handleBeamDisablerMessages = function(channel, message, sender) { - print('GOT MESSAGE:::'+message) - handToDisable = message; - if (message === 'left') { - handToDisable = 1; - } - if (message === 'right') { - handToDisable = 0; + + if (sender === MyAvatar.sessionUUID) { + handToDisable = message; + if (message === 'left') { + handToDisable = 1; + } + if (message === 'right') { + handToDisable = 0; + } } + } Messages.messageReceived.connect(handleBeamDisablerMessages); From a10d00f34dcf001816b0a8b3012a0e55e923c3b9 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 24 Nov 2015 15:25:15 -0800 Subject: [PATCH 70/79] add bow to master reset --- unpublishedScripts/hiddenEntityReset.js | 63 +++++++++++++++++++++++++ unpublishedScripts/masterReset.js | 63 ++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/unpublishedScripts/hiddenEntityReset.js b/unpublishedScripts/hiddenEntityReset.js index cf9eaaa451..313b839dbc 100644 --- a/unpublishedScripts/hiddenEntityReset.js +++ b/unpublishedScripts/hiddenEntityReset.js @@ -22,6 +22,7 @@ var dollScriptURL = Script.resolvePath("../examples/toybox/doll/doll.js"); var lightsScriptURL = Script.resolvePath("../examples/toybox/lights/lightSwitch.js"); var targetsScriptURL = Script.resolvePath('../examples/toybox/ping_pong_gun/wallTarget.js'); + var bowScriptURL = Script.resolvePath('../examples/toybox/bow/bow.js'); var basketballResetterScriptURL = Script.resolvePath('basketballsResetter.js'); var targetsResetterScriptURL = Script.resolvePath('targetsResetter.js'); @@ -134,6 +135,9 @@ y: 495.6, z: 503.91 }); + + createBow(); + } function deleteAllToys() { @@ -148,6 +152,64 @@ }); } + function createBow() { + + var startPosition = { + x: 546.41, + y: 495.33, + z: 506.46 + }; + + var BOW_ROTATION = Quat.fromPitchYawRollDegrees(-103.05, -178.60, -87.27); + + var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow-deadly.fbx"; + var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow_collision_hull.obj"; + var BOW_DIMENSIONS = { + x: 0.04, + y: 1.3, + z: 0.21 + }; + + var BOW_GRAVITY = { + x: 0, + y: -9.8, + z: 0 + }; + + + var bow = Entities.addEntity({ + name: 'Hifi-Bow', + type: "Model", + modelURL: MODEL_URL, + position: startPosition, + rotation: BOW_ROTATION, + dimensions: BOW_DIMENSIONS, + collisionsWillMove: true, + gravity: BOW_GRAVITY, + shapeType: 'compound', + compoundShapeURL: COLLISION_HULL_URL, + script: bowScriptURL, + userData: JSON.stringify({ + resetMe: { + resetMe: true + }, + grabbableKey: { + invertSolidWhileHeld: true, + spatialKey: { + relativePosition: { + x: 0, + y: 0.06, + z: 0.11 + }, + relativeRotation: Quat.fromPitchYawRollDegrees(0, -90, 90) + } + } + }) + }); + + } + + function createFire() { @@ -1250,6 +1312,7 @@ } } + function cleanup() { deleteAllToys(); } diff --git a/unpublishedScripts/masterReset.js b/unpublishedScripts/masterReset.js index 60b4e7a72f..faee936edc 100644 --- a/unpublishedScripts/masterReset.js +++ b/unpublishedScripts/masterReset.js @@ -21,6 +21,7 @@ var pingPongScriptURL = Script.resolvePath('../examples/toybox/ping_pong_gun/pin var wandScriptURL = Script.resolvePath("../examples/toybox/bubblewand/wand.js"); var dollScriptURL = Script.resolvePath("../examples/toybox/doll/doll.js"); var lightsScriptURL = Script.resolvePath("../examples/toybox/lights/lightSwitch.js"); +var bowScriptURL = Script.resolvePath("../examples/toybox/bow/bow.js"); var targetsScriptURL = Script.resolvePath('../examples/toybox/ping_pong_gun/wallTarget.js'); var basketballResetterScriptURL = Script.resolvePath('basketballsResetter.js'); var targetsResetterScriptURL = Script.resolvePath('targetsResetter.js'); @@ -36,6 +37,8 @@ MasterReset = function() { deleteAllToys(); createAllToys(); + + function createAllToys() { createBlocks({ x: 548.3, @@ -111,7 +114,7 @@ MasterReset = function() { z: 503.91 }); - + createBow(); } @@ -127,6 +130,62 @@ MasterReset = function() { }); } + function createBow() { + + var startPosition = { + x: 546.41, + y: 495.33, + z: 506.46 + }; + + var BOW_ROTATION = Quat.fromPitchYawRollDegrees(-103.05, -178.60, -87.27); + + var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow-deadly.fbx"; + var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow_collision_hull.obj"; + var BOW_DIMENSIONS = { + x: 0.04, + y: 1.3, + z: 0.21 + }; + + var BOW_GRAVITY = { + x: 0, + y: -9.8, + z: 0 + }; + + var bow = Entities.addEntity({ + name: 'Hifi-Bow', + type: "Model", + modelURL: MODEL_URL, + position: startPosition, + rotation: BOW_ROTATION, + dimensions: BOW_DIMENSIONS, + collisionsWillMove: true, + gravity: BOW_GRAVITY, + shapeType: 'compound', + compoundShapeURL: COLLISION_HULL_URL, + script: bowScriptURL, + userData: JSON.stringify({ + resetMe: { + resetMe: true + }, + grabbableKey: { + invertSolidWhileHeld: true, + spatialKey: { + relativePosition: { + x: 0, + y: 0.06, + z: 0.11 + }, + relativeRotation: Quat.fromPitchYawRollDegrees(0, -90, 90) + } + } + }) + }); + + } + function createFire() { @@ -883,7 +942,7 @@ MasterReset = function() { type: "Model", modelURL: MODEL_URL, shapeType: 'compound', - compoundShapeURL:COLLISION_HULL_URL, + compoundShapeURL: COLLISION_HULL_URL, script: pingPongScriptURL, position: position, rotation: rotation, From 914805ef416b862f2d4a3e8829c0ff50c226a53c Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Tue, 24 Nov 2015 15:31:14 -0800 Subject: [PATCH 71/79] added --- unpublishedScripts/hiddenEntityReset.js | 1 - 1 file changed, 1 deletion(-) diff --git a/unpublishedScripts/hiddenEntityReset.js b/unpublishedScripts/hiddenEntityReset.js index 313b839dbc..6ffa3fdecf 100644 --- a/unpublishedScripts/hiddenEntityReset.js +++ b/unpublishedScripts/hiddenEntityReset.js @@ -176,7 +176,6 @@ z: 0 }; - var bow = Entities.addEntity({ name: 'Hifi-Bow', type: "Model", From f4ba2eb4c42e38842d6cce6f7d5f03b6e3bfb60b Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Tue, 24 Nov 2015 20:25:17 -0600 Subject: [PATCH 72/79] Update BUILD_OSX for CMake env vars. - openssl requires OPENSSL_ROOT_DIR to be set. - qt5 QT_CMAKE_PREFIX_PATH was outdated. --- BUILD_OSX.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/BUILD_OSX.md b/BUILD_OSX.md index 54360ad4b8..c8f19710ca 100644 --- a/BUILD_OSX.md +++ b/BUILD_OSX.md @@ -7,11 +7,18 @@ Please read the [general build guide](BUILD.md) for information on dependencies We no longer require install of qt5 via our [homebrew formulas repository](https://github.com/highfidelity/homebrew-formulas). Versions of Qt that are 5.5.x and above provide a mechanism to disable the wireless scanning we previously had a custom patch for. -###Qt +###OpenSSL and Qt -Assuming you've installed Qt 5 using the homebrew instructions above, you'll need to set QT_CMAKE_PREFIX_PATH so CMake can find your installation of Qt. For Qt 5.5.1 installed via homebrew, set QT_CMAKE_PREFIX_PATH as follows. +Assuming you've installed OpenSSL or Qt 5 using the homebrew instructions above, you'll need to set OPENSSL_ROOT_DIR and QT_CMAKE_PREFIX_PATH so CMake can find your installations. +For OpenSSL installed via homebrew, set OPENSSL_ROOT_DIR: - export QT_CMAKE_PREFIX_PATH=/usr/local/Cellar/qt5/5.5.1/lib/cmake + export OPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2d_1 + +For Qt 5.5.1 installed via homebrew, set QT_CMAKE_PREFIX_PATH as follows. + + export QT_CMAKE_PREFIX_PATH=/usr/local/Cellar/qt5/5.5.1_2/lib/cmake + +Not that these use the versions from homebrew formulae at the time of this writing, and the version in the path will likely change. ###Xcode If Xcode is your editor of choice, you can ask CMake to generate Xcode project files instead of Unix Makefiles. From c237485023fec2ac32f077007eba3c02219b9fee Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 27 Nov 2015 11:21:18 -0800 Subject: [PATCH 73/79] revert playaspawner --- examples/playa/playaSpawner.js | 68 +++++++++++----------------------- 1 file changed, 21 insertions(+), 47 deletions(-) diff --git a/examples/playa/playaSpawner.js b/examples/playa/playaSpawner.js index b929a219b9..61101a0e62 100644 --- a/examples/playa/playaSpawner.js +++ b/examples/playa/playaSpawner.js @@ -41,33 +41,21 @@ orientationOf = function(vector) { var ground, wall; var boxes = []; var dustSystems = []; -var ZERO_VEC = { - x: 0, - y: 0, - z: 0 -}; +var ZERO_VEC = {x: 0, y: 0, z: 0}; Script.include("../libraries/utils.js"); function spawnGround() { var groundModelURL = "https://hifi-public.s3.amazonaws.com/alan/Playa/Ground.fbx"; - var groundPosition = Vec3.sum(MyAvatar.position, { - x: 0, - y: -2, - z: 0 - }); + var groundPosition = Vec3.sum(MyAvatar.position, {x: 0, y: -2, z: 0}); ground = Entities.addEntity({ type: "Model", modelURL: groundModelURL, shapeType: "box", position: groundPosition, - dimensions: { - x: 900, - y: 0.82, - z: 900 - }, + dimensions: {x: 900, y: 0.82, z: 900}, }); - // Script.addEventHandler(ground, "collisionWithEntity", entityCollisionWithGround); + // Script.addEventHandler(ground, "collisionWithEntity", entityCollisionWithGround); } @@ -92,7 +80,7 @@ function spawnGround() { isEmitting: true, polarStart: Math.PI/2, polarFinish: Math.PI/2, - emitOrientation: orientationChange, + emitOrientation: orientationChange, radiusSpread: 0.1, radiusStart: particleRadius, radiusFinish: particleRadius + particleRadius/2, @@ -118,11 +106,7 @@ function spawnBoxes() { var numBoxes = 200; var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(Camera.getOrientation()))); for (var i = 0; i < numBoxes; i++) { - var position = Vec3.sum(center, { - x: Math.random() * numBoxes, - y: Math.random() * 3, - z: Math.random() * numBoxes - }) + var position = Vec3.sum(center, {x: Math.random() * numBoxes, y: Math.random() * 3, z: Math.random() * numBoxes }) var box = Entities.addEntity({ type: "Model", modelURL: boxModelURL, @@ -130,21 +114,9 @@ function spawnBoxes() { shapeType: "box", position: position, collisionsWillMove: true, - dimensions: { - x: 1, - y: 2, - z: 3 - }, - velocity: { - x: 0, - y: -.01, - z: 0 - }, - gravity: { - x: 0, - y: -2.5 - Math.random() * 6, - z: 0 - } + dimensions: {x: 1, y: 2, z: 3}, + velocity: {x: 0, y: -.01, z: 0}, + gravity: {x: 0, y: -2.5 - Math.random() * 6, z: 0} }); boxes.push(box); @@ -155,14 +127,16 @@ spawnGround(); spawnBoxes(); -// function cleanup() { -// Entities.deleteEntity(ground); -// boxes.forEach(function(box) { -// Entities.deleteEntity(box); -// }); -// dustSystems.forEach(function(dustEffect) { -// Entities.deleteEntity(dustEffect); -// }) -// } +function cleanup() { + Entities.deleteEntity(ground); + boxes.forEach(function(box){ + Entities.deleteEntity(box); + }); + dustSystems.forEach(function(dustEffect) { + Entities.deleteEntity(dustEffect); + }) +} + +Script.scriptEnding.connect(cleanup); + -// Script.scriptEnding.connect(cleanup); \ No newline at end of file From 8daeb0fa0df59a3626bae4fdc38e312f55f55e20 Mon Sep 17 00:00:00 2001 From: "James B. Pollack" Date: Fri, 27 Nov 2015 12:18:11 -0800 Subject: [PATCH 74/79] revert hand grab script to current master --- examples/controllers/handControllerGrab.js | 92 +++++++++------------- 1 file changed, 37 insertions(+), 55 deletions(-) diff --git a/examples/controllers/handControllerGrab.js b/examples/controllers/handControllerGrab.js index 0a6aec6e7d..f8a2eeefa5 100644 --- a/examples/controllers/handControllerGrab.js +++ b/examples/controllers/handControllerGrab.js @@ -37,7 +37,6 @@ var BUMPER_ON_VALUE = 0.5; var DISTANCE_HOLDING_RADIUS_FACTOR = 5; // multiplied by distance between hand and object var DISTANCE_HOLDING_ACTION_TIMEFRAME = 0.1; // how quickly objects move to their new position var DISTANCE_HOLDING_ROTATION_EXAGGERATION_FACTOR = 2.0; // object rotates this much more than hand did - var NO_INTERSECT_COLOR = { red: 10, green: 10, @@ -87,7 +86,6 @@ var ZERO_VEC = { y: 0, z: 0 }; - var NULL_ACTION_ID = "{00000000-0000-0000-000000000000}"; var MSEC_PER_SEC = 1000.0; @@ -97,8 +95,7 @@ var ACTION_TTL = 15; // seconds var ACTION_TTL_REFRESH = 5; var PICKS_PER_SECOND_PER_HAND = 5; var MSECS_PER_SEC = 1000.0; -var GRABBABLE_PROPERTIES = [ - "position", +var GRABBABLE_PROPERTIES = ["position", "rotation", "gravity", "ignoreForCollisions", @@ -107,15 +104,17 @@ var GRABBABLE_PROPERTIES = [ "name" ]; + var GRABBABLE_DATA_KEY = "grabbableKey"; // shared with grab.js var GRAB_USER_DATA_KEY = "grabKey"; // shared with grab.js -var BEAM_DISABLER_KEY = 'beamDisablerKey' var DEFAULT_GRABBABLE_DATA = { grabbable: true, invertSolidWhileHeld: false }; +var disabledHand = 'none'; + // states for the state machine var STATE_OFF = 0; @@ -308,14 +307,7 @@ function MyController(hand) { position: closePoint, linePoints: [ZERO_VEC, farPoint], color: color, - lifetime: 0.1, - collisionsWillMove: false, - ignoreForCollisions: true, - userData: JSON.stringify({ - grabbableKey: { - grabbable: false - } - }) + lifetime: 0.1 }); } @@ -330,14 +322,7 @@ function MyController(hand) { position: closePoint, linePoints: [ZERO_VEC, farPoint], color: color, - lifetime: LIFETIME, - collisionsWillMove: false, - ignoreForCollisions: true, - userData: JSON.stringify({ - grabbableKey: { - grabbable: false - } - }) + lifetime: LIFETIME }); } else { var age = Entities.getEntityProperties(this.pointer, "age").age; @@ -411,6 +396,11 @@ function MyController(hand) { this.search = function() { this.grabbedEntity = null; + // if this hand is the one that's disabled, we don't want to search for anything at all + if (this.hand === disabledHand) { + return; + } + if (this.state == STATE_SEARCHING ? this.triggerSmoothedReleased() : this.bumperReleased()) { this.setState(STATE_RELEASE); return; @@ -455,7 +445,17 @@ function MyController(hand) { // the ray is intersecting something we can move. var intersectionDistance = Vec3.distance(pickRay.origin, intersection.intersection); + //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"]) { + if (this.hand === RIGHT_HAND) { + disabledHand = LEFT_HAND; + } else { + disabledHand = RIGHT_HAND; + } + } else { + disabledHand = 'none'; + } if (intersection.properties.name == "Grab Debug Entity") { continue; @@ -526,14 +526,7 @@ function MyController(hand) { green: 255, blue: 0 }, - lifetime: 0.1, - collisionsWillMove: false, - ignoreForCollisions: true, - userData: JSON.stringify({ - grabbableKey: { - grabbable: false - } - }) + lifetime: 0.1 }); } @@ -547,7 +540,8 @@ function MyController(hand) { if (typeof grabbableDataForCandidate.grabbable !== 'undefined' && !grabbableDataForCandidate.grabbable) { continue; } - var propsForCandidate = Entities.getEntityProperties(nearbyEntities[i], GRABBABLE_PROPERTIES); + var propsForCandidate = + Entities.getEntityProperties(nearbyEntities[i], GRABBABLE_PROPERTIES); if (propsForCandidate.type == 'Unknown') { continue; @@ -743,8 +737,15 @@ function MyController(hand) { this.nearGrabbing = function() { var now = Date.now(); + var grabbableData = getEntityCustomData(GRABBABLE_DATA_KEY, this.grabbedEntity, DEFAULT_GRABBABLE_DATA); + var turnOffOtherHand = grabbableData["turnOffOtherHand"]; + if (turnOffOtherHand) { + //don't activate the second hand grab because the script is handling the second hand logic + return; + } + if (this.state == STATE_NEAR_GRABBING && this.triggerSmoothedReleased()) { this.setState(STATE_RELEASE); Entities.callEntityMethod(this.grabbedEntity, "releaseGrab"); @@ -1093,6 +1094,10 @@ function MyController(hand) { this.release = function() { + if (this.hand !== disabledHand) { + //release the disabled hand when we let go with the main one + disabledHand = 'none'; + } this.lineOff(); if (this.grabbedEntity !== null) { @@ -1213,35 +1218,12 @@ mapping.from([Controller.Standard.LB]).peek().to(leftController.bumperPress); Controller.enableMapping(MAPPING_NAME); -var handToDisable = 'none'; function update() { - if (handToDisable !== 0) { - leftController.update(); - } - if (handToDisable !== 1) { - rightController.update(); - } + rightController.update(); + leftController.update(); } -Messages.subscribe('Hifi-Beam-Disabler'); - -handleBeamDisablerMessages = function(channel, message, sender) { - - if (sender === MyAvatar.sessionUUID) { - handToDisable = message; - if (message === 'left') { - handToDisable = 1; - } - if (message === 'right') { - handToDisable = 0; - } - } - -} - -Messages.messageReceived.connect(handleBeamDisablerMessages); - function cleanup() { rightController.cleanup(); leftController.cleanup(); From 017ffb8f2ea638592e2c76455bdb074951fbcb44 Mon Sep 17 00:00:00 2001 From: James Pollack Date: Sat, 28 Nov 2015 22:54:31 -0800 Subject: [PATCH 75/79] change way messages get sent to the hand disabler --- examples/toybox/bow/bow.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/examples/toybox/bow/bow.js b/examples/toybox/bow/bow.js index fad5148992..f38aa64921 100644 --- a/examples/toybox/bow/bow.js +++ b/examples/toybox/bow/bow.js @@ -158,7 +158,10 @@ this.isGrabbed = true; this.initialHand = this.hand; - Messages.sendMessage('Hifi-Beam-Disabler', this.initialHand); + + //disable the opposite hand in handControllerGrab.js by message + var handToDisable = this.initialHand === 'right' ? 'left' : 'right'; + Messages.sendMessage('Hifi-Hand-Disabler', handToDisable); setEntityCustomData('grabbableKey', this.entityID, { grabbable: false, @@ -437,13 +440,13 @@ checkStringHand: function() { //invert the hands because our string will be held with the opposite hand of the first one we pick up the bow with - var triggerLookup; + var triggerLookup; if (this.initialHand === 'left') { - triggerLookup=1; + triggerLookup = 1; this.getStringHandPosition = MyAvatar.getRightPalmPosition; } else if (this.initialHand === 'right') { this.getStringHandPosition = MyAvatar.getLeftPalmPosition; - triggerLookup=0; + triggerLookup = 0; } this.triggerValue = Controller.getValue(TRIGGER_CONTROLS[triggerLookup]); From 0625d4f4c2fe7fb2f57e6fa925ee7bcf1b59d8fd Mon Sep 17 00:00:00 2001 From: James Pollack Date: Sat, 28 Nov 2015 23:40:58 -0800 Subject: [PATCH 76/79] remove debugging string --- examples/toybox/bow/createBow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/toybox/bow/createBow.js b/examples/toybox/bow/createBow.js index 53798c0fc3..880b0920e8 100644 --- a/examples/toybox/bow/createBow.js +++ b/examples/toybox/bow/createBow.js @@ -11,7 +11,7 @@ // -var SCRIPT_URL = Script.resolvePath('bow.js?'+Math.random()); +var SCRIPT_URL = Script.resolvePath('bow.js'); var MODEL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow-deadly.fbx"; var COLLISION_HULL_URL = "https://hifi-public.s3.amazonaws.com/models/bow/new/bow_collision_hull.obj"; From d6b13ef42c6a1f4627dacad65e92f1bec3771a0e Mon Sep 17 00:00:00 2001 From: David Rowe Date: Sat, 28 Nov 2015 11:12:39 +1300 Subject: [PATCH 77/79] Fix crash when try to remove invalid menu item in JavaScript --- libraries/ui/src/VrMenu.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libraries/ui/src/VrMenu.cpp b/libraries/ui/src/VrMenu.cpp index 211e0e0f72..41cf27efb2 100644 --- a/libraries/ui/src/VrMenu.cpp +++ b/libraries/ui/src/VrMenu.cpp @@ -183,6 +183,10 @@ void VrMenu::insertAction(QAction* before, QAction* action) { } void VrMenu::removeAction(QAction* action) { + if (!action) { + qWarning("Attempted to remove invalid menu action"); + return; + } MenuUserData* userData = MenuUserData::forObject(action); if (!userData) { qWarning("Attempted to remove menu action with no found QML object"); From 866037ec7cb9e631c87d0be75c3f5d485060eb20 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Sat, 28 Nov 2015 12:43:00 +1300 Subject: [PATCH 78/79] Fix menu shortcut keys set by JavaScript not working --- interface/src/Menu.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interface/src/Menu.cpp b/interface/src/Menu.cpp index 2b82587bbc..41e65daaf8 100644 --- a/interface/src/Menu.cpp +++ b/interface/src/Menu.cpp @@ -949,6 +949,7 @@ void Menu::addMenuItem(const MenuItemProperties& properties) { QShortcut* shortcut = NULL; if (!properties.shortcutKeySequence.isEmpty()) { shortcut = new QShortcut(properties.shortcutKeySequence, this); + shortcut->setContext(Qt::WidgetWithChildrenShortcut); } // check for positioning requests From 79b06c3c8a639ce82d2f7fc1414d4151ee5f7bcc Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Mon, 30 Nov 2015 11:14:35 -0800 Subject: [PATCH 79/79] cmake: Fix quazip configure on linux/mac if QT_CMAKE_PREFIX_PATH is undefined. --- cmake/externals/quazip/CMakeLists.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmake/externals/quazip/CMakeLists.txt b/cmake/externals/quazip/CMakeLists.txt index ddac942692..ac19e7d680 100644 --- a/cmake/externals/quazip/CMakeLists.txt +++ b/cmake/externals/quazip/CMakeLists.txt @@ -4,7 +4,14 @@ cmake_policy(SET CMP0046 OLD) include(ExternalProject) -string(REPLACE \\ / QT_CMAKE_PREFIX_PATH $ENV{QT_CMAKE_PREFIX_PATH}) +if (WIN32) + # windows shell does not like backslashes expanded on the command line, + # so convert all backslashes in the QT path to forward slashes + string(REPLACE \\ / QT_CMAKE_PREFIX_PATH $ENV{QT_CMAKE_PREFIX_PATH}) +elseif ($ENV{QT_CMAKE_PREFIX_PATH}) + set(QT_CMAKE_PREFIX_PATH $ENV{QT_CMAKE_PREFIX_PATH}) +endif () + ExternalProject_Add( ${EXTERNAL_NAME} URL http://s3-us-west-1.amazonaws.com/hifi-production/dependencies/quazip-0.6.2.zip