forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuaternion.js
More file actions
766 lines (656 loc) · 18.1 KB
/
Copy pathQuaternion.js
File metadata and controls
766 lines (656 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
// Adapted from [gl-matrix](https://github.com/toji/gl-matrix) by toji
// and [vecmath](https://github.com/mattdesl/vecmath) by mattdesl
var Class = require('../utils/Class');
var Vector3 = require('./Vector3');
var Matrix3 = require('./Matrix3');
var EPSILON = 0.000001;
// Some shared 'private' arrays
var siNext = new Int8Array([ 1, 2, 0 ]);
var tmp = new Float32Array([ 0, 0, 0 ]);
var xUnitVec3 = new Vector3(1, 0, 0);
var yUnitVec3 = new Vector3(0, 1, 0);
var tmpvec = new Vector3();
var tmpMat3 = new Matrix3();
/**
* @classdesc
* A quaternion.
*
* @class Quaternion
* @memberof Phaser.Math
* @constructor
* @since 3.0.0
*
* @param {number} [x] - The x component.
* @param {number} [y] - The y component.
* @param {number} [z] - The z component.
* @param {number} [w] - The w component.
*/
var Quaternion = new Class({
initialize:
function Quaternion (x, y, z, w)
{
/**
* The x component of this Quaternion.
*
* @name Phaser.Math.Quaternion#x
* @type {number}
* @default 0
* @since 3.0.0
*/
/**
* The y component of this Quaternion.
*
* @name Phaser.Math.Quaternion#y
* @type {number}
* @default 0
* @since 3.0.0
*/
/**
* The z component of this Quaternion.
*
* @name Phaser.Math.Quaternion#z
* @type {number}
* @default 0
* @since 3.0.0
*/
/**
* The w component of this Quaternion.
*
* @name Phaser.Math.Quaternion#w
* @type {number}
* @default 0
* @since 3.0.0
*/
if (typeof x === 'object')
{
this.x = x.x || 0;
this.y = x.y || 0;
this.z = x.z || 0;
this.w = x.w || 0;
}
else
{
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
this.w = w || 0;
}
},
/**
* Copy the components of a given Quaternion or Vector into this Quaternion.
*
* @method Phaser.Math.Quaternion#copy
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} src - The Quaternion or Vector to copy the components from.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
copy: function (src)
{
this.x = src.x;
this.y = src.y;
this.z = src.z;
this.w = src.w;
return this;
},
/**
* Set the components of this Quaternion.
*
* @method Phaser.Math.Quaternion#set
* @since 3.0.0
*
* @param {(number|object)} [x=0] - The x component, or an object containing x, y, z, and w components.
* @param {number} [y=0] - The y component.
* @param {number} [z=0] - The z component.
* @param {number} [w=0] - The w component.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
set: function (x, y, z, w)
{
if (typeof x === 'object')
{
this.x = x.x || 0;
this.y = x.y || 0;
this.z = x.z || 0;
this.w = x.w || 0;
}
else
{
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
this.w = w || 0;
}
return this;
},
/**
* Add a given Quaternion or Vector to this Quaternion. Addition is component-wise.
*
* @method Phaser.Math.Quaternion#add
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} v - The Quaternion or Vector to add to this Quaternion.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
add: function (v)
{
this.x += v.x;
this.y += v.y;
this.z += v.z;
this.w += v.w;
return this;
},
/**
* Subtract a given Quaternion or Vector from this Quaternion. Subtraction is component-wise.
*
* @method Phaser.Math.Quaternion#subtract
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} v - The Quaternion or Vector to subtract from this Quaternion.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
subtract: function (v)
{
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
this.w -= v.w;
return this;
},
/**
* Scale this Quaternion by the given value.
*
* @method Phaser.Math.Quaternion#scale
* @since 3.0.0
*
* @param {number} scale - The value to scale this Quaternion by.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
scale: function (scale)
{
this.x *= scale;
this.y *= scale;
this.z *= scale;
this.w *= scale;
return this;
},
/**
* Calculate the length of this Quaternion.
*
* @method Phaser.Math.Quaternion#length
* @since 3.0.0
*
* @return {number} The length of this Quaternion.
*/
length: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
var w = this.w;
return Math.sqrt(x * x + y * y + z * z + w * w);
},
/**
* Calculate the length of this Quaternion squared.
*
* @method Phaser.Math.Quaternion#lengthSq
* @since 3.0.0
*
* @return {number} The length of this Quaternion, squared.
*/
lengthSq: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
var w = this.w;
return x * x + y * y + z * z + w * w;
},
/**
* Normalize this Quaternion.
*
* @method Phaser.Math.Quaternion#normalize
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
normalize: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
var w = this.w;
var len = x * x + y * y + z * z + w * w;
if (len > 0)
{
len = 1 / Math.sqrt(len);
this.x = x * len;
this.y = y * len;
this.z = z * len;
this.w = w * len;
}
return this;
},
/**
* Calculate the dot product of this Quaternion and the given Quaternion or Vector.
*
* @method Phaser.Math.Quaternion#dot
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} v - The Quaternion or Vector to dot product with this Quaternion.
*
* @return {number} The dot product of this Quaternion and the given Quaternion or Vector.
*/
dot: function (v)
{
return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
},
/**
* Linearly interpolate this Quaternion towards the given Quaternion or Vector.
*
* @method Phaser.Math.Quaternion#lerp
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} v - The Quaternion or Vector to interpolate towards.
* @param {number} [t=0] - The percentage of interpolation.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
lerp: function (v, t)
{
if (t === undefined) { t = 0; }
var ax = this.x;
var ay = this.y;
var az = this.z;
var aw = this.w;
this.x = ax + t * (v.x - ax);
this.y = ay + t * (v.y - ay);
this.z = az + t * (v.z - az);
this.w = aw + t * (v.w - aw);
return this;
},
/**
* Rotates this Quaternion based on the two given vectors.
*
* @method Phaser.Math.Quaternion#rotationTo
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} a - The transform rotation vector.
* @param {Phaser.Math.Vector3} b - The target rotation vector.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
rotationTo: function (a, b)
{
var dot = a.x * b.x + a.y * b.y + a.z * b.z;
if (dot < -0.999999)
{
if (tmpvec.copy(xUnitVec3).cross(a).length() < EPSILON)
{
tmpvec.copy(yUnitVec3).cross(a);
}
tmpvec.normalize();
return this.setAxisAngle(tmpvec, Math.PI);
}
else if (dot > 0.999999)
{
this.x = 0;
this.y = 0;
this.z = 0;
this.w = 1;
return this;
}
else
{
tmpvec.copy(a).cross(b);
this.x = tmpvec.x;
this.y = tmpvec.y;
this.z = tmpvec.z;
this.w = 1 + dot;
return this.normalize();
}
},
/**
* Set the axes of this Quaternion.
*
* @method Phaser.Math.Quaternion#setAxes
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} view - The view axis.
* @param {Phaser.Math.Vector3} right - The right axis.
* @param {Phaser.Math.Vector3} up - The upwards axis.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
setAxes: function (view, right, up)
{
var m = tmpMat3.val;
m[0] = right.x;
m[3] = right.y;
m[6] = right.z;
m[1] = up.x;
m[4] = up.y;
m[7] = up.z;
m[2] = -view.x;
m[5] = -view.y;
m[8] = -view.z;
return this.fromMat3(tmpMat3).normalize();
},
/**
* Reset this Matrix to an identity (default) Quaternion.
*
* @method Phaser.Math.Quaternion#identity
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
identity: function ()
{
this.x = 0;
this.y = 0;
this.z = 0;
this.w = 1;
return this;
},
/**
* Set the axis angle of this Quaternion.
*
* @method Phaser.Math.Quaternion#setAxisAngle
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} axis - The axis.
* @param {number} rad - The angle in radians.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
setAxisAngle: function (axis, rad)
{
rad = rad * 0.5;
var s = Math.sin(rad);
this.x = s * axis.x;
this.y = s * axis.y;
this.z = s * axis.z;
this.w = Math.cos(rad);
return this;
},
/**
* Multiply this Quaternion by the given Quaternion or Vector.
*
* @method Phaser.Math.Quaternion#multiply
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} b - The Quaternion or Vector to multiply this Quaternion by.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
multiply: function (b)
{
var ax = this.x;
var ay = this.y;
var az = this.z;
var aw = this.w;
var bx = b.x;
var by = b.y;
var bz = b.z;
var bw = b.w;
this.x = ax * bw + aw * bx + ay * bz - az * by;
this.y = ay * bw + aw * by + az * bx - ax * bz;
this.z = az * bw + aw * bz + ax * by - ay * bx;
this.w = aw * bw - ax * bx - ay * by - az * bz;
return this;
},
/**
* Smoothly linearly interpolate this Quaternion towards the given Quaternion or Vector.
*
* @method Phaser.Math.Quaternion#slerp
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} b - The Quaternion or Vector to interpolate towards.
* @param {number} t - The percentage of interpolation.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
slerp: function (b, t)
{
// benchmarks: http://jsperf.com/quaternion-slerp-implementations
var ax = this.x;
var ay = this.y;
var az = this.z;
var aw = this.w;
var bx = b.x;
var by = b.y;
var bz = b.z;
var bw = b.w;
// calc cosine
var cosom = ax * bx + ay * by + az * bz + aw * bw;
// adjust signs (if necessary)
if (cosom < 0)
{
cosom = -cosom;
bx = - bx;
by = - by;
bz = - bz;
bw = - bw;
}
// "from" and "to" quaternions are very close
// ... so we can do a linear interpolation
var scale0 = 1 - t;
var scale1 = t;
// calculate coefficients
if ((1 - cosom) > EPSILON)
{
// standard case (slerp)
var omega = Math.acos(cosom);
var sinom = Math.sin(omega);
scale0 = Math.sin((1.0 - t) * omega) / sinom;
scale1 = Math.sin(t * omega) / sinom;
}
// calculate final values
this.x = scale0 * ax + scale1 * bx;
this.y = scale0 * ay + scale1 * by;
this.z = scale0 * az + scale1 * bz;
this.w = scale0 * aw + scale1 * bw;
return this;
},
/**
* Invert this Quaternion.
*
* @method Phaser.Math.Quaternion#invert
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
invert: function ()
{
var a0 = this.x;
var a1 = this.y;
var a2 = this.z;
var a3 = this.w;
var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
var invDot = (dot) ? 1 / dot : 0;
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
this.x = -a0 * invDot;
this.y = -a1 * invDot;
this.z = -a2 * invDot;
this.w = a3 * invDot;
return this;
},
/**
* Convert this Quaternion into its conjugate.
*
* Sets the x, y and z components.
*
* @method Phaser.Math.Quaternion#conjugate
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
conjugate: function ()
{
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
return this;
},
/**
* Rotate this Quaternion on the X axis.
*
* @method Phaser.Math.Quaternion#rotateX
* @since 3.0.0
*
* @param {number} rad - The rotation angle in radians.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
rotateX: function (rad)
{
rad *= 0.5;
var ax = this.x;
var ay = this.y;
var az = this.z;
var aw = this.w;
var bx = Math.sin(rad);
var bw = Math.cos(rad);
this.x = ax * bw + aw * bx;
this.y = ay * bw + az * bx;
this.z = az * bw - ay * bx;
this.w = aw * bw - ax * bx;
return this;
},
/**
* Rotate this Quaternion on the Y axis.
*
* @method Phaser.Math.Quaternion#rotateY
* @since 3.0.0
*
* @param {number} rad - The rotation angle in radians.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
rotateY: function (rad)
{
rad *= 0.5;
var ax = this.x;
var ay = this.y;
var az = this.z;
var aw = this.w;
var by = Math.sin(rad);
var bw = Math.cos(rad);
this.x = ax * bw - az * by;
this.y = ay * bw + aw * by;
this.z = az * bw + ax * by;
this.w = aw * bw - ay * by;
return this;
},
/**
* Rotate this Quaternion on the Z axis.
*
* @method Phaser.Math.Quaternion#rotateZ
* @since 3.0.0
*
* @param {number} rad - The rotation angle in radians.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
rotateZ: function (rad)
{
rad *= 0.5;
var ax = this.x;
var ay = this.y;
var az = this.z;
var aw = this.w;
var bz = Math.sin(rad);
var bw = Math.cos(rad);
this.x = ax * bw + ay * bz;
this.y = ay * bw - ax * bz;
this.z = az * bw + aw * bz;
this.w = aw * bw - az * bz;
return this;
},
/**
* Create a unit (or rotation) Quaternion from its x, y, and z components.
*
* Sets the w component.
*
* @method Phaser.Math.Quaternion#calculateW
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
calculateW: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
this.w = -Math.sqrt(1.0 - x * x - y * y - z * z);
return this;
},
/**
* Convert the given Matrix into this Quaternion.
*
* @method Phaser.Math.Quaternion#fromMat3
* @since 3.0.0
*
* @param {Phaser.Math.Matrix3} mat - The Matrix to convert from.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
fromMat3: function (mat)
{
// benchmarks:
// http://jsperf.com/typed-array-access-speed
// http://jsperf.com/conversion-of-3x3-matrix-to-quaternion
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
// article "Quaternion Calculus and Fast Animation".
var m = mat.val;
var fTrace = m[0] + m[4] + m[8];
var fRoot;
if (fTrace > 0)
{
// |w| > 1/2, may as well choose w > 1/2
fRoot = Math.sqrt(fTrace + 1.0); // 2w
this.w = 0.5 * fRoot;
fRoot = 0.5 / fRoot; // 1/(4w)
this.x = (m[7] - m[5]) * fRoot;
this.y = (m[2] - m[6]) * fRoot;
this.z = (m[3] - m[1]) * fRoot;
}
else
{
// |w| <= 1/2
var i = 0;
if (m[4] > m[0])
{
i = 1;
}
if (m[8] > m[i * 3 + i])
{
i = 2;
}
var j = siNext[i];
var k = siNext[j];
// This isn't quite as clean without array access
fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1);
tmp[i] = 0.5 * fRoot;
fRoot = 0.5 / fRoot;
tmp[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
tmp[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
this.x = tmp[0];
this.y = tmp[1];
this.z = tmp[2];
this.w = (m[k * 3 + j] - m[j * 3 + k]) * fRoot;
}
return this;
}
});
module.exports = Quaternion;