Skip to content

Commit

Permalink
fix(quaternion): flexible multiply method
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Oct 6, 2019
1 parent 4b25fb8 commit 30c501a
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/quaternion.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function axisAngle(axis, angle) {
return quaternion;
}

function from(x, y, z, w) {
function getQuat(x, y, z, w) {
if (typeof x === 'number') {
return [x, y, z, w];
}
Expand All @@ -103,10 +103,14 @@ function from(x, y, z, w) {
if (isAngle(y)) {
return axisAngle(x, y);
}
if (x) {
return look(x, y || UP);
if (x && y) {
return look(x, y);
}
return [0, 0, 0, 1];
return undefined;
}

function from(x, y, z, w) {
return getQuat(x, y, z, w) || [0, 0, 0, 1];
}

class AQuaternion {
Expand All @@ -133,6 +137,10 @@ class AQuaternion {
}

multiply(other, y, z, w) {
const o = getQuat(other, y, z, w);
if (o) {
return this.multiplyQuaternion(new this.constructor(o));
}
if (typeof other.w === 'number') {
return this.multiplyQuaternion(other);
}
Expand All @@ -159,8 +167,8 @@ class AQuaternion {
return new this.constructor(x, y, z, w);
}

mul(other) {
return this.multiply(other);
mul(other, y, z, w) {
return this.multiply(other, y, z, w);
}

get inverse() {
Expand Down Expand Up @@ -268,7 +276,6 @@ class AQuaternion {
}
}


export class Quaternion extends AQuaternion {
/**
* @param {number | Quaternion | IQuaternion | Vector | Victor | [number, number, number, number] } [x]
Expand Down Expand Up @@ -433,22 +440,15 @@ const LEFT90 = new IQuaternion(LEFT, degree(90));
* @param {number} orientation
* @returns {IQuaternion}
*/
export function fromOrientation(orientationEvent, orientation) {
const { alpha, beta, gamma } = orientationEvent;
const x = iquaternion(RIGHT, degree(beta));
const y = iquaternion(UP, degree(alpha));
const z = iquaternion(FORWARD, degree(gamma));

let rot = y;
rot = rot.multiply(x);
rot = rot.multiply(z);
rot = rot.multiply(LEFT90);

if (orientation) {
const { dir } = rot;
const local = new IQuaternion(dir, degree(orientation));
rot = local.multiplyQuaternion(rot);
}
export function fromOrientation({ alpha, beta, gamma }, orientation) {
let rot = iquaternion(UP, degree(alpha))
.mul(RIGHT, degree(beta))
.mul(FORWARD, degree(gamma))
.mul(LEFT90);

rot = iquaternion(rot.dir, degree(orientation))
.mul(rot);

return rot;
}

Expand Down

0 comments on commit 30c501a

Please sign in to comment.