Skip to main content

Crate keplerian_sim

Crate keplerian_sim 

Source
Expand description

§Keplerian Orbital Mechanics

This library crate contains logic for Keplerian orbits, similar to the ones you’d find in a game like Kerbal Space Program.

Keplerian orbits are special in that they are more stable and predictable than Newtonian orbits. In fact, unlike Newtonian orbits, Keplerian orbits don’t use time steps to calculate the next position of an object. Keplerian orbits use state vectors to determine the object’s full trajectory at any given time.
This way, you don’t need to worry about lag destabilizing Keplerian orbits.

However, Keplerian orbits are significantly more complex to calculate than just using Newtonian physics. It’s also a two-body simulation, meaning that it doesn’t account for external forces like gravity from other bodies or the engines of a spacecraft.

The way Kerbal Space Program handles this is to have an “on-rails” physics system utilizing Keplerian orbits, and an “active” physics system utilizing Newtonian two-body physics.

§Getting started

This crate provides four main structs:

  • Orbit: A struct representing an orbit around a celestial body. Each instance of this struct has some cached data to speed up certain calculations, and has a larger memory footprint.
  • CompactOrbit: A struct representing an orbit around a celestial body. This struct has a smaller memory footprint than the regular Orbit struct, but some calculations may take 2~10x slower because it doesn’t save any cached calculations.

We used to have body, universe, and body_presets modules, however these were removed from the main library because some programs have different needs on what to store on each body. The code was moved to the simulate example file in the repository: https://github.com/Not-A-Normal-Robot/keplerian-sim/blob/0d60ed756dc6b09c60d779167cfa0e3346e09213/examples/simulate.rs

§Example

use glam::DVec3;

use keplerian_sim::{Orbit, OrbitTrait};

// Create a perfectly circular orbit with a radius of 1 meter
let orbit = Orbit::default();
assert_eq!(orbit.get_position_at_time(0.0), DVec3::new(1.0, 0.0, 0.0));

Modules§

reexports
Re-exports for dependencies.

Structs§

CompactOrbit
A minimal struct representing a Keplerian orbit.
CompactOrbit2D
A minimal struct representing a 2D Keplerian orbit.
Matrix3x2
A struct representing a 3x2 matrix.
Orbit
A struct representing a Keplerian orbit with some cached values.
Orbit2D
A struct representing a 2D Keplerian orbit with some cached values.
StateVectors
A struct representing a position and velocity at a point in the orbit.
StateVectors2D
A struct representing a position and velocity at a point in the orbit.

Enums§

ApoapsisSetterError
An error to describe why setting the periapsis of an orbit failed.
MuSetterMode
A mode to describe how the gravitational parameter setter should behave.
MuSetterMode2D
A mode to describe how the gravitational parameter setter should behave.

Traits§

OrbitTrait
A trait that defines the methods that a Keplerian orbit must implement.
OrbitTrait2D
A trait that defines the methods that a 2D-constrained Keplerian orbit must implement.

Functions§

sinhcosh
Get the hyperbolic sine and cosine of a number.