@@ -26,23 +26,36 @@ export default class Physics {
26
26
/*----- Constants ----------------------------------------------------------*/
27
27
/** @const {number} G Gravitational constant. */
28
28
static G = 6.67e-11 ;
29
- /** @const {number} timeScale Time scale (seconds per loop). */
30
- static timeScale = 0.5e3 ;
31
- /** @const {number} interval Interval time for physics loop. */
32
- static interval = 1e3 / 120 ;
29
+ /** @const {number} TIME_SCALE Time scale (seconds per loop). */
30
+ static TIME_SCALE = 0.5e3 ;
31
+ /** @const {number} INTERVAL Interval time for physics loop. */
32
+ static INTERVAL = 1e3 / 120 ;
33
33
34
- /*----- Calculation Methods ------------------------------------------------*/
35
- /** Update `Celestial#position`s based on `Celestial#velocity` */
36
- updatePositions ( ) {
37
- this . model . scene . forEach (
38
- /** @arg obj {Celestial} */
39
- ( obj ) => obj . position . add ( obj . velocity . copy ( ) . scale ( Physics . timeScale ) )
40
- ) ;
34
+ /*----- Calculation Functions ----------------------------------------------*/
35
+ /** @const {Object} INTERSECT_CHECKS Intersect function store. */
36
+ static INTERSECT_CHECKS = {
37
+ CIRCLE : {
38
+ CIRCLE :
39
+ /**
40
+ * @arg {Celestial} a
41
+ * @arg {Celestial} b
42
+ * @returns {boolean }
43
+ */
44
+ ( a , b ) => a . position . vectorTo ( b . position ) . magnitude > a . size + b . size ,
45
+ } ,
46
+ } ;
47
+ /**
48
+ * Check for intersections between two Celestial instances
49
+ * @arg {Celestial} a
50
+ * @arg {Celestial} b
51
+ */
52
+ checkIntersection ( a , b ) {
53
+ return Physics . INTERSECT_CHECKS [ a . hitBox ] [ b . hitBox ] ( a , b ) ;
41
54
}
42
55
/**
43
56
* Calculate gravitational acceleration between two Celestial instances
44
- * @param {Celestial } m_1 Celestial to apply acceleration to.
45
- * @param {Celestial } m_2 Celestial applying the acceleration.
57
+ * @arg {Celestial} m_1 Celestial to apply acceleration to.
58
+ * @arg {Celestial} m_2 Celestial applying the acceleration.
46
59
* @returns {Vector } Gravitational acceleration Vector.
47
60
*/
48
61
gravitate ( m_1 , m_2 ) {
@@ -54,6 +67,15 @@ export default class Physics {
54
67
between . magnitude = 1 ;
55
68
return between . scale ( ( Physics . G * m_2 . mass ) / r ** 2 ) ;
56
69
}
70
+
71
+ /*----- Calculation Methods ------------------------------------------------*/
72
+ /** Update `Celestial#position`s based on `Celestial#velocity` */
73
+ updatePositions ( ) {
74
+ this . model . scene . forEach (
75
+ /** @arg obj {Celestial} */
76
+ ( obj ) => obj . position . add ( obj . velocity . copy ( ) . scale ( Physics . TIME_SCALE ) )
77
+ ) ;
78
+ }
57
79
/** Update `Celestial#velocity`s based on gravitational acceleration */
58
80
updateVelocities ( ) {
59
81
this . model . scene . forEach (
@@ -75,7 +97,7 @@ export default class Physics {
75
97
new Vector ( 0 , 0 )
76
98
)
77
99
// Velocity from acceleration
78
- . scale ( Physics . timeScale )
100
+ . scale ( Physics . TIME_SCALE )
79
101
)
80
102
) ;
81
103
}
@@ -85,7 +107,7 @@ export default class Physics {
85
107
loop ( ) {
86
108
this . _intervalId = window . setInterval (
87
109
this . step . bind ( this ) ,
88
- Physics . interval
110
+ Physics . INTERVAL
89
111
) ;
90
112
console . log ( `Physics running on loop ${ this . _intervalId } ` ) ;
91
113
}
0 commit comments