File: dgBody.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (733 lines) | stat: -rw-r--r-- 23,083 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source distribution.
 */

#if !defined(AFX_DGBODY_H__C16EDCD6_53C4_4C6F_A70A_591819F7187E__INCLUDED_)
#define AFX_DGBODY_H__C16EDCD6_53C4_4C6F_A70A_591819F7187E__INCLUDED_

#include "dgBodyMasterList.h"
#include "hpl1/engine/libraries/newton/core/dg.h"


class dgLink;
class dgBody;
class dgWorld;
class dgCollision;
class dgBroadPhaseCell;

#define DG_MIN_SPEED_ATT dgFloat32(0.0f)
#define DG_MAX_SPEED_ATT dgFloat32(0.02f)
#define DG_INFINITE_MASS dgFloat32(1.0e15f)
#define DG_FREEZE_MAG dgFloat32(0.1f)
#define DG_FREEZE_MAG2 dgFloat32(DG_FREEZE_MAG *DG_FREEZE_MAG)

#define DG_ErrTolerance (1.0e-2f)
#define DG_ErrTolerance2 (DG_ErrTolerance * DG_ErrTolerance)

DG_MSC_VECTOR_ALIGMENT
struct dgLineBox {
	dgVector m_l0;
	dgVector m_l1;
	dgVector m_boxL0;
	dgVector m_boxL1;
} DG_GCC_VECTOR_ALIGMENT;

class dgConvexCastReturnInfo {
public:
	dgFloat32 m_point[4];            // collision point in global space
	dgFloat32 m_normal[4];           // surface normal at collision point in global space
	dgFloat32 m_normalOnHitPoint[4]; // surface normal at the surface of the hit body,
	// is the same as the normal calculate by a raycast passing by the hit point in the direction of the cast
	dgFloat32 m_penetration;         // contact penetration at collision point
	dgInt32 m_contaID;               // collision ID at contact point
	const dgBody *m_hitBody;         // body hit at contact point
};

typedef void(dgApi *OnBodyDestroy)(const NewtonBody *const me);
typedef void(dgApi *OnApplyExtForceAndTorque)(NewtonBody *const me, dFloat timestep, int32 threadIndex);
typedef void(dgApi *OnMatrixUpdateCallback)(const NewtonBody *const body, const dFloat *const matrix, int32 threadIndex);
typedef dgUnsigned32(dgApi *OnRayPrecastAction)(const NewtonBody *const body, const NewtonCollision *const collision, void *const userData);
typedef dgFloat32(dgApi *OnRayCastAction)(const NewtonBody *const body, const dFloat *const hitNormal, int collisionID, void *const userData, dFloat intersectParam);
typedef dgUnsigned32(dgApi *GetBuoyancyPlane)(const int32 collisionID, void *const context, const dFloat *const globalSpaceMatrix, dFloat *const globalSpacePlane);

#define OverlapTest(body0, body1) dgOverlapTest((body0)->m_minAABB, (body0)->m_maxAABB, (body1)->m_minAABB, (body1)->m_maxAABB)
//#define OverlapTest_SSE(body0,body1) dgOverlapTest_SSE ((body0)->m_minAABB, (body0)->m_maxAABB, (body1)->m_minAABB, (body1)->m_maxAABB)

class dgBroadPhaseList {
public:
	dgBroadPhaseCell *m_cell;
	void *m_axisArrayNode[3];

	void reset() {
		m_cell = NULL;
		for (uint i = 0; i < ARRAYSIZE(m_axisArrayNode); i++) m_axisArrayNode[i] = NULL;
	}
};

DG_MSC_VECTOR_ALIGMENT
class dgBody {
public:
	DG_CLASS_ALLOCATOR(allocator)

	dgBody();
	~dgBody();

	void AddForce(const dgVector &force);
	void AddTorque(const dgVector &torque);
	void AttachCollision(dgCollision *collision);

