From 138af7a4919661814184dc6ec4fb6c4070e3e1da Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 25 Mar 2019 15:15:45 -0400 Subject: [PATCH] Stabilize facing up and facing down --- src/extensions/scratch3_gdx_for/index.js | 39 ++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/extensions/scratch3_gdx_for/index.js b/src/extensions/scratch3_gdx_for/index.js index 9d5f77619..e201a7907 100644 --- a/src/extensions/scratch3_gdx_for/index.js +++ b/src/extensions/scratch3_gdx_for/index.js @@ -80,6 +80,12 @@ const SHAKEN_THRESHOLD = 30; */ const FACING_THRESHOLD = 9; +/** + * An offset for the facing threshold, used to check that we are no longer facing up. + * @type {number} + */ +const FACING_THRESHOLD_OFFSET = 5; + /** * Threshold for acceleration magnitude, below which we are in freefall. * @type {number} @@ -783,15 +789,44 @@ class Scratch3GdxForBlocks { case GestureValues.STARTED_FALLING: return this.isFreeFalling(); case GestureValues.TURNED_FACE_UP: - return this._peripheral.getAccelerationZ() > FACING_THRESHOLD; + return this._isFacing(GestureValues.TURNED_FACE_UP); case GestureValues.TURNED_FACE_DOWN: - return this._peripheral.getAccelerationZ() < FACING_THRESHOLD * -1; + return this._isFacing(GestureValues.TURNED_FACE_DOWN); default: log.warn(`unknown gesture value in whenGesture: ${args.GESTURE}`); return false; } } + _isFacing (direction) { + if (typeof this._facingUp === 'undefined') { + this._facingUp = false; + } + if (typeof this._facingDown === 'undefined') { + this._facingDown = false; + } + + // If the sensor is already facing up or down, reduce the threshold. + // This prevents small fluctations in acceleration while it is being + // turned from causing the hat block to trigger multiple times. + let threshold = FACING_THRESHOLD; + if (this._facingUp || this._facingDown) { + threshold -= FACING_THRESHOLD_OFFSET; + } + + this._facingUp = this._peripheral.getAccelerationZ() > threshold; + this._facingDown = this._peripheral.getAccelerationZ() < threshold * -1; + + switch (direction) { + case GestureValues.TURNED_FACE_UP: + return this._facingUp; + case GestureValues.TURNED_FACE_DOWN: + return this._facingDown; + default: + return false; + } + } + whenTilted (args) { return this._isTilted(args.TILT); }