feat: position orbiter along orbit

chore: move pi to header
This commit is contained in:
ktyl 2023-08-14 01:46:29 +02:00
parent ad51be518b
commit 8391c0c23e
3 changed files with 36 additions and 25 deletions

View File

@ -62,11 +62,8 @@ int main()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float time = glfwGetTime(); float time = glfwGetTime();
float r = .7;
float x = cos(time); glm::vec3 pos = orbit.getPosition(time);
float y = sin(time);
glm::vec3 pos(x, 0, y);
pos *= r;
orbiter.setPosition(pos); orbiter.setPosition(pos);
// Render lit objects // Render lit objects

View File

@ -3,31 +3,23 @@
#include "astro/stateVectorIndices.hpp" #include "astro/stateVectorIndices.hpp"
#include "astro/orbitalElementConversions.hpp" #include "astro/orbitalElementConversions.hpp"
Orbit::Orbit(int vertexCount) Orbit::Orbit(int vertexCount) :
_keplerianElements(std::vector<float>(6))
{ {
const float pi = 3.14159265359; _keplerianElements[astro::semiMajorAxisIndex] = .75;
_keplerianElements[astro::eccentricityIndex] = .1;
std::vector<float> keplerianElements(6); _keplerianElements[astro::inclinationIndex] = _pi / 2.0 + 0.1;
keplerianElements[astro::semiMajorAxisIndex] = .75; _keplerianElements[astro::argumentOfPeriapsisIndex] = 0;
keplerianElements[astro::eccentricityIndex] = .1; _keplerianElements[astro::longitudeOfAscendingNodeIndex] = 0;
keplerianElements[astro::inclinationIndex] = pi / 2.0 + 0.1;
keplerianElements[astro::argumentOfPeriapsisIndex] = 0;
keplerianElements[astro::longitudeOfAscendingNodeIndex] = 0;
for (int i = 0; i < vertexCount; i++) for (int i = 0; i < vertexCount; i++)
{ {
float a = (float)i / (float)vertexCount; float t = (float)i / (float)vertexCount;
a *= 2.0 * pi; glm::vec3 pos = getPosition(t);
keplerianElements[astro::trueAnomalyIndex] = a; _vertices.push_back(pos.x);
_vertices.push_back(pos.y);
std::vector<float> cartesian = astro::convertKeplerianToCartesianElements( _vertices.push_back(pos.z);
keplerianElements,
1.0);
_vertices.push_back(cartesian[astro::xPositionIndex]);
_vertices.push_back(cartesian[astro::yPositionIndex]);
_vertices.push_back(cartesian[astro::zPositionIndex]);
} }
glGenVertexArrays(1, &_vao); glGenVertexArrays(1, &_vao);
@ -46,6 +38,22 @@ Orbit::Orbit(int vertexCount)
glBindVertexArray(0); 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<float> kepler(_keplerianElements);
kepler[astro::trueAnomalyIndex] = a;
std::vector<float> cartesian = astro::convertKeplerianToCartesianElements(kepler, 1.0);
return glm::vec3(
cartesian[astro::xPositionIndex],
cartesian[astro::yPositionIndex],
cartesian[astro::zPositionIndex]);
}
void Orbit::render() void Orbit::render()
{ {
glBindVertexArray(_vao); glBindVertexArray(_vao);

View File

@ -10,10 +10,16 @@ class Orbit
public: public:
Orbit(int vertexCount); Orbit(int vertexCount);
void render(); void render();
glm::vec3 getPosition(float t);
~Orbit(); ~Orbit();
private: private:
const float _pi = 3.14159265359;
GLuint _vbo; GLuint _vbo;
GLuint _vao; GLuint _vao;
std::vector<float> _vertices; std::vector<float> _vertices;
std::vector<float> _keplerianElements;
}; };