Detect strokes for more precise beat detection

This commit is contained in:
Diego Marcos
2018-10-15 16:04:15 -07:00
parent 215c4ea4e9
commit e94ca0d8c6
2 changed files with 47 additions and 8 deletions

View File

@@ -428,7 +428,7 @@ AFRAME.registerComponent('beat', {
if (!this.hitColliderEl.getObject3D('mesh')) { return; }
const saberEls = this.saberEls;
const boundingBox = this.boundingBox.setFromObject(
const hitBoundingBox = this.beatBoundingBox.setFromObject(
this.hitColliderEl.getObject3D('mesh'));
const beatBoundingBox = this.beatBoundingBox.setFromObject(
this.blockEl.getObject3D('mesh'));
@@ -436,10 +436,14 @@ AFRAME.registerComponent('beat', {
for (let i = 0; i < saberEls.length; i++) {
let saberBoundingBox = saberEls[i].components['saber-controls'].boundingBox;
if (!boundingBox || !saberBoundingBox) { break; }
if (!hitBoundingBox || !saberBoundingBox) { break; }
if (saberBoundingBox.intersectsBox(boundingBox)) {
this.el.emit('beathit', null, true);
if (saberBoundingBox.intersectsBox(hitBoundingBox)) {
if (saberEls[i].components['saber-controls'].swinging) {
this.el.emit('beathit', null, true);
} else {
this.wrongHit(saberEls[i].getAttribute('saber-controls').hand);
}
this.el.parentNode.components['beat-hit-sound'].playSound(this.el);
this.destroyBeat(saberEls[i]);
break;
@@ -448,7 +452,7 @@ AFRAME.registerComponent('beat', {
if (saberBoundingBox.intersectsBox(beatBoundingBox)) {
this.el.parentNode.components['beat-hit-sound'].playSound(this.el);
this.destroyBeat(saberEls[i]);
if (this.data.type === 'dot') {
if (this.data.type === 'dot' && saberEls[i].components['saber-controls'].swinging) {
this.el.emit('beathit', null, true);
} else {
this.wrongHit(saberEls[i].getAttribute('saber-controls').hand);

View File

@@ -1,4 +1,4 @@
AFRAME.registerComponent('saber-controls', {
AFRAME.registerComponent('saber-controls', {
schema: {
bladeEnabled: {default: false},
hand: {default: 'right', oneOf: ['left', 'right']},
@@ -11,6 +11,10 @@ AFRAME.registerComponent('saber-controls', {
this.boundingBox = new THREE.Box3();
this.controllerType = '';
this.bladeEl = el.querySelector('.blade');
this.bladeTipPosition = new THREE.Vector3();
this.swinging = false;
this.strokeCount = 0;
el.addEventListener('controllerconnected', this.initSaber.bind(this));
@@ -28,9 +32,40 @@ AFRAME.registerComponent('saber-controls', {
}
},
tick: function () {
if (!this.data.bladeEnabled) { return; }
tick: function (time, delta) {
//if (!this.data.bladeEnabled) { return; }
this.boundingBox.setFromObject(this.bladeEl.getObject3D('mesh'));
this.detectStroke(delta);
},
detectStroke: function (delta) {
var bladeObject
var distance;
this.bladeTipPosition.set(0, 0.4, 0);
bladeObject = this.el.object3D;
bladeObject.parent.updateMatrixWorld();
bladeObject.localToWorld(this.bladeTipPosition);
if (!this.bladeTipPreviousPosition) {
this.bladeTipPreviousPosition = this.bladeTipPosition.clone();
return;
}
distance = this.bladeTipPosition.distanceTo(this.bladeTipPreviousPosition) * 1000000;
if (distance > 2000) {
if (!this.startSwinging) {
this.startSwinging = true;
this.swingDuration = 0;
}
if (this.swingDuration > 100) {
this.swinging = true;
} else {
this.swingDuration += delta;
}
} else {
this.swinging = false;
this.startSwinging = false;
}
this.bladeTipPreviousPosition = this.bladeTipPosition.clone();
},
initSaber: function (evt) {