finished hmdControls.js

This commit is contained in:
Sam Gondelman 2015-06-18 18:43:47 -07:00
parent 012a508eb2
commit decae5c1f1

View file

@ -9,7 +9,7 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
var MOVE_DISTANCE = 0.3; var MOVE_DISTANCE = 10.0;
var PITCH_INCREMENT = 0.5; // degrees var PITCH_INCREMENT = 0.5; // degrees
var YAW_INCREMENT = 0.5; // degrees var YAW_INCREMENT = 0.5; // degrees
var VR_YAW_INCREMENT = 15.0; // degrees var VR_YAW_INCREMENT = 15.0; // degrees
@ -50,52 +50,63 @@ var warpLine = Overlays.addOverlay("line3d", {
visible: false, visible: false,
}); });
var velocity = { x: 0, y: 0, z: 0 };
var VERY_LONG_TIME = 1000000.0;
var hmdControls = (function () { var hmdControls = (function () {
function findAction(name) {
var actions = Controller.getAllActions();
for (var i = 0; i < actions.length; i++) {
if (actions[i].actionName == name) {
return i;
}
}
// If the action isn't found, it will default to the first available action
return 0;
}
function onActionEvent(action, state) { function onActionEvent(action, state) {
if (state < THRESHOLD) { if (state < THRESHOLD) {
if (action == 6 || action == 7) { if (action == findAction("YAW_LEFT") || action == findAction("YAW_RIGHT")) {
yawTimer = CAMERA_UPDATE_TIME; yawTimer = CAMERA_UPDATE_TIME;
} else if (action == 8 || action == 9) { } else if (action == findAction("PITCH_UP") || action == findAction("PITCH_DOWN")) {
pitchTimer = CAMERA_UPDATE_TIME; pitchTimer = CAMERA_UPDATE_TIME;
} }
return; return;
} }
switch (action) { switch (action) {
case 0: // backward case findAction("LONGITUDINAL_BACKWARD"):
var direction = Quat.getFront(Camera.getOrientation()); var direction = {x: 0.0, y: 0.0, z:1.0};
direction = Vec3.multiply(-1, direction);
direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE);
MyAvatar.position = Vec3.sum(MyAvatar.position, direction); velocity = Vec3.sum(velocity, direction);
break; break;
case 1: // forward case findAction("LONGITUDINAL_FORWARD"):
var direction = Quat.getFront(Camera.getOrientation()); var direction = {x: 0.0, y: 0.0, z:-1.0};
direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE);
MyAvatar.position = Vec3.sum(MyAvatar.position, direction); velocity = Vec3.sum(velocity, direction);
break; break;
case 2: // left case findAction("LATERAL_LEFT"):
var direction = Quat.getRight(Camera.getOrientation()); var direction = {x:-1.0, y: 0.0, z: 0.0}
direction = Vec3.multiply(-1, direction);
direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE);
MyAvatar.position = Vec3.sum(MyAvatar.position, direction); velocity = Vec3.sum(velocity, direction);
break; break;
case 3: // right case findAction("LATERAL_RIGHT"):
var direction = Quat.getRight(Camera.getOrientation()); var direction = {x:1.0, y: 0.0, z: 0.0};
direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE);
MyAvatar.position = Vec3.sum(MyAvatar.position, direction); velocity = Vec3.sum(velocity, direction);
break; break;
case 4: // down case findAction("VERTICAL_DOWN"):
var direction = Quat.getUp(Camera.getOrientation()); var direction = {x: 0.0, y: -1.0, z: 0.0};
direction = Vec3.multiply(-1, direction);
direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE);
MyAvatar.position = Vec3.sum(MyAvatar.position, direction); velocity = Vec3.sum(velocity, direction);
break; break;
case 5: // up case findAction("VERTICAL_UP"):
var direction = Quat.getUp(Camera.getOrientation()); var direction = {x: 0.0, y: 1.0, z: 0.0};
direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE); direction = Vec3.multiply(Vec3.normalize(direction), shifted ? SHIFT_MAG * MOVE_DISTANCE : MOVE_DISTANCE);
MyAvatar.position = Vec3.sum(MyAvatar.position, direction); velocity = Vec3.sum(velocity, direction);
break; break;
case 6: // yaw left case findAction("YAW_LEFT"):
if (yawTimer < 0.0 && Menu.isOptionChecked("Enable VR Mode")) { if (yawTimer < 0.0 && Menu.isOptionChecked("Enable VR Mode")) {
MyAvatar.bodyYaw = MyAvatar.bodyYaw + (shifted ? SHIFT_MAG * VR_YAW_INCREMENT : VR_YAW_INCREMENT); MyAvatar.bodyYaw = MyAvatar.bodyYaw + (shifted ? SHIFT_MAG * VR_YAW_INCREMENT : VR_YAW_INCREMENT);
yawTimer = CAMERA_UPDATE_TIME; yawTimer = CAMERA_UPDATE_TIME;
@ -103,7 +114,7 @@ var hmdControls = (function () {
MyAvatar.bodyYaw = MyAvatar.bodyYaw + (shifted ? SHIFT_MAG * YAW_INCREMENT : YAW_INCREMENT); MyAvatar.bodyYaw = MyAvatar.bodyYaw + (shifted ? SHIFT_MAG * YAW_INCREMENT : YAW_INCREMENT);
} }
break; break;
case 7: // yaw right case findAction("YAW_RIGHT"):
if (yawTimer < 0.0 && Menu.isOptionChecked("Enable VR Mode")) { if (yawTimer < 0.0 && Menu.isOptionChecked("Enable VR Mode")) {
MyAvatar.bodyYaw = MyAvatar.bodyYaw - (shifted ? SHIFT_MAG * VR_YAW_INCREMENT : VR_YAW_INCREMENT); MyAvatar.bodyYaw = MyAvatar.bodyYaw - (shifted ? SHIFT_MAG * VR_YAW_INCREMENT : VR_YAW_INCREMENT);
yawTimer = CAMERA_UPDATE_TIME; yawTimer = CAMERA_UPDATE_TIME;
@ -111,23 +122,23 @@ var hmdControls = (function () {
MyAvatar.bodyYaw = MyAvatar.bodyYaw - (shifted ? SHIFT_MAG * YAW_INCREMENT : YAW_INCREMENT); MyAvatar.bodyYaw = MyAvatar.bodyYaw - (shifted ? SHIFT_MAG * YAW_INCREMENT : YAW_INCREMENT);
} }
break; break;
case 8: // pitch down case findAction("PITCH_DOWN"):
if (!Menu.isOptionChecked("Enable VR Mode")) { if (!Menu.isOptionChecked("Enable VR Mode")) {
MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch - (shifted ? SHIFT_MAG * PITCH_INCREMENT : PITCH_INCREMENT))); MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch - (shifted ? SHIFT_MAG * PITCH_INCREMENT : PITCH_INCREMENT)));
} }
break; break;
case 9: // pitch up case findAction("PITCH_UP"):
if (!Menu.isOptionChecked("Enable VR Mode")) { if (!Menu.isOptionChecked("Enable VR Mode")) {
MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch + (shifted ? SHIFT_MAG * PITCH_INCREMENT : PITCH_INCREMENT))); MyAvatar.headPitch = Math.max(-180, Math.min(180, MyAvatar.headPitch + (shifted ? SHIFT_MAG * PITCH_INCREMENT : PITCH_INCREMENT)));
} }
break; break;
case 12: // shift case findAction("SHIFT"): // speed up
if (shiftTimer < 0.0) { if (shiftTimer < 0.0) {
shifted = !shifted; shifted = !shifted;
shiftTimer = SHIFT_UPDATE_TIME; shiftTimer = SHIFT_UPDATE_TIME;
} }
break; break;
case 13: // action1 = start/end warp case findAction("ACTION1"): // start/end warp
if (warpTimer < 0.0) { if (warpTimer < 0.0) {
warpActive = !warpActive; warpActive = !warpActive;
if (!warpActive) { if (!warpActive) {
@ -136,7 +147,7 @@ var hmdControls = (function () {
warpTimer = WARP_UPDATE_TIME; warpTimer = WARP_UPDATE_TIME;
} }
break; break;
case 14: // action2 = cancel warp case findAction("ACTION2"): // cancel warp
warpActive = false; warpActive = false;
Overlays.editOverlay(warpSphere, { Overlays.editOverlay(warpSphere, {
visible: false, visible: false,
@ -163,6 +174,10 @@ var hmdControls = (function () {
if (warpActive) { if (warpActive) {
updateWarp(); updateWarp();
} }
MyAvatar.motorVelocity = velocity;
MyAvatar.motorTimescale = 0.0;
velocity = { x: 0, y: 0, z: 0 };
} }
function updateWarp() { function updateWarp() {
@ -227,6 +242,8 @@ var hmdControls = (function () {
function tearDown() { function tearDown() {
Controller.releaseActionEvents(); Controller.releaseActionEvents();
MyAvatar.motorVelocity = {x:0.0, y:0.0, z:0.0}
MyAvatar.motorTimescale = VERY_LONG_TIME;
} }
setUp(); setUp();