vibe coding an orbital mechanics simulation to try out claude code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

118 lines
4.3 KiB

#include <catch2/catch_test_macros.hpp>
#include "../src/physics.h"
#include "../src/simulation.h"
#include "../src/spacecraft.h"
#include "../src/config_loader.h"
#include <cmath>
TEST_CASE("Debug orbit rendering coordinates", "[debug][rendering]") {
const double TIME_STEP = 60.0;
SimulationState* sim = create_simulation(10, 10, 0, TIME_STEP);
REQUIRE(load_system_config(sim, "tests/test_orbit_rendering.toml"));
Spacecraft* craft = &sim->spacecraft[0];
CelestialBody* parent = &sim->bodies[craft->parent_index];
// Calculate orbit parameters (same as render_orbit() does)
Vec3 r_vec = craft->local_position;
Vec3 velocity = craft->local_velocity;
Vec3 parent_pos = parent->global_position;
double r = vec3_magnitude(r_vec);
double v = vec3_magnitude(velocity);
double mu = G * parent->mass;
// Calculate eccentricity vector
double v_squared = v * v;
double r_dot_v = vec3_dot(r_vec, velocity);
Vec3 e_vec = {
(v_squared - mu / r) * r_vec.x - r_dot_v * velocity.x,
(v_squared - mu / r) * r_vec.y - r_dot_v * velocity.y,
(v_squared - mu / r) * r_vec.z - r_dot_v * velocity.z
};
double e = vec3_magnitude(e_vec) / mu;
// Calculate semi-major axis
double specific_energy = (v * v) / 2.0 - mu / r;
double a = -mu / (2.0 * specific_energy);
INFO("=== Orbit Parameters ===");
INFO("Semi-major axis (a): " << a << " m");
INFO("Eccentricity (e): " << e);
INFO("Orbital radius (r): " << r << " m");
INFO("Velocity (v): " << v << " m/s");
// Calculate rendering scale factors
double distance_scale = 1e-9;
double size_scale = 0.02;
INFO("");
INFO("=== Rendering Scale Factors ===");
INFO("Distance scale (linear): " << distance_scale);
INFO("Size scale (log): " << size_scale);
// Calculate Earth's rendered size
float earth_render_radius = size_scale * log10(parent->radius);
INFO("Earth rendered radius: " << earth_render_radius << " units");
// Generate orbit points at 0°, 90°, 180°, 270°
double b = a * sqrt(1.0 - e * e);
double c = a * e;
INFO("");
INFO("=== Orbit Point Coordinates ===");
for (int i = 0; i < 4; i++) {
double theta = (double)i / 4.0 * 2.0 * M_PI;
// Orbital plane coordinates
double x_orbit = a * cos(theta) - c;
double y_orbit = b * sin(theta);
// Sim coordinates (using orbital_to_cartesian logic with circular orbit fix)
Vec3 e_dir = vec3_normalize(e_vec);
Vec3 periapsis_dir;
// For circular orbits, use position direction as periapsis direction
if (vec3_magnitude(e_dir) < 0.001) {
periapsis_dir = vec3_normalize(r_vec);
} else {
periapsis_dir = e_dir;
}
Vec3 h_vec = vec3_cross(r_vec, velocity);
Vec3 normal = vec3_normalize(h_vec);
Vec3 q_vec = vec3_cross(normal, periapsis_dir);
Vec3 sim_pos = {
periapsis_dir.x * x_orbit + q_vec.x * y_orbit + parent_pos.x,
periapsis_dir.y * x_orbit + q_vec.y * y_orbit + parent_pos.y,
periapsis_dir.z * x_orbit + q_vec.z * y_orbit + parent_pos.z
};
// Render coordinates (using sim_to_render logic)
double render_x = sim_pos.x * distance_scale;
double render_y = sim_pos.z * distance_scale;
double render_z = -sim_pos.y * distance_scale;
double distance_from_center = sqrt(render_x*render_x + render_y*render_y + render_z*render_z);
INFO("\nPoint " << i << " (" << (i * 90) << "°):");
INFO(" Orbit plane: (" << x_orbit << ", " << y_orbit << ") m");
INFO(" Sim pos: (" << sim_pos.x << ", " << sim_pos.y << ", " << sim_pos.z << ") m");
INFO(" Render pos: (" << render_x << ", " << render_y << ", " << render_z << ") units");
INFO(" Distance from center: " << distance_from_center << " units");
INFO(" % of Earth radius: " << (distance_from_center / earth_render_radius * 100.0) << "%");
}
INFO("");
INFO("=== Scaling Diagnosis ===");
double orbit_radius_render = r * distance_scale;
INFO("Orbital radius (render units): " << orbit_radius_render);
INFO("Ratio to Earth rendered radius: " << (orbit_radius_render / earth_render_radius));
INFO("Earth rendered radius: " << earth_render_radius);
REQUIRE(true);
destroy_simulation(sim);
}