// // hmdControls.js // examples // // Created by Sam Gondelman on 6/17/15 // 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 // var MOVE_DISTANCE = 0.3; var PITCH_INCREMENT = 0.5; // degrees var VR_PITCH_INCREMENT = 15.0; // degrees var YAW_INCREMENT = 0.5; // degrees var VR_YAW_INCREMENT = 15.0; // degrees var BOOM_SPEED = 0.5; var THRESHOLD = 0.2; var CAMERA_UPDATE_TIME = 0.5; var pitchTimer = CAMERA_UPDATE_TIME; var yawTimer = CAMERA_UPDATE_TIME; var shifted = false; var SHIFT_UPDATE_TIME = 0.5; var shiftTimer = SHIFT_UPDATE_TIME; var SHIFT_MAG = 4.0; var hmdControls = (function () { function onActionEvent(action, state) { if (state < THRESHOLD) { if (action == 6 || action == 7) { yawTimer = CAMERA_UPDATE_TIME; } else if (action == 8 || action == 9) { pitchTimer = CAMERA_UPDATE_TIME; } return; } switch (action) { case 0: // backward var direction = Quat.getFront(Camera.getOrientation()); direction = Vec3.multiply(-1, direction); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); MyAvatar.position = Vec3.sum(MyAvatar.position, direction); break; case 1: // forward var direction = Quat.getFront(Camera.getOrientation()); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); MyAvatar.position = Vec3.sum(MyAvatar.position, direction); break; case 2: // left var direction = Quat.getRight(Camera.getOrientation()); direction = Vec3.multiply(-1, direction); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); MyAvatar.position = Vec3.sum(MyAvatar.position, direction); break; case 3: // right var direction = Quat.getRight(Camera.getOrientation()); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); MyAvatar.position = Vec3.sum(MyAvatar.position, direction); break; case 4: // down var direction = Quat.getUp(Camera.getOrientation()); direction = Vec3.multiply(-1, direction); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); MyAvatar.position = Vec3.sum(MyAvatar.position, direction); break; case 5: // up var direction = Quat.getUp(Camera.getOrientation()); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); MyAvatar.position = Vec3.sum(MyAvatar.position, direction); break; case 6: // yaw left if (yawTimer < 0.0 && Menu.isOptionChecked("Enable VR Mode")) { MyAvatar.bodyYaw = MyAvatar.bodyYaw + (shifted ? SHIFT_MAG * VR_YAW_INCREMENT : VR_YAW_INCREMENT); yawTimer = CAMERA_UPDATE_TIME; } else if (!Menu.isOptionChecked("Enable VR Mode")) { MyAvatar.bodyYaw = MyAvatar.bodyYaw + (shifted ? SHIFT_MAG * YAW_INCREMENT : YAW_INCREMENT); } break; case 7: // yaw right if (yawTimer < 0.0 && Menu.isOptionChecked("Enable VR Mode")) { MyAvatar.bodyYaw = MyAvatar.bodyYaw - (shifted ? SHIFT_MAG * VR_YAW_INCREMENT : VR_YAW_INCREMENT); yawTimer = CAMERA_UPDATE_TIME; } else if (!Menu.isOptionChecked("Enable VR Mode")) { MyAvatar.bodyYaw = MyAvatar.bodyYaw - (shifted ? SHIFT_MAG * YAW_INCREMENT : YAW_INCREMENT); } break; case 8: // pitch down if (pitchTimer < 0.0 && Menu.isOptionChecked("Enable VR Mode")) { MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch - (shifted ? SHIFT_MAG * VR_PITCH_INCREMENT : VR_PITCH_INCREMENT))); pitchTimer = CAMERA_UPDATE_TIME; } else if (!Menu.isOptionChecked("Enable VR Mode")) { MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch - (shifted ? SHIFT_MAG * PITCH_INCREMENT : PITCH_INCREMENT))); } break; case 9: // pitch up if (pitchTimer < 0.0 && Menu.isOptionChecked("Enable VR Mode")) { MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch + (shifted ? SHIFT_MAG * VR_PITCH_INCREMENT : VR_PITCH_INCREMENT))); pitchTimer = CAMERA_UPDATE_TIME; } else if (!Menu.isOptionChecked("Enable VR Mode")) { MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch + (shifted ? SHIFT_MAG * PITCH_INCREMENT : PITCH_INCREMENT))); } break; case 12: // shift if (shiftTimer < 0.0) { shifted = !shifted; shiftTimer = SHIFT_UPDATE_TIME; } break; default: break; } } function update(dt) { if (yawTimer >= 0.0) { yawTimer = yawTimer - dt; } if (pitchTimer >= 0.0) { pitchTimer = pitchTimer - dt; } if (shiftTimer >= 0.0) { shiftTimer = shiftTimer - dt; } } function setUp() { Controller.captureActionEvents(); Controller.actionEvent.connect(onActionEvent); Script.update.connect(update); } function tearDown() { Controller.releaseActionEvents(); } setUp(); Script.scriptEnding.connect(tearDown); }());