ScummVM API documentation
dgCollision.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_DGCOLLISION_H__57E159CE_6B6F_42DE_891C_1F6C38EB9D29__INCLUDED_
23 #define AFX_DGCOLLISION_H__57E159CE_6B6F_42DE_891C_1F6C38EB9D29__INCLUDED_
24 
25 #include "dgBody.h"
26 #include "hpl1/engine/libraries/newton/core/dg.h"
27 
28 class dgBody;
29 class dgCollision;
30 class dgMeshEffect;
31 class dgContactPoint;
32 class dgPolygonSoupDesc;
33 class dgCollisionConvex;
35 class dgPolygonSoupRayHitDesc;
36 
37 #define DG_MAX_COLLISION_PADDING dgFloat32(1.0f / 8.0f)
38 //#define DG_MAX_COLLISION_PADDING dgFloat32 (1.0f)
39 
40 typedef dgInt32(*dgCollisionCompoundBreakableCallback)(dgMeshEffect *const solid, void *userData, dgMatrix &planeMatrixOut);
41 
42 #ifdef _DEBUG
43 // #define DG_DEBUG_AABB
44 #endif
45 
46 //#define SERIALIZE_END 'dne '
47 #define SERIALIZE_END 0x646e6520
48 #define PREFILTER_RAYCAST(filter, body, collision, userData) (filter && !filter(body, collision, userData))
49 
50 enum dgCollisionID {
51  m_boxCollision = 0,
52  m_coneCollision,
53  m_sphereCollision,
54  m_capsuleCollision,
55  m_cylinderCollision,
56  m_compoundCollision,
57  m_convexHullCollision,
58  m_convexCollisionModifier,
59  m_chamferCylinderCollision,
60  m_boundingBoxHierachy,
61  m_nullCollision,
62  m_heightField,
63  m_userMeshCollision,
64  m_sceneCollision,
65  m_compoundBreakable,
66 
67  m_polygonCollision,
68  m_ellipseCollision,
69  m_convexConvexIntance,
70 
71 };
72 
74 public:
75  struct dgBoxData {
76  dgFloat32 m_x;
77  dgFloat32 m_y;
78  dgFloat32 m_z;
79  };
80 
81  struct dgSphereData {
82  dgFloat32 m_r0;
83  dgFloat32 m_r1;
84  dgFloat32 m_r2;
85  };
86 
87  struct dgCylinderData {
88  dgFloat32 m_r0;
89  dgFloat32 m_r1;
90  dgFloat32 m_height;
91  };
92 
93  struct dgCapsuleData {
94  dgFloat32 m_r0;
95  dgFloat32 m_r1;
96  dgFloat32 m_height;
97  };
98 
99  struct dgConeData {
100  dgFloat32 m_r;
101  dgFloat32 m_height;
102  };
103 
105  dgFloat32 m_r;
106  dgFloat32 m_height;
107  };
108 
110  dgInt32 m_vertexCount;
111  dgInt32 m_strideInBytes;
112  dgInt32 m_faceCount;
113  dgVector *m_vertex;
114  };
115 
117  dgCollision *m_child;
118  };
119 
121  dgInt32 m_chidrenCount;
122  dgCollision **m_chidren;
123  };
124 
126  dgInt32 m_vertexCount;
127  dgInt32 m_indexCount;
128  };
129 
131  dgInt32 m_width;
132  dgInt32 m_height;
133  dgInt32 m_gridsDiagonals;
134  dgFloat32 m_horizonalScale;
135  dgFloat32 m_verticalScale;
136  dgUnsigned16 *m_elevation;
137  dgInt8 *m_atributes;
138  };
139 
140  struct dgSceneData {
141  dgInt32 m_childrenProxyCount;
142  };
143 
144  dgMatrix m_offsetMatrix;
145  dgInt32 m_collisionType;
146  dgInt32 m_refCount;
147  dgInt32 m_userDadaID;
148  union {
149  dgBoxData m_box;
150  dgConeData m_cone;
151  dgSphereData m_sphere;
152  dgCapsuleData m_capsule;
153  dgCylinderData m_cylinder;
154  dgChamferCylinderData m_chamferCylinder;
155  dgConvexHullData m_convexHull;
156  dgConvexModifierData m_convexModifierData;
157  dgCoumpountCollisionData m_compoundCollision;
158  dgCollisionBVHData m_bvhCollision;
159  dgHeightMapCollisionData m_heightFieldCollision;
160  dgSceneData m_sceneCollision;
161  dgFloat32 m_paramArray[32];
162  };
163 };
164 
165 typedef dgInt32(dgApi *OnCompoundCollisionPrefilter)(const dgBody *bodyA, const dgCollision *collisionA, const dgBody *bodyB, const dgCollision *collisionB);
166 typedef void(dgApi *OnDebugCollisionMeshCallback)(void *userData, int vertexCount, const dgFloat32 *FaceArray, int faceId);
167 
169 public:
171  for (unsigned i = 0; i < sizeof(m_planes) / sizeof(m_planes[0]); i++)
172  m_planes[i] = dgPlane(0.0, 0.0, 0.0, 0.0);
173  }
174  dgPlane m_planes[2];
175 };
176 
177 DG_MSC_VECTOR_ALIGMENT
178 class dgCollision { //: public dgRef
179 public:
180  enum RTTI {
181  dgCollisionNull_RTTI = 1 << 0,
182  dgCollisionBox_RTTI = 1 << 1,
183  dgCollisionCone_RTTI = 1 << 2,
184  dgCollisionSphere_RTTI = 1 << 3,
185  dgCollisionEllipse_RTTI = 1 << 4,
186  dgCollisionCapsule_RTTI = 1 << 5,
187  dgCollisionCylinder_RTTI = 1 << 6,
188  dgCollisionConvexHull_RTTI = 1 << 7,
189  dgCollisionChamferCylinder_RTTI = 1 << 8,
190  dgCollisionConvexModifier_RTTI = 1 << 9,
191  dgCollisionConvexPolygon_RTTI = 1 << 10,
192  dgConvexCollision_RTTI = 1 << 11,
193  dgCollisionCompound_RTTI = 1 << 12,
194 
195  dgCollisionBVH_RTTI = 1 << 13,
196  dgCollisionMesh_RTTI = 1 << 14,
197  dgCollisionUserMesh_RTTI = 1 << 15,
198  dgCollisionHeightField_RTTI = 1 << 16,
199  dgCollisionScene_RTTI = 1 << 17,
200  dgCollisionCompoundBreakable_RTTI = 1 << 18,
201  };
202 
203  DG_CLASS_ALLOCATOR(allocator)
204 
205  const dgMatrix &GetOffsetMatrix() const;
206  void SetOffsetMatrix(const dgMatrix &matrix);
207 
208  dgCollisionID GetCollisionPrimityType() const;
209  static dgUnsigned32 Quantize(dgFloat32 value);
210  static dgUnsigned32 MakeCRC(void *buffer, int size);
211 
212  dgUnsigned32 SetUserDataID() const;
213  void SetUserDataID(dgUnsigned32 userData);
214 
215  dgInt32 IsType(RTTI type) const {
216  return type & m_rtti;
217  }
218 
219  virtual void *GetUserData() const;
220  virtual void SetUserData(void *const userData);
221 
222  virtual dgVector SupportVertex(const dgVector &dir) const = 0;
223  virtual void SetCollisionBBox(const dgVector &p0, const dgVector &p1) = 0;
224  virtual void CalcAABB(const dgMatrix &matrix, dgVector &p0, dgVector &p1) const = 0;
225  virtual void CalcAABBSimd(const dgMatrix &matrix, dgVector &p0, dgVector &p1) const = 0;
226  virtual bool OOBBTest(const dgMatrix &matrix, const dgCollisionConvex *const shape, void *const cacheOrder) const = 0;
227  virtual bool IsEdgeIntersection() const;
228 
229  virtual void DebugCollision(const dgMatrix &matrix, OnDebugCollisionMeshCallback callback, void *const userData) const = 0;
230  virtual dgFloat32 RayCast(const dgVector &localP0, const dgVector &localP1, dgContactPoint &contactOut, OnRayPrecastAction preFilter, const dgBody *const body, void *const userData) const = 0;
231  virtual dgFloat32 RayCastSimd(const dgVector &localP0, const dgVector &localP1, dgContactPoint &contactOut, OnRayPrecastAction preFilter, const dgBody *const body, void *const userData) const {
232  NEWTON_ASSERT(0);
233  return 0;
234  };
235  virtual dgFloat32 GetVolume() const = 0;
236 
237  virtual dgFloat32 GetBoxMinRadius() const = 0;
238  virtual dgFloat32 GetBoxMaxRadius() const = 0;
239  virtual void CalculateInertia(dgVector &inertia, dgVector &origin) const = 0;
240  virtual dgVector CalculateVolumeIntegral(const dgMatrix &globalMatrix, GetBuoyancyPlane bouyancyPlane, void *const context) const = 0;
241  virtual void Serialize(dgSerialize callback, void *const userData) const = 0;
242 
243  virtual void GetCollisionInfo(dgCollisionInfo *info) const;
244  virtual void SerializeLow(dgSerialize callback, void *const userData) const;
245 
246  dgUnsigned32 GetSignature() const;
247 
248  // interface for the convex hull modifier
249  virtual dgMatrix ModifierGetMatrix() const;
250  virtual void ModifierSetMatrix(const dgMatrix &matrix);
251 
252  virtual bool IsTriggerVolume() const;
253  virtual void SetAsTriggerVolume(bool mode);
254 
255  virtual void SetBreakImpulse(dgFloat32 force);
256  virtual dgFloat32 GetBreakImpulse() const;
257 
258  dgCollision *AddRef();
259  virtual dgInt32 Release();
260  dgInt32 GetRefCount() const;
261 
262  dgMemoryAllocator *GetAllocator() const;
263 
264 protected:
265  dgCollision(dgMemoryAllocator *const allocator, dgUnsigned32 signature, const dgMatrix &matrix, dgCollisionID id);
266  dgCollision(dgWorld *const world, dgDeserialize deserialization, void *const userData);
267  virtual ~dgCollision();
268 
269  void SetSignature(dgInt32 signature);
270  virtual dgInt32 CalculateSignature() const = 0;
271 
272  dgMatrix m_offset;
273  dgMemoryAllocator *m_allocator;
274  dgInt32 m_rtti;
275  dgInt32 m_refCount;
276  dgUnsigned32 m_userDataID;
277  dgUnsigned32 m_signature;
278  dgCollisionID m_collsionId;
279 
280 private:
281  // dgAddRtti(dgRef);
282 
283  friend class dgBody;
284  friend class dgWorld;
285  friend class dgMinkowskiConv;
286  friend class dgCollisionCompound;
287 } DG_GCC_VECTOR_ALIGMENT;
288 
289 inline dgCollisionID dgCollision::GetCollisionPrimityType() const {
290  return m_collsionId;
291 }
292 
293 inline dgUnsigned32 dgCollision::GetSignature() const {
294  return m_signature;
295 }
296 
297 inline void dgCollision::SetSignature(dgInt32 signature) {
298  m_signature = dgUnsigned32(signature);
299 }
300 
301 inline const dgMatrix &dgCollision::GetOffsetMatrix() const {
302  return m_offset;
303 }
304 
305 inline dgMatrix dgCollision::ModifierGetMatrix() const {
306  return dgGetIdentityMatrix();
307 }
308 
309 inline void dgCollision::ModifierSetMatrix(const dgMatrix &matrix) {
310 }
311 
312 inline void dgCollision::SetAsTriggerVolume(bool mode) {
313 }
314 
315 inline bool dgCollision::IsTriggerVolume() const {
316  return false;
317 }
318 
319 inline dgUnsigned32 dgCollision::SetUserDataID() const {
320  return m_userDataID;
321 }
322 
323 inline void dgCollision::SetUserDataID(dgUnsigned32 userData) {
324  m_userDataID = userData;
325 }
326 
327 inline dgCollision *dgCollision::AddRef() {
328  m_refCount++;
329  return this;
330 }
331 
332 inline dgInt32 dgCollision::Release() {
333  m_refCount--;
334  if (m_refCount) {
335  return m_refCount;
336  }
337  delete this;
338  return 0;
339 }
340 
341 inline dgInt32 dgCollision::GetRefCount() const {
342  return m_refCount;
343 }
344 
345 inline void dgCollision::SetBreakImpulse(dgFloat32 force) {
346 }
347 
348 inline dgFloat32 dgCollision::GetBreakImpulse() const {
349  return dgFloat32(1.0e10f);
350 }
351 
352 inline dgMemoryAllocator *dgCollision::GetAllocator() const {
353  return m_allocator;
354 }
355 
356 inline bool dgCollision::IsEdgeIntersection() const {
357  return false;
358 }
359 
360 #endif
Definition: dgPlane.h:29
Definition: dgCollision.h:140
Definition: dgCollision.h:168
Definition: dgCollision.h:81
Definition: dgCollision.h:93
Definition: dgCollision.h:73
Definition: dgBody.h:85
Definition: dgCollision.h:109
Definition: dgVector.h:86
Definition: dgCollision.h:99
Definition: dgCollision.h:75
Definition: dgMeshEffect.h:78
Definition: dgContact.h:88
Definition: dgCollision.h:116
Definition: dgCollisionConvex.h:39
Definition: dgCollision.h:178
Definition: dgMatrix.h:41
Definition: dgMemory.h:80
Definition: dgCollision.h:104
Definition: dgWorld.h:118
Definition: dgCollision.h:125
Definition: dgCollisionConvexHull.h:28
Definition: dgCollision.h:87
Definition: dgCollision.h:120
Definition: dgCollisionCompound.h:31
Definition: dgCollision.h:130