Skip to content

Commit 30c501a

Browse files
committed
fix(quaternion): flexible multiply method
1 parent 4b25fb8 commit 30c501a

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/quaternion.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function axisAngle(axis, angle) {
9393
return quaternion;
9494
}
9595

96-
function from(x, y, z, w) {
96+
function getQuat(x, y, z, w) {
9797
if (typeof x === 'number') {
9898
return [x, y, z, w];
9999
}
@@ -103,10 +103,14 @@ function from(x, y, z, w) {
103103
if (isAngle(y)) {
104104
return axisAngle(x, y);
105105
}
106-
if (x) {
107-
return look(x, y || UP);
106+
if (x && y) {
107+
return look(x, y);
108108
}
109-
return [0, 0, 0, 1];
109+
return undefined;
110+
}
111+
112+
function from(x, y, z, w) {
113+
return getQuat(x, y, z, w) || [0, 0, 0, 1];
110114
}
111115

112116
class AQuaternion {
@@ -133,6 +137,10 @@ class AQuaternion {
133137
}
134138

135139
multiply(other, y, z, w) {
140+
const o = getQuat(other, y, z, w);
141+
if (o) {
142+
return this.multiplyQuaternion(new this.constructor(o));
143+
}
136144
if (typeof other.w === 'number') {
137145
return this.multiplyQuaternion(other);
138146
}
@@ -159,8 +167,8 @@ class AQuaternion {
159167
return new this.constructor(x, y, z, w);
160168
}
161169

162-
mul(other) {
163-
return this.multiply(other);
170+
mul(other, y, z, w) {
171+
return this.multiply(other, y, z, w);
164172
}
165173

166174
get inverse() {
@@ -268,7 +276,6 @@ class AQuaternion {
268276
}
269277
}
270278

271-
272279
export class Quaternion extends AQuaternion {
273280
/**
274281
* @param {number | Quaternion | IQuaternion | Vector | Victor | [number, number, number, number] } [x]
@@ -433,22 +440,15 @@ const LEFT90 = new IQuaternion(LEFT, degree(90));
433440
* @param {number} orientation
434441
* @returns {IQuaternion}
435442
*/
436-
export function fromOrientation(orientationEvent, orientation) {
437-
const { alpha, beta, gamma } = orientationEvent;
438-
const x = iquaternion(RIGHT, degree(beta));
439-
const y = iquaternion(UP, degree(alpha));
440-
const z = iquaternion(FORWARD, degree(gamma));
441-
442-
let rot = y;
443-
rot = rot.multiply(x);
444-
rot = rot.multiply(z);
445-
rot = rot.multiply(LEFT90);
446-
447-
if (orientation) {
448-
const { dir } = rot;
449-
const local = new IQuaternion(dir, degree(orientation));
450-
rot = local.multiplyQuaternion(rot);
451-
}
443+
export function fromOrientation({ alpha, beta, gamma }, orientation) {
444+
let rot = iquaternion(UP, degree(alpha))
445+
.mul(RIGHT, degree(beta))
446+
.mul(FORWARD, degree(gamma))
447+
.mul(LEFT90);
448+
449+
rot = iquaternion(rot.dir, degree(orientation))
450+
.mul(rot);
451+
452452
return rot;
453453
}
454454

0 commit comments

Comments
 (0)