	void SetGroupID(dgUnsigned32 id);
	void SetMatrix(const dgMatrix &matrix);
	void SetMatrixIgnoreSleep(const dgMatrix &matrix);
	void SetUserData(void *const userData);
	void SetForce(const dgVector &force);
	void SetTorque(const dgVector &torque);
	void SetOmega(const dgVector &omega);
	void SetVelocity(const dgVector &velocity);
	void SetLinearDamping(dgFloat32 linearDamp);
	void SetAngularDamping(const dgVector &angularDamp);
	void SetCentreOfMass(const dgVector &com);
	void SetAparentMassMatrix(const dgVector &massMatrix);
	void SetMassMatrix(dgFloat32 mass, dgFloat32 Ix, dgFloat32 Iy, dgFloat32 Iz);
	//  void SetGyroscopicTorqueMode (bool mode);
	void SetCollisionWithLinkedBodies(bool state);
	//  void SetFreezeTreshhold (dgFloat32 freezeAccel2, dgFloat32 freezeAlpha2, dgFloat32 freezeSpeed2, dgFloat32 freezeOmega2);

	void SetContinuesCollisionMode(bool mode);
	void SetDestructorCallback(OnBodyDestroy destructor);
	void SetMatrixUpdateCallback(OnMatrixUpdateCallback callback);
	OnMatrixUpdateCallback GetMatrixUpdateCallback() const;
	//  void SetAutoactiveNotify (OnActivation activate);
	void SetExtForceAndTorqueCallback(OnApplyExtForceAndTorque callback);
	OnApplyExtForceAndTorque GetExtForceAndTorqueCallback() const;

	dgConstraint *GetFirstJoint() const;
	dgConstraint *GetNextJoint(const dgConstraint *joint) const;

	dgConstraint *GetFirstContact() const;
	dgConstraint *GetNextContact(const dgConstraint *joint) const;

	void *GetUserData() const;
	dgWorld *GetWorld() const;
	const dgVector &GetMass() const;
	const dgVector &GetInvMass() const;
	const dgVector &GetAparentMass() const;

	const dgVector &GetOmega() const;
	const dgVector &GetVelocity() const;
	const dgVector &GetForce() const;
	const dgVector &GetTorque() const;
	const dgVector &GetNetForce() const;
	const dgVector &GetNetTorque() const;

	dgCollision *GetCollision() const;
	dgUnsigned32 GetGroupID() const;
	const dgMatrix &GetMatrix() const;
	const dgVector &GetPosition() const;
	const dgQuaternion &GetRotation() const;

	dgFloat32 GetLinearDamping() const;
	dgVector GetAngularDamping() const;
	dgVector GetCentreOfMass() const;
	bool IsInEquelibrium() const;

	void GetAABB(dgVector &p0, dgVector &p1) const;

	bool GetSleepState() const;
	bool GetAutoSleep() const;
	void SetAutoSleep(bool state);

	bool GetFreeze() const;
	void SetFreeze(bool state);

	void Freeze();
	void Unfreeze();

	dgInt32 GetUniqueID() const;

	bool GetCollisionWithLinkedBodies() const;
	bool GetContinuesCollisionMode() const;

	void AddBuoyancyForce(dgFloat32 fluidDensity, dgFloat32 fluidLinearViscousity, dgFloat32 fluidAngularViscousity,
	                      const dgVector &gravityVector, GetBuoyancyPlane buoyancyPlane, void *const context);

	dgVector CalculateInverseDynamicForce(const dgVector &desiredVeloc, dgFloat32 timestep) const;

	//  dgFloat32 RayCast (const dgVector& globalP0, const dgVector& globalP1,
	dgFloat32 RayCast(const dgLineBox &line,
	                  OnRayCastAction filter, OnRayPrecastAction preFilter, void *const userData, dgFloat32 minT) const;
	//  dgFloat32 RayCastSimd (const dgVector& globalP0, const dgVector& globalP1,
	//                     OnRayCastAction filter, OnRayPrecastAction preFilter, void* userData, dgFloat32 minT) const;

