From 4927a77382c6147702f7021d7c1431b9d033d182 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 24 Apr 2014 18:15:39 -0700 Subject: [PATCH 1/6] Some testing with editModels.js --- examples/editModels.js | 293 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 293 insertions(+) create mode 100644 examples/editModels.js diff --git a/examples/editModels.js b/examples/editModels.js new file mode 100644 index 0000000000..2724a17373 --- /dev/null +++ b/examples/editModels.js @@ -0,0 +1,293 @@ +// +// editModels.js +// examples +// +// Created by Clément Brisset on 4/24/14. +// Copyright 2014 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 +// + + +var LEFT_BUTTON_FWD = 5; +var LEFT_BUTTON_3 = 3; +var RIGHT_BUTTON_FWD = 11; +var RIGHT_BUTTON_3 = 9; +var leftBallAlreadyInHand = false; +var rightBallAlreadyInHand = false; +var leftHandParticle; +var rightHandParticle; +var targetRadius = 0.25; + + + + +var LEFT_PALM = 0; +var LEFT_TIP = 1; +var LEFT_TRIGGER = 0; + +var RIGHT_PALM = 2; +var RIGHT_TIP = 3; +var RIGHT_TRIGGER = 1; + +var previewLineWidth = 4; +var leftLaser = Overlays.addOverlay("line3d", { + position: { x: 0, y: 0, z: 0}, + end: { x: 0, y: 0, z: 0}, + color: { red: 255, green: 0, blue: 0}, + alpha: 1, + visible: false, + lineWidth: previewLineWidth + }); +var rightLaser = Overlays.addOverlay("line3d", { + position: { x: 0, y: 0, z: 0}, + end: { x: 0, y: 0, z: 0}, + color: { red: 255, green: 0, blue: 0}, + alpha: 1, + visible: false, + lineWidth: previewLineWidth + }); + +var particle = Particles.addParticle({ position: { x: 0, y: 0, z:0 }, + velocity: { x: 0, y: 0, z: 0}, + gravity: { x: 0, y: 0, z: 0}, + radius: 0.05, + damping: 0.999, + color: { red: 255, green: 0, blue: 0 }, + modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst", + }) + + +var wantDebugging = false; +function debugPrint(message) { + if (wantDebugging) { + print(message); + } +} + +function getBallHoldPosition(whichSide) { + var normal; + var tipPosition; + if (whichSide == LEFT_PALM) { + normal = Controller.getSpatialControlNormal(LEFT_PALM); + tipPosition = Controller.getSpatialControlPosition(LEFT_TIP); + } else { + normal = Controller.getSpatialControlNormal(RIGHT_PALM); + tipPosition = Controller.getSpatialControlPosition(RIGHT_TIP); + } + + var BALL_FORWARD_OFFSET = 0.08; // put the ball a bit forward of fingers + position = { x: BALL_FORWARD_OFFSET * normal.x, + y: BALL_FORWARD_OFFSET * normal.y, + z: BALL_FORWARD_OFFSET * normal.z }; + + position.x += tipPosition.x; + position.y += tipPosition.y; + position.z += tipPosition.z; + + return position; +} + +function checkControllerSide(whichSide) { + var palmPosition; + var tipPosition; + var overlay; + var triggerValue; + var BUTTON_3; + + if (whichSide == LEFT_PALM) { + palmPosition = Controller.getSpatialControlPosition(LEFT_PALM); + tipPosition = Controller.getSpatialControlPosition(LEFT_TIP); + overlay = leftLaser; + triggerValue = Controller.getTriggerValue(LEFT_TRIGGER); + BUTTON_3 = LEFT_BUTTON_3; + } else { + palmPosition = Controller.getSpatialControlPosition(RIGHT_PALM); + tipPosition = Controller.getSpatialControlPosition(RIGHT_TIP); + overlay = rightLaser; + triggerValue = Controller.getTriggerValue(RIGHT_TRIGGER); + BUTTON_3 = RIGHT_BUTTON_3; + } + + var createButtonPressed = (Controller.isButtonPressed(BUTTON_3)); + if (createButtonPressed) { + Particles.editParticle(particle, { + position: { x: 0, y: 0, z:0 }, + velocity: { x: 0, y: 0, z: 0}, + gravity: { x: 0, y: 0, z: 0}, + radius: 0.05, + damping: 0.999, + color: { red: 255, green: 0, blue: 0 }, + modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst", + }); + } + + + var vector = Vec3.subtract(tipPosition, palmPosition); + var endPosition = Vec3.sum(palmPosition, Vec3.multiply(vector, 100)); + + Overlays.editOverlay(overlay, { + position: palmPosition, + end: endPosition, + visible: true + }); + + + if (triggerValue > 0.9) { + + } + + return; + var BUTTON_FWD; + var BUTTON_3; + var palmPosition; + var ballAlreadyInHand; + var handMessage; + + if (whichSide == LEFT_PALM) { + BUTTON_FWD = LEFT_BUTTON_FWD; + BUTTON_3 = LEFT_BUTTON_3; + palmPosition = Controller.getSpatialControlPosition(LEFT_PALM); + ballAlreadyInHand = leftBallAlreadyInHand; + handMessage = "LEFT"; + } else { + BUTTON_FWD = RIGHT_BUTTON_FWD; + BUTTON_3 = RIGHT_BUTTON_3; + palmPosition = Controller.getSpatialControlPosition(RIGHT_PALM); + ballAlreadyInHand = rightBallAlreadyInHand; + handMessage = "RIGHT"; + } + + var grabButtonPressed = (Controller.isButtonPressed(BUTTON_FWD) || Controller.isButtonPressed(BUTTON_3)); + + // If I don't currently have a ball in my hand, then try to catch closest one + if (!ballAlreadyInHand && grabButtonPressed) { + var closestParticle = Particles.findClosestParticle(palmPosition, targetRadius); + + if (closestParticle.isKnownID) { + + debugPrint(handMessage + " HAND- CAUGHT SOMETHING!!"); + + if (whichSide == LEFT_PALM) { + leftBallAlreadyInHand = true; + leftHandParticle = closestParticle; + } else { + rightBallAlreadyInHand = true; + rightHandParticle = closestParticle; + } + var ballPosition = getBallHoldPosition(whichSide); + var properties = { position: { x: ballPosition.x, + y: ballPosition.y, + z: ballPosition.z }, + velocity : { x: 0, y: 0, z: 0}, inHand: true }; + Particles.editParticle(closestParticle, properties); + + + return; // exit early + } + } + + // change ball color logic... + // + //if (wasButtonJustPressed()) { + // rotateColor(); + //} + + // If '3' is pressed, and not holding a ball, make a new one + if (Controller.isButtonPressed(BUTTON_3) && !ballAlreadyInHand) { + var ballPosition = getBallHoldPosition(whichSide); + var properties = { position: ballPosition, + velocity: { x: 0, y: 0, z: 0}, + gravity: { x: 0, y: 0, z: 0}, + inHand: true, + radius: 0.05, + damping: 0.999, + color: { red: 255, green: 0, blue: 0 }, + modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst", + }; + + newParticle = Particles.addParticle(properties); + if (whichSide == LEFT_PALM) { + leftBallAlreadyInHand = true; + leftHandParticle = newParticle; + } else { + rightBallAlreadyInHand = true; + rightHandParticle = newParticle; + } + + return; // exit early + } + + if (ballAlreadyInHand) { + if (whichSide == LEFT_PALM) { + handParticle = leftHandParticle; + whichTip = LEFT_TIP; + } else { + handParticle = rightHandParticle; + whichTip = RIGHT_TIP; + } + + // If holding the ball keep it in the palm + if (grabButtonPressed) { + debugPrint(">>>>> " + handMessage + "-BALL IN HAND, grabbing, hold and move"); + var ballPosition = getBallHoldPosition(whichSide); + var properties = { position: { x: ballPosition.x, + y: ballPosition.y, + z: ballPosition.z }, + }; + Particles.editParticle(handParticle, properties); + } else { + debugPrint(">>>>> " + handMessage + "-BALL IN HAND, not grabbing, THROW!!!"); + // If toy ball just released, add velocity to it! + var tipVelocity = Controller.getSpatialControlVelocity(whichTip); + var THROWN_VELOCITY_SCALING = 1.5; + var properties = { + velocity: { x: tipVelocity.x * THROWN_VELOCITY_SCALING, + y: tipVelocity.y * THROWN_VELOCITY_SCALING, + z: tipVelocity.z * THROWN_VELOCITY_SCALING } , + inHand: false, + gravity: { x: 0, y: -2, z: 0}, + }; + + Particles.editParticle(handParticle, properties); + + if (whichSide == LEFT_PALM) { + leftBallAlreadyInHand = false; + leftHandParticle = false; + } else { + rightBallAlreadyInHand = false; + rightHandParticle = false; + } + } + } +} + + +function checkController(deltaTime) { + var numberOfButtons = Controller.getNumberOfButtons(); + var numberOfTriggers = Controller.getNumberOfTriggers(); + var numberOfSpatialControls = Controller.getNumberOfSpatialControls(); + var controllersPerTrigger = numberOfSpatialControls / numberOfTriggers; + + // this is expected for hydras + if (!(numberOfButtons==12 && numberOfTriggers == 2 && controllersPerTrigger == 2)) { + debugPrint("no hydra connected?"); + return; // bail if no hydra + } + + checkControllerSide(LEFT_PALM); + checkControllerSide(RIGHT_PALM); +} + +function scriptEnding() { + Overlays.deleteOverlay(leftLaser); + Overlays.deleteOverlay(rightLaser); +} +Script.scriptEnding.connect(scriptEnding); + +// register the call back so it fires before each data send +Script.update.connect(checkController); + + + From 0457f2035de829492f0d5bd1ec9af13e614b7848 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 28 Apr 2014 14:02:09 -0700 Subject: [PATCH 2/6] more editModels work --- examples/editModels.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/editModels.js b/examples/editModels.js index 2724a17373..e0412122cc 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -42,7 +42,7 @@ var leftLaser = Overlays.addOverlay("line3d", { }); var rightLaser = Overlays.addOverlay("line3d", { position: { x: 0, y: 0, z: 0}, - end: { x: 0, y: 0, z: 0}, + end: { x: 0, y: 0, z: 0}, color: { red: 255, green: 0, blue: 0}, alpha: 1, visible: false, @@ -112,20 +112,24 @@ function checkControllerSide(whichSide) { var createButtonPressed = (Controller.isButtonPressed(BUTTON_3)); if (createButtonPressed) { + var position = MyAvatar.position; + var forwardVector = Quat.getFront(MyAvatar.orientation); + position = Vec3.sum(position, Vec3.multiply(forwardVector, 2)); Particles.editParticle(particle, { - position: { x: 0, y: 0, z:0 }, + position: position, velocity: { x: 0, y: 0, z: 0}, gravity: { x: 0, y: 0, z: 0}, radius: 0.05, damping: 0.999, color: { red: 255, green: 0, blue: 0 }, + lifetime: 1000, modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst", }); } var vector = Vec3.subtract(tipPosition, palmPosition); - var endPosition = Vec3.sum(palmPosition, Vec3.multiply(vector, 100)); + var endPosition = Vec3.sum(palmPosition, Vec3.multiply(vector, 4)); Overlays.editOverlay(overlay, { position: palmPosition, @@ -136,6 +140,7 @@ function checkControllerSide(whichSide) { if (triggerValue > 0.9) { + } return; From 8068a915aac103640bdb82080efc3e2e757fc9b1 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 30 Apr 2014 16:50:40 -0700 Subject: [PATCH 3/6] More work on editModels --- examples/editModels.js | 427 +++++++++++---------------- libraries/script-engine/src/Vec3.cpp | 4 + libraries/script-engine/src/Vec3.h | 3 +- 3 files changed, 182 insertions(+), 252 deletions(-) diff --git a/examples/editModels.js b/examples/editModels.js index e0412122cc..f9fc5fc31e 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -9,266 +9,175 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +var LASER_WIDTH = 4; +var LASER_COLOR = { red: 255, green: 0, blue: 0 }; +var LASER_LENGTH_FACTOR = 4; -var LEFT_BUTTON_FWD = 5; -var LEFT_BUTTON_3 = 3; -var RIGHT_BUTTON_FWD = 11; -var RIGHT_BUTTON_3 = 9; -var leftBallAlreadyInHand = false; -var rightBallAlreadyInHand = false; -var leftHandParticle; -var rightHandParticle; -var targetRadius = 0.25; +var LEFT = 0; +var RIGHT = 1; - - -var LEFT_PALM = 0; -var LEFT_TIP = 1; -var LEFT_TRIGGER = 0; - -var RIGHT_PALM = 2; -var RIGHT_TIP = 3; -var RIGHT_TRIGGER = 1; - -var previewLineWidth = 4; -var leftLaser = Overlays.addOverlay("line3d", { - position: { x: 0, y: 0, z: 0}, - end: { x: 0, y: 0, z: 0}, - color: { red: 255, green: 0, blue: 0}, - alpha: 1, - visible: false, - lineWidth: previewLineWidth - }); -var rightLaser = Overlays.addOverlay("line3d", { - position: { x: 0, y: 0, z: 0}, - end: { x: 0, y: 0, z: 0}, - color: { red: 255, green: 0, blue: 0}, +function controller(wichSide) { + this.side = wichSide; + this.palm = 2 * wichSide; + this.tip = 2 * wichSide + 1; + this.trigger = wichSide; + + this.oldPalmPosition = Controller.getSpatialControlPosition(this.palm); + this.palmPosition = Controller.getSpatialControlPosition(this.palm); + + this.oldTipPosition = Controller.getSpatialControlPosition(this.tip); + this.tipPosition = Controller.getSpatialControlPosition(this.tip); + + this.triggerValue = Controller.getTriggerValue(this.trigger); + + this.pressed = false; // is trigger pressed + this.pressing = false; // is trigger being pressed (is pressed now but wasn't previously) + + this.grabbing = false; + this.particleID = 0; + this.oldParticlePosition = { x: 0, y: 0, z: 0 }; + + this.laser = Overlays.addOverlay("line3d", { + position: this.palmPosition, + end: this.tipPosition, + color: LASER_COLOR, alpha: 1, visible: false, - lineWidth: previewLineWidth + lineWidth: LASER_WIDTH }); + + this.grab = function (particle) { + if (!particle.isKnownID) { + var identify = Particles.identifyParticle(particle); + if (!identify.isKnownID) { + return; + } + } + this.grabbing = true; + this.particle = identify; + + var properties = Particles.getParticleProperties(this.particle); + + this.oldParticlePosition = properties.position; + + print("Grabbed: " + this.oldParticlePosition); + } + + this.release = function () { + this.grabbing = false; + this.particleID = -1; + this.oldParticlePosition = { x: 0, y: 0, z: 0 }; + } + + this.checkTrigger = function () { + if (this.triggerValue > 0.9) { + if (this.pressed) { + this.pressing = false; + } else { + this.pressing = true; + } + this.pressed = true; + } else { + this.pressing = false; + this.pressed = false; + } + } + + this.moveLaser = function () { + var vector = Vec3.subtract(this.tipPosition, this.palmPosition); + var endPosition = Vec3.sum(this.palmPosition, Vec3.multiply(vector, LASER_LENGTH_FACTOR)); + + Overlays.editOverlay(this.laser, { + position: this.palmPosition, + end: endPosition, + visible: true + }); + } + + this.moveParticle = function () { + if (this.grabbing) { + + + + } + } + + this.update = function () { + this.oldPalmPosition = this.palmPosition; + this.oldTipPosition = this.tipPosition; + this.palmPosition = Controller.getSpatialControlPosition(this.palm); + this.tipPosition = Controller.getSpatialControlPosition(this.tip); + this.triggerValue = Controller.getTriggerValue(this.trigger); + + this.checkTrigger(); + + if (this.pressing) { + var particle = -1; + +// P P - Particle +// /| A - Palm +// / | d B - unit vector toward tip +// / | X - base of the perpendicular line +// A---X----->B d - distance fom axis +// x x - distance from A +// +// |X-A| = (P-A).B, in B unit +// X == A + ((P-A).B)B +// d = |P-X| in standard unit + + var A = this.palmPosition; + var B = Vec3.subtract(this.tipPosition, A); + var P = particlePosition; + + var x = Vec3.dot(Vec3.subtract(P, A), B); + var X = Vec3.sum(A, Vec3.multiply(B, x)); + var d = Vec3.length(Vec3.subtract(P, X)); + + + if (d < 0.5 && 0 < x && x < LASER_LENGTH_FACTOR) { + particle = particleTest; + } + Vec3.print("A = ", A); + Vec3.print("B = ", B); + Vec3.print("P = ", P); + Vec3.print("X = ", X); + print("d = " + d + ", x = " + x); + this.grab(particle); + } + + if (!this.pressed && this.grabbing) { + this.release(); + } + + + this.moveLaser(); + this.moveParticle(); + } + + this.cleanup = function () { + Overlays.deleteOverlay(this.laser); + } +} -var particle = Particles.addParticle({ position: { x: 0, y: 0, z:0 }, +var leftController = new controller(LEFT); +var rightController = new controller(RIGHT); + + + +var particlePosition = MyAvatar.position; +var particleRadius = 0.05; +var particleTest = Particles.addParticle({ position: particlePosition, velocity: { x: 0, y: 0, z: 0}, gravity: { x: 0, y: 0, z: 0}, - radius: 0.05, + radius: particleRadius, damping: 0.999, color: { red: 255, green: 0, blue: 0 }, - modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst", + lifetime: 100, + modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst" }) -var wantDebugging = false; -function debugPrint(message) { - if (wantDebugging) { - print(message); - } -} - -function getBallHoldPosition(whichSide) { - var normal; - var tipPosition; - if (whichSide == LEFT_PALM) { - normal = Controller.getSpatialControlNormal(LEFT_PALM); - tipPosition = Controller.getSpatialControlPosition(LEFT_TIP); - } else { - normal = Controller.getSpatialControlNormal(RIGHT_PALM); - tipPosition = Controller.getSpatialControlPosition(RIGHT_TIP); - } - - var BALL_FORWARD_OFFSET = 0.08; // put the ball a bit forward of fingers - position = { x: BALL_FORWARD_OFFSET * normal.x, - y: BALL_FORWARD_OFFSET * normal.y, - z: BALL_FORWARD_OFFSET * normal.z }; - - position.x += tipPosition.x; - position.y += tipPosition.y; - position.z += tipPosition.z; - - return position; -} - -function checkControllerSide(whichSide) { - var palmPosition; - var tipPosition; - var overlay; - var triggerValue; - var BUTTON_3; - - if (whichSide == LEFT_PALM) { - palmPosition = Controller.getSpatialControlPosition(LEFT_PALM); - tipPosition = Controller.getSpatialControlPosition(LEFT_TIP); - overlay = leftLaser; - triggerValue = Controller.getTriggerValue(LEFT_TRIGGER); - BUTTON_3 = LEFT_BUTTON_3; - } else { - palmPosition = Controller.getSpatialControlPosition(RIGHT_PALM); - tipPosition = Controller.getSpatialControlPosition(RIGHT_TIP); - overlay = rightLaser; - triggerValue = Controller.getTriggerValue(RIGHT_TRIGGER); - BUTTON_3 = RIGHT_BUTTON_3; - } - - var createButtonPressed = (Controller.isButtonPressed(BUTTON_3)); - if (createButtonPressed) { - var position = MyAvatar.position; - var forwardVector = Quat.getFront(MyAvatar.orientation); - position = Vec3.sum(position, Vec3.multiply(forwardVector, 2)); - Particles.editParticle(particle, { - position: position, - velocity: { x: 0, y: 0, z: 0}, - gravity: { x: 0, y: 0, z: 0}, - radius: 0.05, - damping: 0.999, - color: { red: 255, green: 0, blue: 0 }, - lifetime: 1000, - modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst", - }); - } - - - var vector = Vec3.subtract(tipPosition, palmPosition); - var endPosition = Vec3.sum(palmPosition, Vec3.multiply(vector, 4)); - - Overlays.editOverlay(overlay, { - position: palmPosition, - end: endPosition, - visible: true - }); - - - if (triggerValue > 0.9) { - - - } - - return; - var BUTTON_FWD; - var BUTTON_3; - var palmPosition; - var ballAlreadyInHand; - var handMessage; - - if (whichSide == LEFT_PALM) { - BUTTON_FWD = LEFT_BUTTON_FWD; - BUTTON_3 = LEFT_BUTTON_3; - palmPosition = Controller.getSpatialControlPosition(LEFT_PALM); - ballAlreadyInHand = leftBallAlreadyInHand; - handMessage = "LEFT"; - } else { - BUTTON_FWD = RIGHT_BUTTON_FWD; - BUTTON_3 = RIGHT_BUTTON_3; - palmPosition = Controller.getSpatialControlPosition(RIGHT_PALM); - ballAlreadyInHand = rightBallAlreadyInHand; - handMessage = "RIGHT"; - } - - var grabButtonPressed = (Controller.isButtonPressed(BUTTON_FWD) || Controller.isButtonPressed(BUTTON_3)); - - // If I don't currently have a ball in my hand, then try to catch closest one - if (!ballAlreadyInHand && grabButtonPressed) { - var closestParticle = Particles.findClosestParticle(palmPosition, targetRadius); - - if (closestParticle.isKnownID) { - - debugPrint(handMessage + " HAND- CAUGHT SOMETHING!!"); - - if (whichSide == LEFT_PALM) { - leftBallAlreadyInHand = true; - leftHandParticle = closestParticle; - } else { - rightBallAlreadyInHand = true; - rightHandParticle = closestParticle; - } - var ballPosition = getBallHoldPosition(whichSide); - var properties = { position: { x: ballPosition.x, - y: ballPosition.y, - z: ballPosition.z }, - velocity : { x: 0, y: 0, z: 0}, inHand: true }; - Particles.editParticle(closestParticle, properties); - - - return; // exit early - } - } - - // change ball color logic... - // - //if (wasButtonJustPressed()) { - // rotateColor(); - //} - - // If '3' is pressed, and not holding a ball, make a new one - if (Controller.isButtonPressed(BUTTON_3) && !ballAlreadyInHand) { - var ballPosition = getBallHoldPosition(whichSide); - var properties = { position: ballPosition, - velocity: { x: 0, y: 0, z: 0}, - gravity: { x: 0, y: 0, z: 0}, - inHand: true, - radius: 0.05, - damping: 0.999, - color: { red: 255, green: 0, blue: 0 }, - modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst", - }; - - newParticle = Particles.addParticle(properties); - if (whichSide == LEFT_PALM) { - leftBallAlreadyInHand = true; - leftHandParticle = newParticle; - } else { - rightBallAlreadyInHand = true; - rightHandParticle = newParticle; - } - - return; // exit early - } - - if (ballAlreadyInHand) { - if (whichSide == LEFT_PALM) { - handParticle = leftHandParticle; - whichTip = LEFT_TIP; - } else { - handParticle = rightHandParticle; - whichTip = RIGHT_TIP; - } - - // If holding the ball keep it in the palm - if (grabButtonPressed) { - debugPrint(">>>>> " + handMessage + "-BALL IN HAND, grabbing, hold and move"); - var ballPosition = getBallHoldPosition(whichSide); - var properties = { position: { x: ballPosition.x, - y: ballPosition.y, - z: ballPosition.z }, - }; - Particles.editParticle(handParticle, properties); - } else { - debugPrint(">>>>> " + handMessage + "-BALL IN HAND, not grabbing, THROW!!!"); - // If toy ball just released, add velocity to it! - var tipVelocity = Controller.getSpatialControlVelocity(whichTip); - var THROWN_VELOCITY_SCALING = 1.5; - var properties = { - velocity: { x: tipVelocity.x * THROWN_VELOCITY_SCALING, - y: tipVelocity.y * THROWN_VELOCITY_SCALING, - z: tipVelocity.z * THROWN_VELOCITY_SCALING } , - inHand: false, - gravity: { x: 0, y: -2, z: 0}, - }; - - Particles.editParticle(handParticle, properties); - - if (whichSide == LEFT_PALM) { - leftBallAlreadyInHand = false; - leftHandParticle = false; - } else { - rightBallAlreadyInHand = false; - rightHandParticle = false; - } - } - } -} - - function checkController(deltaTime) { var numberOfButtons = Controller.getNumberOfButtons(); var numberOfTriggers = Controller.getNumberOfTriggers(); @@ -277,17 +186,33 @@ function checkController(deltaTime) { // this is expected for hydras if (!(numberOfButtons==12 && numberOfTriggers == 2 && controllersPerTrigger == 2)) { - debugPrint("no hydra connected?"); + print("no hydra connected?"); return; // bail if no hydra } + + leftController.update(); + rightController.update(); - checkControllerSide(LEFT_PALM); - checkControllerSide(RIGHT_PALM); + + + ///// TEMP /////// + var createButtonPressed = Controller.isButtonPressed(3) || Controller.isButtonPressed(9); + if (createButtonPressed) { + particlePosition = MyAvatar.position; + var forwardVector = Quat.getFront(MyAvatar.orientation); + particlePosition = Vec3.sum(particlePosition, Vec3.multiply(forwardVector, 2)); + Particles.editParticle(particleTest, { + position: particlePosition, + lifetime: 100000 + }); + } + ////////////////////////////////// } function scriptEnding() { - Overlays.deleteOverlay(leftLaser); - Overlays.deleteOverlay(rightLaser); + leftController.cleanup(); + rightController.cleanup(); + Particles.deleteParticle(particleTest); } Script.scriptEnding.connect(scriptEnding); diff --git a/libraries/script-engine/src/Vec3.cpp b/libraries/script-engine/src/Vec3.cpp index badc980913..0cbb43f89a 100644 --- a/libraries/script-engine/src/Vec3.cpp +++ b/libraries/script-engine/src/Vec3.cpp @@ -17,6 +17,10 @@ glm::vec3 Vec3::cross(const glm::vec3& v1, const glm::vec3& v2) { return glm::cross(v1,v2); } +float Vec3::dot(const glm::vec3& v1, const glm::vec3& v2) { + return glm::dot(v1,v2); +} + glm::vec3 Vec3::multiply(const glm::vec3& v1, float f) { return v1 * f; } diff --git a/libraries/script-engine/src/Vec3.h b/libraries/script-engine/src/Vec3.h index e401cd71bd..3dce565fa1 100644 --- a/libraries/script-engine/src/Vec3.h +++ b/libraries/script-engine/src/Vec3.h @@ -24,8 +24,9 @@ class Vec3 : public QObject { Q_OBJECT -public slots: + public slots: glm::vec3 cross(const glm::vec3& v1, const glm::vec3& v2); + float dot(const glm::vec3& v1, const glm::vec3& v2); glm::vec3 multiply(const glm::vec3& v1, float f); glm::vec3 multiplyQbyV(const glm::quat& q, const glm::vec3& v); glm::vec3 sum(const glm::vec3& v1, const glm::vec3& v2); From ab0a0165c742563243032e60b8606091731fd1fb Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 2 May 2014 11:19:12 -0700 Subject: [PATCH 4/6] Working version of editModels.js --- examples/editModels.js | 348 ++++++++++++++---- interface/src/ui/overlays/Sphere3DOverlay.cpp | 6 +- 2 files changed, 284 insertions(+), 70 deletions(-) diff --git a/examples/editModels.js b/examples/editModels.js index f9fc5fc31e..5918818800 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -11,7 +11,7 @@ var LASER_WIDTH = 4; var LASER_COLOR = { red: 255, green: 0, blue: 0 }; -var LASER_LENGTH_FACTOR = 4; +var LASER_LENGTH_FACTOR = 1; var LEFT = 0; var RIGHT = 1; @@ -29,6 +29,18 @@ function controller(wichSide) { this.oldTipPosition = Controller.getSpatialControlPosition(this.tip); this.tipPosition = Controller.getSpatialControlPosition(this.tip); + this.oldUp = Controller.getSpatialControlNormal(this.palm); + this.up = this.oldUp; + + this.oldFront = Vec3.normalize(Vec3.subtract(this.tipPosition, this.palmPosition)); + this.front = this.oldFront; + + this.oldRight = Vec3.cross(this.front, this.up); + this.right = this.oldRight; + + this.oldRotation = Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(this.palm)); + this.rotation = this.oldRotation; + this.triggerValue = Controller.getTriggerValue(this.trigger); this.pressed = false; // is trigger pressed @@ -36,7 +48,7 @@ function controller(wichSide) { this.grabbing = false; this.particleID = 0; - this.oldParticlePosition = { x: 0, y: 0, z: 0 }; + this.oldParticleProperties; this.laser = Overlays.addOverlay("line3d", { position: this.palmPosition, @@ -47,6 +59,34 @@ function controller(wichSide) { lineWidth: LASER_WIDTH }); + this.guideScale = 0.02; + this.ball = Overlays.addOverlay("sphere", { + position: this.palmPosition, + size: this.guideScale, + solid: true, + color: { red: 0, green: 255, blue: 0 }, + alpha: 1, + visible: false, + }); + this.leftRight = Overlays.addOverlay("line3d", { + position: this.palmPosition, + end: this.tipPosition, + color: { red: 0, green: 0, blue: 255 }, + alpha: 1, + visible: false, + lineWidth: LASER_WIDTH + }); + this.topDown = Overlays.addOverlay("line3d", { + position: this.palmPosition, + end: this.tipPosition, + color: { red: 0, green: 0, blue: 255 }, + alpha: 1, + visible: false, + lineWidth: LASER_WIDTH + }); + + + this.grab = function (particle) { if (!particle.isKnownID) { var identify = Particles.identifyParticle(particle); @@ -54,20 +94,17 @@ function controller(wichSide) { return; } } + print("Grabbing"); this.grabbing = true; - this.particle = identify; + this.particleID = identify; - var properties = Particles.getParticleProperties(this.particle); - - this.oldParticlePosition = properties.position; - - print("Grabbed: " + this.oldParticlePosition); + this.oldParticleProperties = Particles.getParticleProperties(this.particleID); } this.release = function () { this.grabbing = false; this.particleID = -1; - this.oldParticlePosition = { x: 0, y: 0, z: 0 }; + this.oldParticleProperties = { }; } this.checkTrigger = function () { @@ -85,22 +122,68 @@ function controller(wichSide) { } this.moveLaser = function () { - var vector = Vec3.subtract(this.tipPosition, this.palmPosition); - var endPosition = Vec3.sum(this.palmPosition, Vec3.multiply(vector, LASER_LENGTH_FACTOR)); + var endPosition = Vec3.sum(this.palmPosition, Vec3.multiply(this.front, LASER_LENGTH_FACTOR)); Overlays.editOverlay(this.laser, { position: this.palmPosition, end: endPosition, visible: true }); + + + Overlays.editOverlay(this.ball, { + position: endPosition, + visible: true + }); + Overlays.editOverlay(this.leftRight, { + position: Vec3.sum(endPosition, Vec3.multiply(this.right, 2 * this.guideScale)), + end: Vec3.sum(endPosition, Vec3.multiply(this.right, -2 * this.guideScale)), + visible: true + }); + Overlays.editOverlay(this.topDown, {position: Vec3.sum(endPosition, Vec3.multiply(this.up, 2 * this.guideScale)), + end: Vec3.sum(endPosition, Vec3.multiply(this.up, -2 * this.guideScale)), + visible: true + }); } - this.moveParticle = function () { - if (this.grabbing) { - - - + this.checkParticle = function (particle) { + if (!particle.isKnownID) { + var identify = Particles.identifyParticle(particle); + if (!identify.isKnownID) { + print("Unknown ID (checkParticle)"); + return; + } } + // P P - Particle + // /| A - Palm + // / | d B - unit vector toward tip + // / | X - base of the perpendicular line + // A---X----->B d - distance fom axis + // x x - distance from A + // + // |X-A| = (P-A).B + // X == A + ((P-A).B)B + // d = |P-X| + + var A = this.palmPosition; + var B = this.front; + var P = Particles.getParticleProperties(particle).position; + + this.x = Vec3.dot(Vec3.subtract(P, A), B); + this.y = Vec3.dot(Vec3.subtract(P, A), this.up); + this.z = Vec3.dot(Vec3.subtract(P, A), this.right); + var X = Vec3.sum(A, Vec3.multiply(B, this.x)); + var d = Vec3.length(Vec3.subtract(P, X)); + +// Vec3.print("A: ", A); +// Vec3.print("B: ", B); +// Vec3.print("Particle pos: ", P); +// print("d: " + d + ", x: " + this.x); + if (d < 0.5 && 0 < this.x && this.x < LASER_LENGTH_FACTOR) { + return true; + } + + return false; } this.update = function () { @@ -108,55 +191,45 @@ function controller(wichSide) { this.oldTipPosition = this.tipPosition; this.palmPosition = Controller.getSpatialControlPosition(this.palm); this.tipPosition = Controller.getSpatialControlPosition(this.tip); + + this.oldUp = this.up; + this.up = Vec3.normalize(Controller.getSpatialControlNormal(this.palm)); + + this.oldFront = this.front; + this.front = Vec3.normalize(Vec3.subtract(this.tipPosition, this.palmPosition)); + + this.oldRight = this.right; + this.right = Vec3.normalize(Vec3.cross(this.front, this.up)); + + this.oldRotation = this.rotation; + this.rotation = Quat.multiply(MyAvatar.orientation, Controller.getSpatialControlRawRotation(this.palm)); + this.triggerValue = Controller.getTriggerValue(this.trigger); this.checkTrigger(); if (this.pressing) { - var particle = -1; - -// P P - Particle -// /| A - Palm -// / | d B - unit vector toward tip -// / | X - base of the perpendicular line -// A---X----->B d - distance fom axis -// x x - distance from A -// -// |X-A| = (P-A).B, in B unit -// X == A + ((P-A).B)B -// d = |P-X| in standard unit - - var A = this.palmPosition; - var B = Vec3.subtract(this.tipPosition, A); - var P = particlePosition; - - var x = Vec3.dot(Vec3.subtract(P, A), B); - var X = Vec3.sum(A, Vec3.multiply(B, x)); - var d = Vec3.length(Vec3.subtract(P, X)); - - - if (d < 0.5 && 0 < x && x < LASER_LENGTH_FACTOR) { - particle = particleTest; + if (this.checkParticle(particleTest1)) { + this.grab(particleTest1); } - Vec3.print("A = ", A); - Vec3.print("B = ", B); - Vec3.print("P = ", P); - Vec3.print("X = ", X); - print("d = " + d + ", x = " + x); - this.grab(particle); } if (!this.pressed && this.grabbing) { this.release(); } - + if(this.grabbing) { + this.oldParticleProperties = Particles.getParticleProperties(this.particleID); + } + this.moveLaser(); - this.moveParticle(); } this.cleanup = function () { Overlays.deleteOverlay(this.laser); + Overlays.deleteOverlay(this.ball); + Overlays.deleteOverlay(this.leftRight); + Overlays.deleteOverlay(this.topDown); } } @@ -164,20 +237,132 @@ var leftController = new controller(LEFT); var rightController = new controller(RIGHT); - -var particlePosition = MyAvatar.position; var particleRadius = 0.05; -var particleTest = Particles.addParticle({ position: particlePosition, - velocity: { x: 0, y: 0, z: 0}, - gravity: { x: 0, y: 0, z: 0}, - radius: particleRadius, - damping: 0.999, - color: { red: 255, green: 0, blue: 0 }, - lifetime: 100, - modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst" - }) +var particleTest1 = Particles.addParticle({ position: { x: 0, y: 0, z:0 }, + velocity: { x: 0, y: 0, z: 0}, + gravity: { x: 0, y: 0, z: 0}, + radius: particleRadius, + color: { red: 0, green: 0, blue: 255 }, + modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst", + lifetime: 600, + }) +var particleTest2 = Particles.addParticle({ position: { x: 0, y: 0, z:0 }, + velocity: { x: 0, y: 0, z: 0}, + gravity: { x: 0, y: 0, z: 0}, + radius: particleRadius, + color: { red: 0, green: 0, blue: 255 }, + alpha: 0.2, + lifetime: 600, + }) +var diff = { x: 0, y: 0, z: 0 }; +function moveParticles() { + if (leftController.grabbing) { + if (rightController.grabbing) { + + var newPosition = Vec3.sum(leftController.palmPosition, + Vec3.multiply(leftController.front, leftController.x)); + newPosition = Vec3.sum(newPosition, + Vec3.multiply(leftController.up, leftController.y)); + newPosition = Vec3.sum(newPosition, + Vec3.multiply(leftController.right, leftController.z)); + + var rotation = Quat.multiply(leftController.rotation, + Quat.inverse(leftController.oldRotation)); + rotation = Quat.multiply(rotation, Particles.getParticleProperties(leftController.particleID).modelRotation); + + Particles.editParticle(leftController.particleID, { + position: newPosition, + modelRotation: rotation, + lifetime: 600 + }); + Particles.editParticle(particleTest2, { + position: newPosition, + lifetime: 600 + }); + + return; + var leftVector = Vec3.sum(Vec3.multiply(leftController.front, leftController.x), + Vec3.multiply(leftController.up, leftController.y)); + leftVector = Vec3.sum(leftVector, + Vec3.multiply(leftController.right, leftController.z)); + + var rightVector = Vec3.sum(Vec3.multiply(rightController.front, rightController.x), + Vec3.multiply(rightController.up, rightController.y)); + rightVector = Vec3.sum(rightVector, + Vec3.multiply(rightController.right, rightController.z)); + + var newPosition = Vec3.sum(Vec3.sum(leftController.palmPosition, leftVector), + Vec3.sum(rightController.palmPosition, rightVector)); + newPosition = Vec3.multiply(newPosition, 0.5); + + + var rotantion = Quat.multiply(MyAvatar.orientation, + Quat.inverse(leftController.oldRotation)); + rotation = Quat.multiply(rotation, Particles.getParticleProperties(leftController.particleID).modelRotation); + + Particles.editParticle(leftController.particleID, { + position: newPosition, + //modelRotation: rotation, + lifetime: 600 + }); + Particles.editParticle(particleTest2, { + position: newPosition, + lifetime: 600 + }); + leftController.checkParticle(leftController.particleID); + rightController.checkParticle(rightController.particleID); + return; + } else { + var newPosition = Vec3.sum(leftController.palmPosition, + Vec3.multiply(leftController.front, leftController.x)); + newPosition = Vec3.sum(newPosition, + Vec3.multiply(leftController.up, leftController.y)); + newPosition = Vec3.sum(newPosition, + Vec3.multiply(leftController.right, leftController.z)); + + var rotation = Quat.multiply(leftController.rotation, + Quat.inverse(leftController.oldRotation)); + rotation = Quat.multiply(rotation, Particles.getParticleProperties(leftController.particleID).modelRotation); + + Particles.editParticle(leftController.particleID, { + position: newPosition, + modelRotation: rotation, + lifetime: 600 + }); + Particles.editParticle(particleTest2, { + position: newPosition, + lifetime: 600 + }); + } + } + + + if (rightController.grabbing) { + var newPosition = Vec3.sum(rightController.palmPosition, + Vec3.multiply(rightController.front, rightController.x)); + newPosition = Vec3.sum(newPosition, + Vec3.multiply(rightController.up, rightController.y)); + newPosition = Vec3.sum(newPosition, + Vec3.multiply(rightController.right, rightController.z)); + + var rotation = Quat.multiply(rightController.rotation, + Quat.inverse(rightController.oldRotation)); + rotation = Quat.multiply(rotation, Particles.getParticleProperties(rightController.particleID).modelRotation); + + Particles.editParticle(rightController.particleID, { + position: newPosition, + modelRotation: rotation, + lifetime: 600 + }); + Particles.editParticle(particleTest2, { + position: newPosition, + lifetime: 600 + }); + } +} + function checkController(deltaTime) { var numberOfButtons = Controller.getNumberOfButtons(); var numberOfTriggers = Controller.getNumberOfTriggers(); @@ -186,24 +371,52 @@ function checkController(deltaTime) { // this is expected for hydras if (!(numberOfButtons==12 && numberOfTriggers == 2 && controllersPerTrigger == 2)) { - print("no hydra connected?"); + //print("no hydra connected?"); return; // bail if no hydra } leftController.update(); rightController.update(); + moveParticles(); ///// TEMP /////// - var createButtonPressed = Controller.isButtonPressed(3) || Controller.isButtonPressed(9); + if (!particleTest1.isKnownID) { + var identify = Particles.identifyParticle(particleTest1); + if (!identify.isKnownID) { + print("Unknown ID (temp)"); + return; + } + } + var createButtonPressed = Controller.isButtonPressed(3); if (createButtonPressed) { - particlePosition = MyAvatar.position; + var position = MyAvatar.position; var forwardVector = Quat.getFront(MyAvatar.orientation); - particlePosition = Vec3.sum(particlePosition, Vec3.multiply(forwardVector, 2)); - Particles.editParticle(particleTest, { - position: particlePosition, - lifetime: 100000 + position = Vec3.sum(position, Vec3.multiply(forwardVector, 2)); + Particles.editParticle(particleTest1, { + position: position, + modelRotation: Quat.fromVec3Radians({ x: 0, y: 0 , z: 0 }), + lifetime: 600 + }); + Particles.editParticle(particleTest2, { + position: position, + lifetime: 600 + }); + } + createButtonPressed = Controller.isButtonPressed(9); + if (createButtonPressed) { + var position = MyAvatar.position; + var forwardVector = Quat.getFront(MyAvatar.orientation); + position = Vec3.sum(position, Vec3.multiply(forwardVector, 2)); + Particles.editParticle(particleTest1, { + position: position, + modelRotation: Quat.fromVec3Radians({ x: 0, y: 0 , z: 0 }), + lifetime: 600 + }); + Particles.editParticle(particleTest2, { + position: position, + lifetime: 600 }); } ////////////////////////////////// @@ -212,7 +425,8 @@ function checkController(deltaTime) { function scriptEnding() { leftController.cleanup(); rightController.cleanup(); - Particles.deleteParticle(particleTest); + Particles.deleteParticle(particleTest1); + Particles.deleteParticle(particleTest2); } Script.scriptEnding.connect(scriptEnding); diff --git a/interface/src/ui/overlays/Sphere3DOverlay.cpp b/interface/src/ui/overlays/Sphere3DOverlay.cpp index e5f31fa1be..c2ac92f45c 100644 --- a/interface/src/ui/overlays/Sphere3DOverlay.cpp +++ b/interface/src/ui/overlays/Sphere3DOverlay.cpp @@ -33,9 +33,9 @@ void Sphere3DOverlay::render() { glDisable(GL_LIGHTING); glPushMatrix(); - glTranslatef(_position.x + _size * 0.5f, - _position.y + _size * 0.5f, - _position.z + _size * 0.5f); + glTranslatef(_position.x, + _position.y, + _position.z); glLineWidth(_lineWidth); const int slices = 15; if (_isSolid) { From 0b4516e09b6efcfe92b584703f317fd707d75514 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 2 May 2014 15:32:29 -0700 Subject: [PATCH 5/6] editModels,js switched to use actual models instead of particles. --- examples/editModels.js | 223 ++++++++++++----------------------------- 1 file changed, 64 insertions(+), 159 deletions(-) diff --git a/examples/editModels.js b/examples/editModels.js index 5918818800..50b0137c4f 100644 --- a/examples/editModels.js +++ b/examples/editModels.js @@ -11,12 +11,11 @@ var LASER_WIDTH = 4; var LASER_COLOR = { red: 255, green: 0, blue: 0 }; -var LASER_LENGTH_FACTOR = 1; +var LASER_LENGTH_FACTOR = 1.5; var LEFT = 0; var RIGHT = 1; - function controller(wichSide) { this.side = wichSide; this.palm = 2 * wichSide; @@ -47,8 +46,7 @@ function controller(wichSide) { this.pressing = false; // is trigger being pressed (is pressed now but wasn't previously) this.grabbing = false; - this.particleID = 0; - this.oldParticleProperties; + this.modelID; this.laser = Overlays.addOverlay("line3d", { position: this.palmPosition, @@ -87,24 +85,23 @@ function controller(wichSide) { - this.grab = function (particle) { - if (!particle.isKnownID) { - var identify = Particles.identifyParticle(particle); + this.grab = function (modelID) { + if (!modelID.isKnownID) { + var identify = Models.identifyModel(modelID); if (!identify.isKnownID) { + print("Unknown ID " + identify.id + "(grab)"); return; } + modelID = identify; } - print("Grabbing"); + print("Grabbing " + modelID.id); this.grabbing = true; - this.particleID = identify; - - this.oldParticleProperties = Particles.getParticleProperties(this.particleID); + this.modelID = modelID; } this.release = function () { this.grabbing = false; - this.particleID = -1; - this.oldParticleProperties = { }; + this.modelID = 0; } this.checkTrigger = function () { @@ -146,15 +143,16 @@ function controller(wichSide) { }); } - this.checkParticle = function (particle) { - if (!particle.isKnownID) { - var identify = Particles.identifyParticle(particle); + this.checkModel = function (modelID) { + if (!modelID.isKnownID) { + var identify = Models.identifyModel(modelID); if (!identify.isKnownID) { - print("Unknown ID (checkParticle)"); + print("Unknown ID " + identify.id + "(checkModel)"); return; } + modelID = identify; } - // P P - Particle + // P P - Model // /| A - Palm // / | d B - unit vector toward tip // / | X - base of the perpendicular line @@ -167,7 +165,7 @@ function controller(wichSide) { var A = this.palmPosition; var B = this.front; - var P = Particles.getParticleProperties(particle).position; + var P = Models.getModelProperties(modelID).position; this.x = Vec3.dot(Vec3.subtract(P, A), B); this.y = Vec3.dot(Vec3.subtract(P, A), this.up); @@ -179,10 +177,9 @@ function controller(wichSide) { // Vec3.print("B: ", B); // Vec3.print("Particle pos: ", P); // print("d: " + d + ", x: " + this.x); - if (d < 0.5 && 0 < this.x && this.x < LASER_LENGTH_FACTOR) { + if (d < Models.getModelProperties(modelID).radius && 0 < this.x && this.x < LASER_LENGTH_FACTOR) { return true; } - return false; } @@ -209,18 +206,22 @@ function controller(wichSide) { this.checkTrigger(); if (this.pressing) { - if (this.checkParticle(particleTest1)) { - this.grab(particleTest1); + Vec3.print("Looking at: ", this.palmPosition); + var foundModels = Models.findModels(this.palmPosition, LASER_LENGTH_FACTOR); + for (var i = 0; i < foundModels.length; i++) { + print("Model found ID (" + foundModels[i].id + ")"); + if (this.checkModel(foundModels[i])) { + if (this.grab(foundModels[i])) { + return; + } + } } } if (!this.pressed && this.grabbing) { + // release if trigger not pressed anymore. this.release(); } - - if(this.grabbing) { - this.oldParticleProperties = Particles.getParticleProperties(this.particleID); - } this.moveLaser(); } @@ -236,83 +237,39 @@ function controller(wichSide) { var leftController = new controller(LEFT); var rightController = new controller(RIGHT); - -var particleRadius = 0.05; -var particleTest1 = Particles.addParticle({ position: { x: 0, y: 0, z:0 }, - velocity: { x: 0, y: 0, z: 0}, - gravity: { x: 0, y: 0, z: 0}, - radius: particleRadius, - color: { red: 0, green: 0, blue: 255 }, - modelURL: "http://highfidelity-public.s3-us-west-1.amazonaws.com/models/heads/defaultAvatar_head.fst", - lifetime: 600, - }) -var particleTest2 = Particles.addParticle({ position: { x: 0, y: 0, z:0 }, - velocity: { x: 0, y: 0, z: 0}, - gravity: { x: 0, y: 0, z: 0}, - radius: particleRadius, - color: { red: 0, green: 0, blue: 255 }, - alpha: 0.2, - lifetime: 600, - }) - - -var diff = { x: 0, y: 0, z: 0 }; -function moveParticles() { +function moveModels() { if (leftController.grabbing) { if (rightController.grabbing) { + var properties = Models.getModelProperties(leftController.modelID); - var newPosition = Vec3.sum(leftController.palmPosition, - Vec3.multiply(leftController.front, leftController.x)); - newPosition = Vec3.sum(newPosition, - Vec3.multiply(leftController.up, leftController.y)); - newPosition = Vec3.sum(newPosition, - Vec3.multiply(leftController.right, leftController.z)); + var oldLeftPoint = Vec3.sum(leftController.oldPalmPosition, Vec3.multiply(leftController.oldFront, leftController.x)); + var oldRightPoint = Vec3.sum(rightController.oldPalmPosition, Vec3.multiply(rightController.oldFront, rightController.x)); + var oldMiddle = Vec3.multiply(Vec3.sum(oldLeftPoint, oldRightPoint), 0.5); + var oldLength = Vec3.length(Vec3.subtract(oldLeftPoint, oldRightPoint)); + + + var leftPoint = Vec3.sum(leftController.palmPosition, Vec3.multiply(leftController.front, leftController.x)); + var rightPoint = Vec3.sum(rightController.palmPosition, Vec3.multiply(rightController.front, rightController.x)); + + var middle = Vec3.multiply(Vec3.sum(leftPoint, rightPoint), 0.5); + var length = Vec3.length(Vec3.subtract(leftPoint, rightPoint)); + + var ratio = length / oldLength; + + var newPosition = Vec3.sum(middle, + Vec3.multiply(Vec3.subtract(properties.position, oldMiddle), ratio)); + Vec3.print("Ratio : " + ratio + " New position: ", newPosition); var rotation = Quat.multiply(leftController.rotation, Quat.inverse(leftController.oldRotation)); - rotation = Quat.multiply(rotation, Particles.getParticleProperties(leftController.particleID).modelRotation); + rotation = Quat.multiply(rotation, properties.modelRotation); - Particles.editParticle(leftController.particleID, { - position: newPosition, - modelRotation: rotation, - lifetime: 600 - }); - Particles.editParticle(particleTest2, { - position: newPosition, - lifetime: 600 - }); - - return; - var leftVector = Vec3.sum(Vec3.multiply(leftController.front, leftController.x), - Vec3.multiply(leftController.up, leftController.y)); - leftVector = Vec3.sum(leftVector, - Vec3.multiply(leftController.right, leftController.z)); - - var rightVector = Vec3.sum(Vec3.multiply(rightController.front, rightController.x), - Vec3.multiply(rightController.up, rightController.y)); - rightVector = Vec3.sum(rightVector, - Vec3.multiply(rightController.right, rightController.z)); - - var newPosition = Vec3.sum(Vec3.sum(leftController.palmPosition, leftVector), - Vec3.sum(rightController.palmPosition, rightVector)); - newPosition = Vec3.multiply(newPosition, 0.5); - - - var rotantion = Quat.multiply(MyAvatar.orientation, - Quat.inverse(leftController.oldRotation)); - rotation = Quat.multiply(rotation, Particles.getParticleProperties(leftController.particleID).modelRotation); - - Particles.editParticle(leftController.particleID, { + Models.editModel(leftController.modelID, { position: newPosition, //modelRotation: rotation, - lifetime: 600 + radius: properties.radius * ratio }); - Particles.editParticle(particleTest2, { - position: newPosition, - lifetime: 600 - }); - leftController.checkParticle(leftController.particleID); - rightController.checkParticle(rightController.particleID); + return; } else { var newPosition = Vec3.sum(leftController.palmPosition, @@ -324,17 +281,13 @@ function moveParticles() { var rotation = Quat.multiply(leftController.rotation, Quat.inverse(leftController.oldRotation)); - rotation = Quat.multiply(rotation, Particles.getParticleProperties(leftController.particleID).modelRotation); + rotation = Quat.multiply(rotation, + Models.getModelProperties(leftController.modelID).modelRotation); - Particles.editParticle(leftController.particleID, { - position: newPosition, - modelRotation: rotation, - lifetime: 600 - }); - Particles.editParticle(particleTest2, { - position: newPosition, - lifetime: 600 - }); + Models.editModel(leftController.modelID, { + position: newPosition, + modelRotation: rotation + }); } } @@ -349,17 +302,13 @@ function moveParticles() { var rotation = Quat.multiply(rightController.rotation, Quat.inverse(rightController.oldRotation)); - rotation = Quat.multiply(rotation, Particles.getParticleProperties(rightController.particleID).modelRotation); + rotation = Quat.multiply(rotation, + Models.getModelProperties(rightController.modelID).modelRotation); - Particles.editParticle(rightController.particleID, { - position: newPosition, - modelRotation: rotation, - lifetime: 600 - }); - Particles.editParticle(particleTest2, { - position: newPosition, - lifetime: 600 - }); + Models.editModel(rightController.modelID, { + position: newPosition, + modelRotation: rotation + }); } } @@ -377,56 +326,12 @@ function checkController(deltaTime) { leftController.update(); rightController.update(); - moveParticles(); - - - - ///// TEMP /////// - if (!particleTest1.isKnownID) { - var identify = Particles.identifyParticle(particleTest1); - if (!identify.isKnownID) { - print("Unknown ID (temp)"); - return; - } - } - var createButtonPressed = Controller.isButtonPressed(3); - if (createButtonPressed) { - var position = MyAvatar.position; - var forwardVector = Quat.getFront(MyAvatar.orientation); - position = Vec3.sum(position, Vec3.multiply(forwardVector, 2)); - Particles.editParticle(particleTest1, { - position: position, - modelRotation: Quat.fromVec3Radians({ x: 0, y: 0 , z: 0 }), - lifetime: 600 - }); - Particles.editParticle(particleTest2, { - position: position, - lifetime: 600 - }); - } - createButtonPressed = Controller.isButtonPressed(9); - if (createButtonPressed) { - var position = MyAvatar.position; - var forwardVector = Quat.getFront(MyAvatar.orientation); - position = Vec3.sum(position, Vec3.multiply(forwardVector, 2)); - Particles.editParticle(particleTest1, { - position: position, - modelRotation: Quat.fromVec3Radians({ x: 0, y: 0 , z: 0 }), - lifetime: 600 - }); - Particles.editParticle(particleTest2, { - position: position, - lifetime: 600 - }); - } - ////////////////////////////////// + moveModels(); } function scriptEnding() { leftController.cleanup(); rightController.cleanup(); - Particles.deleteParticle(particleTest1); - Particles.deleteParticle(particleTest2); } Script.scriptEnding.connect(scriptEnding); From 48facce85ff0b94c6c669f2a137814b64d8d465c Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 2 May 2014 15:34:07 -0700 Subject: [PATCH 6/6] coding standard --- libraries/script-engine/src/Vec3.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/script-engine/src/Vec3.h b/libraries/script-engine/src/Vec3.h index 3dce565fa1..5a3eeca7be 100644 --- a/libraries/script-engine/src/Vec3.h +++ b/libraries/script-engine/src/Vec3.h @@ -24,7 +24,7 @@ class Vec3 : public QObject { Q_OBJECT - public slots: +public slots: glm::vec3 cross(const glm::vec3& v1, const glm::vec3& v2); float dot(const glm::vec3& v1, const glm::vec3& v2); glm::vec3 multiply(const glm::vec3& v1, float f);