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 double GetMtElliptic(double time) const { return normalize_radian(M0 + (time - epoch) * nu()); }
120
122
123 double GetOrbitingRadius(const double& v) const {
125 }
126
127 // Orbital period
128 double T() const { return 2 * PI * sqrt(semi_major_axis * semi_major_axis * semi_major_axis / GM); }
129
130 // Mean motion
131 double nu() const { return sqrt(GM / abs(semi_major_axis * semi_major_axis * semi_major_axis)); }
132
133 double GetApoapsis() const { return semi_major_axis * (1 + eccentricity); }
134
135 double GetPeriapsis() const { return semi_major_axis * (1 - eccentricity); }
136
137 double TimeToTrueAnomaly(double v2) const;
138};
139
142};
143
152glm::dvec3 ConvertOrbParams(const double LAN, const double i, const double w, const glm::dvec3& vec);
153
154glm::dvec3 ConvertToOrbitalVector(const double LAN, const double i, const double w, const double v,
155 const glm::dvec3& vec);
156
157glm::dvec3 MatrixConvertOrbParams(const double LAN, const double i, const double w, const glm::dvec3& vec);
158
165void UpdateOrbit(Orbit& orb, const second& time);
166
172double GetTrueAnomaly(const Orbit& orb, const second& epoch);
173
181glm::dvec3 CalculateVelocity(const double& E, const kilometer& r, const double& GM, const kilometer& a,
182 const double& e);
183
184glm::dvec3 CalculateVelocityElliptic(const double& E, const kilometer& r, const double& GM, const kilometer& a,
185 const double& e);
186
187glm::dvec3 CalculateVelocityHyperbolic(const double& E, const double& r, const double& GM, const double& a,
188 const double& e);
189
190double GetOrbitingRadius(const double& e, const kilometer& a, const radian& v);
191
193double GetCircularOrbitingVelocity(const double& GM, const double& radius);
194
200double GetTrueAnomalyToAsymptope(const Orbit& orbit);
201
209Orbit Vec3ToOrbit(const glm::dvec3& position, const glm::dvec3& velocity, const double& GM, const double& time);
210
221glm::dvec3 OrbitToVec3(const double& a, const double& e, const radian& i, const radian& LAN, const radian& w,
222 const radian& v);
223
224double OrbitVelocity(const double v, const double e, const double a, const double GM);
225double OrbitVelocityAtR(const double GM, const double a, const double r);
226double AvgOrbitalVelocity(const Orbit& orb);
227
228glm::dvec3 OrbitVelocityToVec3(const Orbit& orb, double v);
229glm::dvec3 OrbitVelocityToVec3(const Orbit& orb);
230
239double SolveKeplerElliptic(const double& mean_anomaly, const double& ecc, const int steps = 200);
240
249double SolveKeplerHyperbolic(const double& mean_anomaly, const double& ecc, const int steps = 200);
250
256double EccentricAnomalyToTrueAnomaly(const double& ecc, const double& E);
257
258double HyperbolicAnomalyToTrueAnomaly(const double& ecc, const double& H);
259
267double GetMtElliptic(const double& M0, const double& nu, const double& time, const double& epoch);
268
276double GetMtHyperbolic(const double& M0, const double& nu, const double& time, const double& epoch);
277
282radian TrueAnomalyElliptic(const Orbit& orbit, const second& time);
283radian TrueAnomalyElliptic(const Orbit& orbit, const second& time, double& E_out);
284
285radian TrueAnomalyHyperbolic(const Orbit& orbit, const second& time);
286
293radian EccentricAnomaly(double v, double e);
294radian HyperbolicAnomaly(double v, double e);
301inline Vec3AU toVec3AU(const Orbit& orb, radian theta) {
302 glm::dvec3 vec = OrbitToVec3(orb.semi_major_axis, orb.eccentricity, orb.inclination, orb.LAN, orb.w, theta);
303 return vec / KmInAu;
304}
305
316Orbit ApplyImpulse(const Orbit& orbit, const glm::dvec3& impulse, double time);
317
324inline glm::dvec3 toVec3(const Orbit& orb, radian theta) {
325 return OrbitToVec3(orb.semi_major_axis, orb.eccentricity, orb.inclination, orb.LAN, orb.w, theta);
326}
327
328glm::dvec3 OrbitTimeToVec3(const Orbit& orb, const second& time);
329
330inline glm::dvec3 toVec3(const Orbit& orb) { return toVec3(orb, orb.v); }
336inline Vec3AU toVec3AU(const Orbit& orb) { return toVec3AU(orb, orb.v); }
337
343inline void UpdatePos(Kinematics& kin, const Orbit& orb) {
344 // Calculate time
345 kin.position = toVec3AU(orb);
346}
347
348double CalculateTransferTime(const Orbit& orb1, const Orbit& orb2);
349double CalculateTransferAngle(const Orbit& start_orbit, const Orbit& end_orbit);
350double CalculatePhaseAngle(const Orbit& start_orbit, const Orbit& end_orbit, double epoch);
351
352// https://orbital-mechanics.space/the-orbit-equation/hyperbolic-trajectories.html
353// True anomaly of the asymptope for a hyperbolic orbit
354// The hyperbolic asymtope is defined by -GetHyperbolicAsymtope < v <GetHyperbolicAsymtope
355double GetHyperbolicAsymptopeAnomaly(double eccentricity);
356
357double FlightPathAngle(double eccentricity, double v);
358
359glm::dvec3 GetOrbitNormal(const Orbit& orbit);
360
361double TrueAnomalyFromVector(const Orbit& orbit, const glm::dvec3& vec);
362
363double AscendingTrueAnomaly(const Orbit& start, const Orbit& dest);
364} // 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:301
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:343
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:123
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:121
double nu() const
Definition: orbit.h:131
double GetApoapsis() const
Definition: orbit.h:133
radian inclination
Definition: orbit.h:59
entt::entity reference_body
Definition: orbit.h:103
double GetPeriapsis() const
Definition: orbit.h:135
double epoch
Definition: orbit.h:82
double GetMtElliptic(double time) const
Definition: orbit.h:119
radian v
True anomaly v Radians
Definition: orbit.h:91
double T() const
Definition: orbit.h:128
radian w
Definition: orbit.h:73
double TimeToTrueAnomaly(double v2) const
Definition: orbit.cpp:387
double true_anomaly
Definition: orbit.h:141