	void CalcInvInertiaMatrix();
	void CalcInvInertiaMatrixSimd();
	const dgMatrix &GetCollisionMatrix() const;

	dgBodyMasterList::dgListNode *GetMasterList() const;

	void InvalidateCache();

private:
	void SetMatrixOriginAndRotation(const dgMatrix &matrix);

	void CalculateContinueVelocity(dgFloat32 timestep, dgVector &veloc, dgVector &omega) const;
	void CalculateContinueVelocitySimd(dgFloat32 timestep, dgVector &veloc, dgVector &omega) const;

	dgVector GetTrajectory(const dgVector &veloc, const dgVector &omega) const;
	void IntegrateVelocity(dgFloat32 timestep);
	void UpdateMatrix(dgFloat32 timestep, dgInt32 threadIndex);
	void UpdateCollisionMatrix(dgFloat32 timestep, dgInt32 threadIndex);
	void UpdateCollisionMatrixSimd(dgFloat32 timestep, dgInt32 threadIndex);

	void ApplyExtenalForces(dgFloat32 timestep, dgInt32 threadIndex);
	void AddImpulse(const dgVector &pointVeloc, const dgVector &pointPosit);

	void ApplyImpulseArray(dgInt32 count, dgInt32 strideInBytes, const dgFloat32 *const impulseArray, const dgFloat32 *const pointArray);

	//  void AddGyroscopicTorque();
	void AddDamingAcceleration();

	dgMatrix CalculateInertiaMatrix() const;
	dgMatrix CalculateInvInertiaMatrix() const;

	dgMatrix m_matrix;
	dgMatrix m_collisionWorldMatrix;
	dgMatrix m_invWorldInertiaMatrix;
	dgQuaternion m_rotation;

	dgVector m_veloc;
	dgVector m_omega;
	dgVector m_accel;
	dgVector m_alpha;
	dgVector m_netForce;
	dgVector m_netTorque;
	dgVector m_prevExternalForce;
	dgVector m_prevExternalTorque;

	dgVector m_mass;
	dgVector m_invMass;
	dgVector m_aparentMass;
	dgVector m_localCentreOfMass;
	dgVector m_globalCentreOfMass;
	dgVector m_minAABB;
	dgVector m_maxAABB;
	dgVector m_dampCoef;

	dgInt32 m_index;
	dgInt32 m_uniqueID;
	dgInt32 m_bodyGroupId;
	dgInt32 m_genericLRUMark;
	dgInt32 m_sleepingCounter;
	dgUnsigned32 m_dynamicsLru;
	dgUnsigned32 m_isInDerstruionArrayLRU;

	dgUnsigned32 m_freeze : 1;
	dgUnsigned32 m_sleeping : 1;
	dgUnsigned32 m_autoSleep : 1;
	dgUnsigned32 m_isInWorld : 1;
	dgUnsigned32 m_equilibrium : 1;
	dgUnsigned32 m_continueCollisionMode : 1;
	dgUnsigned32 m_spawnnedFromCallback : 1;
	dgUnsigned32 m_collideWithLinkedBodies : 1;
	dgUnsigned32 m_solverInContinueCollision : 1;
	dgUnsigned32 m_inCallback : 1;

	void *m_userData;
	dgWorld *m_world;
	dgCollision *m_collision;
	dgBroadPhaseList m_collisionCell;
	dgBodyMasterList::dgListNode *m_masterNode;

	OnBodyDestroy m_destructor;
	OnMatrixUpdateCallback m_matrixUpdate;
	OnApplyExtForceAndTorque m_applyExtForces;

	void reset();

