|
|
|
|
@ -90,25 +90,12 @@ int find_dominant_body(SimulationState* sim, int body_index) {
|
|
|
|
|
int new_parent = 0; |
|
|
|
|
double min_distance = INFINITY; |
|
|
|
|
|
|
|
|
|
// Debug: Print SOI check details for spacecraft
|
|
|
|
|
bool is_spacecraft = (strncmp(body->name, "Spacecraft", 10) == 0); |
|
|
|
|
if (is_spacecraft) { |
|
|
|
|
printf("DEBUG [find_dominant_body for %s]:\n", body->name); |
|
|
|
|
printf(" Current parent: %d (root)\n", parent_idx); |
|
|
|
|
printf(" Body pos: (%.2e, %.2e, %.2e)\n", body->position.x, body->position.y, body->position.z); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < sim->body_count; i++) { |
|
|
|
|
if (i == body_index) continue; |
|
|
|
|
|
|
|
|
|
CelestialBody* potential = &sim->bodies[i]; |
|
|
|
|
double distance = vec3_distance(body->position, potential->position); |
|
|
|
|
|
|
|
|
|
if (is_spacecraft) { |
|
|
|
|
printf(" Checking body %d (%s): distance=%.2e, SOI=%.2e, within=%d\n", |
|
|
|
|
i, potential->name, distance, potential->soi_radius, distance < potential->soi_radius); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// If within SOI and closer than current, switch to this body
|
|
|
|
|
if (distance < potential->soi_radius && distance < min_distance) { |
|
|
|
|
min_distance = distance; |
|
|
|
|
@ -116,10 +103,6 @@ int find_dominant_body(SimulationState* sim, int body_index) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (is_spacecraft) { |
|
|
|
|
printf(" Selected new parent: %d (%s)\n", new_parent, sim->bodies[new_parent].name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return new_parent; |
|
|
|
|
} |
|
|
|
|
// Update sphere of influence radius using Hill sphere approximation
|
|
|
|
|
@ -145,18 +128,6 @@ void update_simulation(SimulationState* sim) {
|
|
|
|
|
|
|
|
|
|
int new_parent = find_dominant_body(sim, i); |
|
|
|
|
|
|
|
|
|
// Debug: Print spacecraft state before parent switch
|
|
|
|
|
if (strncmp(body->name, "Spacecraft", 10) == 0) { |
|
|
|
|
printf("DEBUG [Before find_dominant_body]:\n"); |
|
|
|
|
printf(" Body: %s (idx %d)\n", body->name, i); |
|
|
|
|
printf(" Current parent: %d\n", body->parent_index); |
|
|
|
|
printf(" Global pos: (%.2e, %.2e, %.2e)\n", body->position.x, body->position.y, body->position.z); |
|
|
|
|
printf(" Local pos: (%.2e, %.2e, %.2e)\n", body->local_position.x, body->local_position.y, body->local_position.z); |
|
|
|
|
printf(" Global vel: (%.2e, %.2e, %.2e)\n", body->velocity.x, body->velocity.y, body->velocity.z); |
|
|
|
|
printf(" Local vel: (%.2e, %.2e, %.2e)\n", body->local_velocity.x, body->local_velocity.y, body->local_velocity.z); |
|
|
|
|
printf(" New parent from find_dominant_body: %d\n", new_parent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (new_parent != body->parent_index) { |
|
|
|
|
// Convert current local coordinates to global coordinates using old parent
|
|
|
|
|
if (body->parent_index >= 0 && body->parent_index < sim->body_count) { |
|
|
|
|
@ -172,14 +143,6 @@ void update_simulation(SimulationState* sim) {
|
|
|
|
|
// Update parent index
|
|
|
|
|
body->parent_index = new_parent; |
|
|
|
|
|
|
|
|
|
// Debug: Print after parent index change, before new local coord calculation
|
|
|
|
|
if (strncmp(body->name, "Spacecraft", 10) == 0) { |
|
|
|
|
printf("DEBUG [Parent switch detected]:\n"); |
|
|
|
|
printf(" Old parent: %d -> New parent: %d\n", i, new_parent); |
|
|
|
|
printf(" Global pos after old->global transform: (%.2e, %.2e, %.2e)\n", body->position.x, body->position.y, body->position.z); |
|
|
|
|
printf(" Global vel after old->global transform: (%.2e, %.2e, %.2e)\n", body->velocity.x, body->velocity.y, body->velocity.z); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Convert global coordinates to local coordinates using new parent
|
|
|
|
|
if (body->parent_index >= 0 && body->parent_index < sim->body_count) { |
|
|
|
|
CelestialBody* new_parent_body = &sim->bodies[body->parent_index]; |
|
|
|
|
@ -190,67 +153,16 @@ void update_simulation(SimulationState* sim) {
|
|
|
|
|
body->local_position = body->position; |
|
|
|
|
body->local_velocity = body->velocity; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debug: Print after new local coord calculation
|
|
|
|
|
if (strncmp(body->name, "Spacecraft", 10) == 0) { |
|
|
|
|
printf("DEBUG [After new local coords]:\n"); |
|
|
|
|
printf(" Local pos: (%.2e, %.2e, %.2e)\n", body->local_position.x, body->local_position.y, body->local_position.z); |
|
|
|
|
printf(" Local vel: (%.2e, %.2e, %.2e)\n", body->local_velocity.x, body->local_velocity.y, body->local_velocity.z); |
|
|
|
|
if (new_parent >= 0 && new_parent < sim->body_count) { |
|
|
|
|
CelestialBody* parent = &sim->bodies[new_parent]; |
|
|
|
|
printf(" New parent pos: (%.2e, %.2e, %.2e)\n", parent->position.x, parent->position.y, parent->position.z); |
|
|
|
|
printf(" New parent vel: (%.2e, %.2e, %.2e)\n", parent->velocity.x, parent->velocity.y, parent->velocity.z); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (body->parent_index >= 0 && body->parent_index < sim->body_count) { |
|
|
|
|
CelestialBody* parent = &sim->bodies[body->parent_index]; |
|
|
|
|
|
|
|
|
|
// Debug: Print before RK4 integration
|
|
|
|
|
if (strncmp(body->name, "Spacecraft", 10) == 0) { |
|
|
|
|
printf("DEBUG [Before RK4 integration]:\n"); |
|
|
|
|
printf(" Parent: %s (idx %d)\n", parent->name, body->parent_index); |
|
|
|
|
printf(" Parent mass: %.2e kg\n", parent->mass); |
|
|
|
|
printf(" Local pos: (%.2e, %.2e, %.2e)\n", body->local_position.x, body->local_position.y, body->local_position.z); |
|
|
|
|
printf(" Local vel: (%.2e, %.2e, %.2e)\n", body->local_velocity.x, body->local_velocity.y, body->local_velocity.z); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
rk4_step(&body->local_position, &body->local_velocity, |
|
|
|
|
sim->dt, body->mass, parent->mass); |
|
|
|
|
|
|
|
|
|
// Debug: Print after RK4 integration
|
|
|
|
|
if (strncmp(body->name, "Spacecraft", 10) == 0) { |
|
|
|
|
printf("DEBUG [After RK4 integration]:\n"); |
|
|
|
|
printf(" Local pos: (%.2e, %.2e, %.2e)\n", body->local_position.x, body->local_position.y, body->local_position.z); |
|
|
|
|
printf(" Local vel: (%.2e, %.2e, %.2e)\n", body->local_velocity.x, body->local_velocity.y, body->local_velocity.z); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debug: Print before compute_global_coordinates for spacecraft
|
|
|
|
|
for (int i = 0; i < sim->body_count; i++) { |
|
|
|
|
if (strncmp(sim->bodies[i].name, "Spacecraft", 10) == 0) { |
|
|
|
|
CelestialBody* body = &sim->bodies[i]; |
|
|
|
|
printf("DEBUG [Before compute_global_coordinates]:\n"); |
|
|
|
|
printf(" Parent: %d\n", body->parent_index); |
|
|
|
|
printf(" Local pos: (%.2e, %.2e, %.2e)\n", body->local_position.x, body->local_position.y, body->local_position.z); |
|
|
|
|
printf(" Local vel: (%.2e, %.2e, %.2e)\n", body->local_velocity.x, body->local_velocity.y, body->local_velocity.z); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
compute_global_coordinates(sim); |
|
|
|
|
|
|
|
|
|
// Debug: Print after compute_global_coordinates for spacecraft
|
|
|
|
|
for (int i = 0; i < sim->body_count; i++) { |
|
|
|
|
if (strncmp(sim->bodies[i].name, "Spacecraft", 10) == 0) { |
|
|
|
|
CelestialBody* body = &sim->bodies[i]; |
|
|
|
|
printf("DEBUG [After compute_global_coordinates]:\n"); |
|
|
|
|
printf(" Global pos: (%.2e, %.2e, %.2e)\n", body->position.x, body->position.y, body->position.z); |
|
|
|
|
printf(" Global vel: (%.2e, %.2e, %.2e)\n", body->velocity.x, body->velocity.y, body->velocity.z); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sim->time += sim->dt; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|