Newton Dynamics  4.00
ndBodyDynamic.h
1 /* Copyright (c) <2003-2022> <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 #ifndef __ND_BODY_DYNAMIC_BODY_H__
23 #define __ND_BODY_DYNAMIC_BODY_H__
24 
25 #include "ndNewtonStdafx.h"
26 
27 #define D_MAX_SPEED_ATT ndFloat32(0.02f)
28 #define D_FREEZE_ACCEL ndFloat32(1.0f)
29 //#define D_FREEZE_ACCEL ndFloat32(0.5f)
30 #define D_FREEZE_SPEED ndFloat32(0.032f)
31 
32 #define D_FREEZE_ACCEL2 (D_FREEZE_ACCEL * D_FREEZE_ACCEL)
33 #define D_FREEZE_SPEED2 (D_FREEZE_SPEED * D_FREEZE_SPEED)
34 
35 #define D_FREEZE_MAG D_FREEZE_ACCEL
36 #define D_FREEZE_MAG2 (D_FREEZE_MAG * D_FREEZE_MAG)
37 
38 #define D_ERR_TOLERANCE ndFloat32(1.0e-2f)
39 #define D_ERR_TOLERANCE2 (D_ERR_TOLERANCE * D_ERR_TOLERANCE)
40 
41 D_MSV_NEWTON_ALIGN_32
43 {
44  public:
45  D_CLASS_REFLECTION(ndBodyDynamic);
46  D_NEWTON_API ndBodyDynamic();
47  D_NEWTON_API ndBodyDynamic(const ndLoadSaveBase::ndLoadDescriptor& desc);
48  D_NEWTON_API virtual ~ndBodyDynamic ();
49 
50  D_NEWTON_API virtual ndBodyDynamic* GetAsBodyDynamic() { return this; }
51  D_NEWTON_API virtual void ApplyExternalForces(ndInt32 threadIndex, ndFloat32 timestep);
52  D_NEWTON_API virtual void AddDampingAcceleration(ndFloat32 timestep);
53  D_NEWTON_API virtual void IntegrateVelocity(ndFloat32 timestep);
54 
55  D_NEWTON_API virtual void Save(const ndLoadSaveBase::ndSaveDescriptor& desc) const;
56 
57  D_NEWTON_API void SetForce(const ndVector& force);
58  D_NEWTON_API void SetTorque(const ndVector& torque);
59 
60  D_NEWTON_API void AddImpulse(const ndVector& pointVeloc, const ndVector& pointPosit, ndFloat32 timestep);
61  D_NEWTON_API void ApplyImpulsePair(const ndVector& linearImpulse, const ndVector& angularImpulse, ndFloat32 timestep);
62  D_NEWTON_API void ApplyImpulsesAtPoint(ndInt32 count, const ndVector* const impulseArray, const ndVector* const pointArray, ndFloat32 timestep);
63 
64  D_NEWTON_API ndFloat32 GetLinearDamping() const;
65  D_NEWTON_API void SetLinearDamping(ndFloat32 linearDamp);
66 
67  D_NEWTON_API ndVector GetCachedDamping() const;
68  D_NEWTON_API ndVector GetAngularDamping() const;
69  D_NEWTON_API void SetAngularDamping(const ndVector& angularDamp);
70 
71  virtual ndVector GetForce() const;
72  virtual ndVector GetTorque() const;
73 
74  private:
75  void SaveExternalForces();
76  D_NEWTON_API virtual void IntegrateGyroSubstep(const ndVector& timestep);
77  D_NEWTON_API virtual ndJacobian IntegrateForceAndToque(const ndVector& force, const ndVector& torque, const ndVector& timestep) const;
78  D_NEWTON_API virtual void EvaluateSleepState(ndFloat32 freezeSpeed2, ndFloat32 freezeAccel2);
79 
80  ndVector m_externalForce;
81  ndVector m_externalTorque;
82  ndVector m_impulseForce;
83  ndVector m_impulseTorque;
84  ndVector m_savedExternalForce;
85  ndVector m_savedExternalTorque;
86  ndVector m_dampCoef;
87  ndVector m_cachedDampCoef;
88  ndFloat32 m_cachedTimeStep;
89 
90  friend class ndDynamicsUpdate;
91  friend class ndDynamicsUpdateSoa;
92  friend class ndDynamicsUpdateAvx2;
93  friend class ndDynamicsUpdateCuda;
94  friend class ndDynamicsUpdateOpencl;
95 } D_GCC_NEWTON_ALIGN_32 ;
96 
97 inline ndVector ndBodyDynamic::GetForce() const
98 {
99  return m_externalForce;
100 }
101 
102 inline ndVector ndBodyDynamic::GetTorque() const
103 {
104  return m_externalTorque;
105 }
106 
107 inline void ndBodyDynamic::SaveExternalForces()
108 {
109  m_savedExternalForce = m_externalForce;
110  m_savedExternalTorque = m_externalTorque;
111 }
112 
113 inline ndVector ndBodyDynamic::GetCachedDamping() const
114 {
115  return m_cachedDampCoef;
116 }
117 
118 #endif
119 
120 
ndDynamicsUpdateCuda
Definition: ndDynamicsUpdateCuda.h:31
ndDynamicsUpdateSoa
Definition: ndDynamicsUpdateSoa.h:70
ndBodyDynamic
Definition: ndBodyDynamic.h:43
ndDynamicsUpdateOpencl
Definition: ndDynamicsUpdateOpencl.h:31
ndBodyKinematic
Definition: ndBodyKinematic.h:40
ndDynamicsUpdateAvx2
Definition: ndDynamicsUpdateAvx2.h:31
ndJacobian
Definition: ndConstraint.h:99
ndDynamicsUpdate
Definition: ndDynamicsUpdate.h:45
ndLoadSaveBase::ndLoadDescriptor
Definition: ndSaveLoadSytem.h:59
ndLoadSaveBase::ndSaveDescriptor
Definition: ndSaveLoadSytem.h:93
ndVector
Definition: ndVectorArmNeon.h:41