Pioneer
Body.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 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 "DeleteEmitter.h"
8 #include "FrameId.h"
9 #include "lua/PropertiedObject.h"
10 #include "matrix3x3.h"
11 #include "vector3.h"
12 #include <string>
13 
14 class Space;
15 class Camera;
16 class Frame;
17 class SystemBody;
18 
19 namespace Graphics {
20  class Renderer;
21 }
22 struct CollisionContact;
23 
24 // ObjectType is used as a form of RTTI for Body and its children.
25 // Think carefully before adding more entries; we'd like to switch
26 // to a composition-based system instead.
27 enum class ObjectType { // <enum name=PhysicsObjectType scope='ObjectType' public>
28  // only creating enum strings for types that are exposed to Lua
29  BODY,
30  MODELBODY,
31  DYNAMICBODY, // <enum skip>
32  SHIP,
33  PLAYER,
35  TERRAINBODY, // <enum skip>
36  PLANET,
37  STAR,
38  CARGOBODY,
39  PROJECTILE, // <enum skip>
40  MISSILE,
41  HYPERSPACECLOUD // <enum skip>
42 };
43 
44 #define OBJDEF(__thisClass, __parentClass, __TYPE) \
45  virtual ObjectType GetType() const override { return ObjectType::__TYPE; } \
46  virtual bool IsType(ObjectType c) const override \
47  { \
48  if (__thisClass::GetType() == (c)) \
49  return true; \
50  else \
51  return __parentClass::IsType(c); \
52  }
53 
54 class Body : public DeleteEmitter, public PropertiedObject {
55 public:
56  virtual ObjectType GetType() const { return ObjectType::BODY; }
57  virtual bool IsType(ObjectType c) const { return GetType() == c; }
58 
59  Body();
60  Body(const Json &jsonObj, Space *space);
61  virtual ~Body();
62  void ToJson(Json &jsonObj, Space *space);
63  static Body *FromJson(const Json &jsonObj, Space *space);
64  virtual void PostLoadFixup(Space *space){};
65 
66  virtual void SetPosition(const vector3d &p) { m_pos = p; }
67  vector3d GetPosition() const { return m_pos; }
68  virtual void SetOrient(const matrix3x3d &r) { m_orient = r; }
69  const matrix3x3d &GetOrient() const { return m_orient; }
70  virtual void SetVelocity(const vector3d &v) { assert(0); }
71  virtual vector3d GetVelocity() const { return vector3d(0.0); }
72 
73  void SetPhysRadius(double r) { m_physRadius = r; }
74  double GetPhysRadius() const { return m_physRadius; }
75  void SetClipRadius(double r) { m_clipRadius = r; }
76  double GetClipRadius() const { return m_clipRadius; }
77  virtual double GetMass() const
78  {
79  assert(0);
80  return 0;
81  }
82 
83  // return true if to do collision response and apply damage
84  virtual bool OnCollision(Body *o, Uint32 flags, double relVel) { return false; }
85  // Attacker may be null
86  virtual bool OnDamage(Body *attacker, float kgDamage, const CollisionContact &contactData) { return false; }
87  // Override to clear any pointers you hold to the body
88  virtual void NotifyRemoved(const Body *const removedBody) {}
89 
90  // before all bodies have had TimeStepUpdate (their moving step),
91  // StaticUpdate() is called. Good for special collision testing (Projectiles)
92  // as you can't test for collisions if different objects are on different 'steps'
93  virtual void StaticUpdate(const float timeStep) {}
94  virtual void TimeStepUpdate(const float timeStep) {}
95  virtual void Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform) = 0;
96 
97  virtual void SetFrame(FrameId f) { m_frame = f; }
98  FrameId GetFrame() const { return m_frame; }
99  void SwitchToFrame(FrameId newFrame);
100  void UpdateFrame(); // check for frame switching
101 
102  vector3d GetVelocityRelTo(const Body *) const;
105  vector3d GetPositionRelTo(const Body *) const;
107 
108  // Should return pointer in Pi::currentSystem
109  virtual const SystemBody *GetSystemBody() const { return nullptr; }
110  // for putting on planet surface, oriented +y up
111  void OrientOnSurface(double radius, double latitude, double longitude);
112 
113  virtual void SetLabel(const std::string &label);
114  const std::string &GetLabel() const { return m_label; }
115 
116  unsigned int GetFlags() const { return m_flags; }
117  // TODO(sturnclaw) use this sparingly, the flags interface is rather fragile and needs work
118  void SetFlag(unsigned int flag, bool enable)
119  {
120  if (enable)
121  m_flags |= flag;
122  else
123  m_flags &= ~flag;
124  }
125 
126  // Only Space::KillBody() should call this method.
127  void MarkDead() { m_dead = true; }
128  bool IsDead() const { return m_dead; }
129 
130  // all Bodies are in space... except where they're not (Ships hidden in hyperspace clouds)
131  virtual bool IsInSpace() const { return true; }
132 
133  // Interpolated between physics ticks.
134  const matrix3x3d &GetInterpOrient() const { return m_interpOrient; }
136  vector3d GetInterpPositionRelTo(FrameId relToId) const;
137  vector3d GetInterpPositionRelTo(const Body *relTo) const;
138  matrix3x3d GetInterpOrientRelTo(FrameId relToId) const;
139 
140  // should set m_interpolatedTransform to the smoothly interpolated value
141  // (interpolated by 0 <= alpha <=1) between the previous and current physics tick
142  virtual void UpdateInterpTransform(double alpha)
143  {
146  }
147 
148  // TODO: abstract this functionality into a component of some fashion
149  // Return the position in body-local coordinates where the target indicator should be displayed.
150  // Usually equal to the center of the body == vector3d(0, 0, 0)
151  virtual vector3d GetTargetIndicatorPosition() const;
152 
153  enum {
155  FLAG_LABEL_HIDDEN = (1 << 1),
156  FLAG_DRAW_LAST = (1 << 2), // causes the body drawn after other bodies in the z-sort
157  FLAG_DRAW_EXCLUDE = (1 << 3) // do not draw this body, intended for e.g. when camera is inside
158  };
159 
160 protected:
161  virtual void SaveToJson(Json &jsonObj, Space *space);
162  unsigned int m_flags;
163 
164  // Interpolated draw orientation-position
167 
168 private:
169  vector3d m_pos;
170  matrix3x3d m_orient;
171  FrameId m_frame; // frame of reference
172  std::string m_label;
173  bool m_dead; // Checked in destructor to make sure body has been marked dead.
174  double m_clipRadius;
175  double m_physRadius;
176 };
177 
178 #endif /* _BODY_H */
ObjectType
Definition: Body.h:27
nlohmann::json Json
Definition: Json.h:8
Definition: Body.h:54
void OrientOnSurface(double radius, double latitude, double longitude)
Definition: Body.cpp:201
virtual vector3d GetTargetIndicatorPosition() const
Definition: Body.cpp:254
vector3d GetPositionRelTo(FrameId) const
Definition: Body.cpp:142
virtual void PostLoadFixup(Space *space)
Definition: Body.h:64
const matrix3x3d & GetOrient() const
Definition: Body.h:69
virtual void TimeStepUpdate(const float timeStep)
Definition: Body.h:94
void SetPhysRadius(double r)
Definition: Body.h:73
virtual void SetOrient(const matrix3x3d &r)
Definition: Body.h:68
vector3d GetInterpPositionRelTo(FrameId relToId) const
Definition: Body.cpp:151
double GetPhysRadius() const
Definition: Body.h:74
virtual vector3d GetVelocity() const
Definition: Body.h:71
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:88
virtual void SetVelocity(const vector3d &v)
Definition: Body.h:70
virtual void SetPosition(const vector3d &p)
Definition: Body.h:66
vector3d GetPosition() const
Definition: Body.h:67
vector3d GetVelocityRelTo(const Body *) const
Definition: Body.cpp:196
virtual void SaveToJson(Json &jsonObj, Space *space)
Definition: Body.cpp:64
void SwitchToFrame(FrameId newFrame)
Definition: Body.cpp:210
virtual bool OnDamage(Body *attacker, float kgDamage, const CollisionContact &contactData)
Definition: Body.h:86
virtual double GetMass() const
Definition: Body.h:77
vector3d m_interpPos
Definition: Body.h:165
virtual bool IsInSpace() const
Definition: Body.h:131
virtual bool OnCollision(Body *o, Uint32 flags, double relVel)
Definition: Body.h:84
matrix3x3d GetInterpOrientRelTo(FrameId relToId) const
Definition: Body.cpp:178
Body()
Definition: Body.cpp:20
vector3d GetInterpPosition() const
Definition: Body.h:135
void ToJson(Json &jsonObj, Space *space)
Definition: Body.cpp:81
void UpdateFrame()
Definition: Body.cpp:226
const std::string & GetLabel() const
Definition: Body.h:114
virtual void StaticUpdate(const float timeStep)
Definition: Body.h:93
virtual void UpdateInterpTransform(double alpha)
Definition: Body.h:142
void SetFlag(unsigned int flag, bool enable)
Definition: Body.h:118
static Body * FromJson(const Json &jsonObj, Space *space)
Definition: Body.cpp:102
void MarkDead()
Definition: Body.h:127
const matrix3x3d & GetInterpOrient() const
Definition: Body.h:134
virtual ~Body()
Definition: Body.cpp:60
virtual void SetLabel(const std::string &label)
Definition: Body.cpp:259
unsigned int m_flags
Definition: Body.h:162
matrix3x3d GetOrientRelTo(FrameId) const
Definition: Body.cpp:170
virtual const SystemBody * GetSystemBody() const
Definition: Body.h:109
@ FLAG_CAN_MOVE_FRAME
Definition: Body.h:154
@ FLAG_LABEL_HIDDEN
Definition: Body.h:155
@ FLAG_DRAW_EXCLUDE
Definition: Body.h:157
@ FLAG_DRAW_LAST
Definition: Body.h:156
void SetClipRadius(double r)
Definition: Body.h:75
double GetClipRadius() const
Definition: Body.h:76
bool IsDead() const
Definition: Body.h:128
unsigned int GetFlags() const
Definition: Body.h:116
matrix3x3d m_interpOrient
Definition: Body.h:166
virtual ObjectType GetType() const
Definition: Body.h:56
virtual void SetFrame(FrameId f)
Definition: Body.h:97
FrameId GetFrame() const
Definition: Body.h:98
virtual bool IsType(ObjectType c) const
Definition: Body.h:57
Definition: Camera.h:76
Definition: DeleteEmitter.h:16
Definition: Frame.h:28
Definition: Renderer.h:39
Definition: PropertiedObject.h:11
Definition: Space.h:19
Definition: SystemBody.h:19
Definition: Background.h:13
Definition: CollisionContact.h:9
Definition: FrameId.h:9
vector3< double > vector3d
Definition: vector3.h:286