From 4b956ab8dd08229c1b5d66f877139014bda79743 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Wed, 25 May 2022 12:30:41 -0400 Subject: [PATCH] move Ellipse3D to game.h --- src/game.cpp | 39 ++++++++++++++++++++++++++++++++------- src/game.h | 23 ++++++++++++++++++++++- src/orbits.cpp | 28 ---------------------------- src/orbits.h | 16 ---------------- 4 files changed, 54 insertions(+), 52 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index a336995..0406286 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -24,12 +24,37 @@ getFreeOrbit(GameState* gs) } void -disableGameOrbit(GameOrbit* orbit) +disableGameOrbit(GameState* gs, GameOrbit* orbit) { - orbit->in_use = false; - orbit->selected = false; - orbit->system = {0}; - orbit->grav_body = nullptr; - orbit->ellipse_entity = nullptr; - orbit->satellite_entity = nullptr; + if (gs->last_selected_orbit == orbit) + gs->last_selected_orbit = nullptr; + + *orbit = {0}; +} + +Ellipse3D +ellipse3DInit(EllipseParameters ep, uint vert_count) +{ + assert(ep.a > 0 && ep.b > 0 && + ep.a >= ep.b && + vert_count > 0); + + Ellipse3D e3d = { nullptr, vert_count}; + // FIXME: should be allocated from GameState->arena + e3d.vertices = UTIL_ALLOC(vert_count, vec3); + ellipse3DUpdate(ep, e3d); + return e3d; +} + +void +ellipse3DUpdate(EllipseParameters ep, Ellipse3D& e3d) +{ + double angle = 2 * M_PI / e3d.vert_count; + + for (uint i = 0; i < e3d.vert_count; i++) { + double a = angle * i; + // NOTE: solving for distance in polar coordinates relative to focus + double r = ep.a * (1 - pow(ep.e, 2)) / (1 + ep.e * cos(a)); + e3d.vertices[i] = vec3(polarToRect(a, r), 0); + } } diff --git a/src/game.h b/src/game.h index 7f972ed..d473aed 100644 --- a/src/game.h +++ b/src/game.h @@ -1,12 +1,25 @@ #pragma once +#include +using glm::vec3; + #include "tangerine.h" #include "orbits.h" +#define DEFAULT_ORBIT_VERTICES 256 + + +struct Ellipse3D +{ + vec3* vertices; + uint vert_count; +}; + struct GameOrbit { TwoBodySystem system; + Ellipse3D e3d; Entity* grav_body; Entity* ellipse_entity; @@ -31,4 +44,12 @@ struct GameState GameOrbit* getFreeOrbit(GameState* gs); -void disableGameOrbit(GameOrbit* orbit); +void disableGameOrbit(GameState* gs, GameOrbit* orbit); + +// NOTE: create vertices for a 3d ellipse +// all vertices are in the x/y plane with z = 0 +Ellipse3D ellipse3DInit(EllipseParameters ep, uint vert_count); + +void ellipse3DUpdate(EllipseParameters ep, Ellipse3D& e3d); + +void updateOrbitColors(GameOrbit* last_selected_orbit, GameOrbit* orbit); diff --git a/src/orbits.cpp b/src/orbits.cpp index d68f0f4..d393e52 100644 --- a/src/orbits.cpp +++ b/src/orbits.cpp @@ -11,7 +11,6 @@ systemInit(TwoBodySystem& system, GravBody gb, OrbitalElements el) system.body = gb; system.elements = el; system.ep = ellipseInitAE(el.a, el.e); - system.e3d = ellipseInit3D(system.ep, ELLIPSE_VERT_COUNT); system.epsilon = orbitGetSpecificEnergy(system.ep.a, gb.mu); system.h = orbitGetAngularMomentum(system.ep.p, gb.mu); system.orbital_period = orbitGetPeriod(system.ep.a, gb.mu); @@ -49,20 +48,6 @@ ellipseInitAE(double a, double e) return ellipseInitAB(a, b); } -Ellipse3D -ellipseInit3D(EllipseParameters ep, uint vert_count) -{ - assert(ep.a > 0 && ep.b > 0 && - ep.a >= ep.b && - vert_count > 0); - - Ellipse3D e3d = { nullptr, vert_count}; - // TODO: need to free this allocation at some point - e3d.vertices = UTIL_ALLOC(vert_count, glm::vec3); - ellipse3DUpdate(ep, e3d); - return e3d; -} - OrbitalElements orbitInit(double a, double e) { @@ -74,19 +59,6 @@ orbitInit(double a, double e) return o; } -void -ellipse3DUpdate(EllipseParameters ep, Ellipse3D& e3d) -{ - double angle = 2 * M_PI / e3d.vert_count; - - for (uint i = 0; i < e3d.vert_count; i++) { - double a = angle * i; - // NOTE: solving for distance in polar coordinates relative to focus - double r = ep.a * (1 - pow(ep.e, 2)) / (1 + ep.e * cos(a)); - e3d.vertices[i] = glm::vec3(polarToRect(a, r), 0); - } -} - // // NOTE: propagate anomaly functions: diff --git a/src/orbits.h b/src/orbits.h index a3e9805..4b9c9e4 100644 --- a/src/orbits.h +++ b/src/orbits.h @@ -30,13 +30,6 @@ struct OrbitalElements double nu; // (ν) true anomaly at T0 }; -// FIXME: move to game.h -struct Ellipse3D -{ - glm::vec3* vertices; - uint vert_count; -}; - struct GravBody { double mu; // (μ) gravitational parameter @@ -60,7 +53,6 @@ struct TwoBodySystem GravBody body; Satellite sat; EllipseParameters ep; - Ellipse3D e3d; // FIXME: should be direct child of GameOrbit OrbitalElements elements; double epsilon; // (ε) specific orbital energy, MJ/kg @@ -109,14 +101,6 @@ ellipsesEqual(EllipseParameters& e1, EllipseParameters& e2) e1.e == e2.e); } -// NOTE: create vertices for a 3d ellipse -// all vertices are in the x/y plane with z = 0 -Ellipse3D -ellipseInit3D(EllipseParameters ep, uint vert_count); - -void -ellipse3DUpdate(EllipseParameters ep, Ellipse3D& e3d); - OrbitalElements orbitInit(double a, double e);