]> git.localhorst.tv Git - blobs.git/blob - src/world/Body.hpp
6f85efd97b4946db46f224ef143e9b91ea1ec1b5
[blobs.git] / src / world / Body.hpp
1 #ifndef BLOBS_WORLD_BODY_HPP_
2 #define BLOBS_WORLD_BODY_HPP_
3
4 #include "Orbit.hpp"
5 #include "../math/geometry.hpp"
6 #include "../math/glm.hpp"
7
8 #include <string>
9 #include <vector>
10
11
12 namespace blobs {
13 namespace app {
14         class Assets;
15 }
16 namespace creature {
17         class Creature;
18 }
19 namespace graphics {
20         class Viewport;
21 }
22 namespace world {
23
24 class Simulation;
25
26 class Body {
27
28 public:
29         Body();
30         ~Body();
31
32         Body(const Body &) = delete;
33         Body &operator =(const Body &) = delete;
34
35         Body(Body &&) = delete;
36         Body &operator =(Body &&) = delete;
37
38 public:
39         bool HasSimulation() const noexcept { return sim; }
40         Simulation &GetSimulation() noexcept { return *sim; }
41         const Simulation &GetSimulation() const noexcept { return *sim; }
42         void SetSimulation(Simulation &) noexcept;
43
44         bool HasParent() const { return parent; }
45         Body &Parent() { return *parent; }
46         const Body &Parent() const { return *parent; }
47         void SetParent(Body &);
48         void UnsetParent();
49
50         const std::vector<Body *> &Children() const noexcept { return children; }
51
52         const std::string &Name() const noexcept { return name; }
53         void Name(const std::string &n) noexcept { name = n; }
54
55         double Mass() const noexcept { return mass; }
56         void Mass(double m) noexcept { mass = m; }
57
58         double Radius() const noexcept { return radius; }
59         void Radius(double r) noexcept { radius = r; }
60
61         Orbit &GetOrbit() noexcept { return orbit; }
62         const Orbit &GetOrbit() const noexcept { return orbit; }
63
64         const glm::dvec2 &SurfaceTilt() const noexcept { return surface_tilt; }
65         void SurfaceTilt(const glm::dvec2 &t) noexcept { surface_tilt = t; }
66
67         const glm::dvec2 &AxialTilt() const noexcept { return axis_tilt; }
68         void AxialTilt(const glm::dvec2 &t) noexcept { axis_tilt = t; }
69
70         double Rotation() const noexcept { return rotation; }
71         void Rotation(double r) noexcept { rotation = r; }
72
73         double AngularMomentum() const noexcept { return angular; }
74         void AngularMomentum(double m) noexcept { angular = m; }
75
76         double Inertia() const noexcept;
77
78         double GravitationalParameter() const noexcept;
79         double OrbitalPeriod() const noexcept;
80         double RotationalPeriod() const noexcept;
81         /// day length relative to parent, not neccessarily a sun
82         /// gives absolute value in seconds
83         /// returns sidereal day for parent-less bodies
84         double DayLength() const noexcept;
85         double SphereOfInfluence() const noexcept;
86
87         math::Sphere CollisionBounds() const noexcept { return math::Sphere{ glm::dvec3(0.0), Radius() }; }
88         const glm::dmat4 &CollisionTransform() const noexcept { return local; }
89
90         const glm::dmat4 &LocalTransform() const noexcept { return local; }
91         const glm::dmat4 &InverseTransform() const noexcept { return inverse_local; }
92
93         const glm::dmat4 &ToParent() const noexcept { return inverse_orbital; }
94         const glm::dmat4 &FromParent() const noexcept { return orbital; }
95
96         glm::dmat4 ToUniverse() const noexcept;
97         glm::dmat4 FromUniverse() const noexcept;
98
99         virtual void Draw(app::Assets &, graphics::Viewport &) { }
100
101         void Tick(double dt);
102         void Cache() noexcept;
103         void CheckCollision() noexcept;
104
105         void AddCreature(creature::Creature *);
106         void RemoveCreature(creature::Creature *);
107         std::vector<creature::Creature *> &Creatures() noexcept { return creatures; }
108         const std::vector<creature::Creature *> &Creatures() const noexcept { return creatures; }
109
110         void Atmosphere(int a) noexcept { atmosphere = a; }
111         int Atmosphere() const noexcept { return atmosphere; }
112         bool HasAtmosphere() const noexcept { return atmosphere >= 0; }
113
114 private:
115         void AddChild(Body &);
116         void RemoveChild(Body &);
117
118 private:
119         Simulation *sim;
120         Body *parent;
121         std::vector<Body *> children;
122         std::string name;
123         double mass;
124         double radius;
125         Orbit orbit;
126         glm::dvec2 surface_tilt;
127         glm::dvec2 axis_tilt;
128         double rotation;
129         double angular;
130
131         glm::dmat4 orbital;
132         glm::dmat4 inverse_orbital;
133         glm::dmat4 local;
134         glm::dmat4 inverse_local;
135
136         std::vector<creature::Creature *> creatures;
137         int atmosphere;
138
139 };
140
141 }
142 }
143
144 #endif