瀏覽代碼

Introduce immutable zero vectors

customisations
alemart 10 月之前
父節點
當前提交
360bea348a
共有 3 個檔案被更改,包括 34 行新增5 行删除
  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 查看文件

20
  * 2D vectors
20
  * 2D vectors
21
  */
21
  */
22
 
22
 
23
+import { Nullable } from '../utils/utils';
24
+
23
 /** Small number */
25
 /** Small number */
24
 const EPSILON = 1e-6;
26
 const EPSILON = 1e-6;
25
 
27
 
28
+/** Immutable zero vector */
29
+let ZERO: Nullable<Vector2> = null;
30
+
26
 // public / non-internal methods do not change the contents of the vector
31
 // public / non-internal methods do not change the contents of the vector
27
 
32
 
28
 
33
 
41
 
46
 
42
 
47
 
43
 
48
 
49
+
44
     /**
50
     /**
45
      * Constructor
51
      * Constructor
46
      */
52
      */
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
      * The length of this vector
78
      * The length of this vector
64
      * @returns sqrt(x^2 + y^2)
79
      * @returns sqrt(x^2 + y^2)
65
      */
80
      */

+ 14
- 0
src/geometry/vector3.ts 查看文件

20
  * 3D vectors
20
  * 3D vectors
21
  */
21
  */
22
 
22
 
23
+import { Nullable } from '../utils/utils';
24
+
23
 /** Small number */
25
 /** Small number */
24
 const EPSILON = 1e-6;
26
 const EPSILON = 1e-6;
25
 
27
 
28
+/** Immutable zero vector */
29
+let ZERO: Nullable<Vector3> = null;
30
+
26
 // public / non-internal methods do not change the contents of the vector
31
 // public / non-internal methods do not change the contents of the vector
27
 
32
 
28
 
33
 
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
      * The length of this vector
80
      * The length of this vector
67
      * @returns sqrt(x^2 + y^2 + z^2)
81
      * @returns sqrt(x^2 + y^2 + z^2)
68
      */
82
      */

+ 5
- 5
src/trackers/pointer-tracker/pointer-tracker.ts 查看文件

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

Loading…
取消
儲存