-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Vector2.js
787 lines (697 loc) · 18.4 KB
/
Vector2.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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 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 FuzzyEqual = require('../math/fuzzy/Equal');
/**
* @classdesc
* A representation of a vector in 2D space.
*
* A two-component vector.
*
* @class Vector2
* @memberof Phaser.Math
* @constructor
* @since 3.0.0
*
* @param {number|Phaser.Types.Math.Vector2Like} [x=0] - The x component, or an object with `x` and `y` properties.
* @param {number} [y=x] - The y component.
*/
var Vector2 = new Class({
initialize:
function Vector2 (x, y)
{
/**
* The x component of this Vector.
*
* @name Phaser.Math.Vector2#x
* @type {number}
* @default 0
* @since 3.0.0
*/
this.x = 0;
/**
* The y component of this Vector.
*
* @name Phaser.Math.Vector2#y
* @type {number}
* @default 0
* @since 3.0.0
*/
this.y = 0;
if (typeof x === 'object')
{
this.x = x.x || 0;
this.y = x.y || 0;
}
else
{
if (y === undefined) { y = x; }
this.x = x || 0;
this.y = y || 0;
}
},
/**
* Make a clone of this Vector2.
*
* @method Phaser.Math.Vector2#clone
* @since 3.0.0
*
* @return {Phaser.Math.Vector2} A clone of this Vector2.
*/
clone: function ()
{
return new Vector2(this.x, this.y);
},
/**
* Copy the components of a given Vector into this Vector.
*
* @method Phaser.Math.Vector2#copy
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector to copy the components from.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
copy: function (src)
{
this.x = src.x || 0;
this.y = src.y || 0;
return this;
},
/**
* Set the component values of this Vector from a given Vector2Like object.
*
* @method Phaser.Math.Vector2#setFromObject
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} obj - The object containing the component values to set for this Vector.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
setFromObject: function (obj)
{
this.x = obj.x || 0;
this.y = obj.y || 0;
return this;
},
/**
* Set the `x` and `y` components of the this Vector to the given `x` and `y` values.
*
* @method Phaser.Math.Vector2#set
* @since 3.0.0
*
* @param {number} x - The x value to set for this Vector.
* @param {number} [y=x] - The y value to set for this Vector.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
set: function (x, y)
{
if (y === undefined) { y = x; }
this.x = x;
this.y = y;
return this;
},
/**
* This method is an alias for `Vector2.set`.
*
* @method Phaser.Math.Vector2#setTo
* @since 3.4.0
*
* @param {number} x - The x value to set for this Vector.
* @param {number} [y=x] - The y value to set for this Vector.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
setTo: function (x, y)
{
return this.set(x, y);
},
/**
* Sets the `x` and `y` values of this object from a given polar coordinate.
*
* @method Phaser.Math.Vector2#setToPolar
* @since 3.0.0
*
* @param {number} azimuth - The angular coordinate, in radians.
* @param {number} [radius=1] - The radial coordinate (length).
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
setToPolar: function (azimuth, radius)
{
if (radius == null) { radius = 1; }
this.x = Math.cos(azimuth) * radius;
this.y = Math.sin(azimuth) * radius;
return this;
},
/**
* Check whether this Vector is equal to a given Vector.
*
* Performs a strict equality check against each Vector's components.
*
* @method Phaser.Math.Vector2#equals
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} v - The vector to compare with this Vector.
*
* @return {boolean} Whether the given Vector is equal to this Vector.
*/
equals: function (v)
{
return ((this.x === v.x) && (this.y === v.y));
},
/**
* Check whether this Vector is approximately equal to a given Vector.
*
* @method Phaser.Math.Vector2#fuzzyEquals
* @since 3.23.0
*
* @param {Phaser.Types.Math.Vector2Like} v - The vector to compare with this Vector.
* @param {number} [epsilon=0.0001] - The tolerance value.
*
* @return {boolean} Whether both absolute differences of the x and y components are smaller than `epsilon`.
*/
fuzzyEquals: function (v, epsilon)
{
return (FuzzyEqual(this.x, v.x, epsilon) && FuzzyEqual(this.y, v.y, epsilon));
},
/**
* Calculate the angle between this Vector and the positive x-axis, in radians.
*
* @method Phaser.Math.Vector2#angle
* @since 3.0.0
*
* @return {number} The angle between this Vector, and the positive x-axis, given in radians.
*/
angle: function ()
{
// computes the angle in radians with respect to the positive x-axis
var angle = Math.atan2(this.y, this.x);
if (angle < 0)
{
angle += 2 * Math.PI;
}
return angle;
},
/**
* Set the angle of this Vector.
*
* @method Phaser.Math.Vector2#setAngle
* @since 3.23.0
*
* @param {number} angle - The angle, in radians.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
setAngle: function (angle)
{
return this.setToPolar(angle, this.length());
},
/**
* Add a given Vector to this Vector. Addition is component-wise.
*
* @method Phaser.Math.Vector2#add
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector to add to this Vector.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
add: function (src)
{
this.x += src.x;
this.y += src.y;
return this;
},
/**
* Subtract the given Vector from this Vector. Subtraction is component-wise.
*
* @method Phaser.Math.Vector2#subtract
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector to subtract from this Vector.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
subtract: function (src)
{
this.x -= src.x;
this.y -= src.y;
return this;
},
/**
* Perform a component-wise multiplication between this Vector and the given Vector.
*
* Multiplies this Vector by the given Vector.
*
* @method Phaser.Math.Vector2#multiply
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector to multiply this Vector by.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
multiply: function (src)
{
this.x *= src.x;
this.y *= src.y;
return this;
},
/**
* Scale this Vector by the given value.
*
* @method Phaser.Math.Vector2#scale
* @since 3.0.0
*
* @param {number} value - The value to scale this Vector by.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
scale: function (value)
{
if (isFinite(value))
{
this.x *= value;
this.y *= value;
}
else
{
this.x = 0;
this.y = 0;
}
return this;
},
/**
* Perform a component-wise division between this Vector and the given Vector.
*
* Divides this Vector by the given Vector.
*
* @method Phaser.Math.Vector2#divide
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector to divide this Vector by.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
divide: function (src)
{
this.x /= src.x;
this.y /= src.y;
return this;
},
/**
* Negate the `x` and `y` components of this Vector.
*
* @method Phaser.Math.Vector2#negate
* @since 3.0.0
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
negate: function ()
{
this.x = -this.x;
this.y = -this.y;
return this;
},
/**
* Calculate the distance between this Vector and the given Vector.
*
* @method Phaser.Math.Vector2#distance
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector to calculate the distance to.
*
* @return {number} The distance from this Vector to the given Vector.
*/
distance: function (src)
{
var dx = src.x - this.x;
var dy = src.y - this.y;
return Math.sqrt(dx * dx + dy * dy);
},
/**
* Calculate the distance between this Vector and the given Vector, squared.
*
* @method Phaser.Math.Vector2#distanceSq
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector to calculate the distance to.
*
* @return {number} The distance from this Vector to the given Vector, squared.
*/
distanceSq: function (src)
{
var dx = src.x - this.x;
var dy = src.y - this.y;
return dx * dx + dy * dy;
},
/**
* Calculate the length (or magnitude) of this Vector.
*
* @method Phaser.Math.Vector2#length
* @since 3.0.0
*
* @return {number} The length of this Vector.
*/
length: function ()
{
var x = this.x;
var y = this.y;
return Math.sqrt(x * x + y * y);
},
/**
* Set the length (or magnitude) of this Vector.
*
* @method Phaser.Math.Vector2#setLength
* @since 3.23.0
*
* @param {number} length
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
setLength: function (length)
{
return this.normalize().scale(length);
},
/**
* Calculate the length of this Vector squared.
*
* @method Phaser.Math.Vector2#lengthSq
* @since 3.0.0
*
* @return {number} The length of this Vector, squared.
*/
lengthSq: function ()
{
var x = this.x;
var y = this.y;
return x * x + y * y;
},
/**
* Normalize this Vector.
*
* Makes the vector a unit length vector (magnitude of 1) in the same direction.
*
* @method Phaser.Math.Vector2#normalize
* @since 3.0.0
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
normalize: function ()
{
var x = this.x;
var y = this.y;
var len = x * x + y * y;
if (len > 0)
{
len = 1 / Math.sqrt(len);
this.x = x * len;
this.y = y * len;
}
return this;
},
/**
* Rotate this Vector to its perpendicular, in the positive direction.
*
* @method Phaser.Math.Vector2#normalizeRightHand
* @since 3.0.0
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
normalizeRightHand: function ()
{
var x = this.x;
this.x = this.y * -1;
this.y = x;
return this;
},
/**
* Rotate this Vector to its perpendicular, in the negative direction.
*
* @method Phaser.Math.Vector2#normalizeLeftHand
* @since 3.23.0
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
normalizeLeftHand: function ()
{
var x = this.x;
this.x = this.y;
this.y = x * -1;
return this;
},
/**
* Calculate the dot product of this Vector and the given Vector.
*
* @method Phaser.Math.Vector2#dot
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector2 to dot product with this Vector2.
*
* @return {number} The dot product of this Vector and the given Vector.
*/
dot: function (src)
{
return this.x * src.x + this.y * src.y;
},
/**
* Calculate the cross product of this Vector and the given Vector.
*
* @method Phaser.Math.Vector2#cross
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector2 to cross with this Vector2.
*
* @return {number} The cross product of this Vector and the given Vector.
*/
cross: function (src)
{
return this.x * src.y - this.y * src.x;
},
/**
* Linearly interpolate between this Vector and the given Vector.
*
* Interpolates this Vector towards the given Vector.
*
* @method Phaser.Math.Vector2#lerp
* @since 3.0.0
*
* @param {Phaser.Types.Math.Vector2Like} src - The Vector2 to interpolate towards.
* @param {number} [t=0] - The interpolation percentage, between 0 and 1.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
lerp: function (src, t)
{
if (t === undefined) { t = 0; }
var ax = this.x;
var ay = this.y;
this.x = ax + t * (src.x - ax);
this.y = ay + t * (src.y - ay);
return this;
},
/**
* Transform this Vector with the given Matrix.
*
* @method Phaser.Math.Vector2#transformMat3
* @since 3.0.0
*
* @param {Phaser.Math.Matrix3} mat - The Matrix3 to transform this Vector2 with.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
transformMat3: function (mat)
{
var x = this.x;
var y = this.y;
var m = mat.val;
this.x = m[0] * x + m[3] * y + m[6];
this.y = m[1] * x + m[4] * y + m[7];
return this;
},
/**
* Transform this Vector with the given Matrix.
*
* @method Phaser.Math.Vector2#transformMat4
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} mat - The Matrix4 to transform this Vector2 with.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
transformMat4: function (mat)
{
var x = this.x;
var y = this.y;
var m = mat.val;
this.x = m[0] * x + m[4] * y + m[12];
this.y = m[1] * x + m[5] * y + m[13];
return this;
},
/**
* Make this Vector the zero vector (0, 0).
*
* @method Phaser.Math.Vector2#reset
* @since 3.0.0
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
reset: function ()
{
this.x = 0;
this.y = 0;
return this;
},
/**
* Limit the length (or magnitude) of this Vector.
*
* @method Phaser.Math.Vector2#limit
* @since 3.23.0
*
* @param {number} max - The maximum length.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
limit: function (max)
{
var len = this.length();
if (len && len > max)
{
this.scale(max / len);
}
return this;
},
/**
* Reflect this Vector off a line defined by a normal.
*
* @method Phaser.Math.Vector2#reflect
* @since 3.23.0
*
* @param {Phaser.Math.Vector2} normal - A vector perpendicular to the line.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
reflect: function (normal)
{
normal = normal.clone().normalize();
return this.subtract(normal.scale(2 * this.dot(normal)));
},
/**
* Reflect this Vector across another.
*
* @method Phaser.Math.Vector2#mirror
* @since 3.23.0
*
* @param {Phaser.Math.Vector2} axis - A vector to reflect across.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
mirror: function (axis)
{
return this.reflect(axis).negate();
},
/**
* Rotate this Vector by an angle amount.
*
* @method Phaser.Math.Vector2#rotate
* @since 3.23.0
*
* @param {number} delta - The angle to rotate by, in radians.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
rotate: function (delta)
{
var cos = Math.cos(delta);
var sin = Math.sin(delta);
return this.set(cos * this.x - sin * this.y, sin * this.x + cos * this.y);
},
/**
* Project this Vector onto another.
*
* @method Phaser.Math.Vector2#project
* @since 3.60.0
*
* @param {Phaser.Math.Vector2} src - The vector to project onto.
*
* @return {Phaser.Math.Vector2} This Vector2.
*/
project: function (src)
{
var scalar = this.dot(src) / src.dot(src);
return this.copy(src).scale(scalar);
}
});
/**
* A static zero Vector2 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector2.ZERO
* @type {Phaser.Math.Vector2}
* @since 3.1.0
*/
Vector2.ZERO = new Vector2();
/**
* A static right Vector2 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector2.RIGHT
* @type {Phaser.Math.Vector2}
* @since 3.16.0
*/
Vector2.RIGHT = new Vector2(1, 0);
/**
* A static left Vector2 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector2.LEFT
* @type {Phaser.Math.Vector2}
* @since 3.16.0
*/
Vector2.LEFT = new Vector2(-1, 0);
/**
* A static up Vector2 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector2.UP
* @type {Phaser.Math.Vector2}
* @since 3.16.0
*/
Vector2.UP = new Vector2(0, -1);
/**
* A static down Vector2 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector2.DOWN
* @type {Phaser.Math.Vector2}
* @since 3.16.0
*/
Vector2.DOWN = new Vector2(0, 1);
/**
* A static one Vector2 for use by reference.
*
* This constant is meant for comparison operations and should not be modified directly.
*
* @constant
* @name Phaser.Math.Vector2.ONE
* @type {Phaser.Math.Vector2}
* @since 3.16.0
*/
Vector2.ONE = new Vector2(1, 1);
module.exports = Vector2;