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-2025 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 <ostream>
22
23#include <entt/entt.hpp>
24#include <glm/vec3.hpp>
25
29
34typedef glm::dvec3 Vec3AU;
35
36double GetOrbitingRadius(const double& e, const double& a, const double& v);
37
41struct Orbit {
47 double eccentricity = 0;
48
55
62
69
75 radian w = 0;
76
83
84 double epoch = 0;
85
93 radian v = 0;
94
102 double GM = SunMu;
103
104 // So we can prepare for moons and stuff
105 entt::entity reference_body = entt::null;
106
107 Orbit() = default;
108
109 // TODO(EhWhoAmI): Mean anomaly is not the true anomaly, we need to convert
110 // mean anomaly to true anomaly
115 LAN(LAN),
116 w(w),
117 M0(M0),
118 v(M0),
119 epoch(0) {}
120
121 Orbit(const Orbit& orbit)
122 : eccentricity(orbit.eccentricity),
125 LAN(orbit.LAN),
126 w(orbit.w),
127 M0(orbit.M0),
128 v(orbit.M0),
129 epoch(orbit.epoch),
130 GM(orbit.GM),
132
133 double GetMtElliptic(double time) const { return normalize_radian(M0 + (time - epoch) * nu()); }
134
136
137 double GetOrbitingRadius(const double& v) const {
139 }
140
141 // Orbital period
142 double T() const { return 2 * PI * sqrt(semi_major_axis * semi_major_axis * semi_major_axis / GM); }
143
144 // Mean motion
145 double nu() const { return sqrt(GM / abs(semi_major_axis * semi_major_axis * semi_major_axis)); }
146
147 double GetApoapsis() const { return semi_major_axis * (1 + eccentricity); }
148
149 double GetPeriapsis() const { return semi_major_axis * (1 - eccentricity); }
150
151 double TimeToTrueAnomaly(double v2) const;
152};
153
154inline std::ostream& operator<<(std::ostream& outs, const Orbit& orb) {
155 return outs << "(" << orb.semi_major_axis << ", " << orb.eccentricity << ", " << orb.inclination << ", " << orb.LAN
156 << ", " << orb.w << ", " << orb.GM << ", " << orb.v << ", " << orb.epoch << ", "
157 << (uint32_t)orb.reference_body << ")";
158}
159
162};
163
172glm::dvec3 ConvertOrbParams(const double LAN, const double i, const double w, const glm::dvec3& vec);
173
174glm::dvec3 ConvertToOrbitalVector(const double LAN, const double i, const double w, const double v,
175 const glm::dvec3& vec);
176
177glm::dvec3 MatrixConvertOrbParams(const double LAN, const double i, const double w, const glm::dvec3& vec);
178
185void UpdateOrbit(Orbit& orb, const second& time);
186
192double GetTrueAnomaly(const Orbit& orb, const second& epoch);
193
201glm::dvec3 CalculateVelocity(const double& E, const kilometer& r, const double& GM, const kilometer& a,
202 const double& e);
203
204glm::dvec3 CalculateVelocityElliptic(const double& E, const kilometer& r, const double& GM, const kilometer& a,
205 const double& e);
206
207glm::dvec3 CalculateVelocityHyperbolic(const double& E, const double& r, const double& GM, const double& a,
208 const double& e);
209
210double GetOrbitingRadius(const double& e, const kilometer& a, const radian& v);
211
213double GetCircularOrbitingVelocity(const double& GM, const double& radius);
214
220double GetTrueAnomalyToAsymptope(const Orbit& orbit);
221
229Orbit Vec3ToOrbit(const glm::dvec3& position, const glm::dvec3& velocity, const double& GM, const double& time);
230
241glm::dvec3 OrbitToVec3(const double& a, const double& e, const radian& i, const radian& LAN, const radian& w,
242 const radian& v);
243
244double OrbitVelocity(const double v, const double e, const double a, const double GM);
245double OrbitVelocityAtR(const double GM, const double a, const double r);
246double AvgOrbitalVelocity(const Orbit& orb);
247
248glm::dvec3 OrbitVelocityToVec3(const Orbit& orb, double v);
249glm::dvec3 OrbitVelocityToVec3(const Orbit& orb);
250
259double SolveKeplerElliptic(const double& mean_anomaly, const double& ecc, const int steps = 200);
260
269double SolveKeplerHyperbolic(const double& mean_anomaly, const double& ecc, const int steps = 200);
270
276double EccentricAnomalyToTrueAnomaly(const double& ecc, const double& E);
277
278double HyperbolicAnomalyToTrueAnomaly(const double& ecc, const double& H);
279
287double GetMtElliptic(const double& M0, const double& nu, const double& time, const double& epoch);
288
296double GetMtHyperbolic(const double& M0, const double& nu, const double& time, const double& epoch);
297
302radian TrueAnomalyElliptic(const Orbit& orbit, const second& time);
303radian TrueAnomalyElliptic(const Orbit& orbit, const second& time, double& E_out);
304
305radian TrueAnomalyHyperbolic(const Orbit& orbit, const second& time);
306
313radian EccentricAnomaly(double v, double e);
314radian HyperbolicAnomaly(double v, double e);
321inline Vec3AU toVec3AU(const Orbit& orb, radian theta) {
322 glm::dvec3 vec = OrbitToVec3(orb.semi_major_axis, orb.eccentricity, orb.inclination, orb.LAN, orb.w, theta);
323 return vec / KmInAu;
324}
325
336Orbit ApplyImpulse(const Orbit& orbit, const glm::dvec3& impulse, double time);
337
344inline glm::dvec3 toVec3(const Orbit& orb, radian theta) {
345 return OrbitToVec3(orb.semi_major_axis, orb.eccentricity, orb.inclination, orb.LAN, orb.w, theta);
346}
347
348glm::dvec3 OrbitTimeToVec3(const Orbit& orb, const second time = 0);
349glm::dvec3 OrbitTimeToVelocityVec3(const Orbit& orb, const second time = 0);
350
351inline glm::dvec3 toVec3(const Orbit& orb) { return toVec3(orb, orb.v); }
357inline Vec3AU toVec3AU(const Orbit& orb) { return toVec3AU(orb, orb.v); }
358
364inline void UpdatePos(Kinematics& kin, const Orbit& orb) {
365 // Calculate time
366 kin.position = toVec3AU(orb);
367}
368
369double CalculateTransferTime(const Orbit& orb1, const Orbit& orb2);
370double CalculateTransferAngle(const Orbit& start_orbit, const Orbit& end_orbit);
371double CalculatePhaseAngle(const Orbit& start_orbit, const Orbit& end_orbit, double epoch);
372
373// https://orbital-mechanics.space/the-orbit-equation/hyperbolic-trajectories.html
374// True anomaly of the asymptope for a hyperbolic orbit
375// The hyperbolic asymtope is defined by -GetHyperbolicAsymtope < v <GetHyperbolicAsymtope
376double GetHyperbolicAsymptopeAnomaly(double eccentricity);
377
378double FlightPathAngle(double eccentricity, double v);
379
380glm::dvec3 GetOrbitNormal(const Orbit& orbit);
381
382double TrueAnomalyFromVector(const Orbit& orbit, const glm::dvec3& vec);
383
384double AscendingTrueAnomaly(const Orbit& start, const Orbit& dest);
385} // namespace cqsp::common::components::types
Definition: coordinates.cpp:22
double TrueAnomalyFromVector(const Orbit &orbit, const glm::dvec3 &vec)
Definition: orbit.cpp:375
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:348
glm::dvec3 CalculateVelocityHyperbolic(const double &E, const double &r, const double &GM, const double &a, const double &e)
Definition: orbit.cpp:292
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
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:370
double GetHyperbolicAsymptopeAnomaly(double eccentricity)
Definition: orbit.cpp:364
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:302
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
std::ostream & operator<<(std::ostream &outs, const Orbit &orb)
Definition: orbit.h:154
constexpr double KmInAu
Definition: units.h:49
double AscendingTrueAnomaly(const Orbit &start, const Orbit &dest)
Definition: orbit.cpp:392
glm::dvec3 OrbitTimeToVec3(const Orbit &orb, const second time)
Definition: orbit.cpp:319
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
glm::dvec3 OrbitTimeToVelocityVec3(const Orbit &orb, const second time)
Definition: orbit.cpp:329
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
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:339
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
glm::dvec3 CalculateVelocity(const double &E, const kilometer &r, const double &GM, const kilometer &a, const double &e)
Definition: orbit.cpp:283
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:366
double GetOrbitingRadius(const double &e, const radian &a, const radian &v)
Definition: orbit.cpp:28
glm::dvec3 Vec3AU
A vector3 where the units are astronomical units
Definition: orbit.h:34
double CalculatePhaseAngle(const Orbit &start_orbit, const Orbit &end_orbit, double epoch)
Definition: orbit.cpp:357
Vec3AU toVec3AU(const Orbit &orb, radian theta)
Convert orbit to AU coordinates
Definition: orbit.h:321
glm::dvec3 CalculateVelocityElliptic(const double &E, const kilometer &r, const double &GM, const kilometer &a, const double &e)
Definition: orbit.cpp:297
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:364
double OrbitVelocityAtR(const double GM, const double a, const double r)
Definition: orbit.cpp:317
Relative position from the parent orbiting object.
Definition: coordinates.h:32
glm::dvec3 position
Definition: coordinates.h:33
double eccentricity
Definition: orbit.h:47
double GetOrbitingRadius(const double &v) const
Definition: orbit.h:137
radian M0
Definition: orbit.h:82
double GM
Gravitational constant of the reference body this is orbiting Graviational constant * mass of orbitin...
Definition: orbit.h:102
radian LAN
Definition: orbit.h:68
kilometer semi_major_axis
Definition: orbit.h:54
Orbit(kilometer semi_major_axis, double eccentricity, radian inclination, radian LAN, radian w, radian M0)
Definition: orbit.h:111
double GetOrbitingRadius() const
Definition: orbit.h:135
double nu() const
Definition: orbit.h:145
double GetApoapsis() const
Definition: orbit.h:147
Orbit(const Orbit &orbit)
Definition: orbit.h:121
radian inclination
Definition: orbit.h:61
entt::entity reference_body
Definition: orbit.h:105
double GetPeriapsis() const
Definition: orbit.h:149
double epoch
Definition: orbit.h:84
double GetMtElliptic(double time) const
Definition: orbit.h:133
radian v
True anomaly v Radians
Definition: orbit.h:93
double T() const
Definition: orbit.h:142
radian w
Definition: orbit.h:75
double TimeToTrueAnomaly(double v2) const
Definition: orbit.cpp:397
double true_anomaly
Definition: orbit.h:161