File tree Expand file tree Collapse file tree 3 files changed +18
-4
lines changed Expand file tree Collapse file tree 3 files changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -42,4 +42,3 @@ model.scene.push(titan);
42
42
view . loop ( ) ;
43
43
physics . loop ( ) ;
44
44
console . log ( model ) ;
45
- window . setTimeout ( physics . stop , 100 ) ;
Original file line number Diff line number Diff line change @@ -38,6 +38,21 @@ export default class Physics {
38
38
( obj ) => obj . position . add ( obj . velocity . copy ( ) . scale ( Physics . timeScale ) )
39
39
) ;
40
40
}
41
+ /**
42
+ * Calculate gravitational acceleration between two Celestial instances
43
+ * @param {Celestial } m_1 Celestial to apply acceleration to.
44
+ * @param {Celestial } m_2 Celestial applying the acceleration.
45
+ * @returns {Vector } Gravitational acceleration Vector.
46
+ */
47
+ gravitate ( m_1 , m_2 ) {
48
+ // Vector between m_1 and m_2
49
+ const between = m_1 . position . vectorTo ( m_2 . position ) ,
50
+ // Distance between m_1 and m_2
51
+ r = between . magnitude ;
52
+ // Make `between` a unit vector
53
+ between . magnitude = 1 ;
54
+ return between . scale ( ( Physics . G * m_2 . mass ) / r ** 2 ) ;
55
+ }
41
56
42
57
/*----- Running Methods ----------------------------------------------------*/
43
58
/** Calculate physics on a set interval. */
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ export class Point {
37
37
* @returns {Point } The updated point.
38
38
*/
39
39
add ( addend ) {
40
- if ( typeof addend === "number" ) {
40
+ if ( typeof addend === "number" ) {
41
41
this . x += addend ;
42
42
this . y += addend ;
43
43
} else {
@@ -52,7 +52,7 @@ export class Point {
52
52
* @returns {Point } The updated point.
53
53
*/
54
54
subtract ( subtrahend ) {
55
- if ( typeof subtrahend === "number" ) {
55
+ if ( typeof subtrahend === "number" ) {
56
56
this . x -= subtrahend ;
57
57
this . y -= subtrahend ;
58
58
} else {
@@ -80,7 +80,7 @@ export class Point {
80
80
}
81
81
/**
82
82
* Get a Vector between two points.
83
- * @param {Point } point
83
+ * @param {Point } point
84
84
* @returns {Vector } A Vector between this Point and another Point.
85
85
*/
86
86
vectorTo ( point ) {
You can’t perform that action at this time.
0 commit comments