Skip to content

Commit

Permalink
feat: simplified DeviceOrientation
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 22, 2020
1 parent 578efe5 commit 929156e
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/lib/controllers/DeviceOrientation.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import { IDisposable } from "../core/types";
import { Euler, EulerOrder } from "../math/Euler";
import { Quaternion } from "../math/Quaternion";
import { makeQuaternionFromAxisAngle, makeQuaternionFromEuler } from "../math/Quaternion.Functions";
import { degToRad } from "../math/Utilities";
import { Vector3 } from "../math/Vector3";
import { Controller } from "./Controller";

export class DeviceOrientationController extends Controller {
deviceOrientation = new Euler(0, 0, 0, EulerOrder.YXZ);
screenOrientation = 0;
onDispose: (() => void) | undefined = undefined;
const zAxis = new Vector3(0, 0, 1);
// - PI/2 around the x-axis
const q1 = new Quaternion(-Math.sqrt(0.5), 0, 0, Math.sqrt(0.5));

constructor(public fnCallback: (orientation: Quaternion) => void) {
super();
export class DeviceOrientation implements IDisposable {
disposed = false;
private deviceOrientation = new Euler(0, 0, 0, EulerOrder.YXZ);
private screenOrientation = 0;
private onDispose: () => void;
private tempValue = new Quaternion();

constructor() {
// this is required because when invoked on an event, "this" is set to "window"
const onDeviceOrientation = (event: DeviceOrientationEvent): void => {
this.deviceOrientation.set(degToRad(event.beta ?? 0), degToRad(event.alpha ?? 0), degToRad(event.gamma ?? 0));
this.deviceOrientation.set(
degToRad(event.beta ?? 0),
degToRad(event.alpha ?? 0 - 180.0),
-degToRad(event.gamma ?? 0),
EulerOrder.YXZ,
);
};
const onOrientationChange = (): void => {
console.log("orientation", window.orientation);
this.screenOrientation = -degToRad(window.orientation as number);
};

Expand Down Expand Up @@ -49,27 +59,20 @@ export class DeviceOrientationController extends Controller {
};
}

update(): void {
if (!this.enabled) {
return;
}

const zAxis = new Vector3(0, 0, 1);
const q1 = new Quaternion(-Math.sqrt(0.5), 0, 0, Math.sqrt(0.5)); // - PI/2 around the x-axis

get orientation(): Quaternion {
const result = makeQuaternionFromEuler(this.deviceOrientation);
result.multiply(q1); // camera looks out the back of the device, not the top
result.multiply(makeQuaternionFromAxisAngle(zAxis, this.screenOrientation)); // adjust for screen orientation
// camera looks out the back of the device, not the top
result.multiply(q1);
// adjust for screen orientation
result.multiply(makeQuaternionFromAxisAngle(zAxis, this.screenOrientation, this.tempValue));

this.fnCallback(result);
return result;
}

dispose(): void {
if (!this.disposed) {
if (this.onDispose !== undefined) {
this.onDispose();
}
super.dispose();
this.onDispose();
this.disposed = true;
}
}
}

0 comments on commit 929156e

Please sign in to comment.