diff options
Diffstat (limited to 'libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/BoxShape.cs')
-rw-r--r-- | libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/BoxShape.cs | 632 |
1 files changed, 316 insertions, 316 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/BoxShape.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/BoxShape.cs index ba6f3b7..7fab5c7 100644 --- a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/BoxShape.cs +++ b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/BoxShape.cs | |||
@@ -1,316 +1,316 @@ | |||
1 | /* | 1 | /* |
2 | Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru | 2 | Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru |
3 | Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com | 3 | Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com |
4 | 4 | ||
5 | This software is provided 'as-is', without any express or implied | 5 | This software is provided 'as-is', without any express or implied |
6 | warranty. In no event will the authors be held liable for any damages | 6 | warranty. In no event will the authors be held liable for any damages |
7 | arising from the use of this software. | 7 | arising from the use of this software. |
8 | 8 | ||
9 | Permission is granted to anyone to use this software for any purpose, | 9 | Permission is granted to anyone to use this software for any purpose, |
10 | including commercial applications, and to alter it and redistribute it | 10 | including commercial applications, and to alter it and redistribute it |
11 | freely, subject to the following restrictions: | 11 | freely, subject to the following restrictions: |
12 | 12 | ||
13 | 1. The origin of this software must not be misrepresented; you must not | 13 | 1. The origin of this software must not be misrepresented; you must not |
14 | claim that you wrote the original software. If you use this software | 14 | claim that you wrote the original software. If you use this software |
15 | in a product, an acknowledgment in the product documentation would be | 15 | in a product, an acknowledgment in the product documentation would be |
16 | appreciated but is not required. | 16 | appreciated but is not required. |
17 | 2. Altered source versions must be plainly marked as such, and must not be | 17 | 2. Altered source versions must be plainly marked as such, and must not be |
18 | misrepresented as being the original software. | 18 | misrepresented as being the original software. |
19 | 3. This notice may not be removed or altered from any source distribution. | 19 | 3. This notice may not be removed or altered from any source distribution. |
20 | */ | 20 | */ |
21 | 21 | ||
22 | using System; | 22 | using System; |
23 | using System.Collections.Generic; | 23 | using System.Collections.Generic; |
24 | using System.Text; | 24 | using System.Text; |
25 | using MonoXnaCompactMaths; | 25 | using MonoXnaCompactMaths; |
26 | 26 | ||
27 | namespace XnaDevRu.BulletX | 27 | namespace XnaDevRu.BulletX |
28 | { | 28 | { |
29 | public class BoxShape : PolyhedralConvexShape | 29 | public class BoxShape : PolyhedralConvexShape |
30 | { | 30 | { |
31 | public BoxShape(Vector3 boxHalfExtents) | 31 | public BoxShape(Vector3 boxHalfExtents) |
32 | { | 32 | { |
33 | ImplicitShapeDimensions = boxHalfExtents; | 33 | ImplicitShapeDimensions = boxHalfExtents; |
34 | } | 34 | } |
35 | 35 | ||
36 | public override int VertexCount | 36 | public override int VertexCount |
37 | { | 37 | { |
38 | get | 38 | get |
39 | { | 39 | { |
40 | return 8; | 40 | return 8; |
41 | } | 41 | } |
42 | } | 42 | } |
43 | 43 | ||
44 | public override int EdgeCount | 44 | public override int EdgeCount |
45 | { | 45 | { |
46 | get | 46 | get |
47 | { | 47 | { |
48 | return 12; | 48 | return 12; |
49 | } | 49 | } |
50 | } | 50 | } |
51 | 51 | ||
52 | public override BroadphaseNativeTypes ShapeType | 52 | public override BroadphaseNativeTypes ShapeType |
53 | { | 53 | { |
54 | get | 54 | get |
55 | { | 55 | { |
56 | return BroadphaseNativeTypes.Box; | 56 | return BroadphaseNativeTypes.Box; |
57 | } | 57 | } |
58 | } | 58 | } |
59 | 59 | ||
60 | public override string Name | 60 | public override string Name |
61 | { | 61 | { |
62 | get | 62 | get |
63 | { | 63 | { |
64 | return "Box"; | 64 | return "Box"; |
65 | } | 65 | } |
66 | } | 66 | } |
67 | 67 | ||
68 | public override int PreferredPenetrationDirectionsCount | 68 | public override int PreferredPenetrationDirectionsCount |
69 | { | 69 | { |
70 | get | 70 | get |
71 | { | 71 | { |
72 | return 6; | 72 | return 6; |
73 | } | 73 | } |
74 | } | 74 | } |
75 | 75 | ||
76 | public override int PlaneCount | 76 | public override int PlaneCount |
77 | { | 77 | { |
78 | get | 78 | get |
79 | { | 79 | { |
80 | return 6; | 80 | return 6; |
81 | } | 81 | } |
82 | } | 82 | } |
83 | 83 | ||
84 | public Vector3 HalfExtents { get { return ImplicitShapeDimensions * LocalScaling; } } | 84 | public Vector3 HalfExtents { get { return ImplicitShapeDimensions * LocalScaling; } } |
85 | 85 | ||
86 | public override void GetEdge(int i, out Vector3 pa, out Vector3 pb) | 86 | public override void GetEdge(int i, out Vector3 pa, out Vector3 pb) |
87 | { | 87 | { |
88 | int edgeVert0 = 0; | 88 | int edgeVert0 = 0; |
89 | int edgeVert1 = 0; | 89 | int edgeVert1 = 0; |
90 | 90 | ||
91 | switch (i) | 91 | switch (i) |
92 | { | 92 | { |
93 | case 0: | 93 | case 0: |
94 | edgeVert0 = 0; | 94 | edgeVert0 = 0; |
95 | edgeVert1 = 1; | 95 | edgeVert1 = 1; |
96 | break; | 96 | break; |
97 | case 1: | 97 | case 1: |
98 | edgeVert0 = 0; | 98 | edgeVert0 = 0; |
99 | edgeVert1 = 2; | 99 | edgeVert1 = 2; |
100 | break; | 100 | break; |
101 | case 2: | 101 | case 2: |
102 | edgeVert0 = 1; | 102 | edgeVert0 = 1; |
103 | edgeVert1 = 3; | 103 | edgeVert1 = 3; |
104 | 104 | ||
105 | break; | 105 | break; |
106 | case 3: | 106 | case 3: |
107 | edgeVert0 = 2; | 107 | edgeVert0 = 2; |
108 | edgeVert1 = 3; | 108 | edgeVert1 = 3; |
109 | break; | 109 | break; |
110 | case 4: | 110 | case 4: |
111 | edgeVert0 = 0; | 111 | edgeVert0 = 0; |
112 | edgeVert1 = 4; | 112 | edgeVert1 = 4; |
113 | break; | 113 | break; |
114 | case 5: | 114 | case 5: |
115 | edgeVert0 = 1; | 115 | edgeVert0 = 1; |
116 | edgeVert1 = 5; | 116 | edgeVert1 = 5; |
117 | 117 | ||
118 | break; | 118 | break; |
119 | case 6: | 119 | case 6: |
120 | edgeVert0 = 2; | 120 | edgeVert0 = 2; |
121 | edgeVert1 = 6; | 121 | edgeVert1 = 6; |
122 | break; | 122 | break; |
123 | case 7: | 123 | case 7: |
124 | edgeVert0 = 3; | 124 | edgeVert0 = 3; |
125 | edgeVert1 = 7; | 125 | edgeVert1 = 7; |
126 | break; | 126 | break; |
127 | case 8: | 127 | case 8: |
128 | edgeVert0 = 4; | 128 | edgeVert0 = 4; |
129 | edgeVert1 = 5; | 129 | edgeVert1 = 5; |
130 | break; | 130 | break; |
131 | case 9: | 131 | case 9: |
132 | edgeVert0 = 4; | 132 | edgeVert0 = 4; |
133 | edgeVert1 = 6; | 133 | edgeVert1 = 6; |
134 | break; | 134 | break; |
135 | case 10: | 135 | case 10: |
136 | edgeVert0 = 5; | 136 | edgeVert0 = 5; |
137 | edgeVert1 = 7; | 137 | edgeVert1 = 7; |
138 | break; | 138 | break; |
139 | case 11: | 139 | case 11: |
140 | edgeVert0 = 6; | 140 | edgeVert0 = 6; |
141 | edgeVert1 = 7; | 141 | edgeVert1 = 7; |
142 | break; | 142 | break; |
143 | default: | 143 | default: |
144 | throw new BulletException(); | 144 | throw new BulletException(); |
145 | 145 | ||
146 | } | 146 | } |
147 | 147 | ||
148 | GetVertex(edgeVert0, out pa); | 148 | GetVertex(edgeVert0, out pa); |
149 | GetVertex(edgeVert1, out pb); | 149 | GetVertex(edgeVert1, out pb); |
150 | } | 150 | } |
151 | 151 | ||
152 | public override void GetVertex(int i, out Vector3 vtx) | 152 | public override void GetVertex(int i, out Vector3 vtx) |
153 | { | 153 | { |
154 | Vector3 halfExtents = HalfExtents; | 154 | Vector3 halfExtents = HalfExtents; |
155 | 155 | ||
156 | vtx = new Vector3( | 156 | vtx = new Vector3( |
157 | halfExtents.X * (1 - (i & 1)) - halfExtents.X * (i & 1), | 157 | halfExtents.X * (1 - (i & 1)) - halfExtents.X * (i & 1), |
158 | halfExtents.Y * (1 - ((i & 2) >> 1)) - halfExtents.Y * ((i & 2) >> 1), | 158 | halfExtents.Y * (1 - ((i & 2) >> 1)) - halfExtents.Y * ((i & 2) >> 1), |
159 | halfExtents.Z * (1 - ((i & 4) >> 2)) - halfExtents.Z * ((i & 4) >> 2)); | 159 | halfExtents.Z * (1 - ((i & 4) >> 2)) - halfExtents.Z * ((i & 4) >> 2)); |
160 | } | 160 | } |
161 | 161 | ||
162 | public override void GetPlane(out Vector3 planeNormal, out Vector3 planeSupport, int i) | 162 | public override void GetPlane(out Vector3 planeNormal, out Vector3 planeSupport, int i) |
163 | { | 163 | { |
164 | //this plane might not be aligned... | 164 | //this plane might not be aligned... |
165 | Vector4 plane; | 165 | Vector4 plane; |
166 | GetPlaneEquation(out plane, i); | 166 | GetPlaneEquation(out plane, i); |
167 | planeNormal = new Vector3(plane.X, plane.Y, plane.Z); | 167 | planeNormal = new Vector3(plane.X, plane.Y, plane.Z); |
168 | planeSupport = LocalGetSupportingVertex(-planeNormal); | 168 | planeSupport = LocalGetSupportingVertex(-planeNormal); |
169 | } | 169 | } |
170 | 170 | ||
171 | public override bool IsInside(Vector3 pt, float tolerance) | 171 | public override bool IsInside(Vector3 pt, float tolerance) |
172 | { | 172 | { |
173 | Vector3 halfExtents = HalfExtents; | 173 | Vector3 halfExtents = HalfExtents; |
174 | 174 | ||
175 | //btScalar minDist = 2*tolerance; | 175 | //btScalar minDist = 2*tolerance; |
176 | 176 | ||
177 | bool result = (pt.X <= ( halfExtents.X + tolerance)) && | 177 | bool result = (pt.X <= ( halfExtents.X + tolerance)) && |
178 | (pt.X >= (-halfExtents.X - tolerance)) && | 178 | (pt.X >= (-halfExtents.X - tolerance)) && |
179 | (pt.Y <= ( halfExtents.Y + tolerance)) && | 179 | (pt.Y <= ( halfExtents.Y + tolerance)) && |
180 | (pt.Y >= (-halfExtents.Y - tolerance)) && | 180 | (pt.Y >= (-halfExtents.Y - tolerance)) && |
181 | (pt.Z <= ( halfExtents.Z + tolerance)) && | 181 | (pt.Z <= ( halfExtents.Z + tolerance)) && |
182 | (pt.Z >= (-halfExtents.Z - tolerance)); | 182 | (pt.Z >= (-halfExtents.Z - tolerance)); |
183 | 183 | ||
184 | return result; | 184 | return result; |
185 | } | 185 | } |
186 | 186 | ||
187 | public override Vector3 LocalGetSupportingVertex(Vector3 vec) | 187 | public override Vector3 LocalGetSupportingVertex(Vector3 vec) |
188 | { | 188 | { |
189 | Vector3 halfExtents = HalfExtents; | 189 | Vector3 halfExtents = HalfExtents; |
190 | 190 | ||
191 | return new Vector3( vec.X < 0.0f ? -halfExtents.X : halfExtents.X, | 191 | return new Vector3( vec.X < 0.0f ? -halfExtents.X : halfExtents.X, |
192 | vec.Y < 0.0f ? -halfExtents.Y : halfExtents.Y, | 192 | vec.Y < 0.0f ? -halfExtents.Y : halfExtents.Y, |
193 | vec.Z < 0.0f ? -halfExtents.Z : halfExtents.Z); | 193 | vec.Z < 0.0f ? -halfExtents.Z : halfExtents.Z); |
194 | } | 194 | } |
195 | 195 | ||
196 | public override Vector3 LocalGetSupportingVertexWithoutMargin(Vector3 vec) | 196 | public override Vector3 LocalGetSupportingVertexWithoutMargin(Vector3 vec) |
197 | { | 197 | { |
198 | Vector3 halfExtents = HalfExtents; | 198 | Vector3 halfExtents = HalfExtents; |
199 | Vector3 margin = new Vector3(Margin, Margin, Margin); | 199 | Vector3 margin = new Vector3(Margin, Margin, Margin); |
200 | halfExtents -= margin; | 200 | halfExtents -= margin; |
201 | 201 | ||
202 | return new Vector3( vec.X < 0.0f ? -halfExtents.X : halfExtents.X, | 202 | return new Vector3( vec.X < 0.0f ? -halfExtents.X : halfExtents.X, |
203 | vec.Y < 0.0f ? -halfExtents.Y : halfExtents.Y, | 203 | vec.Y < 0.0f ? -halfExtents.Y : halfExtents.Y, |
204 | vec.Z < 0.0f ? -halfExtents.Z : halfExtents.Z); | 204 | vec.Z < 0.0f ? -halfExtents.Z : halfExtents.Z); |
205 | } | 205 | } |
206 | 206 | ||
207 | public override void BatchedUnitVectorGetSupportingVertexWithoutMargin(Vector3[] vectors, Vector3[] supportVerticesOut) | 207 | public override void BatchedUnitVectorGetSupportingVertexWithoutMargin(Vector3[] vectors, Vector3[] supportVerticesOut) |
208 | { | 208 | { |
209 | Vector3 halfExtents = HalfExtents; | 209 | Vector3 halfExtents = HalfExtents; |
210 | Vector3 margin = new Vector3(Margin, Margin, Margin); | 210 | Vector3 margin = new Vector3(Margin, Margin, Margin); |
211 | halfExtents -= margin; | 211 | halfExtents -= margin; |
212 | 212 | ||
213 | for (int i = 0; i < vectors.Length; i++) | 213 | for (int i = 0; i < vectors.Length; i++) |
214 | { | 214 | { |
215 | Vector3 vec = vectors[i]; | 215 | Vector3 vec = vectors[i]; |
216 | supportVerticesOut[i] = new Vector3(vec.X < 0.0f ? -halfExtents.X : halfExtents.X, | 216 | supportVerticesOut[i] = new Vector3(vec.X < 0.0f ? -halfExtents.X : halfExtents.X, |
217 | vec.Y < 0.0f ? -halfExtents.Y : halfExtents.Y, | 217 | vec.Y < 0.0f ? -halfExtents.Y : halfExtents.Y, |
218 | vec.Z < 0.0f ? -halfExtents.Z : halfExtents.Z); | 218 | vec.Z < 0.0f ? -halfExtents.Z : halfExtents.Z); |
219 | } | 219 | } |
220 | } | 220 | } |
221 | 221 | ||
222 | public virtual void GetPlaneEquation(out Vector4 plane, int i) | 222 | public virtual void GetPlaneEquation(out Vector4 plane, int i) |
223 | { | 223 | { |
224 | Vector3 halfExtents = HalfExtents; | 224 | Vector3 halfExtents = HalfExtents; |
225 | 225 | ||
226 | switch (i) | 226 | switch (i) |
227 | { | 227 | { |
228 | case 0: | 228 | case 0: |
229 | plane = new Vector4(1, 0, 0, 0); | 229 | plane = new Vector4(1, 0, 0, 0); |
230 | plane.W = -halfExtents.X; | 230 | plane.W = -halfExtents.X; |
231 | break; | 231 | break; |
232 | case 1: | 232 | case 1: |
233 | plane = new Vector4(-1, 0, 0, 0); | 233 | plane = new Vector4(-1, 0, 0, 0); |
234 | plane.W = -halfExtents.X; | 234 | plane.W = -halfExtents.X; |
235 | break; | 235 | break; |
236 | case 2: | 236 | case 2: |
237 | plane = new Vector4(0, 1, 0, 0); | 237 | plane = new Vector4(0, 1, 0, 0); |
238 | plane.W = -halfExtents.Y; | 238 | plane.W = -halfExtents.Y; |
239 | break; | 239 | break; |
240 | case 3: | 240 | case 3: |
241 | plane = new Vector4(0, -1, 0, 0); | 241 | plane = new Vector4(0, -1, 0, 0); |
242 | plane.W = -halfExtents.Y; | 242 | plane.W = -halfExtents.Y; |
243 | break; | 243 | break; |
244 | case 4: | 244 | case 4: |
245 | plane = new Vector4(0, 0, 1, 0); | 245 | plane = new Vector4(0, 0, 1, 0); |
246 | plane.W = -halfExtents.Z; | 246 | plane.W = -halfExtents.Z; |
247 | break; | 247 | break; |
248 | case 5: | 248 | case 5: |
249 | plane = new Vector4(0, 0, -1, 0); | 249 | plane = new Vector4(0, 0, -1, 0); |
250 | plane.W = -halfExtents.Z; | 250 | plane.W = -halfExtents.Z; |
251 | break; | 251 | break; |
252 | default: | 252 | default: |
253 | throw new BulletException(); | 253 | throw new BulletException(); |
254 | } | 254 | } |
255 | } | 255 | } |
256 | 256 | ||
257 | public override void GetPreferredPenetrationDirection(int index, out Vector3 penetrationVector) | 257 | public override void GetPreferredPenetrationDirection(int index, out Vector3 penetrationVector) |
258 | { | 258 | { |
259 | switch (index) | 259 | switch (index) |
260 | { | 260 | { |
261 | case 0: | 261 | case 0: |
262 | penetrationVector = new Vector3(1, 0, 0); | 262 | penetrationVector = new Vector3(1, 0, 0); |
263 | break; | 263 | break; |
264 | case 1: | 264 | case 1: |
265 | penetrationVector = new Vector3(-1, 0, 0); | 265 | penetrationVector = new Vector3(-1, 0, 0); |
266 | break; | 266 | break; |
267 | case 2: | 267 | case 2: |
268 | penetrationVector = new Vector3(0, 1, 0); | 268 | penetrationVector = new Vector3(0, 1, 0); |
269 | break; | 269 | break; |
270 | case 3: | 270 | case 3: |
271 | penetrationVector = new Vector3(0, -1, 0); | 271 | penetrationVector = new Vector3(0, -1, 0); |
272 | break; | 272 | break; |
273 | case 4: | 273 | case 4: |
274 | penetrationVector = new Vector3(0, 0, 1); | 274 | penetrationVector = new Vector3(0, 0, 1); |
275 | break; | 275 | break; |
276 | case 5: | 276 | case 5: |
277 | penetrationVector = new Vector3(0, 0, -1); | 277 | penetrationVector = new Vector3(0, 0, -1); |
278 | break; | 278 | break; |
279 | default: | 279 | default: |
280 | throw new BulletException(); | 280 | throw new BulletException(); |
281 | } | 281 | } |
282 | } | 282 | } |
283 | 283 | ||
284 | public override void GetAabb(Matrix t, out Vector3 aabbMin, out Vector3 aabbMax) | 284 | public override void GetAabb(Matrix t, out Vector3 aabbMin, out Vector3 aabbMax) |
285 | { | 285 | { |
286 | Vector3 halfExtents = HalfExtents; | 286 | Vector3 halfExtents = HalfExtents; |
287 | 287 | ||
288 | Matrix abs_b = MathHelper.Absolute(t); | 288 | Matrix abs_b = MathHelper.Absolute(t); |
289 | Vector3 center = t.Translation; | 289 | Vector3 center = t.Translation; |
290 | Vector3 row1 = new Vector3(abs_b.M11, abs_b.M12, abs_b.M13); | 290 | Vector3 row1 = new Vector3(abs_b.M11, abs_b.M12, abs_b.M13); |
291 | Vector3 row2 = new Vector3(abs_b.M21, abs_b.M22, abs_b.M23); | 291 | Vector3 row2 = new Vector3(abs_b.M21, abs_b.M22, abs_b.M23); |
292 | Vector3 row3 = new Vector3(abs_b.M31, abs_b.M32, abs_b.M33); | 292 | Vector3 row3 = new Vector3(abs_b.M31, abs_b.M32, abs_b.M33); |
293 | Vector3 extent = new Vector3(Vector3.Dot(row1, halfExtents), | 293 | Vector3 extent = new Vector3(Vector3.Dot(row1, halfExtents), |
294 | Vector3.Dot(row2, halfExtents), | 294 | Vector3.Dot(row2, halfExtents), |
295 | Vector3.Dot(row3, halfExtents)); | 295 | Vector3.Dot(row3, halfExtents)); |
296 | extent += new Vector3(Margin, Margin, Margin); | 296 | extent += new Vector3(Margin, Margin, Margin); |
297 | 297 | ||
298 | aabbMin = center - extent; | 298 | aabbMin = center - extent; |
299 | aabbMax = center + extent; | 299 | aabbMax = center + extent; |
300 | } | 300 | } |
301 | 301 | ||
302 | public override void CalculateLocalInertia(float mass, out Vector3 inertia) | 302 | public override void CalculateLocalInertia(float mass, out Vector3 inertia) |
303 | { | 303 | { |
304 | Vector3 halfExtents = HalfExtents; | 304 | Vector3 halfExtents = HalfExtents; |
305 | 305 | ||
306 | float lx = 2f * (halfExtents.X); | 306 | float lx = 2f * (halfExtents.X); |
307 | float ly = 2f * (halfExtents.Y); | 307 | float ly = 2f * (halfExtents.Y); |
308 | float lz = 2f * (halfExtents.Z); | 308 | float lz = 2f * (halfExtents.Z); |
309 | 309 | ||
310 | inertia = new Vector3(); | 310 | inertia = new Vector3(); |
311 | inertia.X = mass / (12.0f) * (ly * ly + lz * lz); | 311 | inertia.X = mass / (12.0f) * (ly * ly + lz * lz); |
312 | inertia.Y = mass / (12.0f) * (lx * lx + lz * lz); | 312 | inertia.Y = mass / (12.0f) * (lx * lx + lz * lz); |
313 | inertia.Z = mass / (12.0f) * (lx * lx + ly * ly); | 313 | inertia.Z = mass / (12.0f) * (lx * lx + ly * ly); |
314 | } | 314 | } |
315 | } | 315 | } |
316 | } | 316 | } |