Quellcode durchsuchen

Introduce immutable zero vectors

customisations
alemart vor 10 Monaten
Ursprung
Commit
360bea348a
3 geänderte Dateien mit 34 neuen und 5 gelöschten Zeilen
  1. 15
    0
      src/geometry/vector2.ts
  2. 14
    0
      src/geometry/vector3.ts
  3. 5
    5
      src/trackers/pointer-tracker/pointer-tracker.ts

+ 15
- 0
src/geometry/vector2.ts Datei anzeigen

@@ -20,9 +20,14 @@
20 20
  * 2D vectors
21 21
  */
22 22
 
23
+import { Nullable } from '../utils/utils';
24
+
23 25
 /** Small number */
24 26
 const EPSILON = 1e-6;
25 27
 
28
+/** Immutable zero vector */
29
+let ZERO: Nullable<Vector2> = null;
30
+
26 31
 // public / non-internal methods do not change the contents of the vector
27 32
 
28 33
 
@@ -41,6 +46,7 @@ export class Vector2
41 46
 
42 47
 
43 48
 
49
+
44 50
     /**
45 51
      * Constructor
46 52
      */
@@ -60,6 +66,15 @@ export class Vector2
60 66
     }
61 67
 
62 68
     /**
69
+     * Immutable zero vector
70
+     * @returns an immutable zero vector
71
+     */
72
+    static get ZERO(): Vector2
73
+    {
74
+        return ZERO || (ZERO = Object.freeze(Vector2.Zero()));
75
+    }
76
+
77
+    /**
63 78
      * The length of this vector
64 79
      * @returns sqrt(x^2 + y^2)
65 80
      */

+ 14
- 0
src/geometry/vector3.ts Datei anzeigen

@@ -20,9 +20,14 @@
20 20
  * 3D vectors
21 21
  */
22 22
 
23
+import { Nullable } from '../utils/utils';
24
+
23 25
 /** Small number */
24 26
 const EPSILON = 1e-6;
25 27
 
28
+/** Immutable zero vector */
29
+let ZERO: Nullable<Vector3> = null;
30
+
26 31
 // public / non-internal methods do not change the contents of the vector
27 32
 
28 33
 
@@ -63,6 +68,15 @@ export class Vector3
63 68
     }
64 69
 
65 70
     /**
71
+     * Immutable zero vector
72
+     * @returns an immutable zero vector
73
+     */
74
+    static get ZERO(): Vector3
75
+    {
76
+        return ZERO || (ZERO = Object.freeze(Vector3.Zero()));
77
+    }
78
+
79
+    /**
66 80
      * The length of this vector
67 81
      * @returns sqrt(x^2 + y^2 + z^2)
68 82
      */

+ 5
- 5
src/trackers/pointer-tracker/pointer-tracker.ts Datei anzeigen

@@ -189,8 +189,8 @@ export class PointerTracker implements Tracker
189 189
         // make all active trackables stationary
190 190
         this._updateAllTrackables({
191 191
             phase: 'stationary',
192
-            velocity: Vector2.Zero(),
193
-            deltaPosition: Vector2.Zero()
192
+            velocity: Vector2.ZERO,
193
+            deltaPosition: Vector2.ZERO
194 194
         });
195 195
 
196 196
         // want to reset?
@@ -284,7 +284,7 @@ export class PointerTracker implements Tracker
284 284
             const position = new Vector2(relX, relY);
285 285
 
286 286
             // determine the position delta
287
-            const deltaPosition = !previous ? Vector2.Zero() :
287
+            const deltaPosition = !previous ? Vector2.ZERO :
288 288
                                   position._clone()._subtract(previous.position);
289 289
 
290 290
             // determine the initial position
@@ -400,8 +400,8 @@ export class PointerTracker implements Tracker
400 400
         // cancel all active pointers
401 401
         this._updateAllTrackables({
402 402
             phase: 'canceled',
403
-            deltaPosition: Vector2.Zero(),
404
-            velocity: Vector2.Zero(),
403
+            velocity: Vector2.ZERO,
404
+            deltaPosition: Vector2.ZERO
405 405
         });
406 406
 
407 407
         // consume all events

Laden…
Abbrechen
Speichern