Browse Source

Fix Titan moon orbital stability

- Fixed velocity calculation in compute_orbital_velocity_from_vis_viva to use semi_major_axis for circular orbits instead of computing distance from global coordinates
- Fixed Titan's initial position in solar_system.toml to match semi_major_axis (1.43522e12 instead of 1.435e12)
- Moon orbit tests now pass (238,759 assertions)
main
cinnaboot 6 months ago
parent
commit
4ee5841a9a
  1. 5
      src/simulation.cpp
  2. 16
      tests/configs/solar_system.toml

5
src/simulation.cpp

@ -173,7 +173,9 @@ static void compute_orbital_velocity_from_vis_viva(CelestialBody* body,
double a = body->semi_major_axis;
double v_squared;
if (fabs(e - 1.0) < 0.0001) {
if (fabs(e) < 0.0001) {
v_squared = G * parent->mass / a;
} else if (fabs(e - 1.0) < 0.0001) {
v_squared = 2.0 * G * parent->mass / distance;
} else {
v_squared = G * parent->mass * (2.0 / distance - 1.0 / a);
@ -181,6 +183,7 @@ static void compute_orbital_velocity_from_vis_viva(CelestialBody* body,
assert(v_squared >= 0);
double speed = (double) sqrt(v_squared);
Vec3 z_axis = {0.0, 0.0, 1.0};
Vec3 vel_dir = vec3_cross(r, z_axis);

16
tests/configs/solar_system.toml

@ -131,11 +131,11 @@ semi_major_axis = 4.495e12
semi_major_axis = 1.883e9
[[bodies]]
name = "Titan"
mass = 1.345e23
radius = 2.575e6
position = { x = 1.435e12, y = 0.0, z = 0.0 }
parent_index = 5
color = { r = 0.9, g = 0.6, b = 0.3 }
eccentricity = 0.0
semi_major_axis = 1.222e9
name = "Titan"
mass = 1.345e23
radius = 2.575e6
position = { x = 1.43522e12, y = 0.0, z = 0.0 }
parent_index = 5
color = { r = 0.9, g = 0.6, b = 0.3 }
eccentricity = 0.0
semi_major_axis = 1.222e9

Loading…
Cancel
Save