From 9f2c05bf6f43b9b9532ca5f3d05fc36990c63b80 Mon Sep 17 00:00:00 2001 From: ktyl Date: Tue, 15 Aug 2023 00:14:19 +0200 Subject: [PATCH] feat: correct elliptical orbital motion --- src/main.cpp | 6 +++--- src/orbit.cpp | 21 ++++++++++++++++----- src/orbit.hpp | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0b6a7b5..da9e8ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -51,8 +51,8 @@ int main() GLuint litProgram = compileShaderProgram("./frag_lit.glsl"); GLuint unlitProgram = compileShaderProgram("./frag_unlit.glsl"); - Icosphere planet(0.3, 3, litProgram); - Icosphere orbiter(0.1, 2, litProgram); + Icosphere planet(0.2, 3, litProgram); + Icosphere orbiter(0.07, 2, litProgram); Orbit orbit(100); // Main loop @@ -63,7 +63,7 @@ int main() float time = glfwGetTime(); - glm::vec3 pos = orbit.getPosition(time * .3); + glm::vec3 pos = orbit.getPosition(time); orbiter.setPosition(pos); // Render lit objects diff --git a/src/orbit.cpp b/src/orbit.cpp index ab2075a..db4dff9 100644 --- a/src/orbit.cpp +++ b/src/orbit.cpp @@ -7,14 +7,14 @@ Orbit::Orbit(int vertexCount) : _keplerianElements(std::vector(6)) { _keplerianElements[astro::semiMajorAxisIndex] = .75; - _keplerianElements[astro::eccentricityIndex] = .3; + _keplerianElements[astro::eccentricityIndex] = .5; _keplerianElements[astro::inclinationIndex] = _pi / 2.0 + 1; _keplerianElements[astro::argumentOfPeriapsisIndex] = 2.0; _keplerianElements[astro::longitudeOfAscendingNodeIndex] = 0; for (int i = 0; i < vertexCount; i++) { - float t = (float)i / (float)vertexCount; + float t = (float)i / (float)vertexCount * 2.0 * _pi; glm::vec3 pos = getPosition(t); _vertices.push_back(pos.x); @@ -40,12 +40,23 @@ Orbit::Orbit(int vertexCount) : // Interpolate a position around the orbit. // t is in range 0..1 and wraps. -glm::vec3 Orbit::getPosition(float t) +glm::vec3 Orbit::getPosition(const float meanAnomaly) { - float a = t * 2.0 * _pi; + // Get eccentric anomaly from elliptical mean anomaly + const float eccentricity = _keplerianElements[astro::eccentricityIndex]; + float eccentricAnomaly = astro::convertEllipticalMeanAnomalyToEccentricAnomaly( + eccentricity, + meanAnomaly, + (float)10e-3, + 100); + + // Get true anomaly from eccentric anomaly + float trueAnomaly = astro::convertEccentricAnomalyToTrueAnomaly( + eccentricAnomaly, + eccentricity); std::vector kepler(_keplerianElements); - kepler[astro::trueAnomalyIndex] = a; + kepler[astro::trueAnomalyIndex] = trueAnomaly; std::vector cartesian = astro::convertKeplerianToCartesianElements(kepler, 1.0); return glm::vec3( diff --git a/src/orbit.hpp b/src/orbit.hpp index 5a79f21..ddebd52 100644 --- a/src/orbit.hpp +++ b/src/orbit.hpp @@ -11,7 +11,7 @@ public: Orbit(int vertexCount); void render(); - glm::vec3 getPosition(float t); + glm::vec3 getPosition(const float meanAnomaly); ~Orbit(); private: