diff --git a/src/orbital_mechanics.cpp b/src/orbital_mechanics.cpp index 1833161..9258ba7 100644 --- a/src/orbital_mechanics.cpp +++ b/src/orbital_mechanics.cpp @@ -89,7 +89,23 @@ double solve_kepler_hyperbolic(double mean_anomaly, double eccentricity) { } double eccentric_to_true_anomaly(double eccentric_anomaly, double eccentricity) { - // E to true anomaly conversion: tan(ν/2) = √((1+e)/(1-e)) · tan(E/2) + if (fabs(1.0 - eccentricity) < 0.01) { + double E = eccentric_anomaly; + double e = eccentricity; + + double cos_E = cos(E); + double sin_E = sin(E); + double denominator = 1.0 - e * cos_E; + + double cos_nu = (cos_E - e) / denominator; + double sin_nu = sin_E * sqrt(1.0 - e * e) / denominator; + + cos_nu = fmax(-1.0, fmin(1.0, cos_nu)); + sin_nu = fmax(-1.0, fmin(1.0, sin_nu)); + + return atan2(sin_nu, cos_nu); + } + double tan_half_E = tan(eccentric_anomaly / 2.0); double tan_half_nu = sqrt((1.0 + eccentricity) / (1.0 - eccentricity)) * tan_half_E; return 2.0 * atan(tan_half_nu);