Skip to content

Commit

Permalink
Merge pull request #6589 from SlightlyEpic/rad
Browse files Browse the repository at this point in the history
Fix rotation properties being in degrees irrespective of angleMode
  • Loading branch information
limzykenneth authored Jan 21, 2024
2 parents 326bdd1 + dc2f6b2 commit 2a766a5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
47 changes: 23 additions & 24 deletions src/events/acceleration.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import p5 from '../core/main';
import * as constants from '../core/constants';

/**
* The system variable deviceOrientation always contains the orientation of
Expand Down Expand Up @@ -619,14 +618,11 @@ p5.prototype.setShakeThreshold = function(val) {

p5.prototype._ondeviceorientation = function(e) {
this._updatePRotations();
if (this._angleMode === constants.radians) {
e.beta = e.beta * (_PI / 180.0);
e.gamma = e.gamma * (_PI / 180.0);
e.alpha = e.alpha * (_PI / 180.0);
}
this._setProperty('rotationX', e.beta);
this._setProperty('rotationY', e.gamma);
this._setProperty('rotationZ', e.alpha);

// Convert from degrees into current angle mode
this._setProperty('rotationX', this._fromDegrees(e.beta));
this._setProperty('rotationY', this._fromDegrees(e.gamma));
this._setProperty('rotationZ', this._fromDegrees(e.alpha));
this._handleMotion();
};
p5.prototype._ondevicemotion = function(e) {
Expand Down Expand Up @@ -656,12 +652,14 @@ p5.prototype._handleMotion = function() {
}

if (typeof context.deviceTurned === 'function') {
// The angles given by rotationX etc is from range -180 to 180.
// The following will convert them to 0 to 360 for ease of calculation
// The angles given by rotationX etc is from range [-180 to 180].
// The following will convert them to [0 to 360] for ease of calculation
// of cases when the angles wrapped around.
// _startAngleX will be converted back at the end and updated.
const wRX = this.rotationX + 180;
const wPRX = this.pRotationX + 180;

// Rotations are converted to degrees and all calculations are done in degrees
const wRX = this._toDegrees(this.rotationX) + 180;
const wPRX = this._toDegrees(this.pRotationX) + 180;
let wSAX = startAngleX + 180;
if ((wRX - wPRX > 0 && wRX - wPRX < 270) || wRX - wPRX < -270) {
rotateDirectionX = 'clockwise';
Expand All @@ -680,8 +678,8 @@ p5.prototype._handleMotion = function() {
startAngleX = wSAX - 180;

// Y-axis is identical to X-axis except for changing some names.
const wRY = this.rotationY + 180;
const wPRY = this.pRotationY + 180;
const wRY = this._toDegrees(this.rotationY) + 180;
const wPRY = this._toDegrees(this.pRotationY) + 180;
let wSAY = startAngleY + 180;
if ((wRY - wPRY > 0 && wRY - wPRY < 270) || wRY - wPRY < -270) {
rotateDirectionY = 'clockwise';
Expand All @@ -701,26 +699,27 @@ p5.prototype._handleMotion = function() {

// Z-axis is already in the range 0 to 360
// so no conversion is needed.
const rotZ = this._toDegrees(this.rotationZ);
const pRotZ = this._toDegrees(this.pRotationZ);
if (
(this.rotationZ - this.pRotationZ > 0 &&
this.rotationZ - this.pRotationZ < 270) ||
this.rotationZ - this.pRotationZ < -270
(rotZ - pRotZ > 0 && rotZ - pRotZ < 270) ||
rotZ - pRotZ < -270
) {
rotateDirectionZ = 'clockwise';
} else if (
this.rotationZ - this.pRotationZ < 0 ||
this.rotationZ - this.pRotationZ > 270
rotZ - pRotZ < 0 ||
rotZ - pRotZ > 270
) {
rotateDirectionZ = 'counter-clockwise';
}
if (rotateDirectionZ !== this.pRotateDirectionZ) {
startAngleZ = this.rotationZ;
startAngleZ = rotZ;
}
if (
Math.abs(this.rotationZ - startAngleZ) > 90 &&
Math.abs(this.rotationZ - startAngleZ) < 270
Math.abs(rotZ - startAngleZ) > 90 &&
Math.abs(rotZ - startAngleZ) < 270
) {
startAngleZ = this.rotationZ;
startAngleZ = rotZ;
this._setProperty('turnAxis', 'Z');
context.deviceTurned();
}
Expand Down
34 changes: 34 additions & 0 deletions src/math/trigonometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,25 @@ p5.prototype.angleMode = function(mode) {
if (typeof mode === 'undefined') {
return this._angleMode;
} else if (mode === constants.DEGREES || mode === constants.RADIANS) {
const prevMode = this._angleMode;

// No change
if(mode === prevMode) return;

// Otherwise adjust pRotation according to new mode
// This is necessary for acceleration events to work properly
if(mode === constants.RADIANS) {
// Change pRotation to radians
this._setProperty('pRotationX', this.pRotationX * constants.DEG_TO_RAD);
this._setProperty('pRotationY', this.pRotationY * constants.DEG_TO_RAD);
this._setProperty('pRotationZ', this.pRotationZ * constants.DEG_TO_RAD);
} else {
// Change pRotation to degrees
this._setProperty('pRotationX', this.pRotationX * constants.RAD_TO_DEG);
this._setProperty('pRotationY', this.pRotationY * constants.RAD_TO_DEG);
this._setProperty('pRotationZ', this.pRotationZ * constants.RAD_TO_DEG);
}

this._angleMode = mode;
}
};
Expand Down Expand Up @@ -456,4 +475,19 @@ p5.prototype._fromRadians = function(angle) {
return angle;
};

/**
* converts angles from DEGREES into the current angleMode
*
* @method _fromDegrees
* @private
* @param {Number} angle
* @returns {Number}
*/
p5.prototype._fromDegrees = function(angle) {
if (this._angleMode === constants.RADIANS) {
return angle * constants.DEG_TO_RAD;
}
return angle;
};

export default p5;
12 changes: 6 additions & 6 deletions test/unit/events/acceleration.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,33 @@ suite('Acceleration Events', function() {
suite('rotation', function() {
test('rotationX should be 45', function() {
window.dispatchEvent(deviceOrientationEvent1);
assert.strictEqual(myp5.rotationX, 45);
assert.strictEqual(myp5.rotationX, 45 * (Math.PI / 180.0));
});
test('rotationY should be 90', function() {
window.dispatchEvent(deviceOrientationEvent1);
assert.strictEqual(myp5.rotationY, 90);
assert.strictEqual(myp5.rotationY, 90 * (Math.PI / 180.0));
});
test('rotationZ should be 10', function() {
window.dispatchEvent(deviceOrientationEvent1);
assert.strictEqual(myp5.rotationZ, 10);
assert.strictEqual(myp5.rotationZ, 10 * (Math.PI / 180.0));
});
});

suite('previous rotation', function() {
test('pRotationX should be 45', function() {
window.dispatchEvent(deviceOrientationEvent1);
window.dispatchEvent(deviceOrientationEvent2);
assert.strictEqual(myp5.pRotationX, 45);
assert.strictEqual(myp5.pRotationX, 45 * (Math.PI / 180.0));
});
test('pRotationY should be 90', function() {
window.dispatchEvent(deviceOrientationEvent1);
window.dispatchEvent(deviceOrientationEvent2);
assert.strictEqual(myp5.pRotationY, 90);
assert.strictEqual(myp5.pRotationY, 90 * (Math.PI / 180.0));
});
test('pRotationZ should be 10', function() {
window.dispatchEvent(deviceOrientationEvent1);
window.dispatchEvent(deviceOrientationEvent2);
assert.strictEqual(myp5.pRotationZ, 10);
assert.strictEqual(myp5.pRotationZ, 10 * (Math.PI / 180.0));
});
});

Expand Down

0 comments on commit 2a766a5

Please sign in to comment.