TDME2  1.9.200
BodyHierarchy.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <vector>
6 
7 #include <reactphysics3d/collision/Collider.h>
8 
9 #include <tdme/tdme.h>
12 #include <tdme/engine/Transform.h>
13 
14 using std::string;
15 using std::unordered_map;
16 using std::vector;
17 
21 
22 /**
23  * Body hierarchy
24  * @author Andreas Drewke
25  */
27 {
28  friend class World;
29 
30 protected:
31 
32  // forbid class copy
34 
35  /**
36  * Protected constructor
37  * @param world world
38  * @param id id
39  * @param type type
40  * @param collisionTypeId collision type id
41  * @param enabled enabled
42  * @param transform transform
43  * @param restitution restitution
44  * @param friction friction
45  * @param mass mass in kg
46  * @param inertiaTensor inertia tensor vector
47  * @param boundingVolumes bounding volumes
48  */
49  BodyHierarchy(World* world, const string& id, BodyType type, uint16_t collisionTypeId, bool enabled, const Transform& transform, float restitution, float friction, float mass, const Vector3& inertiaTensor);
50 
51  /**
52  * Destructor
53  */
55 
56  /**
57  * Reset colliders
58  */
59  void resetColliders() override;
60 
61 private:
64  string id;
65  BodyHierarchyLevel* parent { nullptr };
67  vector<BoundingVolume*> boundingVolumes;
68  vector<reactphysics3d::Collider*> colliders;
69  unordered_map<string, BodyHierarchyLevel*> children;
70  };
72  vector<SubBody*> bodies;
73  BodyHierarchyLevel bodyRoot { string(), nullptr, Transform(), {} };
74 
75  /**
76  * Get body hierarchy level by given body id
77  * @param id id
78  * @return body hierarchy level
79  *
80  */
81  inline BodyHierarchyLevel* getBodyHierarchyLevel(const string& id) {
82  if (id.empty()) return &bodyRoot;
83  return getBodyHierarchyLevel(&bodyRoot, id);
84  }
85 
86  /**
87  * Retrieve body hierarchy level by given body id or nullptr if not found
88  * @param bodyHierarchyLevel body hierarchy level
89  * @param id body id
90  * @return body hierarchy level by given body id or nullptr if not found
91  */
92  inline BodyHierarchyLevel* getBodyHierarchyLevel(BodyHierarchyLevel* bodyHierarchyLevel, const string& id) {
93  if (id == bodyHierarchyLevel->id) return bodyHierarchyLevel;
94  for (const auto& [childId, child]: bodyHierarchyLevel->children) {
95  auto childBodyHierarchyLevel = getBodyHierarchyLevel(child, id);
96  if (childBodyHierarchyLevel != nullptr) return childBodyHierarchyLevel;
97  }
98  return nullptr;
99  }
100 
101  /**
102  * Update hierarchy from given body hierarchy level ongoing
103  * @param parentTransform parent transform
104  * @param bodyHierarchyLevel body hierarchy level
105  * @param depth depth
106  */
107  void updateHierarchy(const Transform& parentTransform, BodyHierarchyLevel* bodyHierarchyLevel, int depth);
108 
109 public:
110 
111  /**
112  * @return body from hierarchy by given unique id
113  */
114  inline const SubBody* getBody(const string& id) {
115  auto bodyHierarchyLevel = getBodyHierarchyLevel(id);
116  if (bodyHierarchyLevel == nullptr || bodyHierarchyLevel->parent == nullptr) return nullptr;
117  return bodyHierarchyLevel;
118  }
119 
120  /**
121  * Adds a body to the hierarchy
122  * @param id unique body id in hierarchy
123  * @param transform transform
124  * @param boundingVolumes bounding volumes
125  * @param parentId parent body id to add body to
126  */
127  void addBody(const string& id, const Transform& transform, const vector<BoundingVolume*>& boundingVolumes, const string& parentId = string());
128 
129  /**
130  * Updates a body from hierarchy by given unique body id
131  * @param id unique body id in hierarchy
132  * @param transform transform
133  */
134  void updateBody(const string& id, const Transform& transform);
135 
136  /**
137  * Removes a body from hierarchy by given unique body id
138  * @param id unique body id in hierarchy
139  */
140  void removeBody(const string& id);
141 
142  /**
143  * Query sub bodies of parent body
144  * @param parentId parent body id
145  * @return bodies
146  */
147  const vector<SubBody*> query(const string& parentId = string());
148 
149  /**
150  * @return bodies
151  */
152  inline const vector<SubBody*>& getBodies() {
153  return bodies;
154  }
155 
156  /**
157  * Update body hierarchy
158  */
159  void update();
160 };
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
void updateBody(const string &id, const Transform &transform)
Updates a body from hierarchy by given unique body id.
BodyHierarchyLevel * getBodyHierarchyLevel(BodyHierarchyLevel *bodyHierarchyLevel, const string &id)
Retrieve body hierarchy level by given body id or nullptr if not found.
Definition: BodyHierarchy.h:92
const vector< SubBody * > & getBodies()
void removeBody(const string &id)
Removes a body from hierarchy by given unique body id.
const SubBody * getBody(const string &id)
void updateHierarchy(const Transform &parentTransform, BodyHierarchyLevel *bodyHierarchyLevel, int depth)
Update hierarchy from given body hierarchy level ongoing.
BodyHierarchyLevel * getBodyHierarchyLevel(const string &id)
Get body hierarchy level by given body id.
Definition: BodyHierarchy.h:81
BodyHierarchy(World *world, const string &id, BodyType type, uint16_t collisionTypeId, bool enabled, const Transform &transform, float restitution, float friction, float mass, const Vector3 &inertiaTensor)
Protected constructor.
void resetColliders() override
Reset colliders.
void update()
Update body hierarchy.
void addBody(const string &id, const Transform &transform, const vector< BoundingVolume * > &boundingVolumes, const string &parentId=string())
Adds a body to the hierarchy.
const vector< SubBody * > query(const string &parentId=string())
Query sub bodies of parent body.
Rigid body class.
Definition: Body.h:41
vector< BoundingVolume * > boundingVolumes
Definition: Body.h:92
uint16_t collisionTypeId
Definition: Body.h:88
Dynamic physics world class.
Definition: World.h:38
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
unordered_map< string, BodyHierarchyLevel * > children
Definition: BodyHierarchy.h:69
vector< reactphysics3d::Collider * > colliders
Definition: BodyHierarchy.h:68
BodyHierarchyLevel(const string &id, BodyHierarchyLevel *parent, const Transform &transform, const vector< BoundingVolume * > &boundingVolumes)
Definition: BodyHierarchy.h:63
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6