	friend class dgWorld;
	friend class dgContact;
	friend class dgCollision;
	friend class dgBodyChunk;
	friend class dgSortArray;
	friend class dgConstraint;
	friend class dgContactArray;
	friend class dgContactSolver;
	friend class dgBroadPhaseCell;
	friend class dgCollisionConvex;
	friend class dgCollisionEllipse;
	friend class dgCollisionCompound;
	friend class dgCollisionUserMesh;
	friend class dgWorldDynamicUpdate;
	friend class dgCollisionConvexHull;
	friend class dgCollisionScene;
	friend class dgCollisionBVH;
	friend class dgBodyMasterList;
	friend class dgJacobianMemory;
	friend class dgBilateralConstraint;
	friend class dgBroadPhaseCollision;
	friend class dgSolverWorlkerThreads;
	friend class dgCollisionConvexModifier;
	friend class dgCollidingPairCollector;
	friend class dgAABBOverlapPairList;
	friend class dgParallelSolverClear;
	friend class dgParallelSolverUpdateForce;
	friend class dgParallelSolverUpdateVeloc;
	friend class dgParallelSolverBodyInertia;
	friend class dgBroadPhaseApplyExternalForce;
	friend class dgParallelSolverBuildJacobianRows;
	friend class dgParallelSolverBuildJacobianMatrix;
} DG_GCC_VECTOR_ALIGMENT;

// *****************************************************************************
//
//	 Implementation
//
// *****************************************************************************

inline void dgBody::reset() {
	m_matrix = dgGetZeroMatrix();
	m_collisionWorldMatrix = dgGetZeroMatrix();
	m_invWorldInertiaMatrix = dgGetZeroMatrix();
	m_rotation = dgQuaternion(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));

	m_veloc = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_omega = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_accel = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_alpha = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_netForce = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_netTorque = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_prevExternalForce = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_prevExternalTorque = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));

	m_mass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_invMass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_aparentMass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_localCentreOfMass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_globalCentreOfMass = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_minAABB = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_maxAABB = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_dampCoef = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));

	m_index = 0;
	m_uniqueID = 0;
	m_bodyGroupId = 0;
	m_genericLRUMark = 0;
	m_sleepingCounter = 0;
	m_dynamicsLru = 0;
	m_isInDerstruionArrayLRU = 0;

	m_freeze = 0;
	m_sleeping = 0;
	m_autoSleep = 0;
	m_isInWorld = 0;
	m_equilibrium = 0;
	m_continueCollisionMode = 0;
	m_spawnnedFromCallback = 0;
	m_collideWithLinkedBodies = 0;
	m_solverInContinueCollision = 0;
	m_inCallback = 0;

	m_userData = NULL;
	m_world = NULL;
	m_collision = NULL;
	m_collisionCell.reset();
	m_masterNode = NULL;

	m_destructor = NULL;
	m_matrixUpdate = NULL;
	m_applyExtForces = NULL;
}

inline void dgBody::SetAutoSleep(bool state) {
	m_autoSleep = dgUnsigned32(state);
	if (m_autoSleep == 0) {
		m_sleeping = false;
	}
}

inline bool dgBody::GetAutoSleep() const {
	return m_autoSleep;
}

inline bool dgBody::GetSleepState() const {
	return m_sleeping;
}

/*
inline bool dgBody::GetActive () const
{
    return m_active;
}
*/

inline bool dgBody::GetCollisionWithLinkedBodies() const {
	return m_collideWithLinkedBodies;
}

inline void dgBody::SetCollisionWithLinkedBodies(bool state) {
	m_collideWithLinkedBodies = dgUnsigned32(state);
}

inline void dgBody::SetUserData(void *const userData) {
	m_userData = userData;
}

inline void *dgBody::GetUserData() const {
	return m_userData;
}

inline dgWorld *dgBody::GetWorld() const {
	return m_world;
}

inline dgUnsigned32 dgBody::GetGroupID() const {
	return dgUnsigned32(m_bodyGroupId);
}

inline void dgBody::SetGroupID(dgUnsigned32 id) {
	m_bodyGroupId = dgInt32(id);
}

