ScummVM API documentation
dgBody.h
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2  *
3  * This software is provided 'as-is', without any express or implied
4  * warranty. In no event will the authors be held liable for any damages
5  * arising from the use of this software.
6  *
7  * Permission is granted to anyone to use this software for any purpose,
8  * including commercial applications, and to alter it and redistribute it
9  * freely, subject to the following restrictions:
10  *
11  * 1. The origin of this software must not be misrepresented; you must not
12  * claim that you wrote the original software. If you use this software
13  * in a product, an acknowledgment in the product documentation would be
14  * appreciated but is not required.
15  *
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  *
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 
22 #if !defined(AFX_DGBODY_H__C16EDCD6_53C4_4C6F_A70A_591819F7187E__INCLUDED_)
23 #define AFX_DGBODY_H__C16EDCD6_53C4_4C6F_A70A_591819F7187E__INCLUDED_
24 
25 #include "dgBodyMasterList.h"
26 #include "hpl1/engine/libraries/newton/core/dg.h"
27 
28 
29 class dgLink;
30 class dgBody;
31 class dgWorld;
32 class dgCollision;
33 class dgBroadPhaseCell;
34 
35 #define DG_MIN_SPEED_ATT dgFloat32(0.0f)
36 #define DG_MAX_SPEED_ATT dgFloat32(0.02f)
37 #define DG_INFINITE_MASS dgFloat32(1.0e15f)
38 #define DG_FREEZE_MAG dgFloat32(0.1f)
39 #define DG_FREEZE_MAG2 dgFloat32(DG_FREEZE_MAG *DG_FREEZE_MAG)
40 
41 #define DG_ErrTolerance (1.0e-2f)
42 #define DG_ErrTolerance2 (DG_ErrTolerance * DG_ErrTolerance)
43 
44 DG_MSC_VECTOR_ALIGMENT
45 struct dgLineBox {
46  dgVector m_l0;
47  dgVector m_l1;
48  dgVector m_boxL0;
49  dgVector m_boxL1;
50 } DG_GCC_VECTOR_ALIGMENT;
51 
53 public:
54  dgFloat32 m_point[4]; // collision point in global space
55  dgFloat32 m_normal[4]; // surface normal at collision point in global space
56  dgFloat32 m_normalOnHitPoint[4]; // surface normal at the surface of the hit body,
57  // is the same as the normal calculate by a raycast passing by the hit point in the direction of the cast
58  dgFloat32 m_penetration; // contact penetration at collision point
59  dgInt32 m_contaID; // collision ID at contact point
60  const dgBody *m_hitBody; // body hit at contact point
61 };
62 
63 typedef void(dgApi *OnBodyDestroy)(const NewtonBody *const me);
64 typedef void(dgApi *OnApplyExtForceAndTorque)(NewtonBody *const me, dFloat timestep, int32 threadIndex);
65 typedef void(dgApi *OnMatrixUpdateCallback)(const NewtonBody *const body, const dFloat *const matrix, int32 threadIndex);
66 typedef dgUnsigned32(dgApi *OnRayPrecastAction)(const NewtonBody *const body, const NewtonCollision *const collision, void *const userData);
67 typedef dgFloat32(dgApi *OnRayCastAction)(const NewtonBody *const body, const dFloat *const hitNormal, int collisionID, void *const userData, dFloat intersectParam);
68 typedef dgUnsigned32(dgApi *GetBuoyancyPlane)(const int32 collisionID, void *const context, const dFloat *const globalSpaceMatrix, dFloat *const globalSpacePlane);
69 
70 #define OverlapTest(body0, body1) dgOverlapTest((body0)->m_minAABB, (body0)->m_maxAABB, (body1)->m_minAABB, (body1)->m_maxAABB)
71 //#define OverlapTest_SSE(body0,body1) dgOverlapTest_SSE ((body0)->m_minAABB, (body0)->m_maxAABB, (body1)->m_minAABB, (body1)->m_maxAABB)
72 
74 public:
75  dgBroadPhaseCell *m_cell;
76  void *m_axisArrayNode[3];
77 
78  void reset() {
79  m_cell = NULL;
80  for (uint i = 0; i < ARRAYSIZE(m_axisArrayNode); i++) m_axisArrayNode[i] = NULL;
81  }
82 };
83 
84 DG_MSC_VECTOR_ALIGMENT
85 class dgBody {
86 public:
87  DG_CLASS_ALLOCATOR(allocator)
88 
89  dgBody();
90  ~dgBody();
91 
92  void AddForce(const dgVector &force);
93  void AddTorque(const dgVector &torque);
94  void AttachCollision(dgCollision *collision);
95 
96  void SetGroupID(dgUnsigned32 id);
97  void SetMatrix(const dgMatrix &matrix);
98  void SetMatrixIgnoreSleep(const dgMatrix &matrix);
99  void SetUserData(void *const userData);
100  void SetForce(const dgVector &force);
101  void SetTorque(const dgVector &torque);
102  void SetOmega(const dgVector &omega);
103  void SetVelocity(const dgVector &velocity);
104  void SetLinearDamping(dgFloat32 linearDamp);
105  void SetAngularDamping(const dgVector &angularDamp);
106  void SetCentreOfMass(const dgVector &com);
107  void SetAparentMassMatrix(const dgVector &massMatrix);
108  void SetMassMatrix(dgFloat32 mass, dgFloat32 Ix, dgFloat32 Iy, dgFloat32 Iz);
109  // void SetGyroscopicTorqueMode (bool mode);
110  void SetCollisionWithLinkedBodies(bool state);
111  // void SetFreezeTreshhold (dgFloat32 freezeAccel2, dgFloat32 freezeAlpha2, dgFloat32 freezeSpeed2, dgFloat32 freezeOmega2);
112 
113  void SetContinuesCollisionMode(bool mode);
114  void SetDestructorCallback(OnBodyDestroy destructor);
115  void SetMatrixUpdateCallback(OnMatrixUpdateCallback callback);
116  OnMatrixUpdateCallback GetMatrixUpdateCallback() const;
117  // void SetAutoactiveNotify (OnActivation activate);
118  void SetExtForceAndTorqueCallback(OnApplyExtForceAndTorque callback);
119  OnApplyExtForceAndTorque GetExtForceAndTorqueCallback() const;
120 
121  dgConstraint *GetFirstJoint() const;
122  dgConstraint *GetNextJoint(const dgConstraint *joint) const;
123 
124  dgConstraint *GetFirstContact() const;
125  dgConstraint *GetNextContact(const dgConstraint *joint) const;
126 
127  void *GetUserData() const;
128  dgWorld *GetWorld() const;
129  const dgVector &GetMass() const;
130  const dgVector &GetInvMass() const;
131  const dgVector &GetAparentMass() const;
132 
133  const dgVector &GetOmega() const;
134  const dgVector &GetVelocity() const;
135  const dgVector &GetForce() const;
136  const dgVector &GetTorque() const;
137  const dgVector &GetNetForce() const;
138  const dgVector &GetNetTorque() const;
139 
140  dgCollision *GetCollision() const;
141  dgUnsigned32 GetGroupID() const;
142  const dgMatrix &GetMatrix() const;
143  const dgVector &GetPosition() const;
144  const dgQuaternion &GetRotation() const;
145 
146  dgFloat32 GetLinearDamping() const;
147  dgVector GetAngularDamping() const;
148  dgVector GetCentreOfMass() const;
149  bool IsInEquelibrium() const;
150 
151  void GetAABB(dgVector &p0, dgVector &p1) const;
152 
153  bool GetSleepState() const;
154  bool GetAutoSleep() const;
155  void SetAutoSleep(bool state);
156 
157  bool GetFreeze() const;
158  void SetFreeze(bool state);
159 
160  void Freeze();
161  void Unfreeze();
162 
163  dgInt32 GetUniqueID() const;
164 
165  bool GetCollisionWithLinkedBodies() const;
166  bool GetContinuesCollisionMode() const;
167 
168  void AddBuoyancyForce(dgFloat32 fluidDensity, dgFloat32 fluidLinearViscousity, dgFloat32 fluidAngularViscousity,
169  const dgVector &gravityVector, GetBuoyancyPlane buoyancyPlane, void *const context);
170 
171  dgVector CalculateInverseDynamicForce(const dgVector &desiredVeloc, dgFloat32 timestep) const;
172 
173  // dgFloat32 RayCast (const dgVector& globalP0, const dgVector& globalP1,
174  dgFloat32 RayCast(const dgLineBox &line,
175  OnRayCastAction filter, OnRayPrecastAction preFilter, void *const userData, dgFloat32 minT) const;
176  // dgFloat32 RayCastSimd (const dgVector& globalP0, const dgVector& globalP1,
177  // OnRayCastAction filter, OnRayPrecastAction preFilter, void* userData, dgFloat32 minT) const;
178 
179  void CalcInvInertiaMatrix();
180  void CalcInvInertiaMatrixSimd();
181  const dgMatrix &GetCollisionMatrix() const;
182 
183  dgBodyMasterList::dgListNode *GetMasterList() const;
184 
185  void InvalidateCache();
186 
187 private:
188  void SetMatrixOriginAndRotation(const dgMatrix &matrix);
189 
190  void CalculateContinueVelocity(dgFloat32 timestep, dgVector &veloc, dgVector &omega) const;
191  void CalculateContinueVelocitySimd(dgFloat32 timestep, dgVector &veloc, dgVector &omega) const;
192 
193  dgVector GetTrajectory(const dgVector &veloc, const dgVector &omega) const;
194  void IntegrateVelocity(dgFloat32 timestep);
195  void UpdateMatrix(dgFloat32 timestep, dgInt32 threadIndex);
196  void UpdateCollisionMatrix(dgFloat32 timestep, dgInt32 threadIndex);
197  void UpdateCollisionMatrixSimd(dgFloat32 timestep, dgInt32 threadIndex);
198 
199  void ApplyExtenalForces(dgFloat32 timestep, dgInt32 threadIndex);
200  void AddImpulse(const dgVector &pointVeloc, const dgVector &pointPosit);
201 
202  void ApplyImpulseArray(dgInt32 count, dgInt32 strideInBytes, const dgFloat32 *const impulseArray, const dgFloat32 *const pointArray);
203 
204  // void AddGyroscopicTorque();
205  void AddDamingAcceleration();
206 
207  dgMatrix CalculateInertiaMatrix() const;
208  dgMatrix CalculateInvInertiaMatrix() const;
209 
210  dgMatrix m_matrix;
211  dgMatrix m_collisionWorldMatrix;
212  dgMatrix m_invWorldInertiaMatrix;
213  dgQuaternion m_rotation;
214 
215  dgVector m_veloc;
216  dgVector m_omega;
217  dgVector m_accel;
218  dgVector m_alpha;
219  dgVector m_netForce;
220  dgVector m_netTorque;
221  dgVector m_prevExternalForce;
222  dgVector m_prevExternalTorque;
223 
224  dgVector m_mass;
225  dgVector m_invMass;
226  dgVector m_aparentMass;
227  dgVector m_localCentreOfMass;
228  dgVector m_globalCentreOfMass;
229  dgVector m_minAABB;
230  dgVector m_maxAABB;
231  dgVector m_dampCoef;
232 
233  dgInt32 m_index;
234  dgInt32 m_uniqueID;
235  dgInt32 m_bodyGroupId;
236  dgInt32 m_genericLRUMark;
237  dgInt32 m_sleepingCounter;
238  dgUnsigned32 m_dynamicsLru;
239  dgUnsigned32 m_isInDerstruionArrayLRU;
240 
241  dgUnsigned32 m_freeze : 1;
242  dgUnsigned32 m_sleeping : 1;
243  dgUnsigned32 m_autoSleep : 1;
244  dgUnsigned32 m_isInWorld : 1;
245  dgUnsigned32 m_equilibrium : 1;
246  dgUnsigned32 m_continueCollisionMode : 1;
247  dgUnsigned32 m_spawnnedFromCallback : 1;
248  dgUnsigned32 m_collideWithLinkedBodies : 1;
249  dgUnsigned32 m_solverInContinueCollision : 1;
250  dgUnsigned32 m_inCallback : 1;
251 
252  void *m_userData;
253  dgWorld *m_world;
254  dgCollision *m_collision;
255  dgBroadPhaseList m_collisionCell;
256  dgBodyMasterList::dgListNode *m_masterNode;
257 
258  OnBodyDestroy m_destructor;
259  OnMatrixUpdateCallback m_matrixUpdate;
260  OnApplyExtForceAndTorque m_applyExtForces;
261 
262  void reset();
263 
264  friend class dgWorld;
265  friend class dgContact;
266  friend class dgCollision;
267  friend class dgBodyChunk;
268  friend class dgSortArray;
269  friend class dgConstraint;
270  friend class dgContactArray;
271  friend class dgContactSolver;
272  friend class dgBroadPhaseCell;
273  friend class dgCollisionConvex;
274  friend class dgCollisionEllipse;
275  friend class dgCollisionCompound;
276  friend class dgCollisionUserMesh;
277  friend class dgWorldDynamicUpdate;
278  friend class dgCollisionConvexHull;
279  friend class dgCollisionScene;
280  friend class dgCollisionBVH;
281  friend class dgBodyMasterList;
282  friend class dgJacobianMemory;
283  friend class dgBilateralConstraint;
284  friend class dgBroadPhaseCollision;
285  friend class dgSolverWorlkerThreads;
286  friend class dgCollisionConvexModifier;
287  friend class dgCollidingPairCollector;
288  friend class dgAABBOverlapPairList;
289  friend class dgParallelSolverClear;
290  friend class dgParallelSolverUpdateForce;
291  friend class dgParallelSolverUpdateVeloc;
292  friend class dgParallelSolverBodyInertia;
293  friend class dgBroadPhaseApplyExternalForce;
296 } DG_GCC_VECTOR_ALIGMENT;
297 
298 // *****************************************************************************
299 //
300 // Implementation
301 //
302 // *****************************************************************************
303 
304 inline void dgBody::reset() {
305  m_matrix = dgGetZeroMatrix();
306  m_collisionWorldMatrix = dgGetZeroMatrix();
307  m_invWorldInertiaMatrix = dgGetZeroMatrix();
308  m_rotation = dgQuaternion(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
309 
310  m_veloc = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
311  m_omega = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
312  m_accel = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
313  m_alpha = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
314  m_netForce = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
315  m_netTorque = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
316  m_prevExternalForce = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
317  m_prevExternalTorque = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
318 
319  m_mass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
320  m_invMass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
321  m_aparentMass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
322  m_localCentreOfMass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
323  m_globalCentreOfMass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
324  m_minAABB = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
325  m_maxAABB = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
326  m_dampCoef = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
327 
328  m_index = 0;
329  m_uniqueID = 0;
330  m_bodyGroupId = 0;
331  m_genericLRUMark = 0;
332  m_sleepingCounter = 0;
333  m_dynamicsLru = 0;
334  m_isInDerstruionArrayLRU = 0;
335 
336  m_freeze = 0;
337  m_sleeping = 0;
338  m_autoSleep = 0;
339  m_isInWorld = 0;
340  m_equilibrium = 0;
341  m_continueCollisionMode = 0;
342  m_spawnnedFromCallback = 0;
343  m_collideWithLinkedBodies = 0;
344  m_solverInContinueCollision = 0;
345  m_inCallback = 0;
346 
347  m_userData = NULL;
348  m_world = NULL;
349  m_collision = NULL;
350  m_collisionCell.reset();
351  m_masterNode = NULL;
352 
353  m_destructor = NULL;
354  m_matrixUpdate = NULL;
355  m_applyExtForces = NULL;
356 }
357 
358 inline void dgBody::SetAutoSleep(bool state) {
359  m_autoSleep = dgUnsigned32(state);
360  if (m_autoSleep == 0) {
361  m_sleeping = false;
362  }
363 }
364 
365 inline bool dgBody::GetAutoSleep() const {
366  return m_autoSleep;
367 }
368 
369 inline bool dgBody::GetSleepState() const {
370  return m_sleeping;
371 }
372 
373 /*
374 inline bool dgBody::GetActive () const
375 {
376  return m_active;
377 }
378 */
379 
380 inline bool dgBody::GetCollisionWithLinkedBodies() const {
381  return m_collideWithLinkedBodies;
382 }
383 
384 inline void dgBody::SetCollisionWithLinkedBodies(bool state) {
385  m_collideWithLinkedBodies = dgUnsigned32(state);
386 }
387 
388 inline void dgBody::SetUserData(void *const userData) {
389  m_userData = userData;
390 }
391 
392 inline void *dgBody::GetUserData() const {
393  return m_userData;
394 }
395 
396 inline dgWorld *dgBody::GetWorld() const {
397  return m_world;
398 }
399 
400 inline dgUnsigned32 dgBody::GetGroupID() const {
401  return dgUnsigned32(m_bodyGroupId);
402 }
403 
404 inline void dgBody::SetGroupID(dgUnsigned32 id) {
405  m_bodyGroupId = dgInt32(id);
406 }
407 
408 inline void dgBody::SetDestructorCallback(OnBodyDestroy destructor) {
409  m_destructor = destructor;
410 }
411 
412 inline void dgBody::SetExtForceAndTorqueCallback(OnApplyExtForceAndTorque callback) {
413  m_applyExtForces = callback;
414 }
415 
416 inline OnApplyExtForceAndTorque dgBody::GetExtForceAndTorqueCallback() const {
417  return m_applyExtForces;
418 }
419 
420 /*
421 inline void dgBody::SetAutoactiveNotify (OnActivation activate)
422 {
423  m_activation = activate;
424  if (m_activation) {
425  m_activation (*this, m_active ? 1 : 0);
426  }
427 }
428 */
429 
430 inline void dgBody::SetMatrixUpdateCallback(OnMatrixUpdateCallback callback) {
431  m_matrixUpdate = callback;
432 }
433 
434 inline OnMatrixUpdateCallback dgBody::GetMatrixUpdateCallback() const {
435  return m_matrixUpdate;
436 }
437 
438 /*
439 inline void dgBody::SetFreezeTreshhold (dgFloat32 freezeAccel2, dgFloat32 freezeAlpha2, dgFloat32 freezeSpeed2, dgFloat32 freezeOmega2)
440 {
441  m_freezeAccel2 = GetMax (freezeAccel2, dgFloat32(DG_FREEZE_MAG2));
442  m_freezeAlpha2 = GetMax (freezeAlpha2, dgFloat32(DG_FREEZE_MAG2));
443  m_freezeSpeed2 = GetMax (freezeSpeed2, dgFloat32(DG_FREEZE_MAG2));
444  m_freezeOmega2 = GetMax (freezeOmega2, dgFloat32(DG_FREEZE_MAG2));
445 }
446 
447 inline void dgBody::GetFreezeTreshhold (dgFloat32& freezeAccel2, dgFloat32& freezeAlpha2, dgFloat32& freezeSpeed2, dgFloat32& freezeOmega2) const
448 {
449  freezeAccel2 = m_freezeAccel2;
450  freezeAlpha2 = m_freezeAlpha2;
451  freezeSpeed2 = m_freezeSpeed2;
452  freezeOmega2 = m_freezeOmega2;
453 }
454 */
455 
456 inline void dgBody::SetOmega(const dgVector &omega) {
457  m_omega = omega;
458 }
459 
460 inline void dgBody::SetVelocity(const dgVector &velocity) {
461  m_veloc = velocity;
462 }
463 
464 inline void dgBody::SetCentreOfMass(const dgVector &com) {
465  m_localCentreOfMass.m_x = com.m_x;
466  m_localCentreOfMass.m_y = com.m_y;
467  m_localCentreOfMass.m_z = com.m_z;
468  m_localCentreOfMass.m_w = dgFloat32(1.0f);
469  m_globalCentreOfMass = m_matrix.TransformVector(m_localCentreOfMass);
470 }
471 
472 inline void dgBody::AddForce(const dgVector &force) {
473  SetForce(m_accel + force);
474 }
475 
476 inline void dgBody::AddTorque(const dgVector &torque) {
477  SetTorque(torque + m_alpha);
478 }
479 
480 inline const dgVector &dgBody::GetMass() const {
481  return m_mass;
482 }
483 
484 inline const dgVector &dgBody::GetAparentMass() const {
485  return m_aparentMass;
486 }
487 
488 inline const dgVector &dgBody::GetInvMass() const {
489  return m_invMass;
490 }
491 
492 inline const dgVector &dgBody::GetOmega() const {
493  return m_omega;
494 }
495 
496 inline const dgVector &dgBody::GetVelocity() const {
497  return m_veloc;
498 }
499 
500 inline const dgVector &dgBody::GetForce() const {
501  return m_accel;
502 }
503 
504 inline const dgVector &dgBody::GetTorque() const {
505  return m_alpha;
506 }
507 
508 inline const dgVector &dgBody::GetNetForce() const {
509  return m_netForce;
510 }
511 
512 inline const dgVector &dgBody::GetNetTorque() const {
513  return m_netTorque;
514 }
515 
516 inline dgCollision *dgBody::GetCollision() const {
517  return m_collision;
518 }
519 
520 inline const dgVector &dgBody::GetPosition() const {
521  return m_matrix.m_posit;
522 }
523 
524 inline const dgQuaternion &dgBody::GetRotation() const {
525  return m_rotation;
526 }
527 
528 inline const dgMatrix &dgBody::GetMatrix() const {
529  return m_matrix;
530 }
531 
532 inline dgVector dgBody::GetCentreOfMass() const {
533  return m_localCentreOfMass;
534 }
535 
536 inline void dgBody::GetAABB(dgVector &p0, dgVector &p1) const {
537  p0.m_x = m_minAABB.m_x;
538  p0.m_y = m_minAABB.m_y;
539  p0.m_z = m_minAABB.m_z;
540  p1.m_x = m_maxAABB.m_x;
541  p1.m_y = m_maxAABB.m_y;
542  p1.m_z = m_maxAABB.m_z;
543 }
544 
545 /*
546 inline void dgBody::SetGyroscopicTorqueMode (bool mode)
547 {
548  m_applyGyroscopic = mode;
549 }
550 
551 inline bool dgBody::GetGyroscopicTorqueMode () const
552 {
553  return m_applyGyroscopic;
554 }
555 */
556 
557 inline const dgMatrix &dgBody::GetCollisionMatrix() const {
558  return m_collisionWorldMatrix;
559 }
560 
561 inline void dgBody::SetContinuesCollisionMode(bool mode) {
562  m_continueCollisionMode = dgUnsigned32(mode);
563 }
564 
565 inline bool dgBody::GetContinuesCollisionMode() const {
566  return m_continueCollisionMode;
567 }
568 
569 inline void dgBody::ApplyExtenalForces(dgFloat32 timestep, dgInt32 threadIndex) {
570  m_accel = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
571  m_alpha = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
572  if (m_applyExtForces) {
573  m_applyExtForces(reinterpret_cast<NewtonBody *>(this), timestep, threadIndex);
574  }
575 }
576 
577 inline dgFloat32 dgBody::GetLinearDamping() const {
578  // return (m_linearDampCoef - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT);
579  return (m_dampCoef.m_w - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT);
580 }
581 
582 inline dgVector dgBody::GetAngularDamping() const {
583  return dgVector((m_dampCoef.m_x - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT),
584  (m_dampCoef.m_y - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT),
585  (m_dampCoef.m_z - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT), dgFloat32(0.0f));
586 }
587 
588 inline void dgBody::SetLinearDamping(dgFloat32 linearDamp) {
589  linearDamp = ClampValue(linearDamp, dgFloat32(0.0f), dgFloat32(1.0f));
590  m_dampCoef.m_w = DG_MIN_SPEED_ATT + (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT) * linearDamp;
591 }
592 
593 inline void dgBody::SetAngularDamping(const dgVector &angularDamp) {
594  dgFloat32 tmp;
595 
596  tmp = ClampValue(angularDamp.m_x, dgFloat32(0.0f), dgFloat32(1.0f));
597  m_dampCoef.m_x = DG_MIN_SPEED_ATT + (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT) * tmp;
598 
599  tmp = ClampValue(angularDamp.m_y, dgFloat32(0.0f), dgFloat32(1.0f));
600  m_dampCoef.m_y = DG_MIN_SPEED_ATT + (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT) * tmp;
601 
602  tmp = ClampValue(angularDamp.m_z, dgFloat32(0.0f), dgFloat32(1.0f));
603  m_dampCoef.m_z = DG_MIN_SPEED_ATT + (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT) * tmp;
604 }
605 
606 inline void dgBody::AddDamingAcceleration() {
607  m_veloc -= m_veloc.Scale(m_dampCoef.m_w);
608  dgVector omega(m_matrix.UnrotateVector(m_omega));
609  omega -= omega.CompProduct(m_dampCoef);
610  m_omega = m_matrix.RotateVector(omega);
611 }
612 
613 /*
614 inline void dgBody::AddGyroscopicTorque()
615 {
616  NEWTON_ASSERT (0);
617  if (m_applyGyroscopic) {
618  const dgVector inertia = m_mass;
619  dgVector omega (m_matrix.UnrotateVector (m_omega));
620  m_alpha -= m_matrix.RotateVector(omega.CompProduct(inertia) * omega);
621  }
622 }
623 */
624 
625 inline void dgBody::SetForce(const dgVector &force) {
626  dgFloat32 errMag2;
627  dgVector error;
628 
629  m_accel = force;
630  error = m_accel - m_prevExternalForce;
631  errMag2 = (error % error) * m_invMass[3] * m_invMass[3];
632  if (errMag2 > DG_ErrTolerance2) {
633  m_sleepingCounter = 0;
634  }
635 }
636 
637 inline void dgBody::SetTorque(const dgVector &torque) {
638  dgFloat32 errMag2;
639  dgVector error;
640 
641  m_alpha = torque;
642  error = m_alpha - m_prevExternalTorque;
643  errMag2 = (error % error) * m_invMass[3] * m_invMass[3];
644  if (errMag2 > DG_ErrTolerance2) {
645  m_sleepingCounter = 0;
646  }
647 }
648 
649 // inline int dgBody::GetApplicationFreezeState() const
650 //{
651 // return m_aplycationFreeze ? 1 : 0;
652 // }
653 // inline void dgBody::SetApplicationFreezeState(dgInt32 state)
654 //{
655 // m_aplycationFreeze = state ? true : false;
656 // }
657 
658 inline dgBodyMasterList::dgListNode *dgBody::GetMasterList() const {
659  return m_masterNode;
660 }
661 
662 inline bool dgBody::GetFreeze() const {
663  return m_freeze;
664 }
665 
666 inline dgInt32 dgBody::GetUniqueID() const {
667  return m_uniqueID;
668 }
669 
670 inline bool dgBody::IsInEquelibrium() const {
671  dgFloat32 invMassMag2 = m_invMass[3] * m_invMass[3];
672  if (m_equilibrium) {
673  dgVector error(m_accel - m_prevExternalForce);
674  dgFloat32 errMag2 = (error % error) * invMassMag2;
675  if (errMag2 < DG_ErrTolerance2) {
676  error = m_alpha - m_prevExternalTorque;
677  errMag2 = (error % error) * invMassMag2;
678  if (errMag2 < DG_ErrTolerance2) {
679  errMag2 = (m_netForce % m_netForce) * invMassMag2;
680  if (errMag2 < DG_ErrTolerance2) {
681  errMag2 = (m_netTorque % m_netTorque) * invMassMag2;
682  if (errMag2 < DG_ErrTolerance2) {
683  errMag2 = m_veloc % m_veloc;
684  if (errMag2 < DG_ErrTolerance2) {
685  errMag2 = m_omega % m_omega;
686  if (errMag2 < DG_ErrTolerance2) {
687  return true;
688  }
689  }
690  }
691  }
692  }
693  }
694  }
695 
696  return false;
697 }
698 
699 inline void dgBody::SetMatrixOriginAndRotation(const dgMatrix &matrix) {
700  m_matrix = matrix;
701 
702 #ifdef _DEBUG
703  for (int i = 0; i < 4; i++) {
704  for (int j = 0; j < 4; j++) {
705  NEWTON_ASSERT(dgCheckFloat(m_matrix[i][j]));
706  }
707  }
708 
709  int j0 = 1;
710  int j1 = 2;
711  for (dgInt32 i = 0; i < 3; i++) {
712  dgFloat32 val;
713  NEWTON_ASSERT(m_matrix[i][3] == 0.0f);
714  val = m_matrix[i] % m_matrix[i];
715  NEWTON_ASSERT(dgAbsf(val - 1.0f) < 1.0e-5f);
716  dgVector tmp(m_matrix[j0] * m_matrix[j1]);
717  val = tmp % m_matrix[i];
718  NEWTON_ASSERT(dgAbsf(val - 1.0f) < 1.0e-5f);
719  j0 = j1;
720  j1 = i;
721  }
722 #endif
723 
724  m_rotation = dgQuaternion(m_matrix);
725  m_globalCentreOfMass = m_matrix.TransformVector(m_localCentreOfMass);
726 
727  // matrix.m_front = matrix.m_front.Scale (dgRsqrt (matrix.m_front % matrix.m_front));
728  // matrix.m_right = matrix.m_front * matrix.m_up;
729  // matrix.m_right = matrix.m_right.Scale (dgRsqrt (matrix.m_right % matrix.m_right));
730  // matrix.m_up = matrix.m_right * matrix.m_front;
731 }
732 
733 #endif // !defined(AFX_DGBODY_H__C16EDCD6_53C4_4C6F_A70A_591819F7187E__INCLUDED_)
#define ARRAYSIZE(x)
Definition: util.h:91
Definition: dgWorldDynamicUpdate.h:226
Definition: dgWorldDynamicUpdate.h:101
Definition: dgContact.h:201
Definition: dgBody.h:45
Definition: dgContact.h:51
Definition: dgBodyMasterList.h:56
Definition: dgCollisionBVH.h:32
Definition: dgBroadPhaseCollision.h:170
Definition: dgBody.h:85
Definition: dgBody.h:73
Definition: dgWorldDynamicUpdate.h:239
Definition: dgWorldDynamicUpdate.h:282
Definition: dgVector.h:86
Definition: dgBroadPhaseCollision.h:66
Definition: dgBody.h:52
Definition: dgQuaternion.h:31
Definition: dgWorldDynamicUpdate.h:158
Definition: dgBilateralConstraint.h:28
Definition: dgWorldDynamicUpdate.h:168
Definition: dgWorldDynamicUpdate.h:342
Definition: dgCollisionEllipse.h:30
Definition: dgWorldDynamicUpdate.h:117
Definition: dgWorldDynamicUpdate.h:188
Definition: dgBroadPhaseCollision.h:49
Definition: dgCollisionScene.h:29
Definition: dgCollisionConvex.h:39
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: dgCollisionConvexModifier.h:32
Definition: dgCollision.h:178
Definition: dgCollisionUserMesh.h:48
Definition: dgMatrix.h:41
Definition: dgBroadPhaseCollision.h:124
Definition: dgConstraint.h:169
Definition: dgWorld.h:118
Definition: dgCollisionConvexHull.h:28
Definition: dgCollisionCompound.h:31