Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
orbit.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2023 Conquer Space
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <math.h>
20
21#include <entt/entt.hpp>
22#include <glm/glm.hpp>
23
27
32typedef glm::dvec3 Vec3AU;
33
34double GetOrbitingRadius(const double& e, const double& a, const double& v);
35
39struct Orbit {
45 double eccentricity = 0;
46
53
60
67
73 radian w = 0;
74
81
82 double epoch = 0;
83
91 radian v = 0;
92
100 double GM = SunMu;
101
102 // So we can prepare for moons and stuff
103 entt::entity reference_body = entt::null;
104
105 Orbit() = default;
106
107 // TODO(EhWhoAmI): Mean anomaly is not the true anomaly, we need to convert
108 // mean anomaly to true anomaly
113 LAN(LAN),
114 w(w),
115 M0(M0),
116 v(M0),
117 epoch(0) {}
118
119 Orbit(const Orbit& orbit)
120 : eccentricity(orbit.eccentricity),
123 LAN(orbit.LAN),
124 w(orbit.w),
125 M0(orbit.M0),
126 v(orbit.M0),
127 epoch(orbit.epoch),
128 GM(orbit.GM),
130
131 double GetMtElliptic(double time) const { return normalize_radian(M0 + (time - epoch) * nu()); }
132
134
135 double GetOrbitingRadius(const double& v) const {
137 }
138
139 // Orbital period
140 double T() const { return 2 * PI * sqrt(semi_major_axis * semi_major_axis * semi_major_axis / GM); }
141
142 // Mean motion
143 double nu() const { return sqrt(GM / abs(semi_major_axis * semi_major_axis * semi_major_axis)); }
144
145 double GetApoapsis() const { return semi_major_axis * (1 + eccentricity); }
146
147 double GetPeriapsis() const { return semi_major_axis * (1 - eccentricity); }
148
149 double TimeToTrueAnomaly(double v2) const;
150};
151
154};
155
164glm::dvec3 ConvertOrbParams(const double LAN, const double i, const double w, const glm::dvec3& vec);
165
166glm::dvec3 ConvertToOrbitalVector(const double LAN, const double i, const double w, const double v,
167 const glm::dvec3& vec);
168
169glm::dvec3 MatrixConvertOrbParams(const double LAN, const double i, const double w, const glm::dvec3& vec);
170
177void UpdateOrbit(Orbit& orb, const second& time);
178
184double GetTrueAnomaly(const Orbit& orb, const second& epoch);
185
193glm::dvec3 CalculateVelocity(const double& E, const kilometer& r, const double& GM, const kilometer& a,
194 const double& e);
195
196glm::dvec3 CalculateVelocityElliptic(const double& E, const kilometer& r, const double& GM, const kilometer& a,
197 const double& e);
198
199glm::dvec3 CalculateVelocityHyperbolic(const double& E, const double& r, const double& GM, const double& a,
200 const double& e);
201
202double GetOrbitingRadius(const double& e, const kilometer& a, const radian& v);
203
205double GetCircularOrbitingVelocity(const double& GM, const double& radius);
206
212double GetTrueAnomalyToAsymptope(const Orbit& orbit);
213
221Orbit Vec3ToOrbit(const glm::dvec3& position, const glm::dvec3& velocity, const double& GM, const double& time);
222
233glm::dvec3 OrbitToVec3(const double& a, const double& e, const radian& i, const radian& LAN, const radian& w,
234 const radian& v);
235
236double OrbitVelocity(const double v, const double e, const double a, const double GM);
237double OrbitVelocityAtR(const double GM, const double a, const double r);
238double AvgOrbitalVelocity(const Orbit& orb);
239
240glm::dvec3 OrbitVelocityToVec3(const Orbit& orb, double v);
241glm::dvec3 OrbitVelocityToVec3(const Orbit& orb);
242
251double SolveKeplerElliptic(const double& mean_anomaly, const double& ecc, const int steps = 200);
252
261double SolveKeplerHyperbolic(const double& mean_anomaly, const double& ecc, const int steps = 200);
262
268double EccentricAnomalyToTrueAnomaly(const double& ecc, const double& E);
269
270double HyperbolicAnomalyToTrueAnomaly(const double& ecc, const double& H);
271
279double GetMtElliptic(const double& M0, const double& nu, const double& time, const double& epoch);
280
288double GetMtHyperbolic(const double& M0, const double& nu, const double& time, const double& epoch);
289
294radian TrueAnomalyElliptic(const Orbit& orbit, const second& time);
295radian TrueAnomalyElliptic(const Orbit& orbit, const second& time, double& E_out);
296
297radian TrueAnomalyHyperbolic(const Orbit& orbit, const second& time);
298
305radian EccentricAnomaly(double v, double e);
306radian HyperbolicAnomaly(double v, double e);
313inline Vec3AU toVec3AU(const Orbit& orb, radian theta) {
314 glm::dvec3 vec = OrbitToVec3(orb.semi_major_axis, orb.eccentricity, orb.inclination, orb.LAN, orb.w, theta);
315 return vec / KmInAu;
316}
317
328Orbit ApplyImpulse(const Orbit& orbit, const glm::dvec3& impulse, double time);
329
336inline glm::dvec3 toVec3(const Orbit& orb, radian theta) {
337 return OrbitToVec3(orb.semi_major_axis, orb.eccentricity, orb.inclination, orb.LAN, orb.w, theta);
338}
339
340glm::dvec3 OrbitTimeToVec3(const Orbit& orb, const second& time);
341
342inline glm::dvec3 toVec3(const Orbit& orb) { return toVec3(orb, orb.v); }
348inline Vec3AU toVec3AU(const Orbit& orb) { return toVec3AU(orb, orb.v); }
349
355inline void UpdatePos(Kinematics& kin, const Orbit& orb) {
356 // Calculate time
357 kin.position = toVec3AU(orb);
358}
359
360double CalculateTransferTime(const Orbit& orb1, const Orbit& orb2);
361double CalculateTransferAngle(const Orbit& start_orbit, const Orbit& end_orbit);
362double CalculatePhaseAngle(const Orbit& start_orbit, const Orbit& end_orbit, double epoch);
363
364// https://orbital-mechanics.space/the-orbit-equation/hyperbolic-trajectories.html
365// True anomaly of the asymptope for a hyperbolic orbit
366// The hyperbolic asymtope is defined by -GetHyperbolicAsymtope < v <GetHyperbolicAsymtope
367double GetHyperbolicAsymptopeAnomaly(double eccentricity);
368
369double FlightPathAngle(double eccentricity, double v);
370
371glm::dvec3 GetOrbitNormal(const Orbit& orbit);
372
373double TrueAnomalyFromVector(const Orbit& orbit, const glm::dvec3& vec);
374
375double AscendingTrueAnomaly(const Orbit& start, const Orbit& dest);
376} // namespace cqsp::common::components::types
Definition: coordinates.cpp:22
double TrueAnomalyFromVector(const Orbit &orbit, const glm::dvec3 &vec)
Definition: orbit.cpp:365
double GetTrueAnomalyToAsymptope(const Orbit &orbit)
Calculates v_inf, the true anomaly for the asymtope for a hyperbolic orbit The orbit is bouded within...
Definition: orbit.cpp:135
double CalculateTransferAngle(const Orbit &start_orbit, const Orbit &end_orbit)
Definition: orbit.cpp:338
glm::dvec3 CalculateVelocityHyperbolic(const double &E, const double &r, const double &GM, const double &a, const double &e)
Definition: orbit.cpp:291
glm::dvec3 OrbitTimeToVec3(const Orbit &orb, const second &time)
Definition: orbit.cpp:318
radian HyperbolicAnomaly(double v, double e)
Definition: orbit.cpp:262
glm::dvec3 ConvertOrbParams(const double LAN, const double i, const double w, const glm::dvec3 &vec)
Transforms a vector to the orbital plane vector
Definition: orbit.cpp:44
double GetOrbitingRadius(const double &e, const double &a, const double &v)
Definition: orbit.cpp:28
glm::dvec3 ConvertToOrbitalVector(const double LAN, const double i, const double w, const double v, const glm::dvec3 &vec)
Definition: orbit.cpp:49
glm::dvec3 GetOrbitNormal(const Orbit &orbit)
Definition: orbit.cpp:360
double GetHyperbolicAsymptopeAnomaly(double eccentricity)
Definition: orbit.cpp:354
glm::dvec3 CalculateVelocityElliptic(const double &E, const double &r, const double &GM, const double &a, const double &e)
Definition: orbit.cpp:296
double second
Definition: units.h:42
double GetMtHyperbolic(const double &M0, const double &nu, const double &time, const double &epoch)
Calculate mean anomaly from time for a hyperbolic object
Definition: orbit.cpp:229
Orbit ApplyImpulse(const Orbit &orbit, const glm::dvec3 &impulse, double time)
Applies impulse based on the vector impulse For some reason y is prograde, I'm not sure what the othe...
Definition: orbit.cpp:301
glm::vec3 toVec3(const SurfaceCoordinate &coord, const float &radius)
Converts surface coordinate to vector3 in space so that we can get the surface coordinate to render i...
Definition: coordinates.cpp:33
glm::dvec3 OrbitToVec3(const double &a, const double &e, const radian &i, const radian &LAN, const radian &w, const radian &v)
Converts an orbit to a vec3.
Definition: orbit.cpp:138
constexpr double KmInAu
Definition: units.h:49
double AscendingTrueAnomaly(const Orbit &start, const Orbit &dest)
Definition: orbit.cpp:382
constexpr double SunMu
Definition: units.h:57
double GetMtElliptic(const double &M0, const double &nu, const double &time, const double &epoch)
Gets the Mean anomaly from the time
Definition: orbit.cpp:223
double kilometer
Definition: units.h:34
glm::dvec3 MatrixConvertOrbParams(const double LAN, const double i, const double w, const glm::dvec3 &vec)
Definition: orbit.cpp:36
double radian
Definition: units.h:37
double SolveKeplerHyperbolic(const double &mean_anomaly, const double &ecc, const int steps)
Computes eccentric anomaly for a hyperbolic or parabolic orbit (e > 1) in radians given mean anomaly ...
Definition: orbit.cpp:193
radian EccentricAnomaly(double v, double e)
Calculates Eccentric anomaly
Definition: orbit.cpp:260
double OrbitVelocity(const double v, const double e, const double a, const double GM)
Definition: orbit.cpp:151
constexpr radian normalize_radian(const radian &radian)
Normalizes a radian to [0, PI*2)
Definition: units.h:70
double SolveKeplerElliptic(const double &mean_anomaly, const double &ecc, const int steps)
Computes eccentric anomaly for a elliptic or circular orbit (e < 1) in radians given mean anomaly and...
Definition: orbit.cpp:173
glm::dvec3 CalculateVelocity(const double &E, const double &r, const double &GM, const double &a, const double &e)
Definition: orbit.cpp:283
double GetTrueAnomaly(const Orbit &orb, const second &epoch)
Get anomaly at epoch
Definition: orbit.cpp:273
constexpr double PI
Definition: units.h:45
double CalculateTransferTime(const Orbit &orb1, const Orbit &orb2)
Definition: orbit.cpp:329
radian TrueAnomalyHyperbolic(const Orbit &orbit, const second &time)
Definition: orbit.cpp:249
void UpdateOrbit(Orbit &orb, const second &time)
Updates the orbit's true anomaly.
Definition: orbit.cpp:264
double GetCircularOrbitingVelocity(const double &GM, const double &radius)
Get the circular orbiting velocity for the radius.
Definition: orbit.cpp:247
double HyperbolicAnomalyToTrueAnomaly(const double &ecc, const double &H)
Definition: orbit.cpp:217
Orbit Vec3ToOrbit(const glm::dvec3 &position, const glm::dvec3 &velocity, const double &GM, const double &time)
Converts position and velocity to orbit. Note: you will have to set the reference body after the orbi...
Definition: orbit.cpp:56
double AvgOrbitalVelocity(const Orbit &orb)
Definition: orbit.cpp:158
double FlightPathAngle(double eccentricity, double v)
Definition: orbit.cpp:356
glm::dvec3 Vec3AU
A vector3 where the units are astronomical units
Definition: orbit.h:32
double CalculatePhaseAngle(const Orbit &start_orbit, const Orbit &end_orbit, double epoch)
Definition: orbit.cpp:347
Vec3AU toVec3AU(const Orbit &orb, radian theta)
Convert orbit to AU coordinates
Definition: orbit.h:313
radian TrueAnomalyElliptic(const Orbit &orbit, const second &time)
Definition: orbit.cpp:234
double EccentricAnomalyToTrueAnomaly(const double &ecc, const double &E)
Calculates true anomaly from eccentricity and eccentric anomaly
Definition: orbit.cpp:213
glm::dvec3 OrbitVelocityToVec3(const Orbit &orb, double v)
Definition: orbit.cpp:160
void UpdatePos(Kinematics &kin, const Orbit &orb)
Updates the position of an orbit, in AU.
Definition: orbit.h:355
double OrbitVelocityAtR(const double GM, const double a, const double r)
Definition: orbit.cpp:316
Relative position from the parent orbiting object.
Definition: coordinates.h:32
glm::dvec3 position
Definition: coordinates.h:33
double eccentricity
Definition: orbit.h:45
double GetOrbitingRadius(const double &v) const
Definition: orbit.h:135
radian M0
Definition: orbit.h:80
double GM
Gravitational constant of the reference body this is orbiting Graviational constant * mass of orbitin...
Definition: orbit.h:100
radian LAN
Definition: orbit.h:66
kilometer semi_major_axis
Definition: orbit.h:52
Orbit(kilometer semi_major_axis, double eccentricity, radian inclination, radian LAN, radian w, radian M0)
Definition: orbit.h:109
double GetOrbitingRadius() const
Definition: orbit.h:133
double nu() const
Definition: orbit.h:143
double GetApoapsis() const
Definition: orbit.h:145
Orbit(const Orbit &orbit)
Definition: orbit.h:119
radian inclination
Definition: orbit.h:59
entt::entity reference_body
Definition: orbit.h:103
double GetPeriapsis() const
Definition: orbit.h:147
double epoch
Definition: orbit.h:82
double GetMtElliptic(double time) const
Definition: orbit.h:131
radian v
True anomaly v Radians
Definition: orbit.h:91
double T() const
Definition: orbit.h:140
radian w
Definition: orbit.h:73
double TimeToTrueAnomaly(double v2) const
Definition: orbit.cpp:387
double true_anomaly
Definition: orbit.h:153