From ef325db052aa4ec933c7e40c245753298ece25cb Mon Sep 17 00:00:00 2001 From: DaveDubUK Date: Sat, 8 Nov 2014 11:47:57 +0000 Subject: [PATCH] Formatting changes to walk.js 1.1 Minor formatting changes for HiFi coding standard compliance --- examples/libraries/walkApi.js | 51 ++-- examples/libraries/walkFilters.js | 48 ++-- examples/libraries/walkInterface.js | 156 ++++++------ examples/walk.js | 358 ++++++++++++++++------------ 4 files changed, 321 insertions(+), 292 deletions(-) diff --git a/examples/libraries/walkApi.js b/examples/libraries/walkApi.js index 90b9dad623..8f9bbfc36c 100644 --- a/examples/libraries/walkApi.js +++ b/examples/libraries/walkApi.js @@ -90,25 +90,22 @@ Motion = function() { for (var i = 0; i < this.avatarJointNames.length; i++) { - if (i > 17 || i < 34) + if (i > 17 || i < 34) { // left hand fingers MyAvatar.setJointData(this.avatarJointNames[i], Quat.fromPitchYawRollDegrees(16, 0, 0)); - - else if (i > 33 || i < 38) + } else if (i > 33 || i < 38) { // left hand thumb MyAvatar.setJointData(this.avatarJointNames[i], Quat.fromPitchYawRollDegrees(4, 0, 0)); - - else if (i > 41 || i < 58) + } else if (i > 41 || i < 58) { // right hand fingers MyAvatar.setJointData(this.avatarJointNames[i], Quat.fromPitchYawRollDegrees(16, 0, 0)); - - else if (i > 57 || i < 62) + } else if (i > 57 || i < 62) { // right hand thumb MyAvatar.setJointData(this.avatarJointNames[i], Quat.fromPitchYawRollDegrees(4, 0, 0)); - - else + } else { // zero out the remaining joints MyAvatar.clearJointData(this.avatarJointNames[i]); + } } } @@ -123,15 +120,16 @@ Motion = function() { this.walkWheelPos = 0; this.advanceWalkWheel = function(angle){ - this.walkWheelPos += angle; - if (motion.walkWheelPos >= 360) + if (motion.walkWheelPos >= 360) { this.walkWheelPos = this.walkWheelPos % 360; + } } // last frame history this.lastDirection = 0; this.lastVelocity = 0; + this.lastStrideLength = 0; // kept for use during transitions }; // end Motion constructor @@ -159,7 +157,7 @@ state = (function () { // status vars powerOn: true, minimised: true, - editing:false, + editing: false, editingTranslation: false, setInternalState: function(newInternalState) { @@ -284,10 +282,11 @@ state = (function () { if (motion.strideLength === 0) { motion.setGender(MALE); - if (motion.direction === BACKWARDS) + if (motion.direction === BACKWARDS) { motion.strideLength = motion.selWalk.calibration.strideLengthBackwards; - else + } else { motion.strideLength = motion.selWalk.calibration.strideLengthForwards; + } } return; } @@ -299,26 +298,30 @@ state = (function () { Transition = function(lastAnimation, nextAnimation, reachPoses, transitionDuration, easingLower, easingUpper) { this.lastAnim = lastAnimation; // name of last animation + this.nextAnimation = nextAnimation; // name of next animation if (lastAnimation === motion.selWalk || nextAnimation === motion.selSideStepLeft || - nextAnimation === motion.selSideStepRight) - this.walkingAtStart = true; // boolean - is the last animation a walking animation? - else - this.walkingAtStart = false; // boolean - is the last animation a walking animation? - this.nextAnimation = nextAnimation; // name of next animation + nextAnimation === motion.selSideStepRight) { + // boolean - is the last animation a walking animation? + this.walkingAtStart = true; + } else { + this.walkingAtStart = false; + } if (nextAnimation === motion.selWalk || nextAnimation === motion.selSideStepLeft || - nextAnimation === motion.selSideStepRight) - this.walkingAtEnd = true; // boolean - is the next animation a walking animation? - else - this.walkingAtEnd = false; // boolean - is the next animation a walking animation? + nextAnimation === motion.selSideStepRight) { + // boolean - is the next animation a walking animation? + this.walkingAtEnd = true; + } else { + this.walkingAtEnd = false; + } this.reachPoses = reachPoses; // placeholder / stub: array of reach poses for squash and stretch techniques this.transitionDuration = transitionDuration; // length of transition (seconds) this.easingLower = easingLower; // Bezier curve handle (normalised) this.easingUpper = easingUpper; // Bezier curve handle (normalised) this.startTime = new Date().getTime(); // Starting timestamp (seconds) this.progress = 0; // how far are we through the transition? - this.walkWheelIncrement = 6; // how much to turn the walkwheel each frame when transitioning to / from walking + this.walkWheelIncrement = 3; // how much to turn the walkwheel each frame when transitioning to / from walking this.walkWheelAdvance = 0; // how many degrees the walk wheel has been advanced during the transition this.walkStopAngle = 0; // what angle should we stop the walk cycle? diff --git a/examples/libraries/walkFilters.js b/examples/libraries/walkFilters.js index 608618e580..d587aea447 100644 --- a/examples/libraries/walkFilters.js +++ b/examples/libraries/walkFilters.js @@ -17,7 +17,6 @@ AveragingFilter = function(length) { this.pastValues = []; for(var i = 0; i < length; i++) { - this.pastValues.push(0); } @@ -26,17 +25,14 @@ AveragingFilter = function(length) { if (this.pastValues.length === 0 && arguments[0]) { return arguments[0]; - } - else if (arguments[0]) { - + } else if (arguments[0]) { // apply quick and simple LP filtering this.pastValues.push(arguments[0]); this.pastValues.shift(); var nextOutputValue = 0; for (var ea in this.pastValues) nextOutputValue += this.pastValues[ea]; return nextOutputValue / this.pastValues.length; - } - else return 0; + } else return 0; }; }; @@ -44,15 +40,10 @@ AveragingFilter = function(length) { // provides LP filtering with a more stable frequency / phase response ButterworthFilter = function(cutOff) { - switch(cutOff) { - - case 5: - - this.gain = 20.20612010; - this.coeffOne = -0.4775922501; - this.coeffTwo = 1.2796324250; - break; - } + // cut off frequency = 5Hz + this.gain = 20.20612010; + this.coeffOne = -0.4775922501; + this.coeffTwo = 1.2796324250; // initialise the arrays this.xv = []; @@ -95,39 +86,42 @@ WaveSynth = function(waveShape, numHarmonics, smoothing) { var harmonics = 0; var multiplier = 0; var iterations = this.numHarmonics * 2 + 2; - if (this.waveShape === TRIANGLE) iterations++; + if (this.waveShape === TRIANGLE) { + iterations++; + } for(var n = 2; n < iterations; n++) { switch(this.waveShape) { - case SAWTOOTH: + case SAWTOOTH: { multiplier = 1 / n; harmonics += multiplier * Math.sin(n * frequency); break; + } - case TRIANGLE: + case TRIANGLE: { if (n % 2 === 1) { - var mulitplier = 1 / (n * n); - - // multiply every (4n-1)th harmonic by -1 - if (n === 3 || n === 7 || n === 11 || n === 15) + // multiply (4n-1)th harmonics by -1 + if (n === 3 || n === 7 || n === 11 || n === 15) { mulitplier *= -1; + } harmonics += mulitplier * Math.sin(n * frequency); } break; + } - case SQUARE: + case SQUARE: { if (n % 2 === 1) { - multiplier = 1 / n; harmonics += multiplier * Math.sin(n * frequency); } break; + } } } @@ -216,13 +210,13 @@ filter = (function() { return pos; }, - // simple clipping filter (faster way to make square waveforms) + // simple clipping filter (clips bottom of wave only, special case for hips y-axis skeleton offset) clipTrough: function(inputValue, peak, strength) { var outputValue = inputValue * strength; - if (outputValue < -peak) + if (outputValue < -peak) { outputValue = -peak; - + } return outputValue; } } diff --git a/examples/libraries/walkInterface.js b/examples/libraries/walkInterface.js index 71e4462933..76b28498d8 100644 --- a/examples/libraries/walkInterface.js +++ b/examples/libraries/walkInterface.js @@ -1363,22 +1363,20 @@ walkInterface = (function() { function setBackground(backgroundID) { for (var i in _backgroundOverlays) { - if (_backgroundOverlays[i] === backgroundID) + if (_backgroundOverlays[i] === backgroundID) { Overlays.editOverlay(_backgroundOverlays[i], { visible: true }); - else Overlays.editOverlay(_backgroundOverlays[i], { - visible: false - }); + } else { + Overlays.editOverlay(_backgroundOverlays[i], { visible: false }); + } } }; // top row menu type buttons (on | walk | stand | fly | hide) function hideMenuButtons() { for (var i in _buttonOverlays) { - Overlays.editOverlay(_buttonOverlays[i], { - visible: false - }); + Overlays.editOverlay(_buttonOverlays[i], { visible: false }); } }; @@ -1401,9 +1399,7 @@ walkInterface = (function() { function setButtonOverlayVisible(buttonOverlayName) { for (var i in _buttonOverlays) { if (_buttonOverlays[i] === buttonOverlayName) { - Overlays.editOverlay(buttonOverlayName, { - visible: true - }); + Overlays.editOverlay(buttonOverlayName, { visible: true }); } } }; @@ -1484,13 +1480,13 @@ walkInterface = (function() { }); } - if (!showButtons) return; + if (!showButtons) { + return; + } // set all the non-selected ones to showing for (var i = 8; i < _bigbuttonOverlays.length; i += 2) { - Overlays.editOverlay(_bigbuttonOverlays[i], { - visible: true - }); + Overlays.editOverlay(_bigbuttonOverlays[i], { visible: true }); } // set the currently selected one @@ -1877,6 +1873,7 @@ walkInterface = (function() { _motion.curJointIndex = 4; initialiseJointsEditingPanel(); return; + } else if (clickX > 78 && clickX < 121 && clickY > 111 && clickY < 128) { _motion.curJointIndex = 5; initialiseJointsEditingPanel(); @@ -1962,7 +1959,7 @@ walkInterface = (function() { return; case _hideButton: - _hideButtonSelected: + case _hideButtonSelected: Overlays.editOverlay(_hideButton, { visible: false @@ -2084,8 +2081,11 @@ walkInterface = (function() { case _standardWalkBigButton: - if (_motion.avatarGender === FEMALE) _motion.selWalk = _motion.femaleStandardWalk; - else _motion.selWalk = _motion.maleStandardWalk; + if (_motion.avatarGender === FEMALE) { + _motion.selWalk = _motion.femaleStandardWalk; + } else { + _motion.selWalk = _motion.maleStandardWalk; + } _motion.curAnim = _motion.selWalk; initialiseWalkStylesPanel(true); return; @@ -2095,7 +2095,9 @@ walkInterface = (function() { // toggle forwards / backwards walk display if (_motion.direction === FORWARDS) { _motion.direction = BACKWARDS; - } else _motion.direction = FORWARDS; + } else { + _motion.direction = FORWARDS; + } return; case _sliderOne: @@ -2240,7 +2242,9 @@ walkInterface = (function() { // workaround for bug (https://worklist.net/20160) if ((event.x > 310 && event.x < 318 && event.y > 1350 && event.y < 1355) || - (event.x > 423 && event.x < 428 && event.y > 1505 && event.y < 1508 )) return; + (event.x > 423 && event.x < 428 && event.y > 1505 && event.y < 1508 )) { + return; + } if (_state.currentState === _state.EDIT_WALK_JOINTS || _state.currentState === _state.EDIT_STANDING || @@ -2252,8 +2256,11 @@ walkInterface = (function() { var thumbClickOffsetX = event.x - _minSliderX; var thumbPositionNormalised = thumbClickOffsetX / _sliderRangeX; - if (thumbPositionNormalised < 0) thumbPositionNormalised = 0; - if (thumbPositionNormalised > 1) thumbPositionNormalised = 1; + if (thumbPositionNormalised < 0) { + thumbPositionNormalised = 0; + } else if (thumbPositionNormalised > 1) { + thumbPositionNormalised = 1; + } var sliderX = thumbPositionNormalised * _sliderRangeX; // sets range if (_movingSliderOne) { @@ -2263,12 +2270,9 @@ walkInterface = (function() { x: sliderX + _minSliderX }); if (_state.editingTranslation) { - _motion.curAnim.joints[0].sway = thumbPositionNormalised * _sliderRanges.joints[0].swayRange; - } else { - _motion.curAnim.joints[_motion.curJointIndex].pitch = thumbPositionNormalised * _sliderRanges.joints[_motion.curJointIndex].pitchRange; } @@ -2280,12 +2284,9 @@ walkInterface = (function() { x: sliderX + _minSliderX }); if (_state.editingTranslation) { - _motion.curAnim.joints[0].bob = thumbPositionNormalised * _sliderRanges.joints[0].bobRange; - } else { - _motion.curAnim.joints[_motion.curJointIndex].yaw = thumbPositionNormalised * _sliderRanges.joints[_motion.curJointIndex].yawRange; } @@ -2297,12 +2298,9 @@ walkInterface = (function() { x: sliderX + _minSliderX }); if (_state.editingTranslation) { - _motion.curAnim.joints[0].thrust = thumbPositionNormalised * _sliderRanges.joints[0].thrustRange; - } else { - _motion.curAnim.joints[_motion.curJointIndex].roll = thumbPositionNormalised * _sliderRanges.joints[_motion.curJointIndex].rollRange; } @@ -2317,11 +2315,8 @@ walkInterface = (function() { var newPhase = 360 * thumbPositionNormalised - 180; if (_state.editingTranslation) { - _motion.curAnim.joints[0].swayPhase = newPhase; - } else { - _motion.curAnim.joints[_motion.curJointIndex].pitchPhase = newPhase; } @@ -2335,11 +2330,8 @@ walkInterface = (function() { var newPhase = 360 * thumbPositionNormalised - 180; if (_state.editingTranslation) { - _motion.curAnim.joints[0].bobPhase = newPhase; - } else { - _motion.curAnim.joints[_motion.curJointIndex].yawPhase = newPhase; } @@ -2353,11 +2345,8 @@ walkInterface = (function() { var newPhase = 360 * thumbPositionNormalised - 180; if (_state.editingTranslation) { - _motion.curAnim.joints[0].thrustPhase = newPhase; - } else { - _motion.curAnim.joints[_motion.curJointIndex].rollPhase = newPhase; } @@ -2368,13 +2357,10 @@ walkInterface = (function() { x: sliderX + _minSliderX }); if (_state.editingTranslation) { - var newOffset = (thumbPositionNormalised - 0.5) * 2 * _sliderRanges.joints[0].swayOffsetRange; _motion.curAnim.joints[0].swayOffset = newOffset; - } else { - var newOffset = (thumbPositionNormalised - 0.5) * 2 * _sliderRanges.joints[_motion.curJointIndex].pitchOffsetRange; _motion.curAnim.joints[_motion.curJointIndex].pitchOffset = newOffset; @@ -2387,13 +2373,10 @@ walkInterface = (function() { x: sliderX + _minSliderX }); if (_state.editingTranslation) { - var newOffset = (thumbPositionNormalised - 0.5) * 2 *_sliderRanges.joints[0].bobOffsetRange; _motion.curAnim.joints[0].bobOffset = newOffset; - } else { - var newOffset = (thumbPositionNormalised - 0.5) * 2 * _sliderRanges.joints[_motion.curJointIndex].yawOffsetRange; _motion.curAnim.joints[_motion.curJointIndex].yawOffset = newOffset; @@ -2406,21 +2389,19 @@ walkInterface = (function() { x: sliderX + _minSliderX }); if (_state.editingTranslation) { - var newOffset = (thumbPositionNormalised - 0.5) * 2 * _sliderRanges.joints[0].thrustOffsetRange; _motion.curAnim.joints[0].thrustOffset = newOffset; - } else { - var newOffset = (thumbPositionNormalised - 0.5) * 2 * _sliderRanges.joints[_motion.curJointIndex].rollOffsetRange; _motion.curAnim.joints[_motion.curJointIndex].rollOffset = newOffset; } } - } // end if editing joints - else if (_state.currentState === _state.EDIT_WALK_TWEAKS) { + // end if editing joints + + } else if (_state.currentState === _state.EDIT_WALK_TWEAKS) { // sliders for commonly required walk adjustments var thumbClickOffsetX = event.x - _minSliderX; @@ -2430,50 +2411,39 @@ walkInterface = (function() { var sliderX = thumbPositionNormalised * _sliderRangeX; // sets range if (_movingSliderOne) { - // walk speed Overlays.editOverlay(_sliderOne, { x: sliderX + _minSliderX }); _motion.curAnim.calibration.frequency = thumbPositionNormalised * MAX_WALK_SPEED; - } else if (_movingSliderTwo) { - // lean (hips pitch offset) Overlays.editOverlay(_sliderTwo, { x: sliderX + _minSliderX }); var newOffset = (thumbPositionNormalised - 0.5) * 2 * _sliderRanges.joints[0].pitchOffsetRange; _motion.curAnim.joints[0].pitchOffset = newOffset; - } else if (_movingSliderThree) { - // stride (upper legs pitch) Overlays.editOverlay(_sliderThree, { x: sliderX + _minSliderX }); _motion.curAnim.joints[1].pitch = thumbPositionNormalised * _sliderRanges.joints[1].pitchRange; - } else if (_movingSliderFour) { - // legs separation (upper legs roll) Overlays.editOverlay(_sliderFour, { x: sliderX + _minSliderX }); _motion.curAnim.joints[1].rollOffset = (thumbPositionNormalised - 0.5) * 2 * _sliderRanges.joints[1].rollOffsetRange; - } else if (_movingSliderFive) { - // legs forward (lower legs pitch offset) Overlays.editOverlay(_sliderFive, { x: sliderX + _minSliderX }); _motion.curAnim.joints[1].pitchOffset = (thumbPositionNormalised - 0.5) * 2 * _sliderRanges.joints[1].pitchOffsetRange; - } else if (_movingSliderSix) { - // lower legs splay (lower legs roll offset) Overlays.editOverlay(_sliderSix, { x: sliderX + _minSliderX @@ -2482,25 +2452,20 @@ walkInterface = (function() { 2 * _sliderRanges.joints[2].rollOffsetRange; } else if (_movingSliderSeven) { - // arms forward (upper arms yaw offset) Overlays.editOverlay(_sliderSeven, { x: sliderX + _minSliderX }); _motion.curAnim.joints[9].yawOffset = (thumbPositionNormalised - 0.5) * 2 * _sliderRanges.joints[9].yawOffsetRange; - } else if (_movingSliderEight) { - // arms out (upper arm pitch offset) Overlays.editOverlay(_sliderEight, { x: sliderX + _minSliderX }); _motion.curAnim.joints[9].pitchOffset = (thumbPositionNormalised - 0.5) * -2 * _sliderRanges.joints[9].pitchOffsetRange; - } else if (_movingSliderNine) { - // lower arms splay (lower arm pitch offset) Overlays.editOverlay(_sliderNine, { x: sliderX + _minSliderX @@ -2508,21 +2473,30 @@ walkInterface = (function() { _motion.curAnim.joints[10].pitchOffset = (thumbPositionNormalised - 0.5) * -2 * _sliderRanges.joints[10].pitchOffsetRange; } - } // if tweaking }; function mouseReleaseEvent(event) { - if (_movingSliderOne) _movingSliderOne = false; - else if (_movingSliderTwo) _movingSliderTwo = false; - else if (_movingSliderThree) _movingSliderThree = false; - else if (_movingSliderFour) _movingSliderFour = false; - else if (_movingSliderFive) _movingSliderFive = false; - else if (_movingSliderSix) _movingSliderSix = false; - else if (_movingSliderSeven) _movingSliderSeven = false; - else if (_movingSliderEight) _movingSliderEight = false; - else if (_movingSliderNine) _movingSliderNine = false; + if (_movingSliderOne) { + _movingSliderOne = false; + } else if (_movingSliderTwo) { + _movingSliderTwo = false; + } else if (_movingSliderThree) { + _movingSliderThree = false; + } else if (_movingSliderFour) { + _movingSliderFour = false; + } else if (_movingSliderFive) { + _movingSliderFive = false; + } else if (_movingSliderSix) { + _movingSliderSix = false; + } else if (_movingSliderSeven) { + _movingSliderSeven = false; + } else if (_movingSliderEight) { + _movingSliderEight = false; + } else if (_movingSliderNine) { + _movingSliderNine = false; + } }; Controller.mousePressEvent.connect(mousePressEvent); @@ -2575,7 +2549,7 @@ walkInterface = (function() { case _state.EDIT_WALK_STYLES: case _state.EDIT_WALK_TWEAKS: - case _state.EDIT_WALK_JOINTS: + case _state.EDIT_WALK_JOINTS: { hideMenuButtons(); initialiseFrontPanel(false); @@ -2604,10 +2578,11 @@ walkInterface = (function() { } else if (_state.currentState === _state.EDIT_WALK_JOINTS) { - if (_state.editingTranslation) + if (_state.editingTranslation) { setBackground(_controlsBackgroundWalkEditHipTrans); - else + } else { setBackground(_controlsBackgroundWalkEditJoints); + } initialiseWalkStylesPanel(false); setSliderThumbsVisible(true); @@ -2619,15 +2594,17 @@ walkInterface = (function() { setButtonOverlayVisible(_onButton); setButtonOverlayVisible(_backButton); return; + } case _state.EDIT_STANDING: case _state.EDIT_SIDESTEP_LEFT: - case _state.EDIT_SIDESTEP_RIGHT: + case _state.EDIT_SIDESTEP_RIGHT: { - if (_state.editingTranslation) + if (_state.editingTranslation) { setBackground(_controlsBackgroundWalkEditHipTrans); - else + } else { setBackground(_controlsBackgroundWalkEditJoints); + } hideMenuButtons(); initialiseWalkStylesPanel(false); initialiseFrontPanel(false); @@ -2654,10 +2631,11 @@ walkInterface = (function() { setButtonOverlayVisible(_onButton); setButtonOverlayVisible(_backButton); return; + } case _state.EDIT_FLYING: case _state.EDIT_FLYING_UP: - case _state.EDIT_FLYING_DOWN: + case _state.EDIT_FLYING_DOWN: { setBackground(_controlsBackgroundWalkEditJoints); hideMenuButtons(); @@ -2685,18 +2663,22 @@ walkInterface = (function() { setButtonOverlayVisible(_onButton); setButtonOverlayVisible(_backButton); return; + } case _state.STANDING: case _state.WALKING: case _state.FLYING: case _state.SIDE_STEP: - default: + default: { hideMenuButtons(); hideJointSelectors(); setBackground(_controlsBackground); - if (_state.powerOn) setButtonOverlayVisible(_onButton); - else setButtonOverlayVisible(_offButton); + if (_state.powerOn) { + setButtonOverlayVisible(_onButton); + } else { + setButtonOverlayVisible(_offButton); + } setButtonOverlayVisible(_configWalkButton); setButtonOverlayVisible(_configStandButton); setButtonOverlayVisible(_configFlyingButton); @@ -2705,9 +2687,9 @@ walkInterface = (function() { initialiseFrontPanel(true); initialiseWalkStylesPanel(false); return; + } } } } }; // end public methods (return) - })(); \ No newline at end of file diff --git a/examples/walk.js b/examples/walk.js index 1e1cd7748b..279516aa2a 100644 --- a/examples/walk.js +++ b/examples/walk.js @@ -14,7 +14,7 @@ // constants var MALE = 1; var FEMALE = 2; -var MAX_WALK_SPEED = 2.5; +var MAX_WALK_SPEED = 2.5;//3.919; var TAKE_FLIGHT_SPEED = 4.55; var TOP_SPEED = 300; var UP = 1; @@ -24,7 +24,7 @@ var RIGHT = 8; var FORWARDS = 16; var BACKWARDS = 32; -// ovelay images location +// location of animation files and overlay images var pathToAssets = 'http://s3.amazonaws.com/hifi-public/WalkScript/'; // load the UI @@ -51,7 +51,7 @@ var SAWTOOTH = 1; var TRIANGLE = 2; var SQUARE = 4; -// filters for synthesising more complex, natural waveforms +// various filters for synthesising more complex, natural waveforms var leanPitchFilter = filter.createAveragingFilter(15); var leanRollFilter = filter.createAveragingFilter(15); var hipsYawShaper = filter.createWaveSynth(TRIANGLE, 3, 2); @@ -71,56 +71,65 @@ Script.update.connect(function(deltaTime) { // check for editing modes first, as these require no positioning calculations switch (state.currentState) { - case state.EDIT_WALK_STYLES: + case state.EDIT_WALK_STYLES: { motion.curAnim = motion.selWalk; animateAvatar(deltaTime, speed); break; + } - case state.EDIT_WALK_TWEAKS: + case state.EDIT_WALK_TWEAKS: { motion.curAnim = motion.selWalk; animateAvatar(deltaTime, speed); break; + } - case state.EDIT_WALK_JOINTS: + case state.EDIT_WALK_JOINTS: { motion.curAnim = motion.selWalk; animateAvatar(deltaTime, speed); break; + } - case state.EDIT_STANDING: + case state.EDIT_STANDING: { motion.curAnim = motion.selStand; motion.direction = FORWARDS; animateAvatar(deltaTime, speed); break; + } - case state.EDIT_SIDESTEP_LEFT: + case state.EDIT_SIDESTEP_LEFT: { motion.curAnim = motion.selSideStepLeft; motion.direction = LEFT; animateAvatar(deltaTime, speed); break; + } - case state.EDIT_SIDESTEP_RIGHT: + case state.EDIT_SIDESTEP_RIGHT: { motion.curAnim = motion.selSideStepRight; motion.direction = RIGHT; animateAvatar(deltaTime, speed); break; + } - case state.EDIT_FLYING: + case state.EDIT_FLYING: { motion.curAnim = motion.selFly; motion.direction = FORWARDS; animateAvatar(deltaTime, speed); break; + } - case state.EDIT_FLYING_UP: + case state.EDIT_FLYING_UP: { motion.curAnim = motion.selFlyUp; motion.direction = UP; animateAvatar(deltaTime, speed); break; + } - case state.EDIT_FLYING_DOWN: + case state.EDIT_FLYING_DOWN: { motion.curAnim = motion.selFlyDown; motion.direction = DOWN; animateAvatar(deltaTime, speed); break; + } default: break; @@ -137,60 +146,78 @@ Script.update.connect(function(deltaTime) { return; } var localVelocity = {x: 0, y: 0, z: 0}; - if (speed > 0) + if (speed > 0) { localVelocity = Vec3.multiplyQbyV(Quat.inverse(MyAvatar.orientation), velocity); + } if (!state.editing) { // determine the candidate animation state var actionToTake = undefined; - if (speed < 0.05) actionToTake = state.STANDING; // as per MIN_AVATAR_SPEED, MyAvatar.cpp - else if (speed < TAKE_FLIGHT_SPEED) actionToTake = state.WALKING; - else if (speed >= TAKE_FLIGHT_SPEED) actionToTake = state.FLYING; + if (speed < 0.05) { + actionToTake = state.STANDING; + } else if (speed < TAKE_FLIGHT_SPEED) { + actionToTake = state.WALKING; + } else if (speed >= TAKE_FLIGHT_SPEED) { + actionToTake = state.FLYING; + } // determine the principle direction if (Math.abs(localVelocity.x) > Math.abs(localVelocity.y) && Math.abs(localVelocity.x) > Math.abs(localVelocity.z)) { - if (localVelocity.x < 0) motion.direction = LEFT; - else motion.direction = RIGHT; + if (localVelocity.x < 0) { + motion.direction = LEFT; + } else { + motion.direction = RIGHT; + } } else if (Math.abs(localVelocity.y) > Math.abs(localVelocity.x) && Math.abs(localVelocity.y) > Math.abs(localVelocity.z)) { - if (localVelocity.y > 0) motion.direction = UP; - else motion.direction = DOWN; + if (localVelocity.y > 0) { + motion.direction = UP; + } else { + motion.direction = DOWN; + } } else if (Math.abs(localVelocity.z) > Math.abs(localVelocity.x) && Math.abs(localVelocity.z) > Math.abs(localVelocity.y)) { - if (localVelocity.z < 0) motion.direction = FORWARDS; - else motion.direction = BACKWARDS; + if (localVelocity.z < 0) { + motion.direction = FORWARDS; + } else { + motion.direction = BACKWARDS; + } } // maybe at walking speed, but sideways? if (actionToTake === state.WALKING && - (motion.direction === LEFT || - motion.direction === RIGHT)) - actionToTake = state.SIDE_STEP; + (motion.direction === LEFT || + motion.direction === RIGHT)) { + actionToTake = state.SIDE_STEP; + } // maybe at walking speed, but flying up or down? if (actionToTake === state.WALKING && - (motion.direction === UP))// || - //motion.direction === DOWN)) - actionToTake = state.FLYING; + (motion.direction === UP || + motion.direction === DOWN)) { + actionToTake = state.FLYING; + } // select appropriate animation and initiate Transition if required + // note: The transitions are not compete, and are the most likely + // candidate for the next worklist item switch (actionToTake) { - case state.STANDING: + case state.STANDING: { // do we need to change state? if (state.currentState !== state.STANDING) { switch (motion.curAnim) { - case motion.selWalk: + case motion.selWalk: { // Walking to standing motion.curTransition = new Transition( @@ -200,57 +227,60 @@ Script.update.connect(function(deltaTime) { {x: 0.1, y: 0.5}, {x: -0.25, y: 1.22}); break; + } - case motion.selSideStepLeft: - case motion.selSideStepRight: + case motion.selFly: + case motion.selFlyUp: + case motion.selFlyDown: { - break; - - default: - - // flying to standing + // Flying to Standing motion.curTransition = new Transition( motion.curAnim, motion.selStand, - [], 0.25, + [], 0.5, {x: 0.5, y: 0.08}, {x: 0.28, y: 1}); break; + } + + default: + + break; } state.setInternalState(state.STANDING); motion.curAnim = motion.selStand; } animateAvatar(deltaTime, speed); break; + } - case state.WALKING: + case state.WALKING: { if (state.currentState !== state.WALKING) { - if (motion.direction === BACKWARDS) - motion.walkWheelPos = motion.selWalk.calibration.startAngleBackwards; - - else motion.walkWheelPos = motion.selWalk.calibration.startAngleForwards; + if (motion.direction === BACKWARDS) { + motion.walkWheelPos = motion.selWalk.calibration.startAngleBackwards; + } else { + motion.walkWheelPos = motion.selWalk.calibration.startAngleForwards; + } switch (motion.curAnim) { - case motion.selStand: + case motion.selStand: { // Standing to Walking motion.curTransition = new Transition( motion.curAnim, motion.selWalk, - [], 0.1, - {x: 0.5, y: 0.1}, - {x: 0.5, y: 0.9}); + [], 0.25, + {x: 0.5, y: 0.5}, + {x: 0.5, y: 0.5}); break; + } - case motion.selSideStepLeft: - case motion.selSideStepRight: - - break; - - default: + case motion.selFly: + case motion.selFlyUp: + case motion.selFlyDown: { // Flying to Walking motion.curTransition = new Transition( @@ -260,69 +290,56 @@ Script.update.connect(function(deltaTime) { {x: 0.24, y: 0.03}, {x: 0.42, y: 1.0}); break; + } + + default: + + break; } state.setInternalState(state.WALKING); } motion.curAnim = motion.selWalk; animateAvatar(deltaTime, speed); break; + } - case state.SIDE_STEP: + case state.SIDE_STEP: { var selSideStep = 0; if (motion.direction === LEFT) { - if (motion.lastDirection !== LEFT) + if (motion.lastDirection !== LEFT) { motion.walkWheelPos = motion.selSideStepLeft.calibration.cycleStart; + } selSideStep = motion.selSideStepLeft; } else { - if (motion.lastDirection !== RIGHT) + if (motion.lastDirection !== RIGHT) { motion.walkWheelPos = motion.selSideStepRight.calibration.cycleStart; + } selSideStep = motion.selSideStepRight; } if (state.currentState !== state.SIDE_STEP) { if (motion.direction === LEFT) { - motion.walkWheelPos = motion.selSideStepLeft.calibration.cycleStart; - switch (motion.curAnim) { - - case motion.selStand: - - break; - - default: - - break; - } - } else { - motion.walkWheelPos = motion.selSideStepRight.calibration.cycleStart; - switch (motion.curAnim) { - - case motion.selStand: - - break; - - default: - - break; - } } state.setInternalState(state.SIDE_STEP); } motion.curAnim = selSideStep; animateAvatar(deltaTime, speed); break; + } - case state.FLYING: + case state.FLYING: { - if (state.currentState !== state.FLYING) + if (state.currentState !== state.FLYING) { state.setInternalState(state.FLYING); + } // change animation for flying directly up or down if (motion.direction === UP) { @@ -332,23 +349,21 @@ Script.update.connect(function(deltaTime) { switch (motion.curAnim) { case motion.selStand: - case motion.selWalk: + case motion.selWalk: { // standing | walking to flying up motion.curTransition = new Transition( motion.curAnim, motion.selFlyUp, - [], 0.25, + [], 0.35, {x: 0.5, y: 0.08}, {x: 0.28, y: 1}); break; + } - case motion.selSideStepLeft: - case motion.selSideStepRight: - - break; - - default: + case motion.selFly: + case motion.selFlyUp: + case motion.selFlyDown: { motion.curTransition = new Transition( motion.curAnim, @@ -357,6 +372,11 @@ Script.update.connect(function(deltaTime) { {x: 0.5, y: 0.08}, {x: 0.28, y: 1}); break; + } + + default: + + break; } motion.curAnim = motion.selFlyUp; } @@ -368,7 +388,7 @@ Script.update.connect(function(deltaTime) { switch (motion.curAnim) { case motion.selStand: - case motion.selWalk: + case motion.selWalk: { motion.curTransition = new Transition( motion.curAnim, @@ -377,21 +397,24 @@ Script.update.connect(function(deltaTime) { {x: 0.5, y: 0.08}, {x: 0.28, y: 1}); break; + } - case motion.selSideStepLeft: - case motion.selSideStepRight: - - break; - - default: + case motion.selFly: + case motion.selFlyUp: + case motion.selFlyDown: { motion.curTransition = new Transition( motion.curAnim, motion.selFlyDown, - [], 0.5, + [], 0.45, {x: 0.5, y: 0.08}, {x: 0.28, y: 1}); break; + } + + default: + + break; } motion.curAnim = motion.selFlyDown; } @@ -403,7 +426,7 @@ Script.update.connect(function(deltaTime) { switch (motion.curAnim) { case motion.selStand: - case motion.selWalk: + case motion.selWalk: { motion.curTransition = new Transition( motion.curAnim, @@ -412,13 +435,11 @@ Script.update.connect(function(deltaTime) { {x: 1.44, y:0.24}, {x: 0.61, y:0.92}); break; + } - case motion.selSideStepLeft: - case motion.selSideStepRight: - - break; - - default: + case motion.selFly: + case motion.selFlyUp: + case motion.selFlyDown: { motion.curTransition = new Transition( motion.curAnim, @@ -427,12 +448,18 @@ Script.update.connect(function(deltaTime) { {x: 0.5, y: 0.08}, {x: 0.28, y: 1}); break; + } + + default: + + break; } motion.curAnim = motion.selFly; } } animateAvatar(deltaTime, speed); break; + }// end case state.FLYING } // end switch(actionToTake) @@ -447,17 +474,20 @@ Script.update.connect(function(deltaTime) { // the faster we go, the further we lean forward. the angle is calcualted here function getLeanPitch(speed) { - if (speed > TOP_SPEED) speed = TOP_SPEED; + if (speed > TOP_SPEED) { + speed = TOP_SPEED; + } var leanProgress = speed / TOP_SPEED; if (motion.direction === LEFT || - motion.direction === RIGHT) + motion.direction === RIGHT) { leanProgress = 0; - - else { + } else { var responseSharpness = 1.5; - if (motion.direction == BACKWARDS) responseSharpness = 3.0; + if (motion.direction == BACKWARDS) { + responseSharpness = 3.0; + } leanProgress = filter.bezier((1 - leanProgress), {x: 0, y: 0.0}, @@ -466,10 +496,11 @@ function getLeanPitch(speed) { {x: 1, y: 1}).y; // determine final pitch and adjust for direction of momentum - if (motion.direction === BACKWARDS) + if (motion.direction === BACKWARDS) { leanProgress = -motion.motionPitchMax * leanProgress; - else + } else { leanProgress = motion.motionPitchMax * leanProgress; + } } // return the smoothed response @@ -480,19 +511,26 @@ function getLeanPitch(speed) { function getLeanRoll(deltaTime, speed) { var leanRollProgress = 0; - if (speed > TOP_SPEED) speed = TOP_SPEED; + if (speed > TOP_SPEED) { + speed = TOP_SPEED; + } // what's our our anglular velocity? var angularVelocityMax = 70; // from observation var angularVelocity = filter.radToDeg(MyAvatar.getAngularVelocity().y); - if (angularVelocity > angularVelocityMax) angularVelocity = angularVelocityMax; - if (angularVelocity < -angularVelocityMax) angularVelocity = -angularVelocityMax; + if (angularVelocity > angularVelocityMax) { + angularVelocity = angularVelocityMax; + } + if (angularVelocity < -angularVelocityMax) { + angularVelocity = -angularVelocityMax; + } leanRollProgress = speed / TOP_SPEED; if (motion.direction !== LEFT && - motion.direction !== RIGHT) + motion.direction !== RIGHT) { leanRollProgress *= (Math.abs(angularVelocity) / angularVelocityMax); + } // apply our response curve leanRollProgress = filter.bezier((1 - leanRollProgress), @@ -502,14 +540,17 @@ function getLeanRoll(deltaTime, speed) { {x: 1, y: 1}).y; // which way to lean? var turnSign = -1; - if (angularVelocity < 0.001) turnSign = 1; + if (angularVelocity < 0.001) { + turnSign = 1; + } if (motion.direction === BACKWARDS || - motion.direction === LEFT) + motion.direction === LEFT) { turnSign *= -1; - + } if (motion.direction === LEFT || - motion.direction === RIGHT) + motion.direction === RIGHT) { leanRollProgress *= 2; + } // add damping with simple averaging filter leanRollProgress = leanRollFilter.process(turnSign * leanRollProgress); @@ -520,12 +561,13 @@ function playFootstep(side) { var options = new AudioInjectionOptions(); options.position = Camera.getPosition(); - options.volume = 0.2; + options.volume = 0.3; var soundNumber = 2; // 0 to 2 - if (side === RIGHT && motion.makesFootStepSounds) + if (side === RIGHT && motion.makesFootStepSounds) { Audio.playSound(walkAssets.footsteps[soundNumber + 1], options); - else if (side === LEFT && motion.makesFootStepSounds) + } else if (side === LEFT && motion.makesFootStepSounds) { Audio.playSound(walkAssets.footsteps[soundNumber], options); + } } // animate the avatar using sine wave generators. inspired by Victorian clockwork dolls @@ -545,13 +587,15 @@ function animateAvatar(deltaTime, speed) { // don't lean into the direction of travel if going up var leanMod = 1; - if (motion.direction === UP) + if (motion.direction === UP) { leanMod = 0; + } // adjust leaning direction for flying var flyingModifier = 1; - if (state.currentState.FLYING) + if (state.currentState.FLYING) { flyingModifier = -1; + } if (motion.curTransition !== nullTransition) { @@ -573,20 +617,22 @@ function animateAvatar(deltaTime, speed) { // find the closest stop point from the walk wheel's angle var angleToLeftStop = 180 - Math.abs(Math.abs(motion.walkWheelPos - leftStop) - 180); var angleToRightStop = 180 - Math.abs(Math.abs(motion.walkWheelPos - rightStop) - 180); - if (motion.walkWheelPos > angleToLeftStop) angleToLeftStop = 360 - angleToLeftStop; - if (motion.walkWheelPos > angleToRightStop) angleToRightStop = 360 - angleToRightStop; - + if (motion.walkWheelPos > angleToLeftStop) { + angleToLeftStop = 360 - angleToLeftStop; + } + if (motion.walkWheelPos > angleToRightStop) { + angleToRightStop = 360 - angleToRightStop; + } motion.curTransition.walkWheelIncrement = 3; // keep the walkwheel turning by setting the walkWheelIncrement // until our feet are tucked nicely underneath us. - if (angleToLeftStop < angleToRightStop) - + if (angleToLeftStop < angleToRightStop) { motion.curTransition.walkStopAngle = leftStop; - - else + } else { motion.curTransition.walkStopAngle = rightStop; + } } else { @@ -622,11 +668,9 @@ function animateAvatar(deltaTime, speed) { motion.curTransition.walkWheelIncrement = 0; } // keep turning walk wheel until both feet are below the avi - motion.advanceWalkWheel(motion.curTransition.walkWheelIncrement); - //motion.curTransition.walkWheelAdvance += motion.curTransition.walkWheelIncrement; - } - else motion.curTransition.walkWheelIncrement = 0; // sidestep + + } else motion.curTransition.walkWheelIncrement = 0; // sidestep } } } // end motion.curTransition !== nullTransition @@ -636,8 +680,9 @@ function animateAvatar(deltaTime, speed) { // if the timing's right, take a snapshot of the stride max and recalibrate var strideMaxAt = motion.curAnim.calibration.forwardStrideMaxAt; - if (motion.direction === BACKWARDS) + if (motion.direction === BACKWARDS) { strideMaxAt = motion.curAnim.calibration.backwardsStrideMaxAt; + } var tolerance = 1.0; if (motion.walkWheelPos < (strideMaxAt + tolerance) && @@ -648,18 +693,20 @@ function animateAvatar(deltaTime, speed) { var footLPos = MyAvatar.getJointPosition("LeftFoot"); motion.strideLength = Vec3.distance(footRPos, footLPos); - if (motion.direction === FORWARDS) + if (motion.direction === FORWARDS) { motion.curAnim.calibration.strideLengthForwards = motion.strideLength; - else if (motion.direction === BACKWARDS) + } else if (motion.direction === BACKWARDS) { motion.curAnim.calibration.strideLengthBackwards = motion.strideLength; + } } else { // use the saved value for stride length - if (motion.direction === FORWARDS) + if (motion.direction === FORWARDS) { motion.strideLength = motion.curAnim.calibration.strideLengthForwards; - else if (motion.direction === BACKWARDS) + } else if (motion.direction === BACKWARDS) { motion.strideLength = motion.curAnim.calibration.strideLengthBackwards; + } } } // end get walk stride length @@ -714,8 +761,9 @@ function animateAvatar(deltaTime, speed) { // if we are in an edit mode, we will need fake time to turn the wheel if (state.currentState !== state.WALKING && - state.currentState !== state.SIDE_STEP) + state.currentState !== state.SIDE_STEP) { degreesTurnedSinceLastFrame = motion.curAnim.calibration.frequency / 70; + } // advance the walk wheel the appropriate amount motion.advanceWalkWheel(degreesTurnedSinceLastFrame); @@ -758,12 +806,6 @@ function animateAvatar(deltaTime, speed) { var sideStepFootPitchModifier = 1; var sideStepHandPitchSign = 1; - // The below code should probably be optimised into some sort of loop, where - // the joints are iterated through. However, this has not been done yet, as there - // are still some quite fundamental changes to be made (e.g. turning on the spot - // animation and sidestepping transitions) so it's been left as is for ease of - // understanding and editing. - // calculate hips translation if (motion.curTransition !== nullTransition) { @@ -774,7 +816,9 @@ function animateAvatar(deltaTime, speed) { motion.curAnim.joints[0].swayPhase)) + motion.curAnim.joints[0].swayOffset; var bobPhase = motion.curAnim.joints[0].bobPhase; - if (motion.direction === motion.BACKWARDS) bobPhase += 90; + if (motion.direction === motion.BACKWARDS) { + bobPhase += 90; + } bobOsc = motion.curAnim.joints[0].bob * Math.sin(filter.degToRad(motion.cumulativeTime * motion.curAnim.calibration.frequency + bobPhase)) + @@ -792,7 +836,9 @@ function animateAvatar(deltaTime, speed) { motion.curTransition.lastAnim.joints[0].swayOffset; var bobPhaseLast = motion.curTransition.lastAnim.joints[0].bobPhase; - if (motion.direction === motion.BACKWARDS) bobPhaseLast +=90; + if (motion.direction === motion.BACKWARDS) { + bobPhaseLast +=90; + } bobOscLast = motion.curTransition.lastAnim.joints[0].bob * Math.sin(filter.degToRad(motion.walkWheelPos + bobPhaseLast)); bobOscLast = filter.clipTrough(bobOscLast, motion.curTransition.lastAnim.joints[0].bob , 2); @@ -804,15 +850,18 @@ function animateAvatar(deltaTime, speed) { motion.curTransition.lastAnim.joints[0].thrustPhase)) + motion.curTransition.lastAnim.joints[0].thrustOffset; - } // end if walking at start of transition - else { + // end if walking at start of transition + + } else { swayOsc = motion.curAnim.joints[0].sway * Math.sin(filter.degToRad(cycle * adjFreq + motion.curAnim.joints[0].swayPhase)) + motion.curAnim.joints[0].swayOffset; var bobPhase = motion.curAnim.joints[0].bobPhase; - if (motion.direction === motion.BACKWARDS) bobPhase += 90; + if (motion.direction === motion.BACKWARDS) { + bobPhase += 90; + } bobOsc = motion.curAnim.joints[0].bob * Math.sin(filter.degToRad(cycle * adjFreq * 2 + bobPhase)); if (state.currentState === state.WALKING || @@ -854,8 +903,9 @@ function animateAvatar(deltaTime, speed) { bobOsc = (transProgress * bobOsc) + ((1 - transProgress) * bobOscLast); thrustOsc = (transProgress * thrustOsc) + ((1 - transProgress) * thrustOscLast); - }// if current transition active - else { + // end if walking at start of transition + + } else { swayOsc = motion.curAnim.joints[0].sway * Math.sin(filter.degToRad(cycle * adjFreq + motion.curAnim.joints[0].swayPhase)) +