My Project: dynamics.mod/NewtonDynamics/source/physics/dgCollisionConvex.h Source File

My Project

dgCollisionConvex.h
Go to the documentation of this file.
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 _DG_CONVEX_COLLISION_H__
23 #define _DG_CONVEX_COLLISION_H__
24
25 #include "dgCollision.h"
26
27 #define DG_CLIP_MAX_COUNT 512
28 #define DG_CLIP_MAX_POINT_COUNT 64
29
30
31 class dgConvexSimplexEdge
32 {
33 public:
34 dgConvexSimplexEdge* m_twin;
35 dgConvexSimplexEdge* m_next;
36 dgConvexSimplexEdge* m_prev;
37 dgInt32 m_vertex;
38 };
39
40
41 DG_MSC_VECTOR_ALIGMENT
42 class dgCollisionConvex: public dgCollision
43 {
44 class dgMinkFace;
45 class dgMinkHull;
46 class dgPerimenterEdge;
47 class dgCollisionPriority
48 {
49 public:
50 dgCollisionPriority()
51 {
52 // set all entry to be no swap order
53 memset (m_swapPriority, false, sizeof (m_swapPriority));
54 for (dgInt32 i = 0; i < m_nullCollision; i ++) {
55 m_swapPriority[i][m_sphereCollision] = true;
56 m_swapPriority[i][m_capsuleCollision] = true;
57 m_swapPriority[i][m_chamferCylinderCollision] = true;
58 }
59 for (dgInt32 i = m_sphereCollision; i <= m_chamferCylinderCollision; i ++) {
60 for (dgInt32 j = m_sphereCollision; j <= m_chamferCylinderCollision; j ++) {
61 m_swapPriority[i][j] = false;
62 }
63 }
64 }
65 bool m_swapPriority[m_nullCollision][m_nullCollision];
66 };
67
68 public:
69 virtual void CalcAABB (const dgMatrix& matrix, dgVector& p0, dgVector& p1) const;
70
71 virtual dgFloat32 RayCast (const dgVector& localP0, const dgVector& localP1, dgFloat32 maxT, dgContactPoint& contactOut, const dgBody* const body, void* const userData, OnRayPrecastAction preFilter) const;
72 virtual dgFloat32 ConvexRayCast (const dgCollisionInstance* const convexShape, const dgMatrix& origin, const dgVector& veloc, dgFloat32 maxT, dgContactPoint& contactOut, const dgBody* const referenceBody, const dgCollisionInstance* const referenceInstance, void* const userData, dgInt32 threadId) const;
73
74 virtual dgVector SupportVertex (const dgVector& dir, dgInt32* const vertexIndex) const;
75
76 virtual dgInt32 CalculatePlaneIntersection (const dgVector& normal, const dgVector& point, dgVector* const contactsOut, dgFloat32 normalSign) const;
77 virtual dgInt32 GetConvexVertexCount() const { return m_vertexCount;}
78
79 bool IntesectionTest (dgCollisionParamProxy& proxy) const;
80 bool CalculateClosestPoints (dgCollisionParamProxy& proxy) const;
81
82 dgInt32 CalculateConvexCastContacts (dgCollisionParamProxy& proxy) const;
83 dgInt32 CalculateConvexToConvexContact (dgCollisionParamProxy& proxy) const;
84 //dgInt32 ConvexCastContacts (const dgMatrix& matrix, const dgMatrix& invMatrix, const dgVector& veloc, dgFloat32& timeStep, const dgCollisionConvex* const convexShape, dgContactPoint* const contact) const;
85
86 protected:
87 dgCollisionConvex (dgMemoryAllocator* const allocator, dgUnsigned32 signature, dgCollisionID id);
88 dgCollisionConvex (dgWorld* const world, dgDeserialize deserialization, void* const userData);
89 ~dgCollisionConvex ();
90
91 virtual void SerializeLow(dgSerialize callback, void* const userData) const;
92
93 virtual dgVector CalculateVolumeIntegral (const dgMatrix& globalMatrix, const dgVector& plane, const dgCollisionInstance& parentScale) const;
94 static void CalculateInertia (void *userData, int vertexCount, const dgFloat32* const FaceArray, int faceId);
95
96 virtual dgFloat32 GetVolume () const;
97
98 virtual dgFloat32 GetBoxMinRadius () const;
99 virtual dgFloat32 GetBoxMaxRadius () const;
100
101 virtual void* GetUserData () const;
102 virtual void SetUserData (void* const userData);
103
104 dgInt32 RayCastClosestFace (dgVector* tetrahedrum, const dgVector& origin, dgFloat32& pointDist) const;
105 dgInt32 SimplifyClipPolygon (dgInt32 count, const dgVector& normal, dgVector* const polygon) const;
106
107 dgVector CalculateVolumeIntegral (const dgPlane& plane) const;
108
109 void SetVolumeAndCG ();
110 bool SanityCheck (dgPolyhedra& hull) const;
111 virtual void DebugCollision (const dgMatrix& matrix, dgCollision::OnDebugCollisionMeshCallback callback, void* const userData) const;
112
113 virtual void MassProperties ();
114 virtual dgMatrix CalculateInertiaAndCenterOfMass (const dgMatrix& m_alignMatrix, const dgVector& localScale, const dgMatrix& matrix) const;
115 virtual dgFloat32 CalculateMassProperties (const dgMatrix& offset, dgVector& inertia, dgVector& crossInertia, dgVector& centerOfMass) const;
116
117 bool SanityCheck(dgInt32 count, const dgVector& normal, dgVector* const contactsOut) const;
118
119 dgPerimenterEdge* ReduceContacts (dgPerimenterEdge* poly, dgInt32 maxCount) const;
120 dgInt32 RectifyConvexSlice (dgInt32 count, const dgVector& normal, dgVector* const contactsOut) const;
121
122 // special feature based contact calculation for conics convex (ex spheres, capsules, tapered capsules, and chamfered cylinders)
123 // in newton we only deal with sub set of conic function, that can be expressed by the equation
124 // ((x - x0) / a)^2 + ((y - y0) / b)^2 + ((z - z0) / c)^2 = 1 and possible a linear or circular sweep of the same equation
125 // this preclude parabolic and hyperbolic conics
126 virtual dgVector ConvexConicSupporVertex (const dgVector& dir) const;
127 virtual dgVector ConvexConicSupporVertex (const dgVector& point, const dgVector& dir) const;
128 virtual dgInt32 CalculateContacts (const dgVector& point, const dgVector& normal, dgCollisionParamProxy& proxy, dgVector* const contactsOut) const;
129
130 dgInt32 CalculateContactsGeneric (const dgVector& point, const dgVector& normal, dgCollisionParamProxy& proxy, dgVector* const contactsOut) const;
131 dgInt32 ConvexPolygonsIntersection (const dgVector& nornal, dgInt32 count1, dgVector* const shape1, dgInt32 count2, dgVector* const shape2, dgVector* const contactOut, dgInt32 maxContacts) const;
132 dgInt32 ConvexPolygonToLineIntersection (const dgVector& normal, dgInt32 count1, dgVector* const shape1, dgInt32 count2, dgVector* const shape2, dgVector* const contactOut, dgVector* const mem) const;
133 dgFloat32 ConvexConicConvexRayCast (const dgCollisionInstance* const convexConicShape, const dgMatrix& conicShapeMatrix, const dgCollisionInstance* const convexCastingShape, const dgMatrix& castingMatrix, const dgVector& castingVeloc, dgFloat32 maxT, dgContactPoint& contactOut) const;
134
135 virtual const dgConvexSimplexEdge** GetVertexToEdgeMapping() const {return NULL;}
136
137 // dgVector ReduceLine (dgInt32& indexOut, dgVector* const lineDiff, dgVector* const lineSum) const;
138 // dgVector ReduceTriangle (dgInt32& indexOut, dgVector* const triangleDiff, dgVector* const triangleSum) const;
139 // dgVector ReduceTetrahedrum (dgInt32& indexOut, dgVector* const tetraDiff, dgVector* const tetraSum) const;
140 // void ReduceDegeneratedTriangle (dgVector& diff0, dgVector& sum0, dgVector& diff1, dgVector& sum1, const dgVector& diff2, const dgVector& sum2) const;
141 // dgBigVector ReduceLineLarge (dgInt32& indexOut, dgBigVector* const lineDiff, dgBigVector* const lineSum) const;
142 // dgBigVector ReduceTriangleLarge (dgInt32& indexOut, dgBigVector* const triangleDiff, dgBigVector* const triangleSum) const;
143 // dgBigVector ReduceTetrahedrumLarge (dgInt32& indexOut, dgBigVector* const tetraDiff, dgBigVector* const tetraSum) const;
144 // void ReduceDegeneratedTriangleLarge (dgBigVector& diff0, dgBigVector& sum0, dgBigVector& diff1, dgBigVector& sum1, const dgBigVector& diff2, const dgBigVector& sum2) const;
145 // void ConvexSupportVertex (const dgVector& dir, const dgVector& scale1, const dgMatrix& matrix, const dgVector& invScale0, const dgCollisionConvex* const convexShape, dgVector& diff, dgVector& sum) const;
146 // void ConvexConicSupportVertex (const dgVector& dir, const dgVector& scale1, const dgMatrix& matrix, const dgVector& invScale0, const dgCollisionConvex* const convexShape, dgVector& diff, dgVector& sum) const;
147 // void ConvexConicSupportVertexLarge (const dgVector& dir, const dgVector& scale1, const dgMatrix& matrix, const dgVector& invScale0, const dgCollisionConvex* const convexShape, dgBigVector& diff, dgBigVector& sum) const;
148 // dgInt32 CalculateConicClosestSimplex (const dgCollisionConvex* const convexShape, const dgVector& scale1, const dgMatrix& matrix, const dgVector& invScale0, const dgVector& separatingVectorHint, dgVector* const diff, dgVector* const sum, dgVector& normal) const;
149 // dgInt32 CalculateConicClosestSimplexLarge (const dgCollisionConvex* const convexShape, const dgVector& scale1, const dgMatrix& matrix, const dgVector& invScale0, const dgVector& separatingVectorHint, dgVector* const diff, dgVector* const sum, dgVector& normal) const;
150 // dgInt32 CalculateIntersectingPlane (const dgCollisionConvex* const convexShape, const dgVector& scale1, const dgMatrix& matrix, const dgVector& invScale0, const dgVector& separatingVectorHint, dgInt32 count, dgVector* const diff, dgVector* const sum, dgVector& normal) const;
151
152 void* m_userData;
153 dgVector* m_vertex;
154 dgConvexSimplexEdge* m_simplex;
155
156
157 dgFloat32 m_boxMinRadius;
158 dgFloat32 m_boxMaxRadius;
159 dgFloat32 m_simplexVolume;
160
161 dgUnsigned16 m_edgeCount;
162 dgUnsigned16 m_vertexCount;
163
164 public:
165 static dgVector m_hullDirs[14];
166 static dgVector m_dummySum[4];
167 static dgInt32 m_rayCastSimplex[4][4];
168 static dgCollisionPriority m_priorityOrder;
169
170 friend class dgWorld;
171 friend class dgMinkowskiConv;
172 friend class dgCollisionCompound;
173 friend class dgBroadPhase;
174 friend class dgCollisionConvexModifier;
175 } DG_GCC_VECTOR_ALIGMENT;
176
177
178
179 #endif //AFX_DGCONVEXCOLLISION_H__57E159CE_6B6F_42DE_891C_1F6C38EB9D29_H
180
181
dgPlane
Definition: dgPlane.h:29
dgCollisionConvex::dgMinkFace
Definition: dgCollisionConvex.cpp:97
dgCollisionConvex::m_edgeCount
dgUnsigned16 m_edgeCount
Definition: dgCollisionConvex.h:161
dgCollisionConvex::m_boxMaxRadius
dgFloat32 m_boxMaxRadius
Definition: dgCollisionConvex.h:158
dgCollision::OnDebugCollisionMeshCallback
void(dgApi * OnDebugCollisionMeshCallback)(void *userData, int vertexCount, const dgFloat32 *FaceArray, int faceId)
Definition: dgCollision.h:220
dgCollisionConvex::m_vertexCount
dgUnsigned16 m_vertexCount
Definition: dgCollisionConvex.h:162
dgCollisionConvex::m_simplex
dgConvexSimplexEdge * m_simplex
Definition: dgCollisionConvex.h:154
dgBody
Definition: dgBody.h:44
userData
void *const userData
Definition: dgBroadPhase.h:35
dgCollisionConvex::GetConvexVertexCount
virtual dgInt32 GetConvexVertexCount() const
Definition: dgCollisionConvex.h:77
dgFloat32
float dgFloat32
Definition: dgTypes.h:233
dgInt32
int dgInt32
Definition: dgTypes.h:218
dgConvexSimplexEdge
Definition: dgCollisionConvex.h:31
dgVector
Definition: dgVector.h:639
dgConvexSimplexEdge::m_prev
dgConvexSimplexEdge * m_prev
Definition: dgCollisionConvex.h:36
dgCollisionConvex::dgMinkHull
Definition: dgCollisionConvex.cpp:109
dgContactPoint
Definition: dgContact.h:78
dgCollisionConvex::GetVertexToEdgeMapping
virtual const dgConvexSimplexEdge ** GetVertexToEdgeMapping() const
Definition: dgCollisionConvex.h:135
dgCollisionConvex::m_userData
void * m_userData
Definition: dgCollisionConvex.h:152
dgSerialize
void(dgApi * dgSerialize)(void *const userData, const void *const buffer, size_t size)
Definition: dgTypes.h:280
DG_MSC_VECTOR_ALIGMENT
#define DG_MSC_VECTOR_ALIGMENT
Definition: dgTypes.h:194
dgPolyhedra
Definition: dgPolyhedra.h:62
dgBroadPhase
Definition: dgBroadPhase.h:65
dgDeserialize
void(dgApi * dgDeserialize)(void *const userData, void *buffer, size_t size)
Definition: dgTypes.h:279
dgCollisionConvex::m_simplexVolume
dgFloat32 m_simplexVolume
Definition: dgCollisionConvex.h:159
DG_GCC_VECTOR_ALIGMENT
dgCollisionConvex DG_GCC_VECTOR_ALIGMENT
OnRayPrecastAction
dgUnsigned32(dgApi * OnRayPrecastAction)(const dgBody *const body, const dgCollisionInstance *const collision, void *const userData)
Definition: dgBroadPhase.h:36
dgCollisionInstance
Definition: dgCollisionInstance.h:30
dgConvexSimplexEdge::m_vertex
dgInt32 m_vertex
Definition: dgCollisionConvex.h:37
m_sphereCollision
Definition: dgCollision.h:55
dgUnsigned32
unsigned dgUnsigned32
Definition: dgTypes.h:219
m_nullCollision
Definition: dgCollision.h:65
dgCollisionConvex
Definition: dgCollisionConvex.h:42
dgConvexSimplexEdge::m_twin
dgConvexSimplexEdge * m_twin
Definition: dgCollisionConvex.h:34
dgCollision.h
dgCollision
Definition: dgCollision.h:217
dgUnsigned16
unsigned short dgUnsigned16
Definition: dgTypes.h:216
dgMatrix
Definition: dgMatrix.h:40
dgMemoryAllocator
Definition: dgMemory.h:73
dgCollisionConvex::m_vertex
dgVector * m_vertex
Definition: dgCollisionConvex.h:153
m_chamferCylinderCollision
Definition: dgCollision.h:57
dgConvexSimplexEdge::m_next
dgConvexSimplexEdge * m_next
Definition: dgCollisionConvex.h:35
dgWorld
Definition: dgWorld.h:129
dgCollisionConvex::m_priorityOrder
static dgCollisionPriority m_priorityOrder
Definition: dgCollisionConvex.h:168
dgCollisionConvex::dgPerimenterEdge
Definition: dgCollisionConvex.cpp:85
dgCollisionConvex::m_boxMinRadius
dgFloat32 m_boxMinRadius
Definition: dgCollisionConvex.h:157
m_capsuleCollision
Definition: dgCollision.h:56
dgCollisionID
dgCollisionID
Definition: dgCollision.h:52
dgCollisionCompound
Definition: dgCollisionCompound.h:35
dgCollisionParamProxy
Definition: dgContact.h:93