88// and [vecmath](https://github.com/mattdesl/vecmath) by mattdesl
99
1010var Class = require ( '../utils/Class' ) ;
11+ var FuzzyEqual = require ( '../math/fuzzy/Equal' ) ;
1112
1213/**
1314 * @classdesc
@@ -187,6 +188,22 @@ var Vector2 = new Class({
187188 return ( ( this . x === v . x ) && ( this . y === v . y ) ) ;
188189 } ,
189190
191+ /**
192+ * Check whether this Vector is approximately equal to a given Vector.
193+ *
194+ * @method Phaser.Math.Vector2#fuzzyEquals
195+ * @since 3.23.0
196+ *
197+ * @param {Phaser.Math.Vector2 } v - The vector to compare with this Vector.
198+ * @param {number } [epsilon=0.0001] - The tolerance value.
199+ *
200+ * @return {boolean } Whether both absolute differences of the x and y components are smaller than `epsilon`.
201+ */
202+ fuzzyEquals : function ( v , epsilon )
203+ {
204+ return ( FuzzyEqual ( this . x , v . x , epsilon ) && FuzzyEqual ( this . y , v . y , epsilon ) ) ;
205+ } ,
206+
190207 /**
191208 * Calculate the angle between this Vector and the positive x-axis, in radians.
192209 *
@@ -209,6 +226,21 @@ var Vector2 = new Class({
209226 return angle ;
210227 } ,
211228
229+ /**
230+ * Set the angle of this Vector.
231+ *
232+ * @method Phaser.Math.Vector2#setAngle
233+ * @since 3.23.0
234+ *
235+ * @param {number } angle - The angle, in radians.
236+ *
237+ * @return {Phaser.Math.Vector2 } This Vector2.
238+ */
239+ setAngle : function ( angle )
240+ {
241+ return this . setToPolar ( angle , this . length ( ) ) ;
242+ } ,
243+
212244 /**
213245 * Add a given Vector to this Vector. Addition is component-wise.
214246 *
@@ -379,6 +411,21 @@ var Vector2 = new Class({
379411 return Math . sqrt ( x * x + y * y ) ;
380412 } ,
381413
414+ /**
415+ * Set the length (or magnitude) of this Vector.
416+ *
417+ * @method Phaser.Math.Vector2#setLength
418+ * @since 3.23.0
419+ *
420+ * @param {number } length
421+ *
422+ * @return {Phaser.Math.Vector2 } This Vector2.
423+ */
424+ setLength : function ( length )
425+ {
426+ return this . normalize ( ) . scale ( length ) ;
427+ } ,
428+
382429 /**
383430 * Calculate the length of this Vector squared.
384431 *
@@ -440,6 +487,24 @@ var Vector2 = new Class({
440487 return this ;
441488 } ,
442489
490+ /**
491+ * Rotate this Vector to its perpendicular, in the negative direction.
492+ *
493+ * @method Phaser.Math.Vector2#normalizeLeftHand
494+ * @since 3.23.0
495+ *
496+ * @return {Phaser.Math.Vector2 } This Vector2.
497+ */
498+ normalizeLeftHand : function ( )
499+ {
500+ var x = this . x ;
501+
502+ this . x = this . y ;
503+ this . y = x * - 1 ;
504+
505+ return this ;
506+ } ,
507+
443508 /**
444509 * Calculate the dot product of this Vector and the given Vector.
445510 *
@@ -554,6 +619,78 @@ var Vector2 = new Class({
554619 this . y = 0 ;
555620
556621 return this ;
622+ } ,
623+
624+ /**
625+ * Limit the length (or magnitude) of this Vector.
626+ *
627+ * @method Phaser.Math.Vector2#limit
628+ * @since 3.23.0
629+ *
630+ * @param {number } max - The maximum length.
631+ *
632+ * @return {Phaser.Math.Vector2 } This Vector2.
633+ */
634+ limit : function ( max )
635+ {
636+ var len = this . length ( ) ;
637+
638+ if ( len && len > max )
639+ {
640+ this . scale ( max / len ) ;
641+ }
642+
643+ return this ;
644+ } ,
645+
646+ /**
647+ * Reflect this Vector off a line defined by a normal.
648+ *
649+ * @method Phaser.Math.Vector2#reflect
650+ * @since 3.23.0
651+ *
652+ * @param {Phaser.Math.Vector2 } normal - A vector perpendicular to the line.
653+ *
654+ * @return {Phaser.Math.Vector2 } This Vector2.
655+ */
656+ reflect : function ( normal )
657+ {
658+ normal = normal . clone ( ) . normalize ( ) ;
659+
660+ return this . subtract ( normal . scale ( 2 * this . dot ( normal ) ) ) ;
661+ } ,
662+
663+ /**
664+ * Reflect this Vector across another.
665+ *
666+ * @method Phaser.Math.Vector2#mirror
667+ * @since 3.23.0
668+ *
669+ * @param {Phaser.Math.Vector2 } axis - A vector to reflect across.
670+ *
671+ * @return {Phaser.Math.Vector2 } This Vector2.
672+ */
673+ mirror : function ( axis )
674+ {
675+ return this . reflect ( axis ) . negate ( ) ;
676+ } ,
677+
678+ /**
679+ * Rotate this Vector by an angle amount.
680+ *
681+ * @method Phaser.Math.Vector2#rotate
682+ * @since 3.23.0
683+ *
684+ * @param {number } delta - The angle to rotate by, in radians.
685+ *
686+ * @return {Phaser.Math.Vector2 } This Vector2.
687+ */
688+ rotate : function ( delta )
689+ {
690+ var cos = Math . cos ( delta ) ;
691+ var sin = Math . sin ( delta ) ;
692+
693+ return this . set ( cos * this . x - sin * this . y , sin * this . x + cos * this . y ) ;
557694 }
558695
559696} ) ;
0 commit comments