inline void dgBody::SetDestructorCallback(OnBodyDestroy destructor) {
	m_destructor = destructor;
}

inline void dgBody::SetExtForceAndTorqueCallback(OnApplyExtForceAndTorque callback) {
	m_applyExtForces = callback;
}

inline OnApplyExtForceAndTorque dgBody::GetExtForceAndTorqueCallback() const {
	return m_applyExtForces;
}

/*
inline void dgBody::SetAutoactiveNotify (OnActivation activate)
{
    m_activation = activate;
    if (m_activation) {
        m_activation (*this, m_active ? 1 : 0);
    }
}
*/

inline void dgBody::SetMatrixUpdateCallback(OnMatrixUpdateCallback callback) {
	m_matrixUpdate = callback;
}

inline OnMatrixUpdateCallback dgBody::GetMatrixUpdateCallback() const {
	return m_matrixUpdate;
}

/*
inline void dgBody::SetFreezeTreshhold (dgFloat32 freezeAccel2, dgFloat32 freezeAlpha2, dgFloat32 freezeSpeed2, dgFloat32 freezeOmega2)
{
    m_freezeAccel2 = GetMax (freezeAccel2, dgFloat32(DG_FREEZE_MAG2));
    m_freezeAlpha2 = GetMax (freezeAlpha2, dgFloat32(DG_FREEZE_MAG2));
    m_freezeSpeed2 = GetMax (freezeSpeed2, dgFloat32(DG_FREEZE_MAG2));
    m_freezeOmega2 = GetMax (freezeOmega2, dgFloat32(DG_FREEZE_MAG2));
}

inline void dgBody::GetFreezeTreshhold (dgFloat32& freezeAccel2, dgFloat32& freezeAlpha2, dgFloat32& freezeSpeed2, dgFloat32& freezeOmega2) const
{
    freezeAccel2 = m_freezeAccel2;
    freezeAlpha2 = m_freezeAlpha2;
    freezeSpeed2 = m_freezeSpeed2;
    freezeOmega2 = m_freezeOmega2;
}
*/

inline void dgBody::SetOmega(const dgVector &omega) {
	m_omega = omega;
}

inline void dgBody::SetVelocity(const dgVector &velocity) {
	m_veloc = velocity;
}

inline void dgBody::SetCentreOfMass(const dgVector &com) {
	m_localCentreOfMass.m_x = com.m_x;
	m_localCentreOfMass.m_y = com.m_y;
	m_localCentreOfMass.m_z = com.m_z;
	m_localCentreOfMass.m_w = dgFloat32(1.0f);
	m_globalCentreOfMass = m_matrix.TransformVector(m_localCentreOfMass);
}

inline void dgBody::AddForce(const dgVector &force) {
	SetForce(m_accel + force);
}

inline void dgBody::AddTorque(const dgVector &torque) {
	SetTorque(torque + m_alpha);
}

inline const dgVector &dgBody::GetMass() const {
	return m_mass;
}

inline const dgVector &dgBody::GetAparentMass() const {
	return m_aparentMass;
}

inline const dgVector &dgBody::GetInvMass() const {
	return m_invMass;
}

inline const dgVector &dgBody::GetOmega() const {
	return m_omega;
}

inline const dgVector &dgBody::GetVelocity() const {
	return m_veloc;
}

inline const dgVector &dgBody::GetForce() const {
	return m_accel;
}

inline const dgVector &dgBody::GetTorque() const {
	return m_alpha;
}

inline const dgVector &dgBody::GetNetForce() const {
	return m_netForce;
}

inline const dgVector &dgBody::GetNetTorque() const {
	return m_netTorque;
}

inline dgCollision *dgBody::GetCollision() const {
	return m_collision;
}

inline const dgVector &dgBody::GetPosition() const {
	return m_matrix.m_posit;
}

inline const dgQuaternion &dgBody::GetRotation() const {
	return m_rotation;
}

inline const dgMatrix &dgBody::GetMatrix() const {
	return m_matrix;
}

inline dgVector dgBody::GetCentreOfMass() const {
	return m_localCentreOfMass;
}

inline void dgBody::GetAABB(dgVector &p0, dgVector &p1) const {
	p0.m_x = m_minAABB.m_x;
	p0.m_y = m_minAABB.m_y;
	p0.m_z = m_minAABB.m_z;
	p1.m_x = m_maxAABB.m_x;
	p1.m_y = m_maxAABB.m_y;
	p1.m_z = m_maxAABB.m_z;
}

/*
inline void dgBody::SetGyroscopicTorqueMode (bool mode)
{
    m_applyGyroscopic = mode;
}

inline bool dgBody::GetGyroscopicTorqueMode () const
{
    return m_applyGyroscopic;
}
*/

inline const dgMatrix &dgBody::GetCollisionMatrix() const {
	return m_collisionWorldMatrix;
}

inline void dgBody::SetContinuesCollisionMode(bool mode) {
	m_continueCollisionMode = dgUnsigned32(mode);
}

inline bool dgBody::GetContinuesCollisionMode() const {
	return m_continueCollisionMode;
}

inline void dgBody::ApplyExtenalForces(dgFloat32 timestep, dgInt32 threadIndex) {
	m_accel = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	m_alpha = dgVector(dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f), dgFloat32(0.0f));
	if (m_applyExtForces) {
		m_applyExtForces(reinterpret_cast<NewtonBody *>(this), timestep, threadIndex);
	}
}

inline dgFloat32 dgBody::GetLinearDamping() const {
	//  return (m_linearDampCoef - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT);
	return (m_dampCoef.m_w - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT);
}

inline dgVector dgBody::GetAngularDamping() const {
	return dgVector((m_dampCoef.m_x - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT),
	                (m_dampCoef.m_y - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT),
	                (m_dampCoef.m_z - DG_MIN_SPEED_ATT) / (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT), dgFloat32(0.0f));
}

inline void dgBody::SetLinearDamping(dgFloat32 linearDamp) {
	linearDamp = ClampValue(linearDamp, dgFloat32(0.0f), dgFloat32(1.0f));
	m_dampCoef.m_w = DG_MIN_SPEED_ATT + (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT) * linearDamp;
}

inline void dgBody::SetAngularDamping(const dgVector &angularDamp) {
	dgFloat32 tmp;

	tmp = ClampValue(angularDamp.m_x, dgFloat32(0.0f), dgFloat32(1.0f));
	m_dampCoef.m_x = DG_MIN_SPEED_ATT + (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT) * tmp;

	tmp = ClampValue(angularDamp.m_y, dgFloat32(0.0f), dgFloat32(1.0f));
	m_dampCoef.m_y = DG_MIN_SPEED_ATT + (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT) * tmp;

	tmp = ClampValue(angularDamp.m_z, dgFloat32(0.0f), dgFloat32(1.0f));
	m_dampCoef.m_z = DG_MIN_SPEED_ATT + (DG_MAX_SPEED_ATT - DG_MIN_SPEED_ATT) * tmp;
}

inline void dgBody::AddDamingAcceleration() {
	m_veloc -= m_veloc.Scale(m_dampCoef.m_w);
	dgVector omega(m_matrix.UnrotateVector(m_omega));
	omega -= omega.CompProduct(m_dampCoef);
	m_omega = m_matrix.RotateVector(omega);
}

/*
inline void dgBody::AddGyroscopicTorque()
{
    NEWTON_ASSERT (0);
    if (m_applyGyroscopic) {
        const dgVector inertia = m_mass;
        dgVector omega (m_matrix.UnrotateVector (m_omega));
        m_alpha -= m_matrix.RotateVector(omega.CompProduct(inertia) * omega);
    }
}
*/

inline void dgBody::SetForce(const dgVector &force) {
	dgFloat32 errMag2;
	dgVector error;

	m_accel = force;
	error = m_accel - m_prevExternalForce;
	errMag2 = (error % error) * m_invMass[3] * m_invMass[3];
	if (errMag2 > DG_ErrTolerance2) {
		m_sleepingCounter = 0;
	}
}

inline void dgBody::SetTorque(const dgVector &torque) {
	dgFloat32 errMag2;
	dgVector error;

	m_alpha = torque;
	error = m_alpha - m_prevExternalTorque;
	errMag2 = (error % error) * m_invMass[3] * m_invMass[3];
	if (errMag2 > DG_ErrTolerance2) {
		m_sleepingCounter = 0;
	}
}

// inline int dgBody::GetApplicationFreezeState() const
//{
//	return m_aplycationFreeze ? 1 : 0;
// }
// inline void dgBody::SetApplicationFreezeState(dgInt32 state)
//{
//	m_aplycationFreeze = state ? true : false;
// }

inline dgBodyMasterList::dgListNode *dgBody::GetMasterList() const {
	return m_masterNode;
}

inline bool dgBody::GetFreeze() const {
	return m_freeze;
}

inline dgInt32 dgBody::GetUniqueID() const {
	return m_uniqueID;
}

inline bool dgBody::IsInEquelibrium() const {
	dgFloat32 invMassMag2 = m_invMass[3] * m_invMass[3];
	if (m_equilibrium) {
		dgVector error(m_accel - m_prevExternalForce);
		dgFloat32 errMag2 = (error % error) * invMassMag2;
		if (errMag2 < DG_ErrTolerance2) {
			error = m_alpha - m_prevExternalTorque;
			errMag2 = (error % error) * invMassMag2;
			if (errMag2 < DG_ErrTolerance2) {
				errMag2 = (m_netForce % m_netForce) * invMassMag2;
				if (errMag2 < DG_ErrTolerance2) {
					errMag2 = (m_netTorque % m_netTorque) * invMassMag2;
					if (errMag2 < DG_ErrTolerance2) {
						errMag2 = m_veloc % m_veloc;
						if (errMag2 < DG_ErrTolerance2) {
							errMag2 = m_omega % m_omega;
							if (errMag2 < DG_ErrTolerance2) {
								return true;
							}
						}
					}
				}
			}
		}
	}

	return false;
}

inline void dgBody::SetMatrixOriginAndRotation(const dgMatrix &matrix) {
	m_matrix = matrix;

#ifdef _DEBUG
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			NEWTON_ASSERT(dgCheckFloat(m_matrix[i][j]));
		}
	}

	int j0 = 1;
	int j1 = 2;
	for (dgInt32 i = 0; i < 3; i++) {
		dgFloat32 val;
		NEWTON_ASSERT(m_matrix[i][3] == 0.0f);
		val = m_matrix[i] % m_matrix[i];
		NEWTON_ASSERT(dgAbsf(val - 1.0f) < 1.0e-5f);
		dgVector tmp(m_matrix[j0] * m_matrix[j1]);
		val = tmp % m_matrix[i];
		NEWTON_ASSERT(dgAbsf(val - 1.0f) < 1.0e-5f);
		j0 = j1;
		j1 = i;
	}
#endif

	m_rotation = dgQuaternion(m_matrix);
	m_globalCentreOfMass = m_matrix.TransformVector(m_localCentreOfMass);

	//  matrix.m_front = matrix.m_front.Scale (dgRsqrt (matrix.m_front % matrix.m_front));
	//  matrix.m_right = matrix.m_front * matrix.m_up;
	//  matrix.m_right = matrix.m_right.Scale (dgRsqrt (matrix.m_right % matrix.m_right));
	//  matrix.m_up = matrix.m_right * matrix.m_front;
}

#endif // !defined(AFX_DGBODY_H__C16EDCD6_53C4_4C6F_A70A_591819F7187E__INCLUDED_)