ScummVM API documentation
dgWorldDynamicUpdate.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_DYNAMIC_UPDATE_H__EC18C699_D48D_448F_A510_A865B2CC0789__INCLUDED_)
23 #define AFX_DYNAMIC_UPDATE_H__EC18C699_D48D_448F_A510_A865B2CC0789__INCLUDED_
24 
25 
26 #include "dgPhysicsStdafx.h"
27 
28 
29 //#define DG_PSD_DAMP_TOL dgFloat32 (1.0e-2f)
30 #define DG_PSD_DAMP_TOL dgFloat32 (1.0e-3f)
31 //#define DG_PSD_DAMP_TOL dgFloat32 (1.0e-4f)
32 
33 
34 #define LINEAR_SOLVER_SUB_STEPS 4
35 
36 
37 class dgIsland;
38 class dgJointInfo;
39 class dgBodyInfo;
40 class dgJacobianMemory;
42 
43 
45 public:
46  dgWorld *m_world;
47  dgInt32 m_count;
48  dgInt32 m_strideInByte;
49  void *m_bodyArray;
50 };
51 
52 
53 
54 template<class T>
55 class dgQueue {
56 public:
57  dgQueue(T *pool, dgInt32 size)
58  : m_pool(pool) {
59 
60  m_mod = size;
61  m_lastIndex = 0;
62  m_firstIndex = 0;
63  }
64 
65  void Insert(T info) {
66  m_pool[m_firstIndex] = info;
67  m_firstIndex ++;
68  if (m_firstIndex >= m_mod) {
69  m_firstIndex = 0;
70  }
71  NEWTON_ASSERT(m_firstIndex != m_lastIndex);
72  }
73 
74  T Remove() {
75  NEWTON_ASSERT(m_firstIndex != m_lastIndex);
76 
77  T element = m_pool[m_lastIndex];
78  m_lastIndex ++;
79  if (m_lastIndex >= m_mod) {
80  m_lastIndex = 0;
81  }
82 
83  return element;
84  }
85 
86  void Reset() {
87  m_lastIndex = m_firstIndex;
88  }
89 
90  bool IsEmpty() const {
91  return (m_firstIndex == m_lastIndex);
92  }
93 
94  dgInt32 m_mod;
95  dgInt32 m_firstIndex;
96  dgInt32 m_lastIndex;
97  T *m_pool;
98 };
99 
100 
102 public:
103  virtual void ThreadExecute();
104 
105  dgInt32 m_count;
106  dgInt32 m_threads;
107  dgInt32 m_useSimd;
108  dgInt32 m_solverMode;
109  dgFloat32 m_timestep;
110  dgWorld *m_world;
111  dgJacobianMemory *m_system;
112  dgWorldDynamicUpdate *m_dynamics;
113 };
114 
115 
116 
118 public:
119  virtual void ThreadExecute();
120 
121  dgInt32 m_count;
122  dgInt32 m_useSimd;
123  dgWorld *m_world;
124  const dgBodyInfo *m_bodyArray;
125  dgJacobian *m_internalVeloc;
126  dgJacobian *m_internalForces;
127 };
128 
130 public:
131  virtual void ThreadExecute();
132 
133  dgInt32 m_count;
134  dgInt32 m_useSimd;
135  dgInt32 *m_locks;
136  dgWorld *m_world;
137  dgFloat32 *m_force;
138  const dgJacobianPair *m_Jt;
139  dgJacobian *m_internalForces;
140  const dgJointInfo *m_constraintArray;
141 };
142 
144 public:
145  virtual void ThreadExecute();
146 
147  dgInt32 m_count;
148  dgInt32 m_hasJointFeeback;
149  dgInt32 *m_locks;
150  dgWorld *m_world;
151  dgFloat32 m_timestep;
152  dgFloat32 *m_force;
153  dgFloat32 **m_jointForceFeeback;
154  const dgJointInfo *m_constraintArray;
155 };
156 
157 
159 public:
160  virtual void ThreadExecute();
161 
162  dgInt32 m_count;
163  dgInt32 m_useSimd;
164  dgWorld *m_world;
165  dgBodyInfo *m_bodyArray;
166 };
167 
169 public:
170  virtual void ThreadExecute();
171 
172  dgInt32 m_count;
173  dgInt32 m_useSimd;
174  dgInt32 m_bodyCount;
175  dgWorld *m_world;
176  dgFloat32 *m_force;
177  dgFloat32 *m_diagDamp;
178  dgFloat32 *m_deltaAccel;
179  dgFloat32 *m_invDJMinvJt;
180  dgFloat32 *m_coordenateAccel;
181  dgFloat32 **m_jointFeebackForce;
182  dgJacobianPair *m_Jt;
183  dgJacobianPair *m_JMinv;
184  dgBodyInfo *m_bodyArray;
185  dgJointInfo *m_constraintArray;
186 };
187 
189 public:
190  virtual void ThreadExecute();
191 
192  bool m_bitMode;
193  dgInt32 m_count;
194  dgInt32 m_jointSolved;
195  dgInt32 *m_rowsCount;
196  dgFloat32 m_timestep;
197  dgFloat32 m_invTimestep;
198  dgWorld *m_world;
199  dgJacobianMemory *m_solverMemory;
200  dgJointInfo *m_constraintArray;
201 };
202 
204 public:
205  virtual void ThreadExecute();
206 
207  dgInt32 m_count;
208  dgInt32 m_useSimd;
209  dgInt32 m_jointStart;
210  dgWorld *m_world;
211  dgFloat32 m_timeStep;
212  dgFloat32 m_invTimeStep;
213  dgFloat32 m_firstPassCoef;
214  const dgJacobianPair *m_Jt;
215  dgFloat32 *m_penetration;
216  const dgFloat32 *m_restitution;
217  const dgFloat32 *m_externAccel;
218  dgFloat32 *m_coordenateAccel;
219  const dgInt32 *m_accelIsMortor;
220  const dgInt32 *m_normalForceIndex;
221  const dgFloat32 *m_penetrationStiffness;
222  const dgJointInfo *m_constraintArray;
223 };
224 
225 
227 public:
228  virtual void ThreadExecute();
229 
230  dgInt32 m_count;
231  dgInt32 m_useSimd;
232  dgWorld *m_world;
233  dgFloat32 m_timeStep;
234  dgJacobian *m_internalVeloc;
235  const dgJacobian *m_internalForces;
236  const dgBodyInfo *m_bodyArray;
237 };
238 
240 public:
241  virtual void ThreadExecute();
242 
243  dgInt32 m_count;
244  dgInt32 m_useSimd;
245  dgWorld *m_world;
246 
247  dgFloat32 m_invStep;
248  dgFloat32 m_invTimeStep;
249  dgFloat32 m_maxAccNorm2;
250  const dgJacobian *m_internalVeloc;
251  const dgBodyInfo *m_bodyArray;
252 };
253 
254 
255 
256 
258 public:
259  virtual void ThreadExecute();
260 
261  dgInt32 m_count;
262  dgInt32 m_useSimd;
263  dgInt32 m_threads;
264  dgWorld *m_world;
265  dgInt32 *m_locks;
266  dgJacobian *m_internalForces;
267  dgFloat32 m_accNorm;
268  dgFloat32 *m_force;
269  const dgFloat32 *m_diagDamp;
270  const dgInt32 *m_normalForceIndex;
271  const dgFloat32 *m_invDJMinvJt;
272  const dgFloat32 *m_coordenateAccel;
273  const dgFloat32 *m_lowerFrictionCoef;
274  const dgFloat32 *m_upperFrictionCoef;
275  const dgJacobianPair *m_Jt;
276  const dgJacobianPair *m_JMinv;
277  const dgJointInfo *m_constraintArray;
278 };
279 
280 
281 
283 public:
284  dgInt32 m_rowCount;
285  dgInt32 m_bodyCount;
286  dgInt32 m_jointCount;
287  dgInt32 m_maxBodiesCount;
288  dgInt32 m_maxJacobiansCount;
289  dgInt32 m_threadIndex;
290  dgFloat32 m_timeStep;
291  dgFloat32 m_invTimeStep;
292  dgWorld *m_world;
293  dgInt32 *m_treadLocks;
294  dgJacobian *m_internalVeloc;
295  dgJacobian *m_internalForces;
296  dgJointInfo *m_constraintArray;
297  dgJacobianPair *m_Jt;
298  dgJacobianPair *m_JMinv;
299  dgFloat32 *m_force;
300  dgFloat32 *m_accel;
301  dgFloat32 *m_deltaAccel;
302  dgFloat32 *m_deltaForce;
303  dgFloat32 *m_diagDamp;
304  dgFloat32 *m_invDJMinvJt;
305  dgFloat32 *m_restitution;
306  dgFloat32 *m_penetration;
307  dgFloat32 *m_coordenateAccel;
308  dgFloat32 *m_penetrationStiffness;
309  dgFloat32 *m_lowerBoundFrictionCoefficent;
310  dgFloat32 *m_upperBoundFrictionCoefficent;
311  dgFloat32 **m_jointFeebackForce;
312  dgBodyInfo *m_bodyArray;
313  dgInt32 *m_normalForceIndex;
314  dgInt32 *m_accelIsMotor;
315 
316  void SwapRows(dgInt32 i, dgInt32 j) const;
317  void SwapRowsSimd(dgInt32 i, dgInt32 j) const;
318 
319  void CalculateReactionsForces(dgInt32 solverMode, dgFloat32 maxAccNorm) const;
320  void CalculateReactionsForcesSimd(dgInt32 solverMode, dgFloat32 maxAccNorm) const;
321 
322  void ApplyExternalForcesAndAcceleration(dgFloat32 maxAccNorm) const;
323  void ApplyExternalForcesAndAccelerationSimd(dgFloat32 maxAccNorm) const;
324 
325  void CalculateSimpleBodyReactionsForces(dgFloat32 maxAccNorm) const;
326  void CalculateSimpleBodyReactionsForcesSimd(dgFloat32 maxAccNorm) const;
327 
328  dgFloat32 CalculateJointForces(dgInt32 joint, dgFloat32 *forceStep, dgFloat32 maxAccNorm) const;
329  dgFloat32 CalculateJointForcesSimd(dgInt32 joint, dgFloat32 *forceStep, dgFloat32 maxAccNorm) const;
330 
331  void CalculateForcesSimulationMode(dgFloat32 maxAccNorm) const;
332  void CalculateForcesSimulationModeSimd(dgFloat32 maxAccNorm) const;
333 
334  void CalculateForcesGameMode(dgInt32 itertions, dgFloat32 maxAccNorm) const;
335  void CalculateForcesGameModeSimd(dgInt32 itertions, dgFloat32 maxAccNorm) const;
336 
337  void CalculateForcesGameModeParallel(dgInt32 itertions, dgFloat32 maxAccNorm, dgInt32 archModel) const;
338  void CalculateReactionsForcesParallel(dgInt32 solverMode, dgFloat32 maxAccNorm, dgInt32 archModel) const;
339 };
340 
341 
343 
345  void UpdateDynamics(dgWorld *const world, dgInt32 archMode, dgFloat32 timestep);
346 
347 private:
348  // single core functions
349  void BuildIsland(dgQueue<dgBody *> &queue, dgInt32 jountCount, dgInt32 hasUnilateralJoints, dgInt32 isContinueCollisionIsland);
350 // dgBody* SpanningTree (dgBody* body, dgBody** const queuePool, dgInt32 queueSize, dgInt32 solveMode);
351  void SpanningTree(dgBody *const body);
352  void ReallocBodyMemory(dgInt32 count);
353  void ReallocJointsMemory(dgInt32 count);
354  void ReallocIslandMemory(dgInt32 count);
355 
356 
357  // multi-cores functions
358  void BuildJacobianMatrix(const dgIsland &island, dgInt32 threadIndex, dgFloat32 timestep);
359  void BuildJacobianMatrixSimd(const dgIsland &island, dgInt32 threadIndex, dgFloat32 timestep);
360  dgInt32 GetJacobialDerivatives(const dgIsland &island, dgInt32 threadIndex, bool bitMode, dgInt32 rowCount, dgFloat32 timestep);
361 
362 
363  void BuildJacobianMatrixParallel(const dgIsland &island, dgFloat32 timestep, dgInt32 archModel);
364  dgInt32 GetJacobialDerivativesParallel(const dgIsland &island, bool bitMode, dgInt32 rowCount, dgFloat32 timestep);
365 
366  void ReallocIntenalForcesMemory(dgInt32 count, dgInt32 threadIndex);
367  void ReallocJacobiansMemory(dgInt32 count, dgInt32 threadIndex);
368 
369 // void SortIslands ();
370 // dgInt32 CompareIslands (const dgIsland& A, const dgIsland& B) const;
371  void IntegrateArray(const dgBodyInfo *body, dgInt32 count, dgFloat32 accelTolerance, dgFloat32 timestep, dgInt32 threadIndex, bool update) const;
372 
373  dgInt32 m_bodies;
374  dgInt32 m_joints;
375  dgInt32 m_islands;
376  dgInt32 m_markLru;
377  dgInt32 m_maxJointCount;
378  dgInt32 m_maxBodiesCount;
379  dgInt32 m_maxIslandCount;
380 
381  dgIsland *m_islandArray;
382  dgBodyInfo *m_bodyArray;
383  dgJointInfo *m_constraintArray;
384 
385 
386  dgJacobianMemory m_solverMemory [DG_MAXIMUN_THREADS];
387  dgSolverWorlkerThreads m_workerThreads[DG_MAXIMUN_THREADS];
388  dgParallelSolverClear m_clearAccumulators[DG_MAXIMUN_THREADS];
389  dgParallelSolverBodyInertia m_parallelBodyInertiaMatrix[DG_MAXIMUN_THREADS];
390  dgParallelSolverUpdateVeloc m_parallelSolverUpdateVeloc[DG_MAXIMUN_THREADS];
391  dgParallelSolverUpdateForce m_parallelSolverUpdateForce[DG_MAXIMUN_THREADS];
392  dgParallelSolverCalculateForces m_parallelSolverCalculateForces[DG_MAXIMUN_THREADS];
393  dgParallelSolverInitInternalForces m_parallelInitIntenalForces[DG_MAXIMUN_THREADS];
394  dgParallelSolverBuildJacobianRows m_parallelSolverBuildJacobianRows[DG_MAXIMUN_THREADS];
395  dgParallelSolverJointAcceleration m_parallelSolverJointAcceleration[DG_MAXIMUN_THREADS];
396  dgParallelSolverInitFeedbackUpdate m_parallelSolverInitFeedbackUpdate[DG_MAXIMUN_THREADS];
397  dgParallelSolverBuildJacobianMatrix m_parallelSolverBuildJacobianMatrix[DG_MAXIMUN_THREADS];
398 
399 
400  dgBody *m_sentinelBody;
401  dgWorld *m_world;
402  friend class dgWorld;
403  friend class dgJacobianMemory;
404  friend class dgSolverWorlkerThreads;
405 };
406 
407 
408 #define DG_CHECK_ACTIVE(body) NEWTON_ASSERT (((body->m_invMass.m_w == dgFloat32 (0.0f)) && !body->m_active) || (body->m_invMass.m_w > dgFloat32 (0.0f)))
409 #endif
Definition: dgWorldDynamicUpdate.h:226
Definition: dgWorldDynamicUpdate.h:129
Definition: dgWorldDynamicUpdate.h:101
Definition: dgWorldDynamicUpdate.h:143
Definition: dgBody.h:85
Definition: dgWorldDynamicUpdate.h:239
Definition: dgWorldDynamicUpdate.h:282
Definition: dgWorldDynamicUpdate.h:158
Definition: dgThreads.h:28
Definition: dgWorldDynamicUpdate.h:203
Definition: dgWorldDynamicUpdate.h:168
Definition: dgWorldDynamicUpdate.h:342
Definition: dgWorldDynamicUpdate.h:117
Definition: dgWorldDynamicUpdate.h:188
Definition: dgConstraint.h:103
Definition: dgConstraint.h:110
Definition: dgWorldDynamicUpdate.h:257
Definition: dgWorldDynamicUpdate.h:44
Definition: dgWorldDynamicUpdate.h:55
Definition: dgWorld.h:118