From 8391c0c23e0c2a25411169f21c717bbfef3985bb Mon Sep 17 00:00:00 2001 From: ktyl Date: Mon, 14 Aug 2023 01:46:29 +0200 Subject: [PATCH] feat: position orbiter along orbit chore: move pi to header --- src/main.cpp | 7 ++----- src/orbit.cpp | 48 ++++++++++++++++++++++++++++-------------------- src/orbit.hpp | 6 ++++++ 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1334c61..9b035d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,11 +62,8 @@ int main() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); float time = glfwGetTime(); - float r = .7; - float x = cos(time); - float y = sin(time); - glm::vec3 pos(x, 0, y); - pos *= r; + + glm::vec3 pos = orbit.getPosition(time); orbiter.setPosition(pos); // Render lit objects diff --git a/src/orbit.cpp b/src/orbit.cpp index 1ed4758..316f581 100644 --- a/src/orbit.cpp +++ b/src/orbit.cpp @@ -3,31 +3,23 @@ #include "astro/stateVectorIndices.hpp" #include "astro/orbitalElementConversions.hpp" -Orbit::Orbit(int vertexCount) +Orbit::Orbit(int vertexCount) : + _keplerianElements(std::vector(6)) { - const float pi = 3.14159265359; - - std::vector keplerianElements(6); - keplerianElements[astro::semiMajorAxisIndex] = .75; - keplerianElements[astro::eccentricityIndex] = .1; - keplerianElements[astro::inclinationIndex] = pi / 2.0 + 0.1; - keplerianElements[astro::argumentOfPeriapsisIndex] = 0; - keplerianElements[astro::longitudeOfAscendingNodeIndex] = 0; + _keplerianElements[astro::semiMajorAxisIndex] = .75; + _keplerianElements[astro::eccentricityIndex] = .1; + _keplerianElements[astro::inclinationIndex] = _pi / 2.0 + 0.1; + _keplerianElements[astro::argumentOfPeriapsisIndex] = 0; + _keplerianElements[astro::longitudeOfAscendingNodeIndex] = 0; for (int i = 0; i < vertexCount; i++) { - float a = (float)i / (float)vertexCount; - a *= 2.0 * pi; + float t = (float)i / (float)vertexCount; + glm::vec3 pos = getPosition(t); - keplerianElements[astro::trueAnomalyIndex] = a; - - std::vector cartesian = astro::convertKeplerianToCartesianElements( - keplerianElements, - 1.0); - - _vertices.push_back(cartesian[astro::xPositionIndex]); - _vertices.push_back(cartesian[astro::yPositionIndex]); - _vertices.push_back(cartesian[astro::zPositionIndex]); + _vertices.push_back(pos.x); + _vertices.push_back(pos.y); + _vertices.push_back(pos.z); } glGenVertexArrays(1, &_vao); @@ -46,6 +38,22 @@ Orbit::Orbit(int vertexCount) glBindVertexArray(0); } +// Interpolate a position around the orbit. +// t is in range 0..1 and wraps. +glm::vec3 Orbit::getPosition(float t) +{ + float a = t * 2.0 * _pi; + + std::vector kepler(_keplerianElements); + kepler[astro::trueAnomalyIndex] = a; + + std::vector cartesian = astro::convertKeplerianToCartesianElements(kepler, 1.0); + return glm::vec3( + cartesian[astro::xPositionIndex], + cartesian[astro::yPositionIndex], + cartesian[astro::zPositionIndex]); +} + void Orbit::render() { glBindVertexArray(_vao); diff --git a/src/orbit.hpp b/src/orbit.hpp index a9773e9..5a79f21 100644 --- a/src/orbit.hpp +++ b/src/orbit.hpp @@ -10,10 +10,16 @@ class Orbit public: Orbit(int vertexCount); void render(); + + glm::vec3 getPosition(float t); + ~Orbit(); private: + const float _pi = 3.14159265359; + GLuint _vbo; GLuint _vao; std::vector _vertices; + std::vector _keplerianElements; };