|
|
|
|
@ -76,22 +76,6 @@ void update_soi(CelestialBody* body, CelestialBody* parent, double semi_major_ax
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void update_simulation(SimulationState* sim) { |
|
|
|
|
for (int i = 0; i < sim->body_count; i++) { |
|
|
|
|
CelestialBody* body = &sim->bodies[i]; |
|
|
|
|
|
|
|
|
|
// FIXME: we shouldn't be simulating root bodies anymore
|
|
|
|
|
if (body->parent_index == -1) { |
|
|
|
|
AccelerationContext ctx; |
|
|
|
|
ctx.sim = sim; |
|
|
|
|
ctx.current_body = body; |
|
|
|
|
ctx.body_index = i; |
|
|
|
|
|
|
|
|
|
rk4_step(body, &ctx, sim->dt); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
compute_global_coordinates(sim); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < sim->body_count; i++) { |
|
|
|
|
CelestialBody* body = &sim->bodies[i]; |
|
|
|
|
|
|
|
|
|
@ -101,6 +85,8 @@ void update_simulation(SimulationState* sim) {
|
|
|
|
|
|
|
|
|
|
int new_parent = find_dominant_body(sim, i); |
|
|
|
|
if (new_parent != body->parent_index && new_parent != -1) { |
|
|
|
|
// FIXME: there's now a bug here when changing parent bodes
|
|
|
|
|
// I think it's that the local position is never updated
|
|
|
|
|
body->parent_index = new_parent; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -123,31 +109,18 @@ static void compute_orbital_velocity_from_vis_viva(CelestialBody* body,
|
|
|
|
|
CelestialBody* parent) { |
|
|
|
|
Vec3 r = vec3_sub(body->position, parent->position); |
|
|
|
|
double distance = vec3_magnitude(r); |
|
|
|
|
|
|
|
|
|
if (distance < 1.0) { |
|
|
|
|
body->velocity = {0.0, 0.0, 0.0}; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
double e = body->eccentricity; |
|
|
|
|
double a = body->semi_major_axis; |
|
|
|
|
|
|
|
|
|
// FIXME: there's a bug here
|
|
|
|
|
// could be unit related, mass is in units of kg, and distance is meters
|
|
|
|
|
// insteam of km, velocity is also m/s
|
|
|
|
|
//double speed = (double) sqrt(G * parent->mass * (2.0 / distance - 1.0 / a));
|
|
|
|
|
double v_squared = G * parent->mass * (2.0 / distance - 1.0 / a); |
|
|
|
|
// FIXME: need error handling here
|
|
|
|
|
// if v_squared is negative, the inital body parameters are bad
|
|
|
|
|
assert(v_squared >= 0); |
|
|
|
|
double speed = (double) sqrt(v_squared); |
|
|
|
|
|
|
|
|
|
if (e > 0.01) { |
|
|
|
|
printf(" %s: eccentric orbit (e=%.2f, a=%.3e m), speed at r=%.3e m: %.3f km/s\n", |
|
|
|
|
body->name, e, a, distance, speed / 1000.0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Vec3 z_axis = {0.0, 0.0, 1.0}; |
|
|
|
|
Vec3 vel_dir = vec3_cross(r, z_axis); |
|
|
|
|
|
|
|
|
|
// NOTE: I suppose this is for the case of a high inclination orbit?
|
|
|
|
|
// FIXME: make a test to see if this is necessary
|
|
|
|
|
if (vec3_magnitude(vel_dir) < 0.01) { |
|
|
|
|
Vec3 x_axis = {1.0, 0.0, 0.0}; |
|
|
|
|
vel_dir = vec3_cross(r, x_axis); |
|
|
|
|
@ -159,6 +132,7 @@ static void compute_orbital_velocity_from_vis_viva(CelestialBody* body,
|
|
|
|
|
body->velocity = vec3_add(body->velocity, parent->velocity); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// FIXME: remove this function since we're already looping in calculate_initial_velocities
|
|
|
|
|
static void set_child_bodies_velocity(SimulationState* sim) { |
|
|
|
|
for (int i = 0; i < sim->body_count; i++) { |
|
|
|
|
CelestialBody* body = &sim->bodies[i]; |
|
|
|
|
@ -198,6 +172,8 @@ void calculate_soi_radii(SimulationState* sim) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// FIXME: actually all the above functions should be called inside this loop
|
|
|
|
|
// or the loop should be in config_loader
|
|
|
|
|
void initialize_local_coordinates(SimulationState* sim) { |
|
|
|
|
for (int i = 0; i < sim->body_count; i++) { |
|
|
|
|
CelestialBody* body = &sim->bodies[i]; |
|
|
|
|
|