瀏覽代碼

Add function to calculate gravity between two bodies

ApisNecros 4 月之前
父節點
當前提交
7f676806ea
共有 1 個文件被更改,包括 10 次插入2 次删除
  1. 10 2
      12/12_1.js

+ 10 - 2
12/12_1.js

@@ -43,5 +43,13 @@ class Vector3 {
  * @returns {void}
  */
 function calculateGravity(moonA, moonB) {
-    
-}
+    // The gravitational pull on Moon A caused by Moon B
+    const gravityA = new Vector3(
+        // eslint-disable-next-line no-nested-ternary
+        moonB.x > moonA.x ? 1 : (moonB.x < moonA.x ? -1 : 0),
+        // eslint-disable-next-line no-nested-ternary
+        moonB.y > moonA.y ? 1 : (moonB.y < moonA.y ? -1 : 0),
+        // eslint-disable-next-line no-nested-ternary
+        moonB.z > moonA.z ? 1 : (moonB.z < moonA.z ? -1 : 0),
+    );
+}