forked from BabylonJS/Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cannon.js
7049 lines (6136 loc) · 214 KB
/
cannon.js
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012 cannon.js Authors
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function () {
/**
* @page About
* cannon.js is a lightweight 3D physics engine for web applications. For more information and source code, go to the Github repository [schteppe/cannon.js](https://github.com/schteppe/cannon.js).
*/
/**
* @library cannon.js
* @version 0.4.3
* @brief A lightweight 3D physics engine for the web
*/
var CANNON = CANNON || {};
// Maintain compatibility with older browsers
if(!this.Int32Array){
this.Int32Array=Array;
this.Float32Array=Array;
}
/**
* @class CANNON.Mat3
* @brief A 3x3 matrix.
* @param array elements Array of nine elements. Optional.
* @author schteppe / http://github.com/schteppe
*/
CANNON.Mat3 = function(elements){
/**
* @property Array elements
* @memberof CANNON.Mat3
* @brief A vector of length 9, containing all matrix elements
* The values in the array are stored in the following order:
* | 0 1 2 |
* | 3 4 5 |
* | 6 7 8 |
*
*/
if(elements){
this.elements = elements;
} else {
this.elements = [0,0,0,0,0,0,0,0,0];
}
};
/**
* @method identity
* @memberof CANNON.Mat3
* @brief Sets the matrix to identity
* @todo Should perhaps be renamed to setIdentity() to be more clear.
* @todo Create another function that immediately creates an identity matrix eg. eye()
*/
CANNON.Mat3.prototype.identity = function(){
this.elements[0] = 1;
this.elements[1] = 0;
this.elements[2] = 0;
this.elements[3] = 0;
this.elements[4] = 1;
this.elements[5] = 0;
this.elements[6] = 0;
this.elements[7] = 0;
this.elements[8] = 1;
};
CANNON.Mat3.prototype.setZero = function(){
var e = this.elements;
e[0] = 0;
e[1] = 0;
e[2] = 0;
e[3] = 0;
e[4] = 0;
e[5] = 0;
e[6] = 0;
e[7] = 0;
e[8] = 0;
};
/**
* @method setTrace
* @memberof CANNON.Mat3
* @brief Sets the matrix diagonal elements from a Vec3
*/
CANNON.Mat3.prototype.setTrace = function(vec3){
var e = this.elements;
e[0] = vec3.x;
e[4] = vec3.y;
e[8] = vec3.z;
};
/**
* @method vmult
* @memberof CANNON.Mat3
* @brief Matrix-Vector multiplication
* @param CANNON.Vec3 v The vector to multiply with
* @param CANNON.Vec3 target Optional, target to save the result in.
*/
CANNON.Mat3.prototype.vmult = function(v,target){
target = target || new CANNON.Vec3();
var e = this.elements,
x = v.x,
y = v.y,
z = v.z;
target.x = e[0]*x + e[1]*y + e[2]*z;
target.y = e[3]*x + e[4]*y + e[5]*z;
target.z = e[6]*x + e[7]*y + e[8]*z;
return target;
};
/**
* @method smult
* @memberof CANNON.Mat3
* @brief Matrix-scalar multiplication
* @param float s
*/
CANNON.Mat3.prototype.smult = function(s){
for(var i=0; i<this.elements.length; i++){
this.elements[i] *= s;
}
};
/**
* @method mmult
* @memberof CANNON.Mat3
* @brief Matrix multiplication
* @param CANNON.Mat3 m Matrix to multiply with from left side.
* @return CANNON.Mat3 The result.
*/
CANNON.Mat3.prototype.mmult = function(m){
var r = new CANNON.Mat3();
for(var i=0; i<3; i++){
for(var j=0; j<3; j++){
var sum = 0.0;
for(var k=0; k<3; k++){
sum += m.elements[i+k*3] * this.elements[k+j*3];
}
r.elements[i+j*3] = sum;
}
}
return r;
};
/**
* @method solve
* @memberof CANNON.Mat3
* @brief Solve Ax=b
* @param CANNON.Vec3 b The right hand side
* @param CANNON.Vec3 target Optional. Target vector to save in.
* @return CANNON.Vec3 The solution x
* @todo should reuse arrays
*/
CANNON.Mat3.prototype.solve = function(b,target){
target = target || new CANNON.Vec3();
// Construct equations
var nr = 3; // num rows
var nc = 4; // num cols
var eqns = [];
for(var i=0; i<nr*nc; i++){
eqns.push(0);
}
var i,j;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
eqns[i+nc*j] = this.elements[i+3*j];
}
}
eqns[3+4*0] = b.x;
eqns[3+4*1] = b.y;
eqns[3+4*2] = b.z;
// Compute right upper triangular version of the matrix - Gauss elimination
var n = 3, k = n, np;
var kp = 4; // num rows
var p, els;
do {
i = k - n;
if (eqns[i+nc*i] === 0) {
// the pivot is null, swap lines
for (j = i + 1; j < k; j++) {
if (eqns[i+nc*j] !== 0) {
np = kp;
do { // do ligne( i ) = ligne( i ) + ligne( k )
p = kp - np;
eqns[p+nc*i] += eqns[p+nc*j];
} while (--np);
break;
}
}
}
if (eqns[i+nc*i] !== 0) {
for (j = i + 1; j < k; j++) {
var multiplier = eqns[i+nc*j] / eqns[i+nc*i];
np = kp;
do { // do ligne( k ) = ligne( k ) - multiplier * ligne( i )
p = kp - np;
eqns[p+nc*j] = p <= i ? 0 : eqns[p+nc*j] - eqns[p+nc*i] * multiplier ;
} while (--np);
}
}
} while (--n);
// Get the solution
target.z = eqns[2*nc+3] / eqns[2*nc+2];
target.y = (eqns[1*nc+3] - eqns[1*nc+2]*target.z) / eqns[1*nc+1];
target.x = (eqns[0*nc+3] - eqns[0*nc+2]*target.z - eqns[0*nc+1]*target.y) / eqns[0*nc+0];
if(isNaN(target.x) || isNaN(target.y) || isNaN(target.z) || target.x===Infinity || target.y===Infinity || target.z===Infinity){
throw "Could not solve equation! Got x=["+target.toString()+"], b=["+b.toString()+"], A=["+this.toString()+"]";
}
return target;
};
/**
* @method e
* @memberof CANNON.Mat3
* @brief Get an element in the matrix by index. Index starts at 0, not 1!!!
* @param int row
* @param int column
* @param float value Optional. If provided, the matrix element will be set to this value.
* @return float
*/
CANNON.Mat3.prototype.e = function( row , column ,value){
if(value===undefined){
return this.elements[column+3*row];
} else {
// Set value
this.elements[column+3*row] = value;
}
};
/**
* @method copy
* @memberof CANNON.Mat3
* @brief Copy the matrix
* @param CANNON.Mat3 target Optional. Target to save the copy in.
* @return CANNON.Mat3
*/
CANNON.Mat3.prototype.copy = function(target){
target = target || new CANNON.Mat3();
for(var i=0; i<this.elements.length; i++){
target.elements[i] = this.elements[i];
}
return target;
};
/**
* @method toString
* @memberof CANNON.Mat3
* @brief Returns a string representation of the matrix.
* @return string
*/
CANNON.Mat3.prototype.toString = function(){
var r = "";
var sep = ",";
for(var i=0; i<9; i++){
r += this.elements[i] + sep;
}
return r;
};
/**
* @method reverse
* @memberof CANNON.Mat3
* @brief reverse the matrix
* @param CANNON.Mat3 target Optional. Target matrix to save in.
* @return CANNON.Mat3 The solution x
*/
CANNON.Mat3.prototype.reverse = function(target){
target = target || new CANNON.Mat3();
// Construct equations
var nr = 3; // num rows
var nc = 6; // num cols
var eqns = [];
for(var i=0; i<nr*nc; i++){
eqns.push(0);
}
var i,j;
for(i=0; i<3; i++){
for(j=0; j<3; j++){
eqns[i+nc*j] = this.elements[i+3*j];
}
}
eqns[3+6*0] = 1;
eqns[3+6*1] = 0;
eqns[3+6*2] = 0;
eqns[4+6*0] = 0;
eqns[4+6*1] = 1;
eqns[4+6*2] = 0;
eqns[5+6*0] = 0;
eqns[5+6*1] = 0;
eqns[5+6*2] = 1;
// Compute right upper triangular version of the matrix - Gauss elimination
var n = 3, k = n, np;
var kp = nc; // num rows
var p;
do {
i = k - n;
if (eqns[i+nc*i] === 0) {
// the pivot is null, swap lines
for (j = i + 1; j < k; j++) {
if (eqns[i+nc*j] !== 0) {
np = kp;
do { // do line( i ) = line( i ) + line( k )
p = kp - np;
eqns[p+nc*i] += eqns[p+nc*j];
} while (--np);
break;
}
}
}
if (eqns[i+nc*i] !== 0) {
for (j = i + 1; j < k; j++) {
var multiplier = eqns[i+nc*j] / eqns[i+nc*i];
np = kp;
do { // do line( k ) = line( k ) - multiplier * line( i )
p = kp - np;
eqns[p+nc*j] = p <= i ? 0 : eqns[p+nc*j] - eqns[p+nc*i] * multiplier ;
} while (--np);
}
}
} while (--n);
// eliminate the upper left triangle of the matrix
i = 2;
do {
j = i-1;
do {
var multiplier = eqns[i+nc*j] / eqns[i+nc*i];
np = nc;
do {
p = nc - np;
eqns[p+nc*j] = eqns[p+nc*j] - eqns[p+nc*i] * multiplier ;
} while (--np);
} while (j--);
} while (--i);
// operations on the diagonal
i = 2;
do {
var multiplier = 1 / eqns[i+nc*i];
np = nc;
do {
p = nc - np;
eqns[p+nc*i] = eqns[p+nc*i] * multiplier ;
} while (--np);
} while (i--);
i = 2;
do {
j = 2;
do {
p = eqns[nr+j+nc*i];
if( isNaN( p ) || p ===Infinity ){
throw "Could not reverse! A=["+this.toString()+"]";
}
target.e( i , j , p );
} while (j--);
} while (i--);
return target;
};
/**
* @class CANNON.Vec3
* @brief 3-dimensional vector
* @param float x
* @param float y
* @param float z
* @author schteppe
*/
var numVecs = 0;
CANNON.Vec3 = function(x,y,z){
/**
* @property float x
* @memberof CANNON.Vec3
*/
this.x = x||0.0;
/**
* @property float y
* @memberof CANNON.Vec3
*/
this.y = y||0.0;
/**
* @property float z
* @memberof CANNON.Vec3
*/
this.z = z||0.0;
/*
numVecs++;
if(numVecs > 180)
console.log(numVecs+" created");
*/
};
/**
* @method cross
* @memberof CANNON.Vec3
* @brief Vector cross product
* @param CANNON.Vec3 v
* @param CANNON.Vec3 target Optional. Target to save in.
* @return CANNON.Vec3
*/
CANNON.Vec3.prototype.cross = function(v,target){
var vx=v.x, vy=v.y, vz=v.z, x=this.x, y=this.y, z=this.z;
target = target || new CANNON.Vec3();
target.x = (y * vz) - (z * vy);
target.y = (z * vx) - (x * vz);
target.z = (x * vy) - (y * vx);
return target;
};
/**
* @method set
* @memberof CANNON.Vec3
* @brief Set the vectors' 3 elements
* @param float x
* @param float y
* @param float z
* @return CANNON.Vec3
*/
CANNON.Vec3.prototype.set = function(x,y,z){
this.x = x;
this.y = y;
this.z = z;
return this;
};
/**
* @method vadd
* @memberof CANNON.Vec3
* @brief Vector addition
* @param CANNON.Vec3 v
* @param CANNON.Vec3 target Optional.
* @return CANNON.Vec3
*/
CANNON.Vec3.prototype.vadd = function(v,target){
if(target){
target.x = v.x + this.x;
target.y = v.y + this.y;
target.z = v.z + this.z;
} else {
return new CANNON.Vec3(this.x + v.x,
this.y + v.y,
this.z + v.z);
}
};
/**
* @method vsub
* @memberof CANNON.Vec3
* @brief Vector subtraction
* @param CANNON.Vec3 v
* @param CANNON.Vec3 target Optional. Target to save in.
* @return CANNON.Vec3
*/
CANNON.Vec3.prototype.vsub = function(v,target){
if(target){
target.x = this.x - v.x;
target.y = this.y - v.y;
target.z = this.z - v.z;
} else {
return new CANNON.Vec3(this.x-v.x,
this.y-v.y,
this.z-v.z);
}
};
/**
* @method crossmat
* @memberof CANNON.Vec3
* @brief Get the cross product matrix a_cross from a vector, such that a x b = a_cross * b = c
* @see http://www8.cs.umu.se/kurser/TDBD24/VT06/lectures/Lecture6.pdf
* @return CANNON.Mat3
*/
CANNON.Vec3.prototype.crossmat = function(){
return new CANNON.Mat3([ 0, -this.z, this.y,
this.z, 0, -this.x,
-this.y, this.x, 0]);
};
/**
* @method normalize
* @memberof CANNON.Vec3
* @brief Normalize the vector. Note that this changes the values in the vector.
* @return float Returns the norm of the vector
*/
CANNON.Vec3.prototype.normalize = function(){
var x=this.x, y=this.y, z=this.z;
var n = Math.sqrt(x*x + y*y + z*z);
if(n>0.0){
var invN = 1/n;
this.x *= invN;
this.y *= invN;
this.z *= invN;
} else {
// Make something up
this.x = 0;
this.y = 0;
this.z = 0;
}
return n;
};
/**
* @method unit
* @memberof CANNON.Vec3
* @brief Get the version of this vector that is of length 1.
* @param CANNON.Vec3 target Optional target to save in
* @return CANNON.Vec3 Returns the unit vector
*/
CANNON.Vec3.prototype.unit = function(target){
target = target || new CANNON.Vec3();
var x=this.x, y=this.y, z=this.z;
var ninv = Math.sqrt(x*x + y*y + z*z);
if(ninv>0.0){
ninv = 1.0/ninv;
target.x = x * ninv;
target.y = y * ninv;
target.z = z * ninv;
} else {
target.x = 1;
target.y = 0;
target.z = 0;
}
return target;
};
/**
* @method norm
* @memberof CANNON.Vec3
* @brief Get the 2-norm (length) of the vector
* @return float
*/
CANNON.Vec3.prototype.norm = function(){
var x=this.x, y=this.y, z=this.z;
return Math.sqrt(x*x + y*y + z*z);
};
/**
* @method norm2
* @memberof CANNON.Vec3
* @brief Get the squared length of the vector
* @return float
*/
CANNON.Vec3.prototype.norm2 = function(){
return this.dot(this);
};
CANNON.Vec3.prototype.distanceTo = function(p){
var x=this.x, y=this.y, z=this.z;
var px=p.x, py=p.y, pz=p.z;
return Math.sqrt((px-x)*(px-x)+
(py-y)*(py-y)+
(pz-z)*(pz-z));
};
/**
* @method mult
* @memberof CANNON.Vec3
* @brief Multiply the vector with a scalar
* @param float scalar
* @param CANNON.Vec3 target
* @return CANNON.Vec3
*/
CANNON.Vec3.prototype.mult = function(scalar,target){
target = target || new CANNON.Vec3();
var x = this.x,
y = this.y,
z = this.z;
target.x = scalar * x;
target.y = scalar * y;
target.z = scalar * z;
return target;
};
/**
* @method dot
* @memberof CANNON.Vec3
* @brief Calculate dot product
* @param CANNON.Vec3 v
* @return float
*/
CANNON.Vec3.prototype.dot = function(v){
return this.x * v.x + this.y * v.y + this.z * v.z;
};
/**
* @method isZero
* @memberof CANNON.Vec3
* @return bool
*/
CANNON.Vec3.prototype.isZero = function(){
return this.x===0 && this.y===0 && this.z===0;
};
/**
* @method negate
* @memberof CANNON.Vec3
* @brief Make the vector point in the opposite direction.
* @param CANNON.Vec3 target Optional target to save in
* @return CANNON.Vec3
*/
CANNON.Vec3.prototype.negate = function(target){
target = target || new CANNON.Vec3();
target.x = -this.x;
target.y = -this.y;
target.z = -this.z;
return target;
};
/**
* @method tangents
* @memberof CANNON.Vec3
* @brief Compute two artificial tangents to the vector
* @param CANNON.Vec3 t1 Vector object to save the first tangent in
* @param CANNON.Vec3 t2 Vector object to save the second tangent in
*/
var Vec3_tangents_n = new CANNON.Vec3();
var Vec3_tangents_randVec = new CANNON.Vec3();
CANNON.Vec3.prototype.tangents = function(t1,t2){
var norm = this.norm();
if(norm>0.0){
var n = Vec3_tangents_n;
var inorm = 1/norm;
n.set(this.x*inorm,this.y*inorm,this.z*inorm);
var randVec = Vec3_tangents_randVec;
if(Math.abs(n.x) < 0.9){
randVec.set(1,0,0);
n.cross(randVec,t1);
} else {
randVec.set(0,1,0);
n.cross(randVec,t1);
}
n.cross(t1,t2);
} else {
// The normal length is zero, make something up
t1.set(1,0,0).normalize();
t2.set(0,1,0).normalize();
}
};
/**
* @method toString
* @memberof CANNON.Vec3
* @brief Converts to a more readable format
* @return string
*/
CANNON.Vec3.prototype.toString = function(){
return this.x+","+this.y+","+this.z;
};
/**
* @method copy
* @memberof CANNON.Vec3
* @brief Copy the vector.
* @param CANNON.Vec3 target
* @return CANNON.Vec3
*/
CANNON.Vec3.prototype.copy = function(target){
target = target || new CANNON.Vec3();
target.x = this.x;
target.y = this.y;
target.z = this.z;
return target;
};
/**
* @method lerp
* @memberof CANNON.Vec3
* @brief Do a linear interpolation between two vectors
* @param CANNON.Vec3 v
* @param float t A number between 0 and 1. 0 will make this function return u, and 1 will make it return v. Numbers in between will generate a vector in between them.
* @param CANNON.Vec3 target
*/
CANNON.Vec3.prototype.lerp = function(v,t,target){
var x=this.x, y=this.y, z=this.z;
target.x = x + (v.x-x)*t;
target.y = y + (v.y-y)*t;
target.z = z + (v.z-z)*t;
};
/**
* @method almostEquals
* @memberof CANNON.Vec3
* @brief Check if a vector equals is almost equal to another one.
* @param CANNON.Vec3 v
* @param float precision
* @return bool
*/
CANNON.Vec3.prototype.almostEquals = function(v,precision){
if(precision===undefined){
precision = 1e-6;
}
if( Math.abs(this.x-v.x)>precision ||
Math.abs(this.y-v.y)>precision ||
Math.abs(this.z-v.z)>precision){
return false;
}
return true;
};
/**
* @method almostZero
* @brief Check if a vector is almost zero
* @param float precision
* @memberof CANNON.Vec3
*/
CANNON.Vec3.prototype.almostZero = function(precision){
if(precision===undefined){
precision = 1e-6;
}
if( Math.abs(this.x)>precision ||
Math.abs(this.y)>precision ||
Math.abs(this.z)>precision){
return false;
}
return true;
};
/**
* @class CANNON.Quaternion
* @brief A Quaternion describes a rotation in 3D space.
* @description The Quaternion is mathematically defined as Q = x*i + y*j + z*k + w, where (i,j,k) are imaginary basis vectors. (x,y,z) can be seen as a vector related to the axis of rotation, while the real multiplier, w, is related to the amount of rotation.
* @param float x Multiplier of the imaginary basis vector i.
* @param float y Multiplier of the imaginary basis vector j.
* @param float z Multiplier of the imaginary basis vector k.
* @param float w Multiplier of the real part.
* @see http://en.wikipedia.org/wiki/Quaternion
*/
CANNON.Quaternion = function(x,y,z,w){
/**
* @property float x
* @memberof CANNON.Quaternion
*/
this.x = x!==undefined ? x : 0;
/**
* @property float y
* @memberof CANNON.Quaternion
*/
this.y = y!==undefined ? y : 0;
/**
* @property float z
* @memberof CANNON.Quaternion
*/
this.z = z!==undefined ? z : 0;
/**
* @property float w
* @memberof CANNON.Quaternion
* @brief The multiplier of the real quaternion basis vector.
*/
this.w = w!==undefined ? w : 1;
};
/**
* @method set
* @memberof CANNON.Quaternion
* @brief Set the value of the quaternion.
* @param float x
* @param float y
* @param float z
* @param float w
*/
CANNON.Quaternion.prototype.set = function(x,y,z,w){
this.x = x;
this.y = y;
this.z = z;
this.w = w;
};
/**
* @method toString
* @memberof CANNON.Quaternion
* @brief Convert to a readable format
* @return string
*/
CANNON.Quaternion.prototype.toString = function(){
return this.x+","+this.y+","+this.z+","+this.w;
};
/**
* @method setFromAxisAngle
* @memberof CANNON.Quaternion
* @brief Set the quaternion components given an axis and an angle.
* @param CANNON.Vec3 axis
* @param float angle in radians
*/
CANNON.Quaternion.prototype.setFromAxisAngle = function(axis,angle){
var s = Math.sin(angle*0.5);
this.x = axis.x * s;
this.y = axis.y * s;
this.z = axis.z * s;
this.w = Math.cos(angle*0.5);
};
// saves axis to targetAxis and returns
CANNON.Quaternion.prototype.toAxisAngle = function(targetAxis){
targetAxis = targetAxis || new CANNON.Vec3();
this.normalize(); // if w>1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
var angle = 2 * Math.acos(this.w);
var s = Math.sqrt(1-this.w*this.w); // assuming quaternion normalised then w is less than 1, so term always positive.
if (s < 0.001) { // test to avoid divide by zero, s is always positive due to sqrt
// if s close to zero then direction of axis not important
targetAxis.x = this.x; // if it is important that axis is normalised then replace with x=1; y=z=0;
targetAxis.y = this.y;
targetAxis.z = this.z;
} else {
targetAxis.x = this.x / s; // normalise axis
targetAxis.y = this.y / s;
targetAxis.z = this.z / s;
}
return [targetAxis,angle];
};
/**
* @method setFromVectors
* @memberof CANNON.Quaternion
* @brief Set the quaternion value given two vectors. The resulting rotation will be the needed rotation to rotate u to v.
* @param CANNON.Vec3 u
* @param CANNON.Vec3 v
*/
CANNON.Quaternion.prototype.setFromVectors = function(u,v){
var a = u.cross(v);
this.x = a.x;
this.y = a.y;
this.z = a.z;
this.w = Math.sqrt(Math.pow(u.norm(),2) * Math.pow(v.norm(),2)) + u.dot(v);
this.normalize();
};
/**
* @method mult
* @memberof CANNON.Quaternion
* @brief Quaternion multiplication
* @param CANNON.Quaternion q
* @param CANNON.Quaternion target Optional.
* @return CANNON.Quaternion
*/
var Quaternion_mult_va = new CANNON.Vec3();
var Quaternion_mult_vb = new CANNON.Vec3();
var Quaternion_mult_vaxvb = new CANNON.Vec3();
CANNON.Quaternion.prototype.mult = function(q,target){
target = target || new CANNON.Quaternion();
var w = this.w,
va = Quaternion_mult_va,
vb = Quaternion_mult_vb,
vaxvb = Quaternion_mult_vaxvb;
va.set(this.x,this.y,this.z);
vb.set(q.x,q.y,q.z);
target.w = w*q.w - va.dot(vb);
va.cross(vb,vaxvb);
target.x = w * vb.x + q.w*va.x + vaxvb.x;
target.y = w * vb.y + q.w*va.y + vaxvb.y;
target.z = w * vb.z + q.w*va.z + vaxvb.z;
return target;
};
/**
* @method inverse
* @memberof CANNON.Quaternion
* @brief Get the inverse quaternion rotation.
* @param CANNON.Quaternion target
* @return CANNON.Quaternion
*/
CANNON.Quaternion.prototype.inverse = function(target){
var x = this.x, y = this.y, z = this.z, w = this.w;
target = target || new CANNON.Quaternion();
this.conjugate(target);
var inorm2 = 1/(x*x + y*y + z*z + w*w);
target.x *= inorm2;
target.y *= inorm2;
target.z *= inorm2;
target.w *= inorm2;
return target;
};
/**
* @method conjugate
* @memberof CANNON.Quaternion
* @brief Get the quaternion conjugate
* @param CANNON.Quaternion target
* @return CANNON.Quaternion
*/
CANNON.Quaternion.prototype.conjugate = function(target){
target = target || new CANNON.Quaternion();
target.x = -this.x;
target.y = -this.y;
target.z = -this.z;
target.w = this.w;
return target;
};
/**
* @method normalize
* @memberof CANNON.Quaternion
* @brief Normalize the quaternion. Note that this changes the values of the quaternion.
*/
CANNON.Quaternion.prototype.normalize = function(){
var l = Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w);
if ( l === 0 ) {
this.x = 0;
this.y = 0;
this.z = 0;
this.w = 0;
} else {
l = 1 / l;
this.x *= l;
this.y *= l;
this.z *= l;
this.w *= l;
}
};
/**
* @method normalizeFast
* @memberof CANNON.Quaternion
* @brief Approximation of quaternion normalization. Works best when quat is already almost-normalized.
* @see http://jsperf.com/fast-quaternion-normalization
* @author unphased, https://github.com/unphased
*/
CANNON.Quaternion.prototype.normalizeFast = function () {
var f = (3.0-(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w))/2.0;
if ( f === 0 ) {
this.x = 0;
this.y = 0;
this.z = 0;
this.w = 0;
} else {
this.x *= f;
this.y *= f;
this.z *= f;
this.w *= f;
}
};
/**
* @method vmult
* @memberof CANNON.Quaternion
* @brief Multiply the quaternion by a vector
* @param CANNON.Vec3 v
* @param CANNON.Vec3 target Optional
* @return CANNON.Vec3
*/
CANNON.Quaternion.prototype.vmult = function(v,target){
target = target || new CANNON.Vec3();
if(this.w===0.0){
target.x = v.x;
target.y = v.y;
target.z = v.z;
} else {
var x = v.x,
y = v.y,
z = v.z;
var qx = this.x,