mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-11 10:39:56 -05:00
Changes the tilt options from x/y to front/back and left/right. Also limits the tilt range to +/-90, just like micro::bit
This commit is contained in:
parent
5a2eb3b296
commit
7cbf4e2a60
1 changed files with 51 additions and 73 deletions
|
@ -275,29 +275,15 @@ class GdxFor {
|
||||||
return this._sensors.force;
|
return this._sensors.force;
|
||||||
}
|
}
|
||||||
|
|
||||||
getTiltX () {
|
getTiltFrontBack (back = false) {
|
||||||
let x = this.getAccelerationX();
|
const x = this.getAccelerationX();
|
||||||
let y = this.getAccelerationY();
|
const y = this.getAccelerationY();
|
||||||
let z = this.getAccelerationZ();
|
const z = this.getAccelerationZ();
|
||||||
|
|
||||||
let xSign = 1;
|
|
||||||
let ySign = 1;
|
|
||||||
let zSign = 1;
|
|
||||||
|
|
||||||
if (x < 0.0) {
|
|
||||||
x *= -1.0; xSign = -1;
|
|
||||||
}
|
|
||||||
if (y < 0.0) {
|
|
||||||
y *= -1.0; ySign = -1;
|
|
||||||
}
|
|
||||||
if (z < 0.0) {
|
|
||||||
z *= -1.0; zSign = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute the yz unit vector
|
// Compute the yz unit vector
|
||||||
|
const y2 = y * y;
|
||||||
const z2 = z * z;
|
const z2 = z * z;
|
||||||
const x2 = x * x;
|
let value = y2 + z2;
|
||||||
let value = z2 + x2;
|
|
||||||
value = Math.sqrt(value);
|
value = Math.sqrt(value);
|
||||||
|
|
||||||
// For sufficiently small zy vector values we are essentially at 90 degrees.
|
// For sufficiently small zy vector values we are essentially at 90 degrees.
|
||||||
|
@ -305,46 +291,28 @@ class GdxFor {
|
||||||
// The snap factor was derived through observation -- just enough to
|
// The snap factor was derived through observation -- just enough to
|
||||||
// still allow single degree steps up to 90 (..., 87, 88, 89, 90).
|
// still allow single degree steps up to 90 (..., 87, 88, 89, 90).
|
||||||
if (value < 0.35) {
|
if (value < 0.35) {
|
||||||
value = 90;
|
value = (x < 0) ? 90 : -90;
|
||||||
} else {
|
} else {
|
||||||
// Compute the x-axis angle
|
value = x / value;
|
||||||
value = y / value;
|
|
||||||
value = Math.atan(value);
|
value = Math.atan(value);
|
||||||
value *= 57.2957795; // convert from rad to deg
|
value = MathUtil.radToDeg(value) * -1;
|
||||||
}
|
}
|
||||||
// Manage the sign of the result
|
|
||||||
let xzSign = xSign;
|
// Back is the inverse of front
|
||||||
if (z > x) xzSign = zSign;
|
if (back) value *= -1;
|
||||||
if (xzSign === -1) value = 180.0 - value;
|
|
||||||
value *= ySign;
|
|
||||||
// Round the result to the nearest degree
|
|
||||||
value += 0.5;
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
getTiltY () {
|
getTiltLeftRight (right = false) {
|
||||||
let x = this.getAccelerationX();
|
const x = this.getAccelerationX();
|
||||||
let y = this.getAccelerationY();
|
const y = this.getAccelerationY();
|
||||||
let z = this.getAccelerationZ();
|
const z = this.getAccelerationZ();
|
||||||
|
|
||||||
let xSign = 1;
|
|
||||||
let ySign = 1;
|
|
||||||
let zSign = 1;
|
|
||||||
|
|
||||||
if (x < 0.0) {
|
|
||||||
x *= -1.0; xSign = -1;
|
|
||||||
}
|
|
||||||
if (y < 0.0) {
|
|
||||||
y *= -1.0; ySign = -1;
|
|
||||||
}
|
|
||||||
if (z < 0.0) {
|
|
||||||
z *= -1.0; zSign = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute the yz unit vector
|
// Compute the yz unit vector
|
||||||
|
const x2 = x * x;
|
||||||
const z2 = z * z;
|
const z2 = z * z;
|
||||||
const y2 = y * y;
|
let value = x2 + z2;
|
||||||
let value = z2 + y2;
|
|
||||||
value = Math.sqrt(value);
|
value = Math.sqrt(value);
|
||||||
|
|
||||||
// For sufficiently small zy vector values we are essentially at 90 degrees.
|
// For sufficiently small zy vector values we are essentially at 90 degrees.
|
||||||
|
@ -352,20 +320,16 @@ class GdxFor {
|
||||||
// The snap factor was derived through observation -- just enough to
|
// The snap factor was derived through observation -- just enough to
|
||||||
// still allow single degree steps up to 90 (..., 87, 88, 89, 90).
|
// still allow single degree steps up to 90 (..., 87, 88, 89, 90).
|
||||||
if (value < 0.35) {
|
if (value < 0.35) {
|
||||||
value = 90;
|
value = (y < 0) ? 90 : -90;
|
||||||
} else {
|
} else {
|
||||||
// Compute the x-axis angle
|
value = y / value;
|
||||||
value = x / value;
|
|
||||||
value = Math.atan(value);
|
value = Math.atan(value);
|
||||||
value *= 57.2957795; // convert from rad to deg
|
value = MathUtil.radToDeg(value) * -1;
|
||||||
}
|
}
|
||||||
// Manage the sign of the result
|
|
||||||
let yzSign = ySign;
|
// Right is the inverse of left
|
||||||
if (z > y) yzSign = zSign;
|
if (right) value *= -1;
|
||||||
if (yzSign === -1) value = 180.0 - value;
|
|
||||||
value *= xSign;
|
|
||||||
// Round the result to the nearest degree
|
|
||||||
value += 0.5;
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,8 +385,10 @@ const GestureValues = {
|
||||||
* @enum {string}
|
* @enum {string}
|
||||||
*/
|
*/
|
||||||
const TiltAxisValues = {
|
const TiltAxisValues = {
|
||||||
X: 'x',
|
FRONT: 'front',
|
||||||
Y: 'y'
|
BACK: 'back',
|
||||||
|
LEFT: 'left',
|
||||||
|
RIGHT: 'right'
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -485,12 +451,20 @@ class Scratch3GdxForBlocks {
|
||||||
get TILT_MENU () {
|
get TILT_MENU () {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
text: 'x',
|
text: 'front',
|
||||||
value: TiltAxisValues.X
|
value: TiltAxisValues.FRONT
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text: 'y',
|
text: 'back',
|
||||||
value: TiltAxisValues.Y
|
value: TiltAxisValues.BACK
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: 'left',
|
||||||
|
value: TiltAxisValues.LEFT
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: 'right',
|
||||||
|
value: TiltAxisValues.RIGHT
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -644,7 +618,7 @@ class Scratch3GdxForBlocks {
|
||||||
TILT: {
|
TILT: {
|
||||||
type: ArgumentType.STRING,
|
type: ArgumentType.STRING,
|
||||||
menu: 'tiltOptions',
|
menu: 'tiltOptions',
|
||||||
defaultValue: TiltAxisValues.X
|
defaultValue: TiltAxisValues.FRONT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -749,10 +723,14 @@ class Scratch3GdxForBlocks {
|
||||||
|
|
||||||
getTilt (args) {
|
getTilt (args) {
|
||||||
switch (args.TILT) {
|
switch (args.TILT) {
|
||||||
case TiltAxisValues.X:
|
case TiltAxisValues.FRONT:
|
||||||
return Math.round(this._peripheral.getTiltX());
|
return Math.round(this._peripheral.getTiltFrontBack(false));
|
||||||
case TiltAxisValues.Y:
|
case TiltAxisValues.BACK:
|
||||||
return Math.round(this._peripheral.getTiltY());
|
return Math.round(this._peripheral.getTiltFrontBack(true));
|
||||||
|
case TiltAxisValues.LEFT:
|
||||||
|
return Math.round(this._peripheral.getTiltLeftRight(false));
|
||||||
|
case TiltAxisValues.RIGHT:
|
||||||
|
return Math.round(this._peripheral.getTiltLeftRight(true));
|
||||||
default:
|
default:
|
||||||
log.warn(`Unknown direction in getTilt: ${args.TILT}`);
|
log.warn(`Unknown direction in getTilt: ${args.TILT}`);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue