TDME2  1.9.200
CollisionResponse.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <tdme/tdme.h>
8 #include <tdme/math/fwd-tdme.h>
9 #include <tdme/math/Math.h>
10 #include <tdme/math/Vector3.h>
12 #include <tdme/utilities/Console.h>
13 
14 using std::vector;
15 
18 using tdme::math::Math;
21 
22 /**
23  * Collision response
24  * @author Andreas Drewke
25  */
27 {
29  friend class CollisionDetection;
30  friend class World;
31 
32 private:
33  static constexpr int32_t HITPOINT_COUNT { 30 };
34  vector<CollisionResponse_Entity> entities;
36  vector<Vector3> fallbackHitPointsVector;
37 
38 public:
39  /**
40  * Public constructor
41  */
42  inline CollisionResponse() {
43  }
44 
45  /**
46  * Reset
47  */
48  inline void reset() {
49  entities.clear();
50  selectedEntity = nullptr;
51  }
52 
53  /**
54  * Adds a collision response entity
55  * @param distance distance
56  * @return Entity or null
57  */
58  inline CollisionResponse_Entity* addResponse(float distance) {
60  auto& entity = entities[entities.size() - 1];
61  entity.distance = distance;
62  // select entity with smallest penetration by default
63  if (selectedEntity == nullptr || distance > selectedEntity->distance) {
64  selectedEntity = &entity;
65  }
66  return &entity;
67  }
68 
69  /**
70  * @return entity count
71  */
72  inline int32_t getEntityCount() {
73  return entities.size();
74  }
75 
76  /**
77  * @return selected entity
78  */
80  return selectedEntity;
81  }
82 
83  /**
84  * Selects entity at given index
85  * @param idx idx
86  * @return
87  */
88  inline CollisionResponse_Entity* getEntity(int32_t idx) {
89  if (idx < 0 || idx >= entities.size()) return nullptr;
90  return &entities[idx];
91  }
92 
93  /**
94  * Selects entity at given index
95  * @param idx idx
96  * @return
97  */
98  inline CollisionResponse* selectEntity(int32_t idx) {
99  if (idx < 0 || idx >= entities.size()) return this;
100  selectedEntity = &entities[idx];
101  return this;
102  }
103 
104  /**
105  * @return if collision entity is selected
106  */
107  inline bool hasEntitySelected() {
108  return selectedEntity != nullptr;
109  }
110 
111  /**
112  * @return collision distance or negative penetration
113  */
114  inline float getDistance() {
115  if (selectedEntity == nullptr) return 0.0f;
116  return selectedEntity->distance;
117  }
118 
119  /**
120  * @return if we have a penetration
121  */
122  inline bool hasPenetration() {
123  if (selectedEntity == nullptr) return false;
124  return selectedEntity->distance < -Math::EPSILON;
125  }
126 
127  /**
128  * @return penetration
129  */
130  inline float getPenetration() {
131  if (selectedEntity == nullptr) return 0.0f;
132  return -selectedEntity->distance;
133  }
134 
135  /**
136  * @return normal
137  */
138  inline const Vector3* getNormal() {
139  if (selectedEntity == nullptr) return nullptr;
140  return &selectedEntity->normal;
141  }
142 
143  /**
144  * @return get hit points
145  */
146  inline const vector<Vector3>& getHitPoints() {
147  if (selectedEntity == nullptr) return fallbackHitPointsVector;
148  return selectedEntity->hitPoints;
149  }
150 
151  /**
152  * @return hit point count
153  */
154  inline int32_t getHitPointCount() {
155  if (selectedEntity == nullptr) return 0;
156  return selectedEntity->hitPoints.size();
157  }
158 
159  /**
160  * Get hit point of given index
161  * @param i i
162  * @return hit point for given hit points index
163  */
164  inline Vector3* getHitPoint(int32_t idx) {
165  if (selectedEntity == nullptr) return nullptr;
166  if (idx < 0 || idx >= selectedEntity->hitPoints.size()) return nullptr;
167  return &selectedEntity->hitPoints[idx];
168  }
169 
170 };
Vector3 * getHitPoint(int32_t idx)
Get hit point of given index.
CollisionResponse * selectEntity(int32_t idx)
Selects entity at given index.
CollisionResponse_Entity * selectedEntity
CollisionResponse_Entity * getEntity(int32_t idx)
Selects entity at given index.
const vector< Vector3 > & getHitPoints()
vector< CollisionResponse_Entity > entities
CollisionResponse_Entity * addResponse(float distance)
Adds a collision response entity.
CollisionResponse_Entity * getSelectedEntity()
Dynamic physics world class.
Definition: World.h:38
Standard math functions.
Definition: Math.h:19
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Console class.
Definition: Console.h:29