Skip to content

Commit 34c6810

Browse files
committed
Add arbitrary deviceorientation.alpha bug fix for correct orientation calibration on iOS-based devices
1 parent 75f4dfa commit 34c6810

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

examples/js/controls/DeviceOrientationControls.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,30 @@ THREE.DeviceOrientationControls = function ( object ) {
3131

3232
this.update = function() {
3333

34+
var compassHeading, fixedAlpha;
3435
var alpha, beta, gamma;
3536

3637
return function () {
3738

3839
if ( this.freeze ) return;
3940

40-
alpha = this.deviceOrientation.gamma ? THREE.Math.degToRad( this.deviceOrientation.alpha ) : 0; // Z
41+
//
42+
// iOS compass-calibrated 'alpha' fix
43+
// see: http://lists.w3.org/Archives/Public/public-geolocation/2011Jul/0014.html
44+
//
45+
compassHeading = this.deviceOrientation.webkitCompassHeading || this.deviceOrientation.compassHeading;
46+
if ( compassHeading ) {
47+
48+
// Switch compassHeading back to 'alpha' representation
49+
fixedAlpha = 360 - compassHeading;
50+
51+
} else {
52+
53+
fixedAlpha = this.deviceOrientation.alpha || 0;
54+
55+
}
56+
57+
alpha = THREE.Math.degToRad( fixedAlpha ); // Z
4158
beta = this.deviceOrientation.beta ? THREE.Math.degToRad( this.deviceOrientation.beta ) : 0; // X'
4259
gamma = this.deviceOrientation.gamma ? THREE.Math.degToRad( this.deviceOrientation.gamma ) : 0; // Y''
4360
orient = this.screenOrientation ? THREE.Math.degToRad( this.screenOrientation ) : 0; // O

0 commit comments

Comments
 (0)