From 7749e5cda43a5417c53b6cfd5fe3914dc42063a0 Mon Sep 17 00:00:00 2001 From: ktyl Date: Fri, 13 Oct 2023 23:28:46 +0100 Subject: [PATCH] chore: extract retrieving cartesian elements --- src/orbit.cpp | 36 ++++++++++++++++++++++++------------ src/orbit.hpp | 7 ++++++- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/orbit.cpp b/src/orbit.cpp index a6b10ef..2df3dec 100644 --- a/src/orbit.cpp +++ b/src/orbit.cpp @@ -45,11 +45,8 @@ void Orbit::setElements(Vector6 keplerianElements) regenerateVertices(); } -// Interpolate a position around the orbit. -// t is in range 0..1 and wraps. -glm::vec3 Orbit::getPosition(const float meanAnomaly) +float Orbit::getEccentricAnomaly(const float meanAnomaly) { - // Get eccentric anomaly from elliptical mean anomaly const float eccentricity = _keplerianElements[astro::eccentricityIndex]; float eccentricAnomaly = astro::convertEllipticalMeanAnomalyToEccentricAnomaly( eccentricity, @@ -57,21 +54,36 @@ glm::vec3 Orbit::getPosition(const float meanAnomaly) (float)10e-3, 100); - // Get true anomaly from eccentric anomaly - float trueAnomaly = astro::convertEccentricAnomalyToTrueAnomaly( - eccentricAnomaly, - eccentricity); + return eccentricAnomaly; +} - std::vector kepler(_keplerianElements); - kepler[astro::trueAnomalyIndex] = trueAnomaly; - - std::vector cartesian = astro::convertKeplerianToCartesianElements(kepler, 1.0); +// Interpolate a position around the orbit. +// t is in range 0..1 and wraps. +glm::vec3 Orbit::getPosition(const float meanAnomaly) +{ + Vector6 cartesian = getCartesianCoordinates(meanAnomaly); return glm::vec3( cartesian[astro::xPositionIndex], cartesian[astro::yPositionIndex], cartesian[astro::zPositionIndex]); } +float Orbit::getTrueAnomaly(const float meanAnomaly) +{ + const float eccentricAnomaly = getEccentricAnomaly(meanAnomaly); + const float eccentricity = _keplerianElements[astro::eccentricityIndex]; + return astro::convertEccentricAnomalyToTrueAnomaly( + eccentricAnomaly, + eccentricity); +} + +Vector6 Orbit::getCartesianCoordinates(const float meanAnomaly) +{ + Vector6 kepler(_keplerianElements); + kepler[astro::trueAnomalyIndex] = getTrueAnomaly(meanAnomaly); + return astro::convertKeplerianToCartesianElements(kepler, 1.0); +} + glm::vec3 Orbit::getTangent(const float meanAnomaly) { float epsilon = 0.01; diff --git a/src/orbit.hpp b/src/orbit.hpp index f717a61..1dd36ef 100644 --- a/src/orbit.hpp +++ b/src/orbit.hpp @@ -13,8 +13,9 @@ public: Orbit(Vector6 keplerianElements); void render(); + // TODO: meanAnomaly in all these arguments actually means eccentricMeanAnomaly, + // will have to change that when adding non-ellipctical orbits - don't get confused! glm::vec3 getPosition(const float meanAnomaly); - glm::vec3 getVelocity(const float meanAnomaly); glm::vec3 getTangent(const float meanAnomaly); void setElements(Vector6 keplerianElements); @@ -31,4 +32,8 @@ private: Vector6 _keplerianElements; void regenerateVertices(); + + float getEccentricAnomaly(const float meanAnomaly); + float getTrueAnomaly(const float meanAnomaly); + Vector6 getCartesianCoordinates(const float meanAnomaly); };