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_FIXED_JOINT_COMPONENTS_H
27 #define REACTPHYSICS3D_FIXED_JOINT_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 FixedJoint;
43 enum class JointType;
44 
45 // Class FixedJointComponents
46 /**
47  * This class represent the component of the ECS with data for the FixedJoint.
48  */
49 class FixedJointComponents : public Components {
50 
51     private:
52 
53         // -------------------- Attributes -------------------- //
54 
55         /// Array of joint entities
56         Entity* mJointEntities;
57 
58         /// Array of pointers to the joints
59         FixedJoint** mJoints;
60 
61         /// Anchor point of body 1 (in local-space coordinates of body 1)
62         Vector3* mLocalAnchorPointBody1;
63 
64         /// Anchor point of body 2 (in local-space coordinates of body 2)
65         Vector3* mLocalAnchorPointBody2;
66 
67         /// Vector from center of body 2 to anchor point in world-space
68         Vector3* mR1World;
69 
70         /// Vector from center of body 2 to anchor point in world-space
71         Vector3* mR2World;
72 
73         /// Inertia tensor of body 1 (in world-space coordinates)
74         Matrix3x3* mI1;
75 
76         /// Inertia tensor of body 2 (in world-space coordinates)
77         Matrix3x3* mI2;
78 
79         /// Accumulated impulse for the 3 translation constraints
80         Vector3* mImpulseTranslation;
81 
82         /// Accumulate impulse for the 3 rotation constraints
83         Vector3* mImpulseRotation;
84 
85         /// Inverse mass matrix K=JM^-1J^-t of the 3 translation constraints (3x3 matrix)
86         Matrix3x3* mInverseMassMatrixTranslation;
87 
88         /// Inverse mass matrix K=JM^-1J^-t of the 3 rotation constraints (3x3 matrix)
89         Matrix3x3* mInverseMassMatrixRotation;
90 
91         /// Bias vector for the 3 translation constraints
92         Vector3* mBiasTranslation;
93 
94         /// Bias vector for the 3 rotation constraints
95         Vector3* mBiasRotation;
96 
97         /// Inverse of the initial orientation difference between the two bodies
98         Quaternion* mInitOrientationDifferenceInv;
99 
100         // -------------------- Methods -------------------- //
101 
102         /// Allocate memory for a given number of components
103         virtual void allocate(uint32 nbComponentsToAllocate) override;
104 
105         /// Destroy a component at a given index
106         virtual void destroyComponent(uint32 index) override;
107 
108         /// Move a component from a source to a destination index in the components array
109         virtual void moveComponentToIndex(uint32 srcIndex, uint32 destIndex) override;
110 
111         /// Swap two components in the array
112         virtual void swapComponents(uint32 index1, uint32 index2) override;
113 
114     public:
115 
116         /// Structure for the data of a transform component
117         struct FixedJointComponent {
118 
119             /// Constructor
FixedJointComponentFixedJointComponent120             FixedJointComponent() {
121 
122             }
123         };
124 
125         // -------------------- Methods -------------------- //
126 
127         /// Constructor
128         FixedJointComponents(MemoryAllocator& allocator);
129 
130         /// Destructor
131         virtual ~FixedJointComponents() override = default;
132 
133         /// Add a component
134         void addComponent(Entity jointEntity, bool isSleeping, const FixedJointComponent& component);
135 
136         /// Return a pointer to a given joint
137         FixedJoint* getJoint(Entity jointEntity) const;
138 
139         /// Set the joint pointer to a given joint
140         void setJoint(Entity jointEntity, FixedJoint* joint) const;
141 
142         /// Return the local anchor point of body 1 for a given joint
143         const Vector3& getLocalAnchorPointBody1(Entity jointEntity) const;
144 
145         /// Set the local anchor point of body 1 for a given joint
146         void setLocalAnchorPointBody1(Entity jointEntity, const Vector3& localAnchorPointBody1);
147 
148         /// Return the local anchor point of body 2 for a given joint
149         const Vector3& getLocalAnchorPointBody2(Entity jointEntity) const;
150 
151         /// Set the local anchor point of body 2 for a given joint
152         void setLocalAnchorPointBody2(Entity jointEntity, const Vector3& localAnchoirPointBody2);
153 
154         /// Return the vector from center of body 1 to anchor point in world-space
155         const Vector3& getR1World(Entity jointEntity) const;
156 
157         /// Set the vector from center of body 1 to anchor point in world-space
158         void setR1World(Entity jointEntity, const Vector3& r1World);
159 
160         /// Return the vector from center of body 2 to anchor point in world-space
161         const Vector3& getR2World(Entity jointEntity) const;
162 
163         /// Set the vector from center of body 2 to anchor point in world-space
164         void setR2World(Entity jointEntity, const Vector3& r2World);
165 
166         /// Return the inertia tensor of body 1 (in world-space coordinates)
167         const Matrix3x3& getI1(Entity jointEntity) const;
168 
169         /// Set the inertia tensor of body 1 (in world-space coordinates)
170         void setI1(Entity jointEntity, const Matrix3x3& i1);
171 
172         /// Return the inertia tensor of body 2 (in world-space coordinates)
173         const Matrix3x3& getI2(Entity jointEntity) const;
174 
175         /// Set the inertia tensor of body 2 (in world-space coordinates)
176         void setI2(Entity jointEntity, const Matrix3x3& i2);
177 
178         /// Return the translation impulse
179         Vector3& getImpulseTranslation(Entity jointEntity);
180 
181         /// Set the translation impulse
182         void setImpulseTranslation(Entity jointEntity, const Vector3& impulseTranslation);
183 
184         /// Return the translation impulse
185         Vector3& getImpulseRotation(Entity jointEntity);
186 
187         /// Set the translation impulse
188         void setImpulseRotation(Entity jointEntity, const Vector3& impulseTranslation);
189 
190         /// Return the translation inverse mass matrix of the constraint
191         Matrix3x3& getInverseMassMatrixTranslation(Entity jointEntity);
192 
193         /// Set the translation inverse mass matrix of the constraint
194         void setInverseMassMatrixTranslation(Entity jointEntity, const Matrix3x3& inverseMassMatrix);
195 
196         /// Return the rotation inverse mass matrix of the constraint
197         Matrix3x3& getInverseMassMatrixRotation(Entity jointEntity);
198 
199         /// Set the rotation inverse mass matrix of the constraint
200         void setInverseMassMatrixRotation(Entity jointEntity, const Matrix3x3& inverseMassMatrix);
201 
202         /// Return the translation bias
203         Vector3& getBiasTranslation(Entity jointEntity);
204 
205         /// Set the translation impulse
206         void setBiasTranslation(Entity jointEntity, const Vector3& impulseTranslation);
207 
208         /// Return the rotation bias
209         Vector3& getBiasRotation(Entity jointEntity);
210 
211         /// Set the rotation impulse
212         void setBiasRotation(Entity jointEntity, const Vector3 &impulseRotation);
213 
214         /// Return the initial orientation difference
215         Quaternion& getInitOrientationDifferenceInv(Entity jointEntity);
216 
217         /// Set the rotation impulse
218         void setInitOrientationDifferenceInv(Entity jointEntity, const Quaternion& initOrientationDifferenceInv);
219 
220         // -------------------- Friendship -------------------- //
221 
222         friend class BroadPhaseSystem;
223         friend class SolveFixedJointSystem;
224 };
225 
226 // Return a pointer to a given joint
getJoint(Entity jointEntity)227 inline FixedJoint* FixedJointComponents::getJoint(Entity jointEntity) const {
228 
229     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
230     return mJoints[mMapEntityToComponentIndex[jointEntity]];
231 }
232 
233 // Set the joint pointer to a given joint
setJoint(Entity jointEntity,FixedJoint * joint)234 inline void FixedJointComponents::setJoint(Entity jointEntity, FixedJoint* joint) const {
235 
236     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
237     mJoints[mMapEntityToComponentIndex[jointEntity]] = joint;
238 }
239 
240 // Return the local anchor point of body 1 for a given joint
getLocalAnchorPointBody1(Entity jointEntity)241 inline const Vector3& FixedJointComponents::getLocalAnchorPointBody1(Entity jointEntity) const {
242 
243     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
244     return mLocalAnchorPointBody1[mMapEntityToComponentIndex[jointEntity]];
245 }
246 
247 // Set the local anchor point of body 1 for a given joint
setLocalAnchorPointBody1(Entity jointEntity,const Vector3 & localAnchorPointBody1)248 inline void FixedJointComponents::setLocalAnchorPointBody1(Entity jointEntity, const Vector3& localAnchorPointBody1) {
249 
250     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
251     mLocalAnchorPointBody1[mMapEntityToComponentIndex[jointEntity]] = localAnchorPointBody1;
252 }
253 
254 // Return the local anchor point of body 2 for a given joint
getLocalAnchorPointBody2(Entity jointEntity)255 inline const Vector3& FixedJointComponents::getLocalAnchorPointBody2(Entity jointEntity) const {
256 
257     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
258     return mLocalAnchorPointBody2[mMapEntityToComponentIndex[jointEntity]];
259 }
260 
261 // Set the local anchor point of body 2 for a given joint
setLocalAnchorPointBody2(Entity jointEntity,const Vector3 & localAnchorPointBody2)262 inline void FixedJointComponents::setLocalAnchorPointBody2(Entity jointEntity, const Vector3& localAnchorPointBody2) {
263 
264     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
265     mLocalAnchorPointBody2[mMapEntityToComponentIndex[jointEntity]] = localAnchorPointBody2;
266 }
267 
268 // Return the vector from center of body 1 to anchor point in world-space
getR1World(Entity jointEntity)269 inline const Vector3& FixedJointComponents::getR1World(Entity jointEntity) const {
270 
271     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
272     return mR1World[mMapEntityToComponentIndex[jointEntity]];
273 }
274 
275 // Set the vector from center of body 1 to anchor point in world-space
setR1World(Entity jointEntity,const Vector3 & r1World)276 inline void FixedJointComponents::setR1World(Entity jointEntity, const Vector3& r1World) {
277 
278     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
279     mR1World[mMapEntityToComponentIndex[jointEntity]] = r1World;
280 }
281 
282 // Return the vector from center of body 2 to anchor point in world-space
getR2World(Entity jointEntity)283 inline const Vector3& FixedJointComponents::getR2World(Entity jointEntity) const {
284 
285     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
286     return mR2World[mMapEntityToComponentIndex[jointEntity]];
287 }
288 
289 // Set the vector from center of body 2 to anchor point in world-space
setR2World(Entity jointEntity,const Vector3 & r2World)290 inline void FixedJointComponents::setR2World(Entity jointEntity, const Vector3& r2World) {
291 
292     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
293     mR2World[mMapEntityToComponentIndex[jointEntity]] = r2World;
294 }
295 
296 // Return the inertia tensor of body 1 (in world-space coordinates)
getI1(Entity jointEntity)297 inline const Matrix3x3& FixedJointComponents::getI1(Entity jointEntity) const {
298 
299     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
300     return mI1[mMapEntityToComponentIndex[jointEntity]];
301 }
302 
303 // Set the inertia tensor of body 1 (in world-space coordinates)
setI1(Entity jointEntity,const Matrix3x3 & i1)304 inline void FixedJointComponents::setI1(Entity jointEntity, const Matrix3x3& i1) {
305 
306     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
307     mI1[mMapEntityToComponentIndex[jointEntity]] = i1;
308 }
309 
310 // Return the inertia tensor of body 2 (in world-space coordinates)
getI2(Entity jointEntity)311 inline const Matrix3x3& FixedJointComponents::getI2(Entity jointEntity) const {
312 
313     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
314     return mI2[mMapEntityToComponentIndex[jointEntity]];
315 }
316 
317 // Set the inertia tensor of body 2 (in world-space coordinates)
setI2(Entity jointEntity,const Matrix3x3 & i2)318 inline void FixedJointComponents::setI2(Entity jointEntity, const Matrix3x3& i2) {
319 
320     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
321     mI2[mMapEntityToComponentIndex[jointEntity]] = i2;
322 }
323 
324 // Return the translation impulse
getImpulseTranslation(Entity jointEntity)325 inline Vector3& FixedJointComponents::getImpulseTranslation(Entity jointEntity) {
326 
327     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
328     return mImpulseTranslation[mMapEntityToComponentIndex[jointEntity]];
329 }
330 
331 // Set the translation impulse
setImpulseTranslation(Entity jointEntity,const Vector3 & impulseTranslation)332 inline void FixedJointComponents::setImpulseTranslation(Entity jointEntity, const Vector3& impulseTranslation) {
333 
334     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
335     mImpulseTranslation[mMapEntityToComponentIndex[jointEntity]] = impulseTranslation;
336 }
337 
338 // Return the translation impulse
getImpulseRotation(Entity jointEntity)339 inline Vector3& FixedJointComponents::getImpulseRotation(Entity jointEntity) {
340 
341     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
342     return mImpulseRotation[mMapEntityToComponentIndex[jointEntity]];
343 }
344 
345 // Set the translation impulse
setImpulseRotation(Entity jointEntity,const Vector3 & impulseTranslation)346 inline void FixedJointComponents::setImpulseRotation(Entity jointEntity, const Vector3& impulseTranslation) {
347 
348     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
349     mImpulseRotation[mMapEntityToComponentIndex[jointEntity]] = impulseTranslation;
350 }
351 
352 // Return the translation inverse mass matrix of the constraint
getInverseMassMatrixTranslation(Entity jointEntity)353 inline Matrix3x3& FixedJointComponents::getInverseMassMatrixTranslation(Entity jointEntity) {
354 
355     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
356     return mInverseMassMatrixTranslation[mMapEntityToComponentIndex[jointEntity]];
357 }
358 
359 
360 // Set the translation inverse mass matrix of the constraint
setInverseMassMatrixTranslation(Entity jointEntity,const Matrix3x3 & inverseMassMatrix)361 inline void FixedJointComponents::setInverseMassMatrixTranslation(Entity jointEntity, const Matrix3x3& inverseMassMatrix) {
362 
363     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
364     mInverseMassMatrixTranslation[mMapEntityToComponentIndex[jointEntity]] = inverseMassMatrix;
365 }
366 
367 // Return the rotation inverse mass matrix of the constraint
getInverseMassMatrixRotation(Entity jointEntity)368 inline Matrix3x3& FixedJointComponents::getInverseMassMatrixRotation(Entity jointEntity) {
369 
370     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
371     return mInverseMassMatrixRotation[mMapEntityToComponentIndex[jointEntity]];
372 }
373 
374 // Set the rotation inverse mass matrix of the constraint
setInverseMassMatrixRotation(Entity jointEntity,const Matrix3x3 & inverseMassMatrix)375 inline void FixedJointComponents::setInverseMassMatrixRotation(Entity jointEntity, const Matrix3x3& inverseMassMatrix) {
376 
377     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
378     mInverseMassMatrixRotation[mMapEntityToComponentIndex[jointEntity]] = inverseMassMatrix;
379 }
380 
381 // Return the translation bias
getBiasTranslation(Entity jointEntity)382 inline Vector3& FixedJointComponents::getBiasTranslation(Entity jointEntity) {
383 
384     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
385     return mBiasTranslation[mMapEntityToComponentIndex[jointEntity]];
386 }
387 
388 // Set the translation impulse
setBiasTranslation(Entity jointEntity,const Vector3 & impulseTranslation)389 inline void FixedJointComponents::setBiasTranslation(Entity jointEntity, const Vector3 &impulseTranslation) {
390 
391     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
392     mBiasTranslation[mMapEntityToComponentIndex[jointEntity]] = impulseTranslation;
393 }
394 
395 // Return the rotation bias
getBiasRotation(Entity jointEntity)396 inline Vector3& FixedJointComponents::getBiasRotation(Entity jointEntity) {
397 
398     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
399     return mBiasRotation[mMapEntityToComponentIndex[jointEntity]];
400 }
401 
402 // Set the rotation impulse
setBiasRotation(Entity jointEntity,const Vector3 & impulseRotation)403 inline void FixedJointComponents::setBiasRotation(Entity jointEntity, const Vector3& impulseRotation) {
404 
405     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
406     mBiasRotation[mMapEntityToComponentIndex[jointEntity]] = impulseRotation;
407 }
408 
409 // Return the initial orientation difference
getInitOrientationDifferenceInv(Entity jointEntity)410 inline Quaternion& FixedJointComponents::getInitOrientationDifferenceInv(Entity jointEntity) {
411 
412     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
413     return mInitOrientationDifferenceInv[mMapEntityToComponentIndex[jointEntity]];
414 }
415 
416 // Set the rotation impulse
setInitOrientationDifferenceInv(Entity jointEntity,const Quaternion & initOrientationDifferenceInv)417 inline void FixedJointComponents::setInitOrientationDifferenceInv(Entity jointEntity, const Quaternion& initOrientationDifferenceInv) {
418 
419     assert(mMapEntityToComponentIndex.containsKey(jointEntity));
420     mInitOrientationDifferenceInv[mMapEntityToComponentIndex[jointEntity]] = initOrientationDifferenceInv;
421 }
422 
423 }
424 
425 #endif
426