Newton Dynamics  4.00
ndWorld.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_WORLD_H__
23 #define __ND_WORLD_H__
24 
25 #include "ndNewtonStdafx.h"
26 #include "ndModelList.h"
27 #include "ndJointList.h"
28 #include "ndSkeletonList.h"
29 
30 class ndWorld;
31 class ndModel;
32 class ndJointList;
33 class ndBodyDynamic;
34 class ndRayCastNotify;
35 class ndDynamicsUpdate;
36 class ndConvexCastNotify;
39 
40 #define D_NEWTON_ENGINE_MAJOR_VERSION 4
41 #define D_NEWTON_ENGINE_MINOR_VERSION 00
42 
43 #define D_SLEEP_ENTRIES 8
44 
45 D_MSV_NEWTON_ALIGN_32
46 class ndWorld: public ndClassAlloc
47 {
48  public:
49  enum ndSolverModes
50  {
51  ndStandardSolver,
52  ndSimdSoaSolver,
53  ndSimdAvx2Solver,
54  ndCudaSolver,
55  ndOpenclSolver1,
56  ndOpenclSolver2,
57  };
58 
59  D_NEWTON_API ndWorld();
60  D_NEWTON_API virtual ~ndWorld();
61 
62  D_NEWTON_API virtual void CleanUp();
63 
64  D_NEWTON_API ndInt32 GetEngineVersion() const;
65 
66  D_NEWTON_API void Sync() const;
67  D_NEWTON_API void Update(ndFloat32 timestep);
68  D_NEWTON_API void CollisionUpdate(ndFloat32 timestep);
69 
70  D_NEWTON_API virtual void OnPostUpdate(ndFloat32 timestep);
71 
72  D_NEWTON_API ndInt32 GetThreadCount() const;
73  D_NEWTON_API void SetThreadCount(ndInt32 count);
74 
75  D_NEWTON_API ndInt32 GetSubSteps() const;
76  D_NEWTON_API void SetSubSteps(ndInt32 subSteps);
77 
78  D_NEWTON_API ndSolverModes GetSelectedSolver() const;
79  D_NEWTON_API void SelectSolver(ndSolverModes solverMode);
80 
81  D_NEWTON_API bool IsGPU() const;
82  D_NEWTON_API ndScene* GetScene() const;
83  D_NEWTON_API const char* GetSolverString() const;
84  D_NEWTON_API ndBodyKinematic* GetSentinelBody() const;
85 
86  D_NEWTON_API virtual bool AddBody(ndSharedPtr<ndBody>& body);
87  D_NEWTON_API virtual void AddModel(ndSharedPtr<ndModel>& model);
88  D_NEWTON_API virtual void AddJoint(ndSharedPtr<ndJointBilateralConstraint>& joint);
89 
90  D_NEWTON_API virtual void RemoveBody(ndBody* const body);
91  D_NEWTON_API virtual void RemoveModel(ndModel* const model);
92  D_NEWTON_API virtual void RemoveJoint(ndJointBilateralConstraint* const joint);
93 
94  D_NEWTON_API const ndJointList& GetJointList() const;
95  D_NEWTON_API const ndModelList& GetModelList() const;
96  D_NEWTON_API const ndBodyListView& GetBodyList() const;
97  D_NEWTON_API const ndBodyList& GetParticleList() const;
98  D_NEWTON_API const ndContactArray& GetContactList() const;
99  D_NEWTON_API const ndSkeletonList& GetSkeletonList() const;
100 
101  D_NEWTON_API ndInt32 GetSolverIterations() const;
102  D_NEWTON_API void SetSolverIterations(ndInt32 iterations);
103 
104  D_NEWTON_API ndFloat32 GetUpdateTime() const;
105  D_NEWTON_API ndUnsigned32 GetFrameNumber() const;
106  D_NEWTON_API ndUnsigned32 GetSubFrameNumber() const;
107  D_NEWTON_API ndFloat32 GetAverageUpdateTime() const;
108  D_NEWTON_API ndFloat32 GetExtensionAverageUpdateTime() const;
109 
110  D_NEWTON_API ndContactNotify* GetContactNotify() const;
111  D_NEWTON_API void SetContactNotify(ndContactNotify* const notify);
112 
113  D_NEWTON_API void DebugScene(ndSceneTreeNotiFy* const notify);
114  D_NEWTON_API void SendBackgroundTask(ndBackgroundTask* const job);
115 
116  D_NEWTON_API void ClearCache();
117  D_NEWTON_API void BodiesInAabb(ndBodiesInAabbNotify& callback, const ndVector& minBox, const ndVector& maxBox) const;
118  D_NEWTON_API bool RayCast(ndRayCastNotify& callback, const ndVector& globalOrigin, const ndVector& globalDest) const;
119  D_NEWTON_API bool ConvexCast(ndConvexCastNotify& callback, const ndShapeInstance& convexShape, const ndMatrix& globalOrigin, const ndVector& globalDest) const;
120 
121  private:
122  void ThreadFunction();
123  void PostUpdate(ndFloat32 timestep);
124 
125  protected:
126  D_NEWTON_API virtual void UpdateSkeletons();
127  D_NEWTON_API virtual void UpdateTransforms();
128  D_NEWTON_API virtual void PostModelTransform();
129 
130  private:
131  void RemoveBody(ndSharedPtr<ndBody>& body);
132  void RemoveModel(ndSharedPtr<ndModel>& model);
133  void RemoveJoint(ndSharedPtr<ndJointBilateralConstraint>& joint);
134 
135  class dgSolverProgressiveSleepEntry
136  {
137  public:
138  ndFloat32 m_maxAccel;
139  ndFloat32 m_maxVeloc;
140  ndInt32 m_steps;
141  };
142 
143  class ndIslandMember
144  {
145  public:
146  ndBodyKinematic* m_root;
147  ndBodyKinematic* m_body;
148  };
149 
150  void ModelUpdate();
151  void ModelPostUpdate();
152  void CalculateAverageUpdateTime();
153  void SubStepUpdate(ndFloat32 timestep);
154  void ParticleUpdate(ndFloat32 timestep);
155 
156  bool SkeletonJointTest(ndJointBilateralConstraint* const jointA) const;
157  static ndInt32 CompareJointByInvMass(const ndJointBilateralConstraint* const jointA, const ndJointBilateralConstraint* const jointB, void* notUsed);
158 
159  ndScene* m_scene;
160  ndDynamicsUpdate* m_solver;
161  ndJointList m_jointList;
162  ndModelList m_modelList;
163  ndSkeletonList m_skeletonList;
164  ndBodyList m_particleSetList;
165  ndArray<ndBody*> m_deletedBodies;
166  ndArray<ndModel*> m_deletedModels;
167  ndArray<ndJointBilateralConstraint*> m_deletedJoints;
168  ndArray<ndSkeletonContainer*> m_activeSkeletons;
169  ndSpinLock m_deletedLock;
170 
171  ndFloat32 m_timestep;
172  ndFloat32 m_freezeAccel2;
173  ndFloat32 m_freezeSpeed2;
174  ndFloat32 m_averageUpdateTime;
175  ndFloat32 m_averageTimestepAcc;
176  ndFloat32 m_averageFramesCount;
177  ndFloat32 m_lastExecutionTime;
178  ndFloat32 m_extensionAverageUpdateTime;
179  ndFloat32 m_extensionAverageTimestepAcc;
180 
181  dgSolverProgressiveSleepEntry m_sleepTable[D_SLEEP_ENTRIES];
182 
183  ndInt32 m_subSteps;
184  ndSolverModes m_solverMode;
185  ndInt32 m_solverIterations;
186  bool m_inUpdate;
187 
188  friend class ndScene;
189  friend class ndWorldScene;
190  friend class ndBodyDynamic;
191  friend class ndDynamicsUpdate;
192  friend class ndSkeletonContainer;
193  friend class ndDynamicsUpdateSoa;
194  friend class ndDynamicsUpdateAvx2;
195  friend class ndDynamicsUpdateCuda;
196  friend class ndDynamicsUpdateOpencl;
197 } D_GCC_NEWTON_ALIGN_32;
198 
199 #endif
ndClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: ndClassAlloc.h:30
ndModelList
Definition: ndModelList.h:30
ndSpinLock
Simple spin lock for synchronizing threads for very short period of time.
Definition: ndUtils.h:212
ndSharedPtr
Definition: ndSharedPtr.h:17
ndSkeletonContainer
Definition: ndSkeletonContainer.h:31
ndScene
Definition: ndScene.h:59
ndArray< ndBody * >
ndModel
Definition: ndModel.h:33
ndBody
Definition: ndBody.h:43
ndContactNotify
Definition: ndContactNotify.h:62
ndBodyList
Definition: ndBodyListView.h:38
ndSceneTreeNotiFy
Definition: ndScene.h:44
ndDynamicsUpdateCuda
Definition: ndDynamicsUpdateCuda.h:31
ndRayCastNotify
Definition: ndRayCastNotify.h:31
ndDynamicsUpdateSoa
Definition: ndDynamicsUpdateSoa.h:70
ndBodiesInAabbNotify
Definition: ndBodiesInAabbNotify.h:30
ndBodyDynamic
Definition: ndBodyDynamic.h:43
ndJointBilateralConstraint
Definition: ndJointBilateralConstraint.h:53
ndDynamicsUpdateOpencl
Definition: ndDynamicsUpdateOpencl.h:31
ndBodyKinematic
Definition: ndBodyKinematic.h:40
ndContactArray
Definition: ndContactArray.h:29
ndBodyListView
Definition: ndBodyListView.h:47
ndDynamicsUpdateAvx2
Definition: ndDynamicsUpdateAvx2.h:31
ndMatrix
Definition: ndMatrix.h:42
ndBackgroundTask
Definition: ndThreadBackgroundWorker.h:35
ndJointList
Definition: ndJointList.h:29
ndDynamicsUpdate
Definition: ndDynamicsUpdate.h:45
ndConvexCastNotify
Definition: ndConvexCastNotify.h:34
ndShapeInstance
Definition: ndShapeInstance.h:62
ndWorldScene
Definition: ndWorldScene.h:26
ndVector
Definition: ndVectorArmNeon.h:41
ndWorld
Definition: ndWorld.h:47
ndSkeletonList
Definition: ndSkeletonList.h:31