Pioneer
Loading...
Searching...
No Matches
Body.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#ifndef _BODY_H
5#define _BODY_H
6
7#include "BodyComponent.h"
8#include "DeleteEmitter.h"
9#include "FrameId.h"
11#include "matrix3x3.h"
12#include "matrix4x4.h"
13#include "vector3.h"
14#include <string>
15
16class Space;
17class Camera;
18class Frame;
19class SystemBody;
20
21namespace Graphics {
22 class Renderer;
23}
24struct CollisionContact;
25
26// ObjectType is used as a form of RTTI for Body and its children.
27// Think carefully before adding more entries; we'd like to switch
28// to a composition-based system instead.
29enum class ObjectType { // <enum name=PhysicsObjectType scope='ObjectType' public>
30 // only creating enum strings for types that are exposed to Lua
31 BODY,
33 DYNAMICBODY, // <enum skip>
34 SHIP,
35 PLAYER,
37 TERRAINBODY, // <enum skip>
38 PLANET,
39 STAR,
41 PROJECTILE, // <enum skip>
42 MISSILE,
43 HYPERSPACECLOUD // <enum skip>
44};
45
46enum class AltitudeType { // <enum name=AltitudeType scope='AltitudeType' public>
47 //SEA_LEVEL if distant, ABOVE_TERRAIN otherwise
48 DEFAULT,
51};
52
53#define OBJDEF(__thisClass, __parentClass, __TYPE) \
54 static constexpr ObjectType StaticType() { return ObjectType::__TYPE; } \
55 static constexpr ObjectType SuperType() { return __parentClass::StaticType(); } \
56 virtual ObjectType GetType() const override { return ObjectType::__TYPE; } \
57 virtual bool IsType(ObjectType c) const override \
58 { \
59 if (__thisClass::GetType() == (c)) \
60 return true; \
61 else \
62 return __parentClass::IsType(c); \
63 }
64
65class Body : public DeleteEmitter, public PropertiedObject {
66public:
67 static constexpr ObjectType StaticType() { return ObjectType::BODY; }
68 virtual ObjectType GetType() const { return ObjectType::BODY; }
69 virtual bool IsType(ObjectType c) const { return GetType() == c; }
70
71 Body();
72 Body(const Json &jsonObj, Space *space);
73 virtual ~Body();
74 void ToJson(Json &jsonObj, Space *space);
75 static Body *FromJson(const Json &jsonObj, Space *space);
76 virtual void PostLoadFixup(Space *space){};
77
78 virtual void SetPosition(const vector3d &p) { m_pos = p; }
79 vector3d GetPosition() const { return m_pos; }
80 virtual void SetOrient(const matrix3x3d &r) { m_orient = r; }
81 const matrix3x3d &GetOrient() const { return m_orient; }
82 virtual void SetVelocity(const vector3d &v) { assert(0); }
83 virtual vector3d GetVelocity() const { return vector3d(0.0); }
84 virtual void SetAngVelocity(const vector3d &v) { assert(0); }
85 virtual vector3d GetAngVelocity() const { return vector3d(0.0); }
86
87 void SetPhysRadius(double r) { m_physRadius = r; }
88 double GetPhysRadius() const { return m_physRadius; }
89 void SetClipRadius(double r) { m_clipRadius = r; }
90 double GetClipRadius() const { return m_clipRadius; }
91 virtual double GetMass() const
92 {
93 assert(0);
94 return 0;
95 }
96
97 // return true if to do collision response and apply damage
98 virtual bool OnCollision(Body *o, Uint32 flags, double relVel) { return false; }
99 // Attacker may be null
100 virtual bool OnDamage(Body *attacker, float kgDamage, const CollisionContact &contactData) { return false; }
101 // Override to clear any pointers you hold to the body
102 virtual void NotifyRemoved(const Body *const removedBody) {}
103
104 // before all bodies have had TimeStepUpdate (their moving step),
105 // StaticUpdate() is called. Good for special collision testing (Projectiles)
106 // as you can't test for collisions if different objects are on different 'steps'
107 virtual void StaticUpdate(const float timeStep) {}
108 virtual void TimeStepUpdate(const float timeStep) {}
109 virtual void Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform) = 0;
110
111 virtual void SetFrame(FrameId f) { m_frame = f; }
112 FrameId GetFrame() const { return m_frame; }
113 void SwitchToFrame(FrameId newFrame);
114 void UpdateFrame(); // check for frame switching
115
116 vector3d GetVelocityRelTo(const Body *) const;
119 vector3d GetPositionRelTo(const Body *) const;
121
122 // Should return pointer in Pi::currentSystem
123 virtual const SystemBody *GetSystemBody() const { return nullptr; }
124 // for putting on planet surface, oriented +y up
125 void OrientOnSurface(double radius, double latitude, double longitude);
126
127 virtual void SetLabel(const std::string &label);
128 const std::string &GetLabel() const { return m_label; }
129
130 unsigned int GetFlags() const { return m_flags; }
131 // TODO(sturnclaw) use this sparingly, the flags interface is rather fragile and needs work
132 void SetFlag(unsigned int flag, bool enable)
133 {
134 if (enable)
135 m_flags |= flag;
136 else
137 m_flags &= ~flag;
138 }
139
140 // Check if a specific component is present. This involves a lookup through std::map
141 // so it's not quite as efficient as it should be.
142 template <typename T>
143 bool HasComponent() const
144 {
145 return m_components & (uint64_t(1) << uint8_t(BodyComponentDB::GetComponentType<T>()->componentIndex));
146 }
147
148 // Return a pointer to the component of type T attached to this instance or nullptr.
149 // This returns a non-const pointer for simplicity as the component is technically
150 // not part of the object.
151 template <typename T>
152 T *GetComponent() const
153 {
154 auto *type = BodyComponentDB::GetComponentType<T>();
155 return m_components & (uint64_t(1) << uint8_t(type->componentIndex)) ? type->get(this) : nullptr;
156 }
157
158 template <typename T>
160 {
161 auto *type = BodyComponentDB::GetComponentType<T>();
162 if (m_components & (uint64_t(1) << uint8_t(type->componentIndex)))
163 return type->get(this);
164
165 m_components |= (uint64_t(1) << uint8_t(type->componentIndex));
166 return type->newComponent(this);
167 }
168
169 // Returns the bitset of components attached to this body. Prefer using HasComponent<> or GetComponent<> instead.
170 uint64_t GetComponentList() const { return m_components; }
171
172 // Only Space::KillBody() should call this method.
173 void MarkDead() { m_dead = true; }
174 bool IsDead() const { return m_dead; }
175
176 // all Bodies are in space... except where they're not (Ships hidden in hyperspace clouds)
177 virtual bool IsInSpace() const { return true; }
178
179 // Interpolated between physics ticks.
180 const matrix3x3d &GetInterpOrient() const { return m_interpOrient; }
183 vector3d GetInterpPositionRelTo(const Body *relTo) const;
185 double GetAltitudeRelTo(const Body* relTo, AltitudeType altType = AltitudeType::DEFAULT);
186
187 // should set m_interpolatedTransform to the smoothly interpolated value
188 // (interpolated by 0 <= alpha <=1) between the previous and current physics tick
189 virtual void UpdateInterpTransform(double alpha)
190 {
193 }
194
195 // TODO: abstract this functionality into a component of some fashion
196 // Return the position in body-local coordinates where the target indicator should be displayed.
197 // Usually equal to the center of the body == vector3d(0, 0, 0)
198 virtual vector3d GetTargetIndicatorPosition() const;
199
200 enum {
203 FLAG_DRAW_LAST = (1 << 2), // causes the body drawn after other bodies in the z-sort
204 FLAG_DRAW_EXCLUDE = (1 << 3) // do not draw this body, intended for e.g. when camera is inside
205 };
206
207private:
208 uint64_t m_components = 0;
209
210protected:
211 virtual void SaveToJson(Json &jsonObj, Space *space);
212 unsigned int m_flags = 0;
213
214 // Interpolated draw orientation-position
217
218private:
219 vector3d m_pos;
220 matrix3x3d m_orient;
221 FrameId m_frame; // frame of reference
222 std::string m_label;
223 bool m_dead; // Checked in destructor to make sure body has been marked dead.
224 double m_clipRadius;
225 double m_physRadius;
226};
227
228#endif /* _BODY_H */
ObjectType
Definition: Body.h:29
AltitudeType
Definition: Body.h:46
nlohmann::json Json
Definition: Json.h:8
Definition: Body.h:65
void OrientOnSurface(double radius, double latitude, double longitude)
Definition: Body.cpp:291
T * AddComponent()
Definition: Body.h:159
virtual vector3d GetTargetIndicatorPosition() const
Definition: Body.cpp:344
vector3d GetPositionRelTo(FrameId) const
Definition: Body.cpp:203
const matrix3x3d & GetOrient() const
Definition: Body.h:81
virtual void PostLoadFixup(Space *space)
Definition: Body.h:76
virtual void TimeStepUpdate(const float timeStep)
Definition: Body.h:108
void SetPhysRadius(double r)
Definition: Body.h:87
virtual void SetOrient(const matrix3x3d &r)
Definition: Body.h:80
vector3d GetInterpPositionRelTo(FrameId relToId) const
Definition: Body.cpp:212
double GetPhysRadius() const
Definition: Body.h:88
virtual vector3d GetVelocity() const
Definition: Body.h:83
virtual void Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform)=0
virtual void NotifyRemoved(const Body *const removedBody)
Definition: Body.h:102
static constexpr ObjectType StaticType()
Definition: Body.h:67
virtual void SetVelocity(const vector3d &v)
Definition: Body.h:82
virtual void SetPosition(const vector3d &p)
Definition: Body.h:78
vector3d GetPosition() const
Definition: Body.h:79
vector3d GetVelocityRelTo(const Body *) const
Definition: Body.cpp:257
virtual void SaveToJson(Json &jsonObj, Space *space)
Definition: Body.cpp:76
void SwitchToFrame(FrameId newFrame)
Definition: Body.cpp:300
virtual bool OnDamage(Body *attacker, float kgDamage, const CollisionContact &contactData)
Definition: Body.h:100
virtual double GetMass() const
Definition: Body.h:91
vector3d m_interpPos
Definition: Body.h:215
virtual bool IsInSpace() const
Definition: Body.h:177
T * GetComponent() const
Definition: Body.h:152
virtual bool OnCollision(Body *o, Uint32 flags, double relVel)
Definition: Body.h:98
matrix3x3d GetInterpOrientRelTo(FrameId relToId) const
Definition: Body.cpp:239
Body()
Definition: Body.cpp:23
vector3d GetInterpPosition() const
Definition: Body.h:181
void ToJson(Json &jsonObj, Space *space)
Definition: Body.cpp:115
const matrix3x3d & GetInterpOrient() const
Definition: Body.h:180
void UpdateFrame()
Definition: Body.cpp:316
uint64_t GetComponentList() const
Definition: Body.h:170
virtual void StaticUpdate(const float timeStep)
Definition: Body.h:107
double GetAltitudeRelTo(const Body *relTo, AltitudeType altType=AltitudeType::DEFAULT)
Definition: Body.cpp:262
virtual void UpdateInterpTransform(double alpha)
Definition: Body.h:189
void SetFlag(unsigned int flag, bool enable)
Definition: Body.h:132
static Body * FromJson(const Json &jsonObj, Space *space)
Definition: Body.cpp:136
void MarkDead()
Definition: Body.h:173
virtual void SetAngVelocity(const vector3d &v)
Definition: Body.h:84
bool HasComponent() const
Definition: Body.h:143
virtual ~Body()
Definition: Body.cpp:61
virtual void SetLabel(const std::string &label)
Definition: Body.cpp:349
unsigned int m_flags
Definition: Body.h:212
matrix3x3d GetOrientRelTo(FrameId) const
Definition: Body.cpp:231
virtual vector3d GetAngVelocity() const
Definition: Body.h:85
virtual const SystemBody * GetSystemBody() const
Definition: Body.h:123
@ FLAG_CAN_MOVE_FRAME
Definition: Body.h:201
@ FLAG_LABEL_HIDDEN
Definition: Body.h:202
@ FLAG_DRAW_EXCLUDE
Definition: Body.h:204
@ FLAG_DRAW_LAST
Definition: Body.h:203
void SetClipRadius(double r)
Definition: Body.h:89
double GetClipRadius() const
Definition: Body.h:90
bool IsDead() const
Definition: Body.h:174
unsigned int GetFlags() const
Definition: Body.h:130
matrix3x3d m_interpOrient
Definition: Body.h:216
virtual ObjectType GetType() const
Definition: Body.h:68
const std::string & GetLabel() const
Definition: Body.h:128
virtual void SetFrame(FrameId f)
Definition: Body.h:111
FrameId GetFrame() const
Definition: Body.h:112
virtual bool IsType(ObjectType c) const
Definition: Body.h:69
Definition: Camera.h:85
Definition: DeleteEmitter.h:16
Definition: Frame.h:28
Definition: Renderer.h:45
Definition: PropertiedObject.h:11
Definition: Space.h:19
Definition: SystemBody.h:137
Definition: Background.h:14
Definition: CollisionContact.h:9
Definition: FrameId.h:9
vector3< double > vector3d
Definition: vector3.h:329