From 5b88e47810f28c8e78845bfa4458e5abd9cbda7d Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Mon, 8 Sep 2014 11:31:39 -0700 Subject: [PATCH] Head movement and hydra/hand movement separated to headMove.js and hydraMove.js --- examples/headMove.js | 77 +++++++++++++++++++++++++++++++++++++++++++ examples/hydraMove.js | 65 +++--------------------------------- 2 files changed, 81 insertions(+), 61 deletions(-) create mode 100644 examples/headMove.js diff --git a/examples/headMove.js b/examples/headMove.js new file mode 100644 index 0000000000..704557b6e7 --- /dev/null +++ b/examples/headMove.js @@ -0,0 +1,77 @@ +// +// headMove.js +// examples +// +// Created by Philip Rosedale on September 8, 2014 +// Copyright 2014 High Fidelity, Inc. +// +// Press the spacebar and move/turn your head to move around. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +var debug = false; + +var movingWithHead = false; +var headStartPosition, headStartDeltaPitch, headStartFinalPitch, headStartRoll, headStartYaw; + +var HEAD_MOVE_DEAD_ZONE = 0.0; +var HEAD_STRAFE_DEAD_ZONE = 0.0; +var HEAD_ROTATE_DEAD_ZONE = 0.0; +var HEAD_THRUST_FWD_SCALE = 12000.0; +var HEAD_THRUST_STRAFE_SCALE = 1000.0; +var HEAD_YAW_RATE = 2.0; +var HEAD_PITCH_RATE = 1.0; +var HEAD_ROLL_THRUST_SCALE = 75.0; +var HEAD_PITCH_LIFT_THRUST = 3.0; + +function moveWithHead(deltaTime) { + if (movingWithHead) { + var deltaYaw = MyAvatar.getHeadFinalYaw() - headStartYaw; + var deltaPitch = MyAvatar.getHeadDeltaPitch() - headStartDeltaPitch; + + var bodyLocalCurrentHeadVector = Vec3.subtract(MyAvatar.getHeadPosition(), MyAvatar.position); + bodyLocalCurrentHeadVector = Vec3.multiplyQbyV(Quat.angleAxis(-deltaYaw, {x:0, y: 1, z:0}), bodyLocalCurrentHeadVector); + var headDelta = Vec3.subtract(bodyLocalCurrentHeadVector, headStartPosition); + headDelta = Vec3.multiplyQbyV(Quat.inverse(Camera.getOrientation()), headDelta); + headDelta.y = 0.0; // Don't respond to any of the vertical component of head motion + + // Thrust based on leaning forward and side-to-side + if (Math.abs(headDelta.z) > HEAD_MOVE_DEAD_ZONE) { + MyAvatar.addThrust(Vec3.multiply(Quat.getFront(Camera.getOrientation()), -headDelta.z * HEAD_THRUST_FWD_SCALE * deltaTime)); + } + if (Math.abs(headDelta.x) > HEAD_STRAFE_DEAD_ZONE) { + MyAvatar.addThrust(Vec3.multiply(Quat.getRight(Camera.getOrientation()), headDelta.x * HEAD_THRUST_STRAFE_SCALE * deltaTime)); + } + if (Math.abs(deltaYaw) > HEAD_ROTATE_DEAD_ZONE) { + var orientation = Quat.multiply(Quat.angleAxis(deltaYaw * HEAD_YAW_RATE * deltaTime, {x:0, y: 1, z:0}), MyAvatar.orientation); + MyAvatar.orientation = orientation; + } + // Thrust Up/Down based on head pitch + MyAvatar.addThrust(Vec3.multiply({ x:0, y:1, z:0 }, (MyAvatar.getHeadFinalPitch() - headStartFinalPitch) * HEAD_PITCH_LIFT_THRUST * deltaTime)); + // For head trackers, adjust pitch by head pitch + MyAvatar.headPitch += deltaPitch * HEAD_PITCH_RATE * deltaTime; + // Thrust strafe based on roll ange + MyAvatar.addThrust(Vec3.multiply(Quat.getRight(Camera.getOrientation()), -(MyAvatar.getHeadFinalRoll() - headStartRoll) * HEAD_ROLL_THRUST_SCALE * deltaTime)); + } +} + +Controller.keyPressEvent.connect(function(event) { + if (event.text == "SPACE" && !movingWithHead) { + movingWithHead = true; + headStartPosition = Vec3.subtract(MyAvatar.getHeadPosition(), MyAvatar.position); + headStartDeltaPitch = MyAvatar.getHeadDeltaPitch(); + headStartFinalPitch = MyAvatar.getHeadFinalPitch(); + headStartRoll = MyAvatar.getHeadFinalRoll(); + headStartYaw = MyAvatar.getHeadFinalYaw(); + } +}); +Controller.keyReleaseEvent.connect(function(event) { + if (event.text == "SPACE") { + movingWithHead = false; + } +}); + +Script.update.connect(moveWithHead); + diff --git a/examples/hydraMove.js b/examples/hydraMove.js index 853c18ebce..236cecab18 100644 --- a/examples/hydraMove.js +++ b/examples/hydraMove.js @@ -2,7 +2,9 @@ // hydraMove.js // examples // -// Created by Brad Hefta-Gaub on 2/10/14. +// Created by Brad Hefta-Gaub on February 10, 2014 +// Updated by Philip Rosedale on September 8, 2014 +// // Copyright 2014 High Fidelity, Inc. // // This is an example script that demonstrates use of the Controller and MyAvatar classes to implement @@ -34,8 +36,7 @@ var grabbingWithRightHand = false; var wasGrabbingWithRightHand = false; var grabbingWithLeftHand = false; var wasGrabbingWithLeftHand = false; -var movingWithHead = false; -var headStartPosition, headStartDeltaPitch, headStartFinalPitch, headStartRoll, headStartYaw; + var EPSILON = 0.000001; var velocity = { x: 0, y: 0, z: 0}; var THRUST_MAG_UP = 100.0; @@ -243,47 +244,6 @@ function handleGrabBehavior(deltaTime) { wasGrabbingWithLeftHand = grabbingWithLeftHand; } -var HEAD_MOVE_DEAD_ZONE = 0.0; -var HEAD_STRAFE_DEAD_ZONE = 0.0; -var HEAD_ROTATE_DEAD_ZONE = 0.0; -var HEAD_THRUST_FWD_SCALE = 12000.0; -var HEAD_THRUST_STRAFE_SCALE = 1000.0; -var HEAD_YAW_RATE = 2.0; -var HEAD_PITCH_RATE = 1.0; -var HEAD_ROLL_THRUST_SCALE = 75.0; -var HEAD_PITCH_LIFT_THRUST = 3.0; - -function moveWithHead(deltaTime) { - if (movingWithHead) { - var deltaYaw = MyAvatar.getHeadFinalYaw() - headStartYaw; - var deltaPitch = MyAvatar.getHeadDeltaPitch() - headStartDeltaPitch; - - var bodyLocalCurrentHeadVector = Vec3.subtract(MyAvatar.getHeadPosition(), MyAvatar.position); - bodyLocalCurrentHeadVector = Vec3.multiplyQbyV(Quat.angleAxis(-deltaYaw, {x:0, y: 1, z:0}), bodyLocalCurrentHeadVector); - var headDelta = Vec3.subtract(bodyLocalCurrentHeadVector, headStartPosition); - headDelta = Vec3.multiplyQbyV(Quat.inverse(Camera.getOrientation()), headDelta); - headDelta.y = 0.0; // Don't respond to any of the vertical component of head motion - - // Thrust based on leaning forward and side-to-side - if (Math.abs(headDelta.z) > HEAD_MOVE_DEAD_ZONE) { - MyAvatar.addThrust(Vec3.multiply(Quat.getFront(Camera.getOrientation()), -headDelta.z * HEAD_THRUST_FWD_SCALE * deltaTime)); - } - if (Math.abs(headDelta.x) > HEAD_STRAFE_DEAD_ZONE) { - MyAvatar.addThrust(Vec3.multiply(Quat.getRight(Camera.getOrientation()), headDelta.x * HEAD_THRUST_STRAFE_SCALE * deltaTime)); - } - if (Math.abs(deltaYaw) > HEAD_ROTATE_DEAD_ZONE) { - var orientation = Quat.multiply(Quat.angleAxis(deltaYaw * HEAD_YAW_RATE * deltaTime, {x:0, y: 1, z:0}), MyAvatar.orientation); - MyAvatar.orientation = orientation; - } - // Thrust Up/Down based on head pitch - MyAvatar.addThrust(Vec3.multiply({ x:0, y:1, z:0 }, (MyAvatar.getHeadFinalPitch() - headStartFinalPitch) * HEAD_PITCH_LIFT_THRUST * deltaTime)); - // For head trackers, adjust pitch by head pitch - MyAvatar.headPitch += deltaPitch * HEAD_PITCH_RATE * deltaTime; - // Thrust strafe based on roll ange - MyAvatar.addThrust(Vec3.multiply(Quat.getRight(Camera.getOrientation()), -(MyAvatar.getHeadFinalRoll() - headStartRoll) * HEAD_ROLL_THRUST_SCALE * deltaTime)); - } -} - // Update for joysticks and move button function flyWithHydra(deltaTime) { var thrustJoystickPosition = Controller.getJoystickPosition(THRUST_CONTROLLER); @@ -318,12 +278,10 @@ function flyWithHydra(deltaTime) { MyAvatar.orientation = Quat.multiply(orientation, deltaOrientation); // change the headPitch based on our x controller - //pitch += viewJoystickPosition.y * JOYSTICK_PITCH_MAG * deltaTime; var newPitch = MyAvatar.headPitch + (viewJoystickPosition.y * JOYSTICK_PITCH_MAG * deltaTime); MyAvatar.headPitch = newPitch; } handleGrabBehavior(deltaTime); - moveWithHead(deltaTime); displayDebug(); } @@ -340,19 +298,4 @@ function scriptEnding() { } Script.scriptEnding.connect(scriptEnding); -Controller.keyPressEvent.connect(function(event) { - if (event.text == "SPACE" && !movingWithHead) { - movingWithHead = true; - headStartPosition = Vec3.subtract(MyAvatar.getHeadPosition(), MyAvatar.position); - headStartDeltaPitch = MyAvatar.getHeadDeltaPitch(); - headStartFinalPitch = MyAvatar.getHeadFinalPitch(); - headStartRoll = MyAvatar.getHeadFinalRoll(); - headStartYaw = MyAvatar.getHeadFinalYaw(); - } -}); -Controller.keyReleaseEvent.connect(function(event) { - if (event.text == "SPACE") { - movingWithHead = false; - } -});