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

My Project

dgCollisionHeightField.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 __DGCOLLISION_HEIGHT_FIELD__
23 #define __DGCOLLISION_HEIGHT_FIELD__
24
25 #include "dgCollision.h"
26 #include "dgCollisionMesh.h"
27
28 class dgCollisionHeightField;
29 typedef dgFloat32 (*dgCollisionHeightFieldRayCastCallback) (const dgBody* const body, const dgCollisionHeightField* const heightFieldCollision, dgFloat32 interception, dgInt32 row, dgInt32 col, dgVector* const normal, int faceId, void* const usedData);
30
31
32 class dgCollisionHeightField: public dgCollisionMesh
33 {
34 public:
35 enum dgElevationType
36 {
37 m_float32Bit = 0,
38 m_unsigned16Bit,
39 };
40
41 enum dgCollisionHeightFieldGridConstruction
42 {
43 m_normalDiagonals = 0,
44 m_invertedDiagonals,
45 m_alternateOddRowsDiagonals,
46 m_alternateEvenRowsDiagonals,
47 m_alternateOddColumsDiagonals,
48 m_alternateEvenColumsDiagonals,
49 m_starDiagonals,
50 m_starInvertexDiagonals,
51 };
52 dgCollisionHeightField (dgWorld* const world, dgInt32 width, dgInt32 height, dgInt32 contructionMode,
53 const void* const elevationMap, dgElevationType elevationDataType, dgFloat32 verticalScale,
54 const dgInt8* const atributeMap, dgFloat32 horizontalScale);
55
56 dgCollisionHeightField (dgWorld* const world, dgDeserialize deserialization, void* const userData);
57
58 virtual ~dgCollisionHeightField(void);
59
60 void SetCollisionRayCastCallback (dgCollisionHeightFieldRayCastCallback rayCastCallback);
61 dgCollisionHeightFieldRayCastCallback GetDebugRayCastCallback() const { return m_userRayCastCallback;}
62
63
64 private:
65 class dgPerIntanceData
66 {
67 public:
68 dgWorld* m_world;
69 dgInt32 m_refCount;
70 dgInt32 m_vertexCount[DG_MAX_THREADS_HIVE_COUNT];
71 dgVector *m_vertex[DG_MAX_THREADS_HIVE_COUNT];
72 };
73
74 void CalculateAABB();
75
76 void AllocateVertex(dgWorld* const world, dgInt32 thread) const;
77 void CalculateMinExtend2d (const dgVector& p0, const dgVector& p1, dgVector& boxP0, dgVector& boxP1) const;
78 void CalculateMinExtend3d (const dgVector& p0, const dgVector& p1, dgVector& boxP0, dgVector& boxP1) const;
79 dgFloat32 RayCastCell (const dgFastRayTest& ray, dgInt32 xIndex0, dgInt32 zIndex0, dgVector& normalOut, dgFloat32 maxT) const;
80
81 virtual void Serialize(dgSerialize callback, void* const userData) const;
82 virtual dgFloat32 RayCast (const dgVector& localP0, const dgVector& localP1, dgFloat32 maxT, dgContactPoint& contactOut, const dgBody* const body, void* const userData, OnRayPrecastAction preFilter) const;
83 virtual void GetCollidingFaces (dgPolygonMeshDesc* const data) const;
84
85 virtual void GetCollisionInfo(dgCollisionInfo* const info) const;
86 virtual dgVector SupportVertex (const dgVector& dir, dgInt32* const vertexIndex) const;
87 virtual void DebugCollision (const dgMatrix& matrixPtr, dgCollision::OnDebugCollisionMeshCallback callback, void* const userData) const;
88 void GetVertexListIndexList (const dgVector& p0, const dgVector& p1, dgMeshVertexListIndexList &data) const;
89 void GetLocalAABB (const dgVector& p0, const dgVector& p1, dgVector& boxP0, dgVector& boxP1) const;
90
91 dgVector m_minBox;
92 dgVector m_maxBox;
93
94 dgInt32 m_width;
95 dgInt32 m_height;
96 dgInt32 m_diagonalMode;
97 dgInt8* m_atributeMap;
98 dgInt8* m_diagonals;
99 void* m_elevationMap;
100 dgFloat32 m_verticalScale;
101 dgFloat32 m_horizontalScale;
102 dgFloat32 m_horizontalScaleInv;
103 dgCollisionHeightFieldRayCastCallback m_userRayCastCallback;
104 dgElevationType m_elevationDataType;
105
106 static dgVector m_yMask;
107 static dgVector m_padding;
108 static dgInt32 m_cellIndices[][4];
109 static dgInt32 m_verticalEdgeMap[][7];
110 static dgInt32 m_horizontalEdgeMap[][7];
111
112 dgPerIntanceData* m_instanceData;
113 friend class dgCollisionCompound;
114 };
115
116
117 DG_INLINE void dgCollisionHeightField::CalculateMinExtend2d (const dgVector& p0, const dgVector& p1, dgVector& boxP0, dgVector& boxP1) const
118 {
119 dgVector q0 (p0.GetMin(p1) - m_padding);
120 dgVector q1 (p0.GetMax(p1) + m_padding);
121
122 boxP0 = ((q0.Scale4(m_horizontalScaleInv).Floor().Scale4(m_horizontalScale) & m_yMask) + dgVector(dgFloat32 (-1.0e10f)).AndNot(m_yMask)) & dgVector::m_triplexMask;
123 boxP1 = (((q1.Scale4(m_horizontalScaleInv).Floor().Scale4(m_horizontalScale) + dgVector(m_horizontalScale)) & m_yMask) + dgVector(dgFloat32 (1.0e10f)).AndNot(m_yMask)) & dgVector::m_triplexMask;
124
125 dgVector minBox ((m_minBox & m_yMask) + boxP0.AndNot(m_yMask));
126 dgVector maxBox ((m_maxBox & m_yMask) + boxP1.AndNot(m_yMask));
127 boxP0 = boxP0.GetMax (minBox);
128 boxP1 = boxP1.GetMin (maxBox);
129 }
130
131
132 DG_INLINE void dgCollisionHeightField::CalculateMinExtend3d (const dgVector& p0, const dgVector& p1, dgVector& boxP0, dgVector& boxP1) const
133 {
134 dgAssert (p0.m_x <= p1.m_x);
135 dgAssert (p0.m_y <= p1.m_y);
136 dgAssert (p0.m_z <= p1.m_z);
137 dgAssert (p0.m_w == dgFloat32 (0.0f));
138 dgAssert (p1.m_w == dgFloat32 (0.0f));
139
140 dgVector q0 (p0 - m_padding);
141 dgVector q1 (p1 + m_padding);
142
143 boxP0 = (q0.Scale4(m_horizontalScaleInv).Floor().Scale4(m_horizontalScale) & m_yMask) + q0.AndNot(m_yMask);
144 boxP1 = ((q1.Scale4(m_horizontalScaleInv).Floor().Scale4(m_horizontalScale) + dgVector(m_horizontalScale)) & m_yMask) + q1.AndNot(m_yMask);
145
146 dgVector minBox ((m_minBox & m_yMask) + boxP0.AndNot(m_yMask));
147 dgVector maxBox ((m_maxBox & m_yMask) + boxP1.AndNot(m_yMask));
148 boxP0 = boxP0.GetMax (minBox);
149 boxP1 = boxP1.GetMin (maxBox);
150 }
151
152
153 #endif
dgCollisionHeightField::dgElevationType
dgElevationType
Definition: dgCollisionHeightField.h:35
dgCollisionHeightField::m_alternateOddColumsDiagonals
Definition: dgCollisionHeightField.h:47
dgCollision::OnDebugCollisionMeshCallback
void(dgApi * OnDebugCollisionMeshCallback)(void *userData, int vertexCount, const dgFloat32 *FaceArray, int faceId)
Definition: dgCollision.h:220
dgFastRayTest
Definition: dgIntersections.h:52
dgCollisionInfo
Definition: dgCollision.h:82
dgBody
Definition: dgBody.h:44
dgCollisionCompound::m_padding
static dgVector m_padding
Definition: dgCollisionCompound.h:234
dgCollisionHeightField::m_alternateEvenRowsDiagonals
Definition: dgCollisionHeightField.h:46
userData
void *const userData
Definition: dgBroadPhase.h:35
DG_INLINE
#define DG_INLINE
Definition: dgTypes.h:178
dgAssert
#define dgAssert(x)
Definition: dgTypes.h:158
dgFloat32
float dgFloat32
Definition: dgTypes.h:233
dgInt32
int dgInt32
Definition: dgTypes.h:218
dgPolygonMeshDesc
Definition: dgCollisionMesh.h:42
dgVector
Definition: dgVector.h:639
dgCollisionHeightField::GetDebugRayCastCallback
dgCollisionHeightFieldRayCastCallback GetDebugRayCastCallback() const
Definition: dgCollisionHeightField.h:61
dgCollisionHeightField::dgCollisionHeightFieldGridConstruction
dgCollisionHeightFieldGridConstruction
Definition: dgCollisionHeightField.h:41
dgVector::m_z
dgFloat32 m_z
Definition: dgVector.h:1027
dgCollision::m_refCount
dgInt32 m_refCount
Definition: dgCollision.h:308
dgContactPoint
Definition: dgContact.h:78
dgSerialize
void(dgApi * dgSerialize)(void *const userData, const void *const buffer, size_t size)
Definition: dgTypes.h:280
dgVector::m_triplexMask
static dgVector m_triplexMask
Definition: dgVector.h:1045
dgCollisionHeightField::m_normalDiagonals
Definition: dgCollisionHeightField.h:43
dgVector::m_x
dgFloat32 m_x
Definition: dgVector.h:1025
DG_MAX_THREADS_HIVE_COUNT
#define DG_MAX_THREADS_HIVE_COUNT
Definition: dgTypes.h:164
dgDeserialize
void(dgApi * dgDeserialize)(void *const userData, void *buffer, size_t size)
Definition: dgTypes.h:279
dgInt8
char dgInt8
Definition: dgTypes.h:212
dgCollisionMesh.h
OnRayPrecastAction
dgUnsigned32(dgApi * OnRayPrecastAction)(const dgBody *const body, const dgCollisionInstance *const collision, void *const userData)
Definition: dgBroadPhase.h:36
dgCollisionMesh
Definition: dgCollisionMesh.h:147
dgVector::AndNot
DG_INLINE dgVector AndNot(const dgVector &data) const
Definition: dgVector.h:966
dgCollisionHeightField::m_starDiagonals
Definition: dgCollisionHeightField.h:49
dgCollisionHeightField
Definition: dgCollisionHeightField.h:32
dgCollision.h
dgCollisionMesh::dgMeshVertexListIndexList
Definition: dgCollisionMesh.h:152
dgCollisionHeightField::m_starInvertexDiagonals
Definition: dgCollisionHeightField.h:50
dgMatrix
Definition: dgMatrix.h:40
dgCollisionHeightField::m_unsigned16Bit
Definition: dgCollisionHeightField.h:38
dgCollisionHeightField::~dgCollisionHeightField
virtual ~dgCollisionHeightField(void)
Definition: dgCollisionHeightField.cpp:310
dgWorld
Definition: dgWorld.h:129
dgCollisionHeightFieldRayCastCallback
dgFloat32(* dgCollisionHeightFieldRayCastCallback)(const dgBody *const body, const dgCollisionHeightField *const heightFieldCollision, dgFloat32 interception, dgInt32 row, dgInt32 col, dgVector *const normal, int faceId, void *const usedData)
Definition: dgCollisionHeightField.h:29
dgCollisionHeightField::SetCollisionRayCastCallback
void SetCollisionRayCastCallback(dgCollisionHeightFieldRayCastCallback rayCastCallback)
Definition: dgCollisionHeightField.cpp:361
dgVector::GetMin
dgVector GetMin(const dgVector &data) const
Definition: dgVector.h:874
dgVector::GetMax
dgVector GetMax(const dgVector &data) const
Definition: dgVector.h:869
dgVector::m_w
dgFloat32 m_w
Definition: dgVector.h:1028
dgCollisionHeightField::m_alternateEvenColumsDiagonals
Definition: dgCollisionHeightField.h:48
dgCollisionHeightField::m_invertedDiagonals
Definition: dgCollisionHeightField.h:44
dgCollisionHeightField::m_float32Bit
Definition: dgCollisionHeightField.h:37
dgCollisionHeightField::dgCollisionHeightField
dgCollisionHeightField(dgWorld *const world, dgInt32 width, dgInt32 height, dgInt32 contructionMode, const void *const elevationMap, dgElevationType elevationDataType, dgFloat32 verticalScale, const dgInt8 *const atributeMap, dgFloat32 horizontalScale)
Definition: dgCollisionHeightField.cpp:56
dgCollisionHeightField::m_alternateOddRowsDiagonals
Definition: dgCollisionHeightField.h:45
dgCollisionCompound
Definition: dgCollisionCompound.h:35
dgVector::m_y
dgFloat32 m_y
Definition: dgVector.h:1026