Skip to content

Documentation / @interstellar-tools/equations

@interstellar-tools/equations ​

Curated, tested orbital mechanics and dynamics formulas: Newtonian gravity, vis-viva, Kepler’s third law, Kepler’s equation solvers, anomaly conversions, Hohmann transfers, rocket equation, J2 perturbations and more. SI units in, SI units out, with clear errors for invalid input.

Documentation ​

The full documentation lives at phun-ky.net/interstellar-tools:

Installation ​

npm
shell
npm i --save @interstellar-tools/equations

WARNING

These equations have not YET been battle-tested or tested to real world mechanics, YMMV!

Here you will find a set of function that can assist you when calculating orbits, angles, solve for kelper or find true anomalies.

Type Aliases ​

Type AliasDescription
HohmannTransferReturnType-

Functions ​

FunctionDescription
planetographicLatitudeOblateCompute planetographic latitude (oblate spheroid) from body-fixed Cartesian coordinates.

Angles ​

FunctionDescription
computeAngleAdvances a body along its orbit by one time step and returns its new true anomaly (ν).
wrapAngleWrap an angle (in radians) to a single-turn range and snap values extremely close to ±τ (±2π) back to 0 to curb floating-point drift.

Anomalies ​

FunctionDescription
advanceMeanAnomalyAdvances an orbit by a time step and returns its new mean anomaly (M).
eccentricToTrueAnomalyConverts Eccentric Anomaly (E) to True Anomaly (V) for an orbit.
meanAnomalyAtThe mean anomaly (M) of an orbit at a given date.
trueAnomalyAtThe true anomaly (ν) of an orbit at a given date: where the body is.
trueToMeanAnomalyConverts true anomaly (ν) to mean anomaly (M) using Kepler's equation.

Cartography ​

FunctionDescription
bodyFixedFromInertialDcmIAUBuild the IAU-style inertial → body-fixed direction cosine matrix (DCM).
eccentricitySquaredOblateSpheroidCompute first eccentricity squared (e2) for an oblate spheroid.
flatteningOblateSpheroidCompute flattening (f) of an oblate spheroid.
isOnTriaxialEllipsoidSurfaceTest whether a 3D point lies on the surface of a triaxial ellipsoid (within a tolerance).
planetocentricLatitudeCompute planetocentric latitude (φc) from body-fixed Cartesian coordinates.

Gravity ​

FunctionDescription
accelerationOn1By2Gravitational acceleration of body 1 due to body 2 (vector, m/s²).
forceOn1By2Gravitational force vector on body 1 due to body 2 (newtons, N).
gravitationalForceCompute the Newtonian gravitational force on body 1 due to body 2.
gravitationalParameterTwo-body gravitational parameter (μ).

Helpers ​

NameDescription
METERS_PERMeters-per-unit lookup for all canonical DistanceUnitType symbols.
NORMALIZE_DISTANCE_UNITLookup table that normalizes distance unit aliases (and the canonical symbols themselves) to a single, canonical DistanceUnitType.
applyMatrix3Apply a 3×3 matrix to a 3D vector (matrix–vector multiplication).
convertDistanceUnitConvert a distance between any two DistanceUnitType units.
det3Compute the determinant of a 3×3 matrix (row-major).
dotCompute the dot product of two 3D vectors.
matMul3Multiply two 3×3 matrices (row-major).
normCompute the Euclidean norm (length / magnitude) of a 3D vector.
norm2piNormalize an angle to the range [0, 2π).
normalizeDistanceUnitNormalize a distance unit string into a canonical DistanceUnitType.
radBrand a numeric value as Radians.
rot1Construct a right-handed rotation matrix about the x-axis (often written R₁).
rot3Construct a right-handed rotation matrix about the z-axis (often written R₃).
scaleScale a 3D vector by a scalar.
subSubtract two 3D vectors (a - b).
toRadConvert degrees → radians.
transpose3Transpose a 3×3 matrix (row-major).

Kepler ​

FunctionDescription
residualCompute the Kepler equation residual for an elliptical orbit:

Kepler Solvers ​

FunctionDescription
solveKeplerSolves Kepler's Equation for the Eccentric Anomaly (E) using an adaptive approach:
solveKeplerBisectionSolve Kepler's equation for the eccentric anomaly (E) via bisection (elliptic case: (0≤e<1)).
solveKeplerHighEccentricitySolves Kepler's Equation for the Eccentric Anomaly (E) in highly eccentric orbits (e>0.9) using an iterative numerical approach.
solveKeplerNewtonRaphsonSolves Kepler's Equation for the Eccentric Anomaly (E) using the Newton-Raphson method with Householder acceleration for fast convergence.

Manoeuvres ​

FunctionDescription
combineBurnsDeltaVCombine non-collinear burns (vector law / law of cosines for velocities).
gravityAssistTurningAngleCompute gravity-assist turning angle (flyby deflection) for a hyperbolic encounter.
hohmannSemiMajorAxisConvenience: compute only the semi-major axis (at) (m) of the Hohmann transfer.
hohmannTransferHohmann transfer (coplanar, circular (r1→r2)).
hohmannTransferTimeConvenience: compute only the transfer time (tt) (s).
oberthEnergyGainOberth effect (specific energy gain near periapsis).
planeChangeDeltaVInclination (plane) change Δv at speed (v).
rocketDeltaVFromIspCompute ideal delta-v from the Tsiolkovsky rocket equation using specific impulse.
rocketDeltaVFromVeCompute ideal delta-v from the Tsiolkovsky rocket equation using effective exhaust velocity.

Orbits ​

FunctionDescription
atmosphericDragAccelerationCompute atmospheric drag acceleration magnitude.
characteristicEnergyC3Compute characteristic energy (C3) from hyperbolic excess speed.
circularSpeedCircular speed (vc) for a body in a circular orbit at radius (r).
cwHillDerivativesCompute the time-derivative of a Clohessy–Wiltshire / Hill relative-motion state (right-hand side for ODE integration).
escapeSpeedEscape speed (vesc) - minimum speed to escape a central body's gravity from radius (r) (ignoring drag/perturbations).
flightPathAngleFromTrueAnomalyFlight-path angle (γ) from true anomaly (ν) and eccentricity (e).
hyperbolicPeriapsisSpeedCompute hyperbolic periapsis speed for a flyby/escape hyperbola.
j2ArgumentOfPerigeeRateCompute argument of perigee rotation (J2 secular drift of ω) for an orbit about an oblate body.
j2NodalPrecessionRateCompute J2 nodal precession (RAAN drift) for an orbit about an oblate body.
keplerPeriodKepler's 3rd law - orbital period (T) from semi-major axis (a) and gravitational parameter (μ).
meanMotionCompute mean motion for a (near-)circular reference orbit.
periApoapsisRadiiPeri/apoapsis radii from semi-major axis (a) and eccentricity (e).
specificAngularMomentumSpecific angular momentum (h) (scalar magnitude).
specificAngularMomentumFromElementsSpecific angular momentum (h) from orbital elements.
specificMechanicalEnergySpecific mechanical energy (ε) of a point mass in a central gravity field.
sphereOfInfluenceRadiusCompute sphere of influence radius (patched conics approximation).
visVivaSpeedVis-viva (orbital speed) - compute speed magnitude (v) from (r), (a), and (μ).