ScummVM API documentation
dgBroadPhaseCollision.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 #ifndef __AFX_BROADPHASE__INCLUDED__
23 #define __AFX_BROADPHASE__INCLUDED__
24 
25 #include "dgBody.h"
26 #include "dgContact.h"
27 #include "hpl1/engine/libraries/newton/core/dg.h"
28 
29 
30 //#define DG_OCTREE_MAX_DEPTH 1
31 //#define DG_OCTREE_MAX_DEPTH 6
32 #define DG_OCTREE_MAX_DEPTH 7
33 
34 typedef void(dgApi *OnBodiesInAABB)(dgBody *body, void *const userData);
35 typedef void(dgApi *OnLeavingWorldAction)(dgBody *body, dgInt32 threadIndex);
36 
37 class dgCellPair {
38 public:
39  dgBroadPhaseCell *m_cell_A;
40  dgBroadPhaseCell *m_cell_B;
41 };
42 
44 public:
45  dgBody *m_body;
46  dgFloat32 m_key;
47 };
48 
49 class dgSortArray : public dgList<dgSortArrayEntry> {
50 public:
51  dgSortArray();
52  ~dgSortArray();
53 
54  void Add(dgBody *const body);
55  void Remove(dgBody *const body);
56  dgFloat32 Sort();
57  void InvalidateCache();
58 
59  dgFloat32 RayCast(dgFloat32 minT, const dgLineBox &line, OnRayCastAction filter, OnRayPrecastAction prefilter, void *const userData) const;
60  bool SanityCheck();
61 
62  dgInt8 m_index;
63  dgInt8 m_isSorted;
64 };
65 
67 public:
70 
71 private:
72  void Init(dgInt32 layer, dgMemoryAllocator *allocator);
73  void Add(dgBody *const body);
74  void Remove(dgBody *const body);
75 
76  void Sort();
77  void UpdateAutoPair(dgWorld *const world, dgInt32 threadIndex);
78 
79  dgSortArray m_sort[3];
80  dgSortArray *m_lastSortArray;
81 
82  dgInt32 m_count;
83  dgInt8 m_active;
84  dgInt8 m_layerIndex;
85 
86  friend class dgBody;
87  friend class dgBroadPhaseLayer;
88  friend class dgBroadPhaseCollision;
90 };
91 
92 class dgBroadPhaseLayer : public dgTree<dgBroadPhaseCell, dgUnsigned32> {
93 public:
96 
97 private:
98  void Init(dgWorld *const world, dgFloat32 cellSize, dgInt32 layerIndex);
99 
100  inline dgUnsigned32 GetKey(dgInt32 x, dgInt32 z) const {
101  dgUnsigned32 key = dgUnsigned32((z << DG_OCTREE_MAX_DEPTH) + x);
102  return key;
103  }
104 
105  inline void KeyToIndex(dgInt32 key, dgInt32 &x, dgInt32 &z) const {
106  x = key & ((1 << DG_OCTREE_MAX_DEPTH) - 1);
107  z = key >> DG_OCTREE_MAX_DEPTH;
108  }
109 
110  inline dgBroadPhaseCell *Find(dgInt32 x, dgInt32 z) const {
111  dgTreeNode *const node = dgTree<dgBroadPhaseCell, dgUnsigned32>::Find(GetKey(x, z));
112  return node ? &node->GetInfo() : NULL;
113  }
114  dgBroadPhaseCell *FindCreate(dgInt32 x, dgInt32 z);
115 
116  dgWorld *m_me;
117  dgFloat32 m_cellSize;
118  dgFloat32 m_invCellSize;
119  dgInt16 m_layerIndex;
120 
121  friend class dgBroadPhaseCollision;
122 };
123 
125 public:
126  virtual void ThreadExecute();
127 
128  dgInt32 m_step;
129  dgInt32 m_count;
130  dgInt32 m_skipForceUpdate;
131  dgFloat32 m_timeStep;
132  dgWorld *m_world;
133  dgBody **m_bodies;
134 };
135 
137 public:
138  virtual void ThreadExecute();
139 
140  dgInt32 m_step;
141  dgInt32 m_count;
142  dgWorld *m_world;
143  dgCellPair *m_pairs;
144 };
145 
147 public:
148  void Realloc(dgInt32 jointsCount, dgInt32 contactCount, dgInt32 threadIndex);
149  virtual void ThreadExecute();
150 
151  dgInt32 m_step;
152  dgInt32 m_count;
153  dgInt32 m_useSimd;
154  dgFloat32 m_timestep;
155  dgWorld *m_world;
156 };
157 
159 public:
160  virtual void ThreadExecute();
161 
162  dgInt32 m_step;
163  dgInt32 m_count;
164  dgInt32 m_useSimd;
165  dgFloat32 m_timestep;
166  dgWorld *m_world;
168 };
169 
171 public:
172  void GetWorldSize(dgVector &p0, dgVector &p1) const;
173  void SetWorldSize(const dgVector &min, const dgVector &max);
174  void RayCast(const dgVector &p0, const dgVector &p1, OnRayCastAction filter, OnRayPrecastAction prefilter, void *const userData);
175  dgInt32 ConvexCast(dgCollision *const shape, const dgMatrix &p0, const dgVector &p1, dgFloat32 &timetoImpact, OnRayPrecastAction prefilter, void *const userData, dgConvexCastReturnInfo *const info, dgInt32 maxContacts, dgInt32 threadIndex);
176  void ForEachBodyInAABB(const dgVector &q0, const dgVector &q1, OnBodiesInAABB callback, void *const userData) const;
177 
178 private:
181 
182  void Init();
183  void Add(dgBody *const body);
184  void Remove(dgBody *const body);
185  void InvalidateCache();
186 
187  dgUnsigned32 UpdateContactsBroadPhaseBegin(dgFloat32 tiemstep, bool collisioUpdate, dgUnsigned32 ticks);
188  void UpdateContactsBroadPhaseEnd(dgFloat32 tiemstep);
189 
190  void UpdateContacts(dgFloat32 tiemstep, bool collisioUpdate);
191  void UpdateContactsSimd(dgFloat32 tiemstep, bool collisioUpdate);
192 
193  void UpdateBodyBroadphase(dgBody *const body, dgInt32 threadIndex);
194 
195  void UpdatePairs(dgBroadPhaseCell &cellA, dgBroadPhaseCell &cellB, dgInt32 threadIndex);
196  void UpdatePairs(dgBody *const body0, dgSortArray::dgListNode *const listNode, dgInt32 axisX, dgInt32 threadIndex);
197 
198  dgVector m_min;
199  dgVector m_max;
200  dgVector m_appMinBox;
201  dgVector m_appMaxBox;
202 
203  dgVector m_boxSize;
204  dgBroadPhaseCell m_inactiveList;
205  dgBroadPhaseLayer m_layerMap[DG_OCTREE_MAX_DEPTH];
206  dgBroadPhaseApplyExternalForce m_applyExtForces[DG_MAXIMUN_THREADS];
207  dgBroadPhaseCellPairsWorkerThread m_cellPairsWorkerThreads[DG_MAXIMUN_THREADS];
208  dgBroadPhaseMaterialCallbackWorkerThread m_materialCallbackWorkerThreads[DG_MAXIMUN_THREADS];
209  dgBroadPhaseCalculateContactsWorkerThread m_calculateContactsWorkerThreads[DG_MAXIMUN_THREADS];
210 
211  // static void ForceAndtorque (void** const m_userParamArray, dgInt32 threadID);
212  // dgWorld* m_me;
213  dgFloat32 m_worlSize;
214 
215  friend class dgBody;
216  friend class dgWorld;
218 };
219 
220 #endif
Definition: dgBody.h:45
Definition: dgBroadPhaseCollision.h:170
Definition: dgBody.h:85
Definition: dgVector.h:86
Definition: dgBroadPhaseCollision.h:66
Definition: dgBody.h:52
Definition: dgBroadPhaseCollision.h:136
Definition: dgThreads.h:28
Definition: dgList.h:33
Definition: dgBroadPhaseCollision.h:146
Definition: dgBroadPhaseCollision.h:158
Definition: dgTree.h:88
Definition: dgBroadPhaseCollision.h:49
Definition: dgBroadPhaseCollision.h:37
Definition: dgCollision.h:178
Definition: dgBroadPhaseCollision.h:92
Definition: dgMatrix.h:41
Definition: dgMemory.h:80
Definition: dgBroadPhaseCollision.h:124
Definition: dgWorld.h:118
Definition: dgBroadPhaseCollision.h:43
Definition: dgContact.h:53