22 #if !defined(AFX_DGBODY_H__C16EDCD6_53C4_4C6F_A70A_591819F7187E__INCLUDED_) 23 #define AFX_DGBODY_H__C16EDCD6_53C4_4C6F_A70A_591819F7187E__INCLUDED_ 25 #include "dgBodyMasterList.h" 26 #include "hpl1/engine/libraries/newton/core/dg.h" 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) 41 #define DG_ErrTolerance (1.0e-2f) 42 #define DG_ErrTolerance2 (DG_ErrTolerance * DG_ErrTolerance) 44 DG_MSC_VECTOR_ALIGMENT
50 } DG_GCC_VECTOR_ALIGMENT;
55 dgFloat32 m_normal[4];
56 dgFloat32 m_normalOnHitPoint[4];
58 dgFloat32 m_penetration;
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);
70 #define OverlapTest(body0, body1) dgOverlapTest((body0)->m_minAABB, (body0)->m_maxAABB, (body1)->m_minAABB, (body1)->m_maxAABB) 76 void *m_axisArrayNode[3];
80 for (uint i = 0; i <
ARRAYSIZE(m_axisArrayNode); i++) m_axisArrayNode[i] = NULL;
84 DG_MSC_VECTOR_ALIGMENT
87 DG_CLASS_ALLOCATOR(allocator)
92 void AddForce(
const dgVector &force);
93 void AddTorque(
const dgVector &torque);
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);
110 void SetCollisionWithLinkedBodies(
bool state);
113 void SetContinuesCollisionMode(
bool mode);
114 void SetDestructorCallback(OnBodyDestroy destructor);
115 void SetMatrixUpdateCallback(OnMatrixUpdateCallback callback);
116 OnMatrixUpdateCallback GetMatrixUpdateCallback()
const;
118 void SetExtForceAndTorqueCallback(OnApplyExtForceAndTorque callback);
119 OnApplyExtForceAndTorque GetExtForceAndTorqueCallback()
const;
127 void *GetUserData()
const;
131 const dgVector &GetAparentMass()
const;
134 const dgVector &GetVelocity()
const;
137 const dgVector &GetNetForce()
const;
138 const dgVector &GetNetTorque()
const;
141 dgUnsigned32 GetGroupID()
const;
143 const dgVector &GetPosition()
const;
146 dgFloat32 GetLinearDamping()
const;
149 bool IsInEquelibrium()
const;
153 bool GetSleepState()
const;
154 bool GetAutoSleep()
const;
155 void SetAutoSleep(
bool state);
157 bool GetFreeze()
const;
158 void SetFreeze(
bool state);
163 dgInt32 GetUniqueID()
const;
165 bool GetCollisionWithLinkedBodies()
const;
166 bool GetContinuesCollisionMode()
const;
168 void AddBuoyancyForce(dgFloat32 fluidDensity, dgFloat32 fluidLinearViscousity, dgFloat32 fluidAngularViscousity,
169 const dgVector &gravityVector, GetBuoyancyPlane buoyancyPlane,
void *
const context);
171 dgVector CalculateInverseDynamicForce(
const dgVector &desiredVeloc, dgFloat32 timestep)
const;
175 OnRayCastAction filter, OnRayPrecastAction preFilter,
void *
const userData, dgFloat32 minT)
const;
179 void CalcInvInertiaMatrix();
180 void CalcInvInertiaMatrixSimd();
181 const dgMatrix &GetCollisionMatrix()
const;
183 dgBodyMasterList::dgListNode *GetMasterList()
const;
185 void InvalidateCache();
188 void SetMatrixOriginAndRotation(
const dgMatrix &matrix);
190 void CalculateContinueVelocity(dgFloat32 timestep,
dgVector &veloc,
dgVector &omega)
const;
191 void CalculateContinueVelocitySimd(dgFloat32 timestep,
dgVector &veloc,
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);
199 void ApplyExtenalForces(dgFloat32 timestep, dgInt32 threadIndex);
202 void ApplyImpulseArray(dgInt32 count, dgInt32 strideInBytes,
const dgFloat32 *
const impulseArray,
const dgFloat32 *
const pointArray);
205 void AddDamingAcceleration();
207 dgMatrix CalculateInertiaMatrix()
const;
208 dgMatrix CalculateInvInertiaMatrix()
const;
235 dgInt32 m_bodyGroupId;
236 dgInt32 m_genericLRUMark;
237 dgInt32 m_sleepingCounter;
238 dgUnsigned32 m_dynamicsLru;
239 dgUnsigned32 m_isInDerstruionArrayLRU;
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;
256 dgBodyMasterList::dgListNode *m_masterNode;
258 OnBodyDestroy m_destructor;
259 OnMatrixUpdateCallback m_matrixUpdate;
260 OnApplyExtForceAndTorque m_applyExtForces;
267 friend class dgBodyChunk;
270 friend class dgContactArray;
271 friend class dgContactSolver;
288 friend class dgAABBOverlapPairList;
296 } DG_GCC_VECTOR_ALIGMENT;
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));
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));
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));
331 m_genericLRUMark = 0;
332 m_sleepingCounter = 0;
334 m_isInDerstruionArrayLRU = 0;
341 m_continueCollisionMode = 0;
342 m_spawnnedFromCallback = 0;
343 m_collideWithLinkedBodies = 0;
344 m_solverInContinueCollision = 0;
350 m_collisionCell.reset();
354 m_matrixUpdate = NULL;
355 m_applyExtForces = NULL;
358 inline void dgBody::SetAutoSleep(
bool state) {
359 m_autoSleep = dgUnsigned32(state);
360 if (m_autoSleep == 0) {
365 inline bool dgBody::GetAutoSleep()
const {
369 inline bool dgBody::GetSleepState()
const {
380 inline bool dgBody::GetCollisionWithLinkedBodies()
const {
381 return m_collideWithLinkedBodies;
384 inline void dgBody::SetCollisionWithLinkedBodies(
bool state) {
385 m_collideWithLinkedBodies = dgUnsigned32(state);
388 inline void dgBody::SetUserData(
void *
const userData) {
389 m_userData = userData;
392 inline void *dgBody::GetUserData()
const {
396 inline dgWorld *dgBody::GetWorld()
const {
400 inline dgUnsigned32 dgBody::GetGroupID()
const {
401 return dgUnsigned32(m_bodyGroupId);
404 inline void dgBody::SetGroupID(dgUnsigned32
id) {
405 m_bodyGroupId = dgInt32(
id);
408 inline void dgBody::SetDestructorCallback(OnBodyDestroy destructor) {
409 m_destructor = destructor;
412 inline void dgBody::SetExtForceAndTorqueCallback(OnApplyExtForceAndTorque callback) {
413 m_applyExtForces = callback;
416 inline OnApplyExtForceAndTorque dgBody::GetExtForceAndTorqueCallback()
const {
417 return m_applyExtForces;
430 inline void dgBody::SetMatrixUpdateCallback(OnMatrixUpdateCallback callback) {
431 m_matrixUpdate = callback;
434 inline OnMatrixUpdateCallback dgBody::GetMatrixUpdateCallback()
const {
435 return m_matrixUpdate;
456 inline void dgBody::SetOmega(
const dgVector &omega) {
460 inline void dgBody::SetVelocity(
const dgVector &velocity) {
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);
472 inline void dgBody::AddForce(
const dgVector &force) {
473 SetForce(m_accel + force);
476 inline void dgBody::AddTorque(
const dgVector &torque) {
477 SetTorque(torque + m_alpha);
480 inline const dgVector &dgBody::GetMass()
const {
484 inline const dgVector &dgBody::GetAparentMass()
const {
485 return m_aparentMass;
488 inline const dgVector &dgBody::GetInvMass()
const {
492 inline const dgVector &dgBody::GetOmega()
const {
496 inline const dgVector &dgBody::GetVelocity()
const {
500 inline const dgVector &dgBody::GetForce()
const {
504 inline const dgVector &dgBody::GetTorque()
const {
508 inline const dgVector &dgBody::GetNetForce()
const {
512 inline const dgVector &dgBody::GetNetTorque()
const {
520 inline const dgVector &dgBody::GetPosition()
const {
521 return m_matrix.m_posit;
524 inline const dgQuaternion &dgBody::GetRotation()
const {
528 inline const dgMatrix &dgBody::GetMatrix()
const {
532 inline dgVector dgBody::GetCentreOfMass()
const {
533 return m_localCentreOfMass;
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;
557 inline const dgMatrix &dgBody::GetCollisionMatrix()
const {
558 return m_collisionWorldMatrix;
561 inline void dgBody::SetContinuesCollisionMode(
bool mode) {
562 m_continueCollisionMode = dgUnsigned32(mode);
565 inline bool dgBody::GetContinuesCollisionMode()
const {
566 return m_continueCollisionMode;
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);
577 inline dgFloat32 dgBody::GetLinearDamping()
const {
579 return (m_dampCoef.m_w - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT);
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));
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;
593 inline void dgBody::SetAngularDamping(
const dgVector &angularDamp) {
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;
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;
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;
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);
625 inline void dgBody::SetForce(
const dgVector &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;
637 inline void dgBody::SetTorque(
const dgVector &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;
658 inline dgBodyMasterList::dgListNode *dgBody::GetMasterList()
const {
662 inline bool dgBody::GetFreeze()
const {
666 inline dgInt32 dgBody::GetUniqueID()
const {
670 inline bool dgBody::IsInEquelibrium()
const {
671 dgFloat32 invMassMag2 = m_invMass[3] * m_invMass[3];
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) {
699 inline void dgBody::SetMatrixOriginAndRotation(
const dgMatrix &matrix) {
703 for (
int i = 0; i < 4; i++) {
704 for (
int j = 0; j < 4; j++) {
705 NEWTON_ASSERT(dgCheckFloat(m_matrix[i][j]));
711 for (dgInt32 i = 0; i < 3; i++) {
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);
725 m_globalCentreOfMass = m_matrix.TransformVector(m_localCentreOfMass);
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:51
Definition: dgBodyMasterList.h:56
Definition: dgCollisionBVH.h:32
Definition: dgBroadPhaseCollision.h:170
Definition: dgWorldDynamicUpdate.h:239
Definition: dgWorldDynamicUpdate.h:282
Definition: dgVector.h:86
Definition: dgBroadPhaseCollision.h:66
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