TDME2  1.9.200
Body.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <string>
5 #include <vector>
6 
7 #include <reactphysics3d/body/RigidBody.h>
8 #include <reactphysics3d/collision/Collider.h>
9 #include <reactphysics3d/collision/shapes/CollisionShape.h>
10 
11 #include <tdme/tdme.h>
12 #include <tdme/engine/fwd-tdme.h>
18 #include <tdme/engine/Transform.h>
19 #include <tdme/math/fwd-tdme.h>
20 #include <tdme/math/Matrix4x4.h>
21 #include <tdme/math/Vector3.h>
22 
23 using std::remove;
24 using std::string;
25 using std::vector;
26 
35 
36 /**
37  * Rigid body class
38  * @author Andreas Drewke
39  */
41 {
42  friend class World;
43 
44 public:
45  enum BodyType {
51  };
52 
53  static constexpr uint16_t COLLISION_TYPEID_STATIC { 1 };
54  static constexpr uint16_t COLLISION_TYPEID_DYNAMIC { 2 };
55  static constexpr uint16_t COLLISION_TYPEID_3 { 4 };
56  static constexpr uint16_t COLLISION_TYPEID_4 { 8 };
57  static constexpr uint16_t COLLISION_TYPEID_5 { 16 };
58  static constexpr uint16_t COLLISION_TYPEID_6 { 32 };
59  static constexpr uint16_t COLLISION_TYPEID_7 { 64 };
60  static constexpr uint16_t COLLISION_TYPEID_8 { 128 };
61  static constexpr uint16_t COLLISION_TYPEID_9 { 256 };
62  static constexpr uint16_t COLLISION_TYPEID_10 { 512 };
63  static constexpr uint16_t COLLISION_TYPEID_11 { 1024 };
64  static constexpr uint16_t COLLISION_TYPEID_12 { 2048 };
65  static constexpr uint16_t COLLISION_TYPEID_13 { 4096 };
66  static constexpr uint16_t COLLISION_TYPEID_14 { 8192 };
67  static constexpr uint16_t COLLISION_TYPEID_15 { 16384 };
68  static constexpr uint16_t COLLISION_TYPEID_16 { 32768 };
69  static constexpr uint16_t COLLISION_TYPEID_RESERVED { 32768 };
70  static constexpr uint16_t COLLISION_TYPEID_ALL { 65535 };
71 
72  /**
73  * @return no rotation inertia tensor
74  */
75  inline static const Vector3 getNoRotationInertiaTensor() {
76  return Vector3(0.0f, 0.0f, 0.0f);
77  }
78 
79 protected:
80  World* world { nullptr };
81  reactphysics3d::RigidBody* rigidBody { nullptr };
82  string id;
84  float restitution;
85  float friction;
86  float mass;
87  uint16_t collisionTypeIds;
88  uint16_t collisionTypeId;
92  vector<BoundingVolume*> boundingVolumes;
93  vector<reactphysics3d::Collider*> colliders;
94  vector<CollisionListener*> collisionListener;
95  bool initiation;
96 
97  // forbid class copy
99 
100  /**
101  * Protected constructor
102  * @param world world
103  * @param id id
104  * @param type type
105  * @param collisionTypeId collision type id
106  * @param enabled enabled
107  * @param transform transform
108  * @param restitution restitution
109  * @param friction friction
110  * @param mass mass in kg
111  * @param inertiaTensor inertia tensor vector
112  * @param boundingVolumes bounding volumes
113  */
114  Body(World* world, const string& id, BodyType type, uint16_t collisionTypeId, bool enabled, const Transform& transform, float restitution, float friction, float mass, const Vector3& inertiaTensor, const vector<BoundingVolume*>& boundingVolumes);
115 
116  /**
117  * Destructor
118  */
119  virtual ~Body();
120 
121  /**
122  * Remove colliders
123  * @param colliders colliders
124  * @param boundingVolumes bounding volumes
125  */
126  void removeColliders(vector<reactphysics3d::Collider*>& colliders, vector<BoundingVolume*>& boundingVolumes);
127 
128  /**
129  * Reset given colliders with given bounding volumes and local transform
130  * @param colliders colliders
131  * @param boundingVolumes bounding volumes
132  * @param transform transform
133  */
134  void resetColliders(vector<reactphysics3d::Collider*>& colliders, vector<BoundingVolume*>& boundingVolumes, const Transform& transform);
135 
136  /**
137  * Reset body colliders
138  */
139  virtual void resetColliders();
140 
141  /**
142  * Fire on collision
143  * @param other other
144  * @param collisionResponse collision response
145  */
146  void fireOnCollision(Body* other, CollisionResponse& collisionResponse);
147 
148  /**
149  * Fire on collision begin
150  * @param other other
151  * @param collisionResponse collision response
152  */
153  void fireOnCollisionBegin(Body* other, CollisionResponse& collisionResponse);
154 
155  /**
156  * Fire on collision end
157  * @param other other
158  */
159  void fireOnCollisionEnd(Body* other);
160 
161 public:
162 
163  /**
164  * @return id
165  */
166  inline const string& getId() {
167  return id;
168  }
169 
170  /**
171  * Return type, see TYPE_*
172  * @return type
173  */
174  inline BodyType getType() {
175  return type;
176  }
177 
178  /**
179  * @return type id
180  */
181  inline uint16_t getCollisionTypeId() {
182  return collisionTypeId;
183  }
184 
185  /**
186  * Set collision type id
187  * @param typeId type id
188  */
189  void setCollisionTypeId(uint16_t typeId);
190 
191  /**
192  * @return collision type ids bitmask
193  */
194  inline uint16_t getCollisionTypeIds() {
195  return collisionTypeIds;
196  }
197 
198  /**
199  * Set up collision type ids
200  * @param collisionTypeIds collisionTypeIds
201  */
202  void setCollisionTypeIds(uint16_t collisionTypeIds);
203 
204  /**
205  * @return if enabled
206  */
207  inline bool isEnabled() {
208  return rigidBody->isActive();
209  }
210 
211  /**
212  * Set up if rigid body is enabled
213  * @param enabled enabled
214  */
215  inline void setEnabled(bool enabled) {
216  rigidBody->setIsActive(enabled);
217  if (enabled == true) rigidBody->setIsSleeping(false);
218  }
219 
220  /**
221  * @return if sleeping
222  */
223  inline bool isSleeping() {
224  return rigidBody->isSleeping();
225  }
226 
227  /**
228  * Set sleeping
229  * @param sleeping sleeping
230  */
231  inline void setSleeping(bool sleeping) {
232  rigidBody->setIsSleeping(sleeping);
233  }
234 
235  /**
236  * @deprecated this method should be removed
237  * @return bounding volumes
238  */
239  inline vector<BoundingVolume*>& getBoundingVolumes() {
240  return boundingVolumes;
241  }
242 
243  /**
244  * Compute world bounding box
245  */
247  auto aabb = rigidBody->getAABB();
248  return BoundingBox(
249  Vector3(
250  aabb.getMin().x,
251  aabb.getMin().y,
252  aabb.getMin().z
253  ),
254  Vector3(
255  aabb.getMax().x,
256  aabb.getMax().y,
257  aabb.getMax().z
258  )
259  );
260  }
261 
262  /**
263  * @return friction
264  */
265  inline float getFriction() {
266  return friction;
267  }
268 
269  /**
270  * Set up friction
271  * @param friction friction
272  */
273  void setFriction(float friction);
274 
275  /**
276  * @return restitution / bouncyness
277  */
278  inline float getRestitution() {
279  return restitution;
280  }
281 
282  /**
283  * Set up restitution
284  * @param restitution restitution
285  */
286  void setRestitution(float restitution);
287 
288  /**
289  * @return mass
290  */
291  inline float getMass() {
292  return mass;
293  }
294 
295  /**
296  * Set up mass
297  * @param mass mass
298  */
299  inline void setMass(float mass) {
300  this->mass = mass;
301  rigidBody->setMass(mass);
302  }
303 
304  /**
305  * @return linear velocity
306  */
307  inline const Vector3 getLinearVelocity() {
308  return Vector3(
309  rigidBody->getLinearVelocity().x,
310  rigidBody->getLinearVelocity().y,
311  rigidBody->getLinearVelocity().z
312  );
313  }
314 
315  /**
316  * Set linear velocity
317  * @param linearVelocity velocity
318  */
319  inline void setLinearVelocity(const Vector3& linearVelocity) {
320  rigidBody->setLinearVelocity(reactphysics3d::Vector3(linearVelocity.getX(), linearVelocity.getY(), linearVelocity.getZ()));
321  }
322 
323  /**
324  * @return angular velocity
325  */
326  inline const Vector3 getAngularVelocity() {
327  return Vector3(
328  rigidBody->getAngularVelocity().x,
329  rigidBody->getAngularVelocity().y,
330  rigidBody->getAngularVelocity().z
331  );
332  }
333 
334  /**
335  * Set angular velocity
336  * @param angularVelocity angular velocity
337  */
338  inline void setAngularVelocity(const Vector3& angularVelocity) {
339  rigidBody->setAngularVelocity(reactphysics3d::Vector3(angularVelocity.getX(), angularVelocity.getY(), angularVelocity.getZ()));
340  }
341 
342  /**
343  * @return return linear damping
344  */
345  inline float getLinearDamping() {
346  return rigidBody->getLinearDamping();
347  }
348 
349  /**
350  * Set linear damping
351  * @param linearDamping linear damping
352  */
353  inline void setLinearDamping(float linearDamping) {
354  rigidBody->setLinearDamping(linearDamping);
355  }
356 
357  /**
358  * @return return angular damping
359  */
360  inline float getAngularDamping() {
361  return rigidBody->getAngularDamping();
362  }
363 
364  /**
365  * Set angular damping
366  * @param angularDamping anuglar damping
367  */
368  inline void setAngularDamping(float angularDamping) {
369  rigidBody->setAngularDamping(angularDamping);
370  }
371 
372  /**
373  * @return transform
374  */
375  inline const Transform& getTransform() {
376  return transform;
377  }
378 
379  /**
380  * Set transform
381  * @param transform transform
382  */
383  inline void setTransform(const Transform& transform) {
384  // store engine transform
385  this->transform = transform;
386 
387  // reset colliders if bounding volumes do not match proxy shapes or if scaling has changed
388  if (initiation == true || colliders.size() != boundingVolumes.size() || transformScale.equals(transform.getScale()) == false) {
389  initiation = false;
390  resetColliders();
392  }
393 
394  // set transform
395  rigidBody->setTransform(
396  reactphysics3d::Transform(
397  reactphysics3d::Vector3(
398  this->transform.getTranslation().getX(),
399  this->transform.getTranslation().getY(),
400  this->transform.getTranslation().getZ()
401  ),
402  reactphysics3d::Quaternion(
403  this->transform.getRotationsQuaternion().getX(),
404  this->transform.getRotationsQuaternion().getY(),
405  this->transform.getRotationsQuaternion().getZ(),
406  this->transform.getRotationsQuaternion().getW()
407  )
408  )
409  );
410  }
411 
412  /**
413  * Add force
414  * @param forceOrigin position of world force
415  * @param force force
416  */
417  inline void addForce(const Vector3& forceOrigin, const Vector3& force) {
418  rigidBody->applyWorldForceAtWorldPosition(
419  reactphysics3d::Vector3(force.getX(), force.getY(), force.getZ()),
420  reactphysics3d::Vector3(forceOrigin.getX(), forceOrigin.getY(), forceOrigin.getZ())
421  );
422  }
423 
424  /**
425  * Add force to center of mass
426  * @param force force
427  */
428  inline void addForce(const Vector3& force) {
429  rigidBody->applyWorldForceAtCenterOfMass(
430  reactphysics3d::Vector3(force.getX(), force.getY(), force.getZ())
431  );
432  }
433 
434  /**
435  * Add torque
436  * @param torque torque
437  */
438  inline void addTorque(const Vector3& torque) {
439  rigidBody->applyWorldTorque(
440  reactphysics3d::Vector3(torque.getX(), torque.getY(), torque.getZ())
441  );
442  }
443 
444  /**
445  * Set angular lock axis factor
446  * @param factor factor
447  */
448  inline void setAngularLockAxisFactor(const Vector3& factor) {
449  rigidBody->setAngularLockAxisFactor(reactphysics3d::Vector3(factor.getX(), factor.getY(), factor.getZ()));
450  }
451 
452  /**
453  * Set linear lock axis factor
454  * @param factor factor
455  */
456  inline void setLinearLockAxisFactor(const Vector3& factor) {
457  rigidBody->setLinearLockAxisFactor(reactphysics3d::Vector3(factor.getX(), factor.getY(), factor.getZ()));
458  }
459 
460  /**
461  * Add a collision listener to this rigid body
462  * @param listener listener
463  */
464  void addCollisionListener(CollisionListener* listener);
465 
466  /**
467  * Remove a collision listener to this rigid body
468  * @param listener listener
469  */
471 
472 };
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
const Vector3 & getScale() const
Definition: Transform.h:71
const Vector3 & getTranslation() const
Definition: Transform.h:56
Rigid body class.
Definition: Body.h:41
vector< BoundingVolume * > boundingVolumes
Definition: Body.h:92
BoundingBox computeWorldBoundingBox()
Compute world bounding box.
Definition: Body.h:246
void setTransform(const Transform &transform)
Set transform.
Definition: Body.h:383
uint16_t getCollisionTypeIds()
Definition: Body.h:194
vector< CollisionListener * > collisionListener
Definition: Body.h:94
void addForce(const Vector3 &force)
Add force to center of mass.
Definition: Body.h:428
void addTorque(const Vector3 &torque)
Add torque.
Definition: Body.h:438
static constexpr uint16_t COLLISION_TYPEID_RESERVED
Definition: Body.h:69
reactphysics3d::RigidBody * rigidBody
Definition: Body.h:81
void fireOnCollisionBegin(Body *other, CollisionResponse &collisionResponse)
Fire on collision begin.
Definition: Body.cpp:249
void setMass(float mass)
Set up mass.
Definition: Body.h:299
void setAngularVelocity(const Vector3 &angularVelocity)
Set angular velocity.
Definition: Body.h:338
void removeColliders(vector< reactphysics3d::Collider * > &colliders, vector< BoundingVolume * > &boundingVolumes)
Remove colliders.
Definition: Body.cpp:139
static constexpr uint16_t COLLISION_TYPEID_12
Definition: Body.h:64
static constexpr uint16_t COLLISION_TYPEID_6
Definition: Body.h:58
void setEnabled(bool enabled)
Set up if rigid body is enabled.
Definition: Body.h:215
void setLinearVelocity(const Vector3 &linearVelocity)
Set linear velocity.
Definition: Body.h:319
void setFriction(float friction)
Set up friction.
Definition: Body.cpp:218
void setRestitution(float restitution)
Set up restitution.
Definition: Body.cpp:225
static constexpr uint16_t COLLISION_TYPEID_STATIC
Definition: Body.h:53
void setSleeping(bool sleeping)
Set sleeping.
Definition: Body.h:231
static constexpr uint16_t COLLISION_TYPEID_11
Definition: Body.h:63
static constexpr uint16_t COLLISION_TYPEID_DYNAMIC
Definition: Body.h:54
void setCollisionTypeId(uint16_t typeId)
Set collision type id.
Definition: Body.cpp:112
void addForce(const Vector3 &forceOrigin, const Vector3 &force)
Add force.
Definition: Body.h:417
void setLinearDamping(float linearDamping)
Set linear damping.
Definition: Body.h:353
void removeCollisionListener(CollisionListener *listener)
Remove a collision listener to this rigid body.
Definition: Body.cpp:237
void fireOnCollisionEnd(Body *other)
Fire on collision end.
Definition: Body.cpp:256
const Transform & getTransform()
Definition: Body.h:375
uint16_t collisionTypeIds
Definition: Body.h:87
vector< BoundingVolume * > & getBoundingVolumes()
Definition: Body.h:239
void setAngularDamping(float angularDamping)
Set angular damping.
Definition: Body.h:368
static constexpr uint16_t COLLISION_TYPEID_13
Definition: Body.h:65
void setAngularLockAxisFactor(const Vector3 &factor)
Set angular lock axis factor.
Definition: Body.h:448
const Vector3 getAngularVelocity()
Definition: Body.h:326
virtual ~Body()
Destructor.
Definition: Body.cpp:105
virtual void resetColliders()
Reset body colliders.
Definition: Body.cpp:128
void setCollisionTypeIds(uint16_t collisionTypeIds)
Set up collision type ids.
Definition: Body.cpp:120
uint16_t collisionTypeId
Definition: Body.h:88
static constexpr uint16_t COLLISION_TYPEID_ALL
Definition: Body.h:70
void setLinearLockAxisFactor(const Vector3 &factor)
Set linear lock axis factor.
Definition: Body.h:456
void addCollisionListener(CollisionListener *listener)
Add a collision listener to this rigid body.
Definition: Body.cpp:232
static constexpr uint16_t COLLISION_TYPEID_8
Definition: Body.h:60
vector< reactphysics3d::Collider * > colliders
Definition: Body.h:93
static constexpr uint16_t COLLISION_TYPEID_14
Definition: Body.h:66
static constexpr uint16_t COLLISION_TYPEID_5
Definition: Body.h:57
Body(World *world, const string &id, BodyType type, uint16_t collisionTypeId, bool enabled, const Transform &transform, float restitution, float friction, float mass, const Vector3 &inertiaTensor, const vector< BoundingVolume * > &boundingVolumes)
Protected constructor.
Definition: Body.cpp:50
uint16_t getCollisionTypeId()
Definition: Body.h:181
void fireOnCollision(Body *other, CollisionResponse &collisionResponse)
Fire on collision.
Definition: Body.cpp:242
static constexpr uint16_t COLLISION_TYPEID_4
Definition: Body.h:56
static constexpr uint16_t COLLISION_TYPEID_10
Definition: Body.h:62
BodyType getType()
Return type, see TYPE_*.
Definition: Body.h:174
const string & getId()
Definition: Body.h:166
static const Vector3 getNoRotationInertiaTensor()
Definition: Body.h:75
static constexpr uint16_t COLLISION_TYPEID_16
Definition: Body.h:68
const Vector3 getLinearVelocity()
Definition: Body.h:307
static constexpr uint16_t COLLISION_TYPEID_7
Definition: Body.h:59
static constexpr uint16_t COLLISION_TYPEID_9
Definition: Body.h:61
static constexpr uint16_t COLLISION_TYPEID_3
Definition: Body.h:55
static constexpr uint16_t COLLISION_TYPEID_15
Definition: Body.h:67
Dynamic physics world class.
Definition: World.h:38
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
float getY() const
Definition: Vector3.h:117
float getX() const
Definition: Vector3.h:100
float getZ() const
Definition: Vector3.h:134
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
bool equals(const Vector3 &vector3, float tolerance=Math::EPSILON) const
Compares this vector3 with given vector3.
Definition: Vector3.h:226
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6