From 73ec97eaae1975f96e2cb3e23eb9f35a36d79643 Mon Sep 17 00:00:00 2001 From: Diego Marcos Date: Tue, 23 Oct 2018 19:10:26 -0700 Subject: [PATCH] More forgiving stroke min speed --- src/components/saber-controls.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/components/saber-controls.js b/src/components/saber-controls.js index f4895e5..138bd56 100644 --- a/src/components/saber-controls.js +++ b/src/components/saber-controls.js @@ -6,7 +6,7 @@ AFRAME.registerComponent('saber-controls', { bladeEnabled: {default: false}, hand: {default: 'right', oneOf: ['left', 'right']}, isPaused: {default: false}, - strokeMinSpeed: {default: 250000}, + strokeMinSpeed: {default: 100000}, strokeMinAngle: {default: 5} }, @@ -54,7 +54,6 @@ AFRAME.registerComponent('saber-controls', { var distanceSamples = this.distanceSamples; var data = this.data; var directionChange; - var minSpeedFactor = this.swinging ? 1 : 1; var startSpeed; var strokeMinSpeed = this.swinging ? startSpeed : this.data.strokeMinSpeed; @@ -67,13 +66,7 @@ AFRAME.registerComponent('saber-controls', { bladeObject.localToWorld(this.bladeTipPosition); // Distance covered but the saber tip in one frame. - distance = this.bladeTipPosition.distanceTo(this.bladeTipPreviousPosition) * 1000000; - - // Calculate angle covered by the saber in one frame. - // Trig: Triangle formed by the laser and the linear distance covered by it's tip - // Arcsin((distanceCoveredByTip / 2.0) / Length of the blade) - this.strokeAngle += ((Math.asin(((distance / 1000000) / 2.0) / 0.9)) / - (2 * Math.PI)) * 360; + distance = this.bladeTipPreviousPosition.sub(this.bladeTipPosition).length() * 1000000; // Sample distance of the last 5 frames. if (this.distanceSamples.length === 5) { @@ -84,7 +77,12 @@ AFRAME.registerComponent('saber-controls', { // Filter out saber movements that are too slow. // Too slow or small angle is considered wrong hit. - if (this.accumulatedDistance > this.data.strokeMinSpeed * minSpeedFactor) { + if (this.accumulatedDistance > this.data.strokeMinSpeed && this.strokeAngle < 180) { + // Calculate angle covered by the saber in one frame. + // Trig: Triangle formed by the saber and the linear distance covered by its tip + // Arcsin((distanceCoveredByTip / 2.0) / Length of the blade) + this.strokeAngle += ((Math.asin(((distance / 1000000) / 2.0) / 0.9)) / + (2 * Math.PI)) * 360 * 2; // Saber has to move more than strokeMinAngle to consider a swing. // This filters out unintentional swings. if (!this.swinging && this.strokeAngle > data.strokeMinAngle) { @@ -94,13 +92,11 @@ AFRAME.registerComponent('saber-controls', { } else { // Stroke finishes. Reset swinging state. if (this.swinging) { - // console.log("Angle " + this.strokeAngle); + console.log("Angle " + this.strokeAngle); this.swinging = false; this.strokeAngle = 0; this.accumulatedDistance = 0; - for (let i = 0; i < this.distanceSamples.length; i++) { - this.distanceSamples[i] = 0; - } + for (let i = 0; i < this.distanceSamples.length; i++) { this.distanceSamples[i] = 0; } } }