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);
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

View File

@ -3,31 +3,23 @@
#include "astro/stateVectorIndices.hpp"
#include "astro/orbitalElementConversions.hpp"
Orbit::Orbit(int vertexCount)
Orbit::Orbit(int vertexCount) :
_keplerianElements(std::vector<float>(6))
{
const float pi = 3.14159265359;
std::vector<float> 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<float> 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<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()
{
glBindVertexArray(_vao);

View File

@ -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<float> _vertices;
std::vector<float> _keplerianElements;
};