Skip to content

Documentation / @interstellar-tools/equations / gravitationalForce

Function: gravitationalForce() ​

ts
function gravitationalForce(
  m1: number,
  m2: number,
  r1: Vector3DTupleType,
  r2: Vector3DTupleType,
  G?: number
): ForceResultInterface;

Defined in: gravity/gravitational-force.ts:98

Compute the Newtonian gravitational force on body 1 due to body 2.

Definitions

r=r2−r1,r=‖r‖,r^=rr

Law & Decomposition

F1←2=Gm1m2r2r^‖F‖=Gm1m2r2,F=⟨Fx,Fy,Fz⟩=‖F‖r^Fx=‖F‖r^x,Fy=‖F‖r^y,Fz=‖F‖r^z

Newton's 3rd law, for context

F1←2=−F2←1

Conventions & units

  • Input masses in kilograms (kg); positions in meters (m).
  • Output force in newtons (N).
  • Returned vector and direction point from body 1 toward body 2; i.e., direction = \hat{\mathbf r}.

Invariants (within numerical tolerance)

  • vector = magnitude * direction
  • magnitude = sqrt(Fx² + Fy² + Fz²)
  • ||direction|| = 1

Parameters ​

ParameterTypeDefault valueDescription
m1numberundefinedMass of body 1 (kg).
m2numberundefinedMass of body 2 (kg).
r1Vector3DTupleTypeundefinedPosition of body 1 as a 3-tuple [x, y, z] in meters.
r2Vector3DTupleTypeundefinedPosition of body 2 as a 3-tuple [x, y, z] in meters.
G?numberG_SIGravitational constant (defaults to G_SI, m³·kg⁻¹·s⁻²).

Returns ​

ForceResultInterface

{ vector, magnitude, direction }.

Throws ​

Error If m1 or m2 are non-finite or negative.

Throws ​

Error If positions coincide (r = 0).

Throws ​

Error If G is non-finite or not positive.

Example ​

ts
import { gravitationalForce } from '@interstellar-tools/equations';
import { G_SI } from '@interstellar-tools/constants';

// Earth–Sun at ~1 AU
const mEarth = 5.972e24; // kg
const mSun = 1.9885e30; // kg
const rEarth: [number, number, number] = [0, 0, 0];
const rSun: [number, number, number] = [1.495978707e11, 0, 0]; // m

const F = gravitationalForce(mEarth, mSun, rEarth, rSun, G_SI);
console.log(F.magnitude.toExponential(3)); // ≈ "3.542e+22" N
console.log(F.vector); // Points from Earth toward Sun (−x here)

Get F directly (recommended)

ts
import { gravitationalForce } from '@interstellar-tools/equations';
import type {
  Vector3DTupleType,
  ForceResultInterface
} from '@interstellar-tools/types';

const m1 = 5.972e24; // kg
const m2 = 1.9885e30; // kg
const r1: Vector3DTupleType = [0, 0, 0]; // m
const r2: Vector3DTupleType = [1.495978707e11, 0, 0]; // m

const {
  vector: F,
  magnitude,
  direction
}: ForceResultInterface = gravitationalForce(m1, m2, r1, r2);

See ​

https://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation