1 /********************************************************************************
2 * ReactPhysics3D physics library, http://www.reactphysics3d.com                 *
3 * Copyright (c) 2010-2020 Daniel Chappuis                                       *
4 *********************************************************************************
5 *                                                                               *
6 * This software is provided 'as-is', without any express or implied warranty.   *
7 * In no event will the authors be held liable for any damages arising from the  *
8 * use of this software.                                                         *
9 *                                                                               *
10 * Permission is granted to anyone to use this software for any purpose,         *
11 * including commercial applications, and to alter it and redistribute it        *
12 * freely, subject to the following restrictions:                                *
13 *                                                                               *
14 * 1. The origin of this software must not be misrepresented; you must not claim *
15 *    that you wrote the original software. If you use this software in a        *
16 *    product, an acknowledgment in the product documentation would be           *
17 *    appreciated but is not required.                                           *
18 *                                                                               *
19 * 2. Altered source versions must be plainly marked as such, and must not be    *
20 *    misrepresented as being the original software.                             *
21 *                                                                               *
22 * 3. This notice may not be removed or altered from any source distribution.    *
23 *                                                                               *
24 ********************************************************************************/
25 
26 #ifndef REACTPHYSICS3D_RIGID_BODY_COMPONENTS_H
27 #define REACTPHYSICS3D_RIGID_BODY_COMPONENTS_H
28 
29 // Libraries
30 #include <reactphysics3d/mathematics/Transform.h>
31 #include <reactphysics3d/mathematics/Matrix3x3.h>
32 #include <reactphysics3d/engine/Entity.h>
33 #include <reactphysics3d/components/Components.h>
34 #include <reactphysics3d/containers/Map.h>
35 
36 // ReactPhysics3D namespace
37 namespace reactphysics3d {
38 
39 // Class declarations
40 class MemoryAllocator;
41 class EntityManager;
42 class RigidBody;
43 enum class BodyType;
44 
45 /// Enumeration for the type of a body
46 /// STATIC : A static body has infinite mass, zero velocity but the position can be
47 ///          changed manually. A static body does not collide with other static or kinematic bodies.
48 /// KINEMATIC : A kinematic body has infinite mass, the velocity can be changed manually and its
49 ///             position is computed by the physics engine. A kinematic body does not collide with
50 ///             other static or kinematic bodies.
51 /// DYNAMIC : A dynamic body has non-zero mass, non-zero velocity determined by forces and its
52 ///           position is determined by the physics engine. A dynamic body can collide with other
53 ///           dynamic, static or kinematic bodies.
54 enum class BodyType {STATIC, KINEMATIC, DYNAMIC};
55 
56 // Class RigidBodyComponents
57 /**
58  * This class represent the component of the ECS that contains data about a rigid body.
59  * The components of the sleeping entities (bodies) are always stored at the end of the array.
60  */
61 class RigidBodyComponents : public Components {
62 
63     private:
64 
65         // -------------------- Attributes -------------------- //
66 
67         /// Array of body entities of each component
68         Entity* mBodiesEntities;
69 
70         /// Array of pointers to the corresponding rigid bodies
71         RigidBody** mRigidBodies;
72 
73         /// Array of boolean values to know if the body is allowed to go to sleep
74         bool* mIsAllowedToSleep;
75 
76         /// Array of boolean values to know if the body is sleeping
77         bool* mIsSleeping;
78 
79         /// Array with values for elapsed time since the body velocity was below the sleep velocity
80         decimal* mSleepTimes;
81 
82         /// Array with the type of bodies (static, kinematic or dynamic)
83         BodyType* mBodyTypes;
84 
85         /// Array with the linear velocity of each component
86         Vector3* mLinearVelocities;
87 
88         /// Array with the angular velocity of each component
89         Vector3* mAngularVelocities;
90 
91         /// Array with the external force of each component
92         Vector3* mExternalForces;
93 
94         /// Array with the external torque of each component
95         Vector3* mExternalTorques;
96 
97         /// Array with the linear damping factor of each component
98         decimal* mLinearDampings;
99 
100         /// Array with the angular damping factor of each component
101         decimal* mAngularDampings;
102 
103         /// Array with the mass of each component
104         decimal* mMasses;
105 
106         /// Array with the inverse mass of each component
107         decimal* mInverseMasses;
108 
109         /// Array with the inertia tensor of each component
110         Vector3* mLocalInertiaTensors;
111 
112         /// Array with the inverse of the inertia tensor of each component
113         Vector3* mInverseInertiaTensorsLocal;
114 
115         /// Array with the constrained linear velocity of each component
116         Vector3* mConstrainedLinearVelocities;
117 
118         /// Array with the constrained angular velocity of each component
119         Vector3* mConstrainedAngularVelocities;
120 
121         /// Array with the split linear velocity of each component
122         Vector3* mSplitLinearVelocities;
123 
124         /// Array with the split angular velocity of each component
125         Vector3* mSplitAngularVelocities;
126 
127         /// Array with the constrained position of each component (for position error correction)
128         Vector3* mConstrainedPositions;
129 
130         /// Array of constrained orientation for each component (for position error correction)
131         Quaternion* mConstrainedOrientations;
132 
133         /// Array of center of mass of each component (in local-space coordinates)
134         Vector3* mCentersOfMassLocal;
135 
136         /// Array of center of mass of each component (in world-space coordinates)
137         Vector3* mCentersOfMassWorld;
138 
139         /// True if the gravity needs to be applied to this component
140         bool* mIsGravityEnabled;
141 
142         /// Array with the boolean value to know if the body has already been added into an island
143         bool* mIsAlreadyInIsland;
144 
145         /// For each body, the list of joints entities the body is part of
146         List<Entity>* mJoints;
147 
148         // -------------------- Methods -------------------- //
149 
150         /// Allocate memory for a given number of components
151         virtual void allocate(uint32 nbComponentsToAllocate) override;
152 
153         /// Destroy a component at a given index
154         virtual void destroyComponent(uint32 index) override;
155 
156         /// Move a component from a source to a destination index in the components array
157         virtual void moveComponentToIndex(uint32 srcIndex, uint32 destIndex) override;
158 
159         /// Swap two components in the array
160         virtual void swapComponents(uint32 index1, uint32 index2) override;
161 
162     public:
163 
164         /// Structure for the data of a rigid body component
165         struct RigidBodyComponent {
166 
167             RigidBody* body;
168             BodyType bodyType;
169             const Vector3& worldPosition;
170 
171             /// Constructor
RigidBodyComponentRigidBodyComponent172             RigidBodyComponent(RigidBody* body, BodyType bodyType, const Vector3& worldPosition)
173                 : body(body), bodyType(bodyType), worldPosition(worldPosition) {
174 
175             }
176         };
177 
178         // -------------------- Methods -------------------- //
179 
180         /// Constructor
181         RigidBodyComponents(MemoryAllocator& allocator);
182 
183         /// Destructor
184         virtual ~RigidBodyComponents() override = default;
185 
186         /// Add a component
187         void addComponent(Entity bodyEntity, bool isSleeping, const RigidBodyComponent& component);
188 
189         /// Return a pointer to a rigid body
190         RigidBody* getRigidBody(Entity bodyEntity);
191 
192         /// Return true if the body is allowed to sleep
193         bool getIsAllowedToSleep(Entity bodyEntity) const;
194 
195         /// Set the value to know if the body is allowed to sleep
196         void setIsAllowedToSleep(Entity bodyEntity, bool isAllowedToSleep) const;
197 
198         /// Return true if the body is sleeping
199         bool getIsSleeping(Entity bodyEntity) const;
200 
201         /// Set the value to know if the body is sleeping
202         void setIsSleeping(Entity bodyEntity, bool isSleeping) const;
203 
204         /// Return the sleep time
205         decimal getSleepTime(Entity bodyEntity) const;
206 
207         /// Set the sleep time
208         void setSleepTime(Entity bodyEntity, decimal sleepTime) const;
209 
210         /// Return the body type of a body
211         BodyType getBodyType(Entity bodyEntity);
212 
213         /// Set the body type of a body
214         void setBodyType(Entity bodyEntity, BodyType bodyType);
215 
216         /// Return the linear velocity of an entity
217         const Vector3& getLinearVelocity(Entity bodyEntity) const;
218 
219         /// Set the linear velocity of an entity
220         void setLinearVelocity(Entity bodyEntity, const Vector3& linearVelocity);
221 
222         /// Return the angular velocity of an entity
223         const Vector3& getAngularVelocity(Entity bodyEntity) const;
224 
225         /// Set the angular velocity of an entity
226         void setAngularVelocity(Entity bodyEntity, const Vector3& angularVelocity);
227 
228         /// Return the external force of an entity
229         const Vector3& getExternalForce(Entity bodyEntity) const;
230 
231         /// Return the external torque of an entity
232         const Vector3& getExternalTorque(Entity bodyEntity) const;
233 
234         /// Return the linear damping factor of an entity
235         decimal getLinearDamping(Entity bodyEntity) const;
236 
237         /// Return the angular damping factor of an entity
238         decimal getAngularDamping(Entity bodyEntity) const;
239 
240         /// Return the mass of an entity
241         decimal getMass(Entity bodyEntity) const;
242 
243         /// Set the mass of an entity
244         void setMass(Entity bodyEntity, decimal mass);
245 
246         /// Return the mass inverse of an entity
247         decimal getMassInverse(Entity bodyEntity) const;
248 
249         /// Set the inverse mass of an entity
250         void setMassInverse(Entity bodyEntity, decimal inverseMass);
251 
252         /// Return the local inertia tensor of an entity
253         const Vector3& getLocalInertiaTensor(Entity bodyEntity);
254 
255         /// Set the local inertia tensor of an entity
256         void setLocalInertiaTensor(Entity bodyEntity, const Vector3& inertiaTensorLocal);
257 
258         /// Return the inverse local inertia tensor of an entity
259         const Vector3& getInertiaTensorLocalInverse(Entity bodyEntity);
260 
261         /// Set the external force of an entity
262         void setExternalForce(Entity bodyEntity, const Vector3& externalForce);
263 
264         /// Set the external force of an entity
265         void setExternalTorque(Entity bodyEntity, const Vector3& externalTorque);
266 
267         /// Set the linear damping factor of an entity
268         void setLinearDamping(Entity bodyEntity, decimal linearDamping);
269 
270         /// Set the angular damping factor of an entity
271         void setAngularDamping(Entity bodyEntity, decimal angularDamping);
272 
273         /// Set the inverse local inertia tensor of an entity
274         void setInverseInertiaTensorLocal(Entity bodyEntity, const Vector3& inertiaTensorLocalInverse);
275 
276         /// Return the constrained linear velocity of an entity
277         const Vector3& getConstrainedLinearVelocity(Entity bodyEntity) const;
278 
279         /// Return the constrained angular velocity of an entity
280         const Vector3& getConstrainedAngularVelocity(Entity bodyEntity) const;
281 
282         /// Return the split linear velocity of an entity
283         const Vector3& getSplitLinearVelocity(Entity bodyEntity) const;
284 
285         /// Return the split angular velocity of an entity
286         const Vector3& getSplitAngularVelocity(Entity bodyEntity) const;
287 
288         /// Return the constrained position of an entity
289         Vector3& getConstrainedPosition(Entity bodyEntity);
290 
291         /// Return the constrained orientation of an entity
292         Quaternion& getConstrainedOrientation(Entity bodyEntity);
293 
294         /// Return the local center of mass of an entity
295         const Vector3& getCenterOfMassLocal(Entity bodyEntity);
296 
297         /// Return the world center of mass of an entity
298         const Vector3& getCenterOfMassWorld(Entity bodyEntity);
299 
300         /// Return true if gravity is enabled for this entity
301         bool getIsGravityEnabled(Entity bodyEntity) const;
302 
303         /// Return true if the entity is already in an island
304         bool getIsAlreadyInIsland(Entity bodyEntity) const;
305 
306         /// Set the constrained linear velocity of an entity
307         void setConstrainedLinearVelocity(Entity bodyEntity, const Vector3& constrainedLinearVelocity);
308 
309         /// Set the constrained angular velocity of an entity
310         void setConstrainedAngularVelocity(Entity bodyEntity, const Vector3& constrainedAngularVelocity);
311 
312         /// Set the split linear velocity of an entity
313         void setSplitLinearVelocity(Entity bodyEntity, const Vector3& splitLinearVelocity);
314 
315         /// Set the split angular velocity of an entity
316         void setSplitAngularVelocity(Entity bodyEntity, const Vector3& splitAngularVelocity);
317 
318         /// Set the constrained position of an entity
319         void setConstrainedPosition(Entity bodyEntity, const Vector3& constrainedPosition);
320 
321         /// Set the constrained orientation of an entity
322         void setConstrainedOrientation(Entity bodyEntity, const Quaternion& constrainedOrientation);
323 
324         /// Set the local center of mass of an entity
325         void setCenterOfMassLocal(Entity bodyEntity, const Vector3& centerOfMassLocal);
326 
327         /// Set the world center of mass of an entity
328         void setCenterOfMassWorld(Entity bodyEntity, const Vector3& centerOfMassWorld);
329 
330         /// Set the value to know if the gravity is enabled for this entity
331         void setIsGravityEnabled(Entity bodyEntity, bool isGravityEnabled);
332 
333         /// Set the value to know if the entity is already in an island
334         void setIsAlreadyInIsland(Entity bodyEntity, bool isAlreadyInIsland);
335 
336         /// Return the list of joints of a body
337         const List<Entity>& getJoints(Entity bodyEntity) const;
338 
339         /// Add a joint to a body component
340         void addJointToBody(Entity bodyEntity, Entity jointEntity);
341 
342         /// Remove a joint from a body component
343         void removeJointFromBody(Entity bodyEntity, Entity jointEntity);
344 
345         // -------------------- Friendship -------------------- //
346 
347         friend class PhysicsWorld;
348         friend class ContactSolverSystem;
349         friend class SolveBallAndSocketJointSystem;
350         friend class SolveFixedJointSystem;
351         friend class SolveHingeJointSystem;
352         friend class SolveSliderJointSystem;
353         friend class DynamicsSystem;
354         friend class BallAndSocketJoint;
355         friend class FixedJoint;
356         friend class HingeJoint;
357         friend class SliderJoint;
358 };
359 
360 // Return a pointer to a body rigid
getRigidBody(Entity bodyEntity)361 inline RigidBody* RigidBodyComponents::getRigidBody(Entity bodyEntity) {
362 
363     assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
364 
365     return mRigidBodies[mMapEntityToComponentIndex[bodyEntity]];
366 }
367 
368 // Return true if the body is allowed to sleep
getIsAllowedToSleep(Entity bodyEntity)369 inline bool RigidBodyComponents::getIsAllowedToSleep(Entity bodyEntity) const {
370 
371     assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
372 
373     return mIsAllowedToSleep[mMapEntityToComponentIndex[bodyEntity]];
374 }
375 
376 // Set the value to know if the body is allowed to sleep
setIsAllowedToSleep(Entity bodyEntity,bool isAllowedToSleep)377 inline void RigidBodyComponents::setIsAllowedToSleep(Entity bodyEntity, bool isAllowedToSleep) const {
378 
379     assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
380 
381     mIsAllowedToSleep[mMapEntityToComponentIndex[bodyEntity]] = isAllowedToSleep;
382 }
383 
384 // Return true if the body is sleeping
getIsSleeping(Entity bodyEntity)385 inline bool RigidBodyComponents::getIsSleeping(Entity bodyEntity) const {
386 
387     assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
388 
389     return mIsSleeping[mMapEntityToComponentIndex[bodyEntity]];
390 }
391 
392 // Set the value to know if the body is sleeping
setIsSleeping(Entity bodyEntity,bool isSleeping)393 inline void RigidBodyComponents::setIsSleeping(Entity bodyEntity, bool isSleeping) const {
394 
395     assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
396 
397     mIsSleeping[mMapEntityToComponentIndex[bodyEntity]] = isSleeping;
398 }
399 
400 // Return the sleep time
getSleepTime(Entity bodyEntity)401 inline decimal RigidBodyComponents::getSleepTime(Entity bodyEntity) const {
402 
403     assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
404 
405     return mSleepTimes[mMapEntityToComponentIndex[bodyEntity]];
406 }
407 
408 // Set the sleep time
setSleepTime(Entity bodyEntity,decimal sleepTime)409 inline void RigidBodyComponents::setSleepTime(Entity bodyEntity, decimal sleepTime) const {
410 
411     assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
412 
413     mSleepTimes[mMapEntityToComponentIndex[bodyEntity]] = sleepTime;
414 }
415 
416 // Return the body type of a body
getBodyType(Entity bodyEntity)417 inline BodyType RigidBodyComponents::getBodyType(Entity bodyEntity) {
418 
419    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
420 
421    return mBodyTypes[mMapEntityToComponentIndex[bodyEntity]];
422 }
423 
424 // Set the body type of a body
setBodyType(Entity bodyEntity,BodyType bodyType)425 inline void RigidBodyComponents::setBodyType(Entity bodyEntity, BodyType bodyType) {
426 
427    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
428 
429    mBodyTypes[mMapEntityToComponentIndex[bodyEntity]] = bodyType;
430 }
431 
432 // Return the linear velocity of an entity
getLinearVelocity(Entity bodyEntity)433 inline const Vector3& RigidBodyComponents::getLinearVelocity(Entity bodyEntity) const {
434 
435    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
436 
437    return mLinearVelocities[mMapEntityToComponentIndex[bodyEntity]];
438 }
439 
440 // Return the angular velocity of an entity
getAngularVelocity(Entity bodyEntity)441 inline const Vector3& RigidBodyComponents::getAngularVelocity(Entity bodyEntity) const {
442 
443    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
444 
445    return mAngularVelocities[mMapEntityToComponentIndex[bodyEntity]];
446 }
447 
448 // Set the linear velocity of an entity
setLinearVelocity(Entity bodyEntity,const Vector3 & linearVelocity)449 inline void RigidBodyComponents::setLinearVelocity(Entity bodyEntity, const Vector3& linearVelocity) {
450 
451    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
452 
453    mLinearVelocities[mMapEntityToComponentIndex[bodyEntity]] = linearVelocity;
454 }
455 
456 // Set the angular velocity of an entity
setAngularVelocity(Entity bodyEntity,const Vector3 & angularVelocity)457 inline void RigidBodyComponents::setAngularVelocity(Entity bodyEntity, const Vector3& angularVelocity) {
458 
459    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
460 
461    mAngularVelocities[mMapEntityToComponentIndex[bodyEntity]] = angularVelocity;
462 }
463 
464 // Return the external force of an entity
getExternalForce(Entity bodyEntity)465 inline const Vector3& RigidBodyComponents::getExternalForce(Entity bodyEntity) const {
466 
467    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
468 
469    return mExternalForces[mMapEntityToComponentIndex[bodyEntity]];
470 }
471 
472 // Return the external torque of an entity
getExternalTorque(Entity bodyEntity)473 inline const Vector3& RigidBodyComponents::getExternalTorque(Entity bodyEntity) const {
474 
475    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
476 
477    return mExternalTorques[mMapEntityToComponentIndex[bodyEntity]];
478 }
479 
480 // Return the linear damping factor of an entity
getLinearDamping(Entity bodyEntity)481 inline decimal RigidBodyComponents::getLinearDamping(Entity bodyEntity) const {
482 
483    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
484 
485    return mLinearDampings[mMapEntityToComponentIndex[bodyEntity]];
486 }
487 
488 // Return the angular damping factor of an entity
getAngularDamping(Entity bodyEntity)489 inline decimal RigidBodyComponents::getAngularDamping(Entity bodyEntity) const {
490 
491    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
492 
493    return mAngularDampings[mMapEntityToComponentIndex[bodyEntity]];
494 }
495 
496 // Return the mass of an entity
getMass(Entity bodyEntity)497 inline decimal RigidBodyComponents::getMass(Entity bodyEntity) const {
498 
499    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
500 
501    return mMasses[mMapEntityToComponentIndex[bodyEntity]];
502 }
503 
504 // Return the inverse mass of an entity
getMassInverse(Entity bodyEntity)505 inline decimal RigidBodyComponents::getMassInverse(Entity bodyEntity) const {
506 
507    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
508 
509    return mInverseMasses[mMapEntityToComponentIndex[bodyEntity]];
510 }
511 
512 // Return the inverse local inertia tensor of an entity
getInertiaTensorLocalInverse(Entity bodyEntity)513 inline const Vector3& RigidBodyComponents::getInertiaTensorLocalInverse(Entity bodyEntity) {
514 
515    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
516 
517    return mInverseInertiaTensorsLocal[mMapEntityToComponentIndex[bodyEntity]];
518 }
519 
520 // Set the external force of an entity
setExternalForce(Entity bodyEntity,const Vector3 & externalForce)521 inline void RigidBodyComponents::setExternalForce(Entity bodyEntity, const Vector3& externalForce) {
522 
523    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
524 
525    mExternalForces[mMapEntityToComponentIndex[bodyEntity]] = externalForce;
526 }
527 
528 // Set the external force of an entity
setExternalTorque(Entity bodyEntity,const Vector3 & externalTorque)529 inline void RigidBodyComponents::setExternalTorque(Entity bodyEntity, const Vector3& externalTorque) {
530 
531    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
532 
533    mExternalTorques[mMapEntityToComponentIndex[bodyEntity]] = externalTorque;
534 }
535 
536 // Set the linear damping factor of an entity
setLinearDamping(Entity bodyEntity,decimal linearDamping)537 inline void RigidBodyComponents::setLinearDamping(Entity bodyEntity, decimal linearDamping) {
538 
539    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
540 
541    mLinearDampings[mMapEntityToComponentIndex[bodyEntity]] = linearDamping;
542 }
543 
544 // Set the angular damping factor of an entity
setAngularDamping(Entity bodyEntity,decimal angularDamping)545 inline void RigidBodyComponents::setAngularDamping(Entity bodyEntity, decimal angularDamping) {
546 
547    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
548 
549    mAngularDampings[mMapEntityToComponentIndex[bodyEntity]] = angularDamping;
550 }
551 
552 // Set the  mass of an entity
setMass(Entity bodyEntity,decimal mass)553 inline void RigidBodyComponents::setMass(Entity bodyEntity, decimal mass) {
554 
555    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
556 
557    mMasses[mMapEntityToComponentIndex[bodyEntity]] = mass;
558 }
559 
560 // Set the mass inverse of an entity
setMassInverse(Entity bodyEntity,decimal inverseMass)561 inline void RigidBodyComponents::setMassInverse(Entity bodyEntity, decimal inverseMass) {
562 
563    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
564 
565    mInverseMasses[mMapEntityToComponentIndex[bodyEntity]] = inverseMass;
566 }
567 
568 // Return the local inertia tensor of an entity
getLocalInertiaTensor(Entity bodyEntity)569 inline const Vector3& RigidBodyComponents::getLocalInertiaTensor(Entity bodyEntity) {
570 
571    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
572 
573    return mLocalInertiaTensors[mMapEntityToComponentIndex[bodyEntity]];
574 }
575 
576 // Set the local inertia tensor of an entity
setLocalInertiaTensor(Entity bodyEntity,const Vector3 & inertiaTensorLocal)577 inline void RigidBodyComponents::setLocalInertiaTensor(Entity bodyEntity, const Vector3& inertiaTensorLocal) {
578 
579    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
580 
581    mLocalInertiaTensors[mMapEntityToComponentIndex[bodyEntity]] = inertiaTensorLocal;
582 }
583 
584 // Set the inverse local inertia tensor of an entity
setInverseInertiaTensorLocal(Entity bodyEntity,const Vector3 & inertiaTensorLocalInverse)585 inline void RigidBodyComponents::setInverseInertiaTensorLocal(Entity bodyEntity, const Vector3& inertiaTensorLocalInverse) {
586 
587    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
588 
589    mInverseInertiaTensorsLocal[mMapEntityToComponentIndex[bodyEntity]] = inertiaTensorLocalInverse;
590 }
591 
592 // Return the constrained linear velocity of an entity
getConstrainedLinearVelocity(Entity bodyEntity)593 inline const Vector3& RigidBodyComponents::getConstrainedLinearVelocity(Entity bodyEntity) const {
594 
595    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
596 
597    return mConstrainedLinearVelocities[mMapEntityToComponentIndex[bodyEntity]];
598 }
599 
600 // Return the constrained angular velocity of an entity
getConstrainedAngularVelocity(Entity bodyEntity)601 inline const Vector3& RigidBodyComponents::getConstrainedAngularVelocity(Entity bodyEntity) const {
602 
603    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
604 
605    return mConstrainedAngularVelocities[mMapEntityToComponentIndex[bodyEntity]];
606 }
607 
608 // Return the split linear velocity of an entity
getSplitLinearVelocity(Entity bodyEntity)609 inline const Vector3& RigidBodyComponents::getSplitLinearVelocity(Entity bodyEntity) const {
610 
611    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
612 
613    return mSplitLinearVelocities[mMapEntityToComponentIndex[bodyEntity]];
614 }
615 
616 // Return the split angular velocity of an entity
getSplitAngularVelocity(Entity bodyEntity)617 inline const Vector3& RigidBodyComponents::getSplitAngularVelocity(Entity bodyEntity) const {
618 
619    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
620 
621    return mSplitAngularVelocities[mMapEntityToComponentIndex[bodyEntity]];
622 }
623 
624 // Return the constrained position of an entity
getConstrainedPosition(Entity bodyEntity)625 inline Vector3& RigidBodyComponents::getConstrainedPosition(Entity bodyEntity) {
626 
627    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
628 
629    return mConstrainedPositions[mMapEntityToComponentIndex[bodyEntity]];
630 }
631 
632 // Return the constrained orientation of an entity
getConstrainedOrientation(Entity bodyEntity)633 inline Quaternion& RigidBodyComponents::getConstrainedOrientation(Entity bodyEntity) {
634 
635    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
636 
637    return mConstrainedOrientations[mMapEntityToComponentIndex[bodyEntity]];
638 }
639 
640 // Return the local center of mass of an entity
getCenterOfMassLocal(Entity bodyEntity)641 inline const Vector3& RigidBodyComponents::getCenterOfMassLocal(Entity bodyEntity) {
642 
643    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
644 
645    return mCentersOfMassLocal[mMapEntityToComponentIndex[bodyEntity]];
646 }
647 
648 // Return the world center of mass of an entity
getCenterOfMassWorld(Entity bodyEntity)649 inline const Vector3& RigidBodyComponents::getCenterOfMassWorld(Entity bodyEntity) {
650 
651    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
652 
653    return mCentersOfMassWorld[mMapEntityToComponentIndex[bodyEntity]];
654 }
655 
656 // Set the constrained linear velocity of an entity
setConstrainedLinearVelocity(Entity bodyEntity,const Vector3 & constrainedLinearVelocity)657 inline void RigidBodyComponents::setConstrainedLinearVelocity(Entity bodyEntity, const Vector3& constrainedLinearVelocity) {
658 
659    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
660 
661    mConstrainedLinearVelocities[mMapEntityToComponentIndex[bodyEntity]] = constrainedLinearVelocity;
662 }
663 
664 // Set the constrained angular velocity of an entity
setConstrainedAngularVelocity(Entity bodyEntity,const Vector3 & constrainedAngularVelocity)665 inline void RigidBodyComponents::setConstrainedAngularVelocity(Entity bodyEntity, const Vector3& constrainedAngularVelocity) {
666 
667    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
668 
669    mConstrainedAngularVelocities[mMapEntityToComponentIndex[bodyEntity]] = constrainedAngularVelocity;
670 }
671 
672 // Set the split linear velocity of an entity
setSplitLinearVelocity(Entity bodyEntity,const Vector3 & splitLinearVelocity)673 inline void RigidBodyComponents::setSplitLinearVelocity(Entity bodyEntity, const Vector3& splitLinearVelocity) {
674 
675    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
676 
677    mSplitLinearVelocities[mMapEntityToComponentIndex[bodyEntity]] = splitLinearVelocity;
678 }
679 
680 // Set the split angular velocity of an entity
setSplitAngularVelocity(Entity bodyEntity,const Vector3 & splitAngularVelocity)681 inline void RigidBodyComponents::setSplitAngularVelocity(Entity bodyEntity, const Vector3& splitAngularVelocity) {
682 
683    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
684 
685    mSplitAngularVelocities[mMapEntityToComponentIndex[bodyEntity]] = splitAngularVelocity;
686 }
687 
688 // Set the constrained position of an entity
setConstrainedPosition(Entity bodyEntity,const Vector3 & constrainedPosition)689 inline void RigidBodyComponents::setConstrainedPosition(Entity bodyEntity, const Vector3& constrainedPosition) {
690 
691    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
692 
693    mConstrainedPositions[mMapEntityToComponentIndex[bodyEntity]] = constrainedPosition;
694 }
695 
696 // Set the constrained orientation of an entity
setConstrainedOrientation(Entity bodyEntity,const Quaternion & constrainedOrientation)697 inline void RigidBodyComponents::setConstrainedOrientation(Entity bodyEntity, const Quaternion& constrainedOrientation) {
698 
699    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
700 
701    mConstrainedOrientations[mMapEntityToComponentIndex[bodyEntity]] = constrainedOrientation;
702 }
703 
704 // Set the local center of mass of an entity
setCenterOfMassLocal(Entity bodyEntity,const Vector3 & centerOfMassLocal)705 inline void RigidBodyComponents::setCenterOfMassLocal(Entity bodyEntity, const Vector3& centerOfMassLocal) {
706 
707    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
708 
709    mCentersOfMassLocal[mMapEntityToComponentIndex[bodyEntity]] = centerOfMassLocal;
710 }
711 
712 // Set the world center of mass of an entity
setCenterOfMassWorld(Entity bodyEntity,const Vector3 & centerOfMassWorld)713 inline void RigidBodyComponents::setCenterOfMassWorld(Entity bodyEntity, const Vector3& centerOfMassWorld) {
714 
715    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
716 
717    mCentersOfMassWorld[mMapEntityToComponentIndex[bodyEntity]] = centerOfMassWorld;
718 }
719 
720 // Return true if gravity is enabled for this entity
getIsGravityEnabled(Entity bodyEntity)721 inline bool RigidBodyComponents::getIsGravityEnabled(Entity bodyEntity) const {
722 
723    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
724 
725    return mIsGravityEnabled[mMapEntityToComponentIndex[bodyEntity]];
726 }
727 
728 // Return true if the entity is already in an island
getIsAlreadyInIsland(Entity bodyEntity)729 inline bool RigidBodyComponents::getIsAlreadyInIsland(Entity bodyEntity) const {
730 
731    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
732 
733    return mIsAlreadyInIsland[mMapEntityToComponentIndex[bodyEntity]];
734 }
735 
736 // Set the value to know if the gravity is enabled for this entity
setIsGravityEnabled(Entity bodyEntity,bool isGravityEnabled)737 inline void RigidBodyComponents::setIsGravityEnabled(Entity bodyEntity, bool isGravityEnabled) {
738 
739    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
740 
741    mIsGravityEnabled[mMapEntityToComponentIndex[bodyEntity]] = isGravityEnabled;
742 }
743 
744 // Set the value to know if the entity is already in an island
setIsAlreadyInIsland(Entity bodyEntity,bool isAlreadyInIsland)745 inline void RigidBodyComponents::setIsAlreadyInIsland(Entity bodyEntity, bool isAlreadyInIsland) {
746 
747    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
748    mIsAlreadyInIsland[mMapEntityToComponentIndex[bodyEntity]] = isAlreadyInIsland;
749 }
750 
751 // Return the list of joints of a body
getJoints(Entity bodyEntity)752 inline const List<Entity>& RigidBodyComponents::getJoints(Entity bodyEntity) const {
753 
754    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
755    return mJoints[mMapEntityToComponentIndex[bodyEntity]];
756 }
757 
758 // Add a joint to a body component
addJointToBody(Entity bodyEntity,Entity jointEntity)759 inline void RigidBodyComponents::addJointToBody(Entity bodyEntity, Entity jointEntity) {
760 
761     assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
762     mJoints[mMapEntityToComponentIndex[bodyEntity]].add(jointEntity);
763 }
764 
765 // Remove a joint from a body component
removeJointFromBody(Entity bodyEntity,Entity jointEntity)766 inline void RigidBodyComponents::removeJointFromBody(Entity bodyEntity, Entity jointEntity) {
767 
768     assert(mMapEntityToComponentIndex.containsKey(bodyEntity));
769     mJoints[mMapEntityToComponentIndex[bodyEntity]].remove(jointEntity);
770 }
771 
772 }
773 
774 #endif
775