aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/BoxShape.cs
blob: ba6f3b727e73d16282ec5452e8fbaf34beb17c2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
  Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
  Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

using System;
using System.Collections.Generic;
using System.Text;
using MonoXnaCompactMaths;

namespace XnaDevRu.BulletX
{
	public class BoxShape : PolyhedralConvexShape
	{
		public BoxShape(Vector3 boxHalfExtents)
		{
			ImplicitShapeDimensions = boxHalfExtents;
		}

		public override int VertexCount
		{
			get
			{
				return 8;
			}
		}

		public override int EdgeCount
		{
			get
			{
				return 12;
			}
		}

		public override BroadphaseNativeTypes ShapeType
		{
			get
			{
				return BroadphaseNativeTypes.Box;
			}
		}

		public override string Name
		{
			get
			{
				return "Box";
			}
		}

		public override int PreferredPenetrationDirectionsCount
		{
			get
			{
				return 6;
			}
		}

		public override int PlaneCount
		{
			get
			{
				return 6;
			}
		}

		public Vector3 HalfExtents { get { return ImplicitShapeDimensions * LocalScaling; } }

		public override void GetEdge(int i, out Vector3 pa, out Vector3 pb)
		{
			int edgeVert0 = 0;
			int edgeVert1 = 0;

			switch (i)
			{
				case 0:
					edgeVert0 = 0;
					edgeVert1 = 1;
					break;
				case 1:
					edgeVert0 = 0;
					edgeVert1 = 2;
					break;
				case 2:
					edgeVert0 = 1;
					edgeVert1 = 3;

					break;
				case 3:
					edgeVert0 = 2;
					edgeVert1 = 3;
					break;
				case 4:
					edgeVert0 = 0;
					edgeVert1 = 4;
					break;
				case 5:
					edgeVert0 = 1;
					edgeVert1 = 5;

					break;
				case 6:
					edgeVert0 = 2;
					edgeVert1 = 6;
					break;
				case 7:
					edgeVert0 = 3;
					edgeVert1 = 7;
					break;
				case 8:
					edgeVert0 = 4;
					edgeVert1 = 5;
					break;
				case 9:
					edgeVert0 = 4;
					edgeVert1 = 6;
					break;
				case 10:
					edgeVert0 = 5;
					edgeVert1 = 7;
					break;
				case 11:
					edgeVert0 = 6;
					edgeVert1 = 7;
					break;
				default:
					throw new BulletException();

			}

			GetVertex(edgeVert0, out pa);
			GetVertex(edgeVert1, out pb);
		}

		public override void GetVertex(int i, out Vector3 vtx)
		{
			Vector3 halfExtents = HalfExtents;

			vtx = new Vector3(
					halfExtents.X * (1 - (i & 1)) - halfExtents.X * (i & 1),
					halfExtents.Y * (1 - ((i & 2) >> 1)) - halfExtents.Y * ((i & 2) >> 1),
					halfExtents.Z * (1 - ((i & 4) >> 2)) - halfExtents.Z * ((i & 4) >> 2));
		}

		public override void GetPlane(out Vector3 planeNormal, out Vector3 planeSupport, int i)
		{
			//this plane might not be aligned...
			Vector4 plane;
			GetPlaneEquation(out plane, i);
			planeNormal = new Vector3(plane.X, plane.Y, plane.Z);
			planeSupport = LocalGetSupportingVertex(-planeNormal);
		}

		public override bool IsInside(Vector3 pt, float tolerance)
		{
			Vector3 halfExtents = HalfExtents;

			//btScalar minDist = 2*tolerance;

			bool result =	(pt.X <= ( halfExtents.X + tolerance)) &&
							(pt.X >= (-halfExtents.X - tolerance)) &&
							(pt.Y <= ( halfExtents.Y + tolerance)) &&
							(pt.Y >= (-halfExtents.Y - tolerance)) &&
							(pt.Z <= ( halfExtents.Z + tolerance)) &&
							(pt.Z >= (-halfExtents.Z - tolerance));

			return result;
		}

		public override Vector3 LocalGetSupportingVertex(Vector3 vec)
		{
			Vector3 halfExtents = HalfExtents;

			return new Vector3( vec.X < 0.0f ? -halfExtents.X : halfExtents.X,
								vec.Y < 0.0f ? -halfExtents.Y : halfExtents.Y,
								vec.Z < 0.0f ? -halfExtents.Z : halfExtents.Z);
		}

		public override Vector3 LocalGetSupportingVertexWithoutMargin(Vector3 vec)
		{
			Vector3 halfExtents = HalfExtents;
			Vector3 margin = new Vector3(Margin, Margin, Margin);
			halfExtents -= margin;

			return new Vector3( vec.X < 0.0f ? -halfExtents.X : halfExtents.X,
								vec.Y < 0.0f ? -halfExtents.Y : halfExtents.Y,
								vec.Z < 0.0f ? -halfExtents.Z : halfExtents.Z);
		}

		public override void BatchedUnitVectorGetSupportingVertexWithoutMargin(Vector3[] vectors, Vector3[] supportVerticesOut)
		{
			Vector3 halfExtents = HalfExtents;
			Vector3 margin = new Vector3(Margin, Margin, Margin);
			halfExtents -= margin;

			for (int i = 0; i < vectors.Length; i++)
			{
				Vector3 vec = vectors[i];
				supportVerticesOut[i] = new Vector3(vec.X < 0.0f ? -halfExtents.X : halfExtents.X,
													vec.Y < 0.0f ? -halfExtents.Y : halfExtents.Y,
													vec.Z < 0.0f ? -halfExtents.Z : halfExtents.Z);
			}
		}

		public virtual void GetPlaneEquation(out Vector4 plane, int i)
		{
			Vector3 halfExtents = HalfExtents;

			switch (i)
			{
				case 0:
					plane = new Vector4(1, 0, 0, 0);
					plane.W = -halfExtents.X;
					break;
				case 1:
					plane = new Vector4(-1, 0, 0, 0);
					plane.W = -halfExtents.X;
					break;
				case 2:
					plane = new Vector4(0, 1, 0, 0);
					plane.W = -halfExtents.Y;
					break;
				case 3:
					plane = new Vector4(0, -1, 0, 0);
					plane.W = -halfExtents.Y;
					break;
				case 4:
					plane = new Vector4(0, 0, 1, 0);
					plane.W = -halfExtents.Z;
					break;
				case 5:
					plane = new Vector4(0, 0, -1, 0);
					plane.W = -halfExtents.Z;
					break;
				default:
					throw new BulletException();
			}
		}

		public override void GetPreferredPenetrationDirection(int index, out Vector3 penetrationVector)
		{
			switch (index)
			{
				case 0:
					penetrationVector = new Vector3(1, 0, 0);
					break;
				case 1:
					penetrationVector = new Vector3(-1, 0, 0);
					break;
				case 2:
					penetrationVector = new Vector3(0, 1, 0);
					break;
				case 3:
					penetrationVector = new Vector3(0, -1, 0);
					break;
				case 4:
					penetrationVector = new Vector3(0, 0, 1);
					break;
				case 5:
					penetrationVector = new Vector3(0, 0, -1);
					break;
				default:
					throw new BulletException();
			}
		}

		public override void GetAabb(Matrix t, out Vector3 aabbMin, out Vector3 aabbMax)
		{
			Vector3 halfExtents = HalfExtents;

			Matrix abs_b = MathHelper.Absolute(t);
			Vector3 center = t.Translation;
			Vector3 row1 = new Vector3(abs_b.M11, abs_b.M12, abs_b.M13);
			Vector3 row2 = new Vector3(abs_b.M21, abs_b.M22, abs_b.M23);
			Vector3 row3 = new Vector3(abs_b.M31, abs_b.M32, abs_b.M33);
			Vector3 extent = new Vector3(Vector3.Dot(row1, halfExtents),
										 Vector3.Dot(row2, halfExtents),
										 Vector3.Dot(row3, halfExtents));
			extent += new Vector3(Margin, Margin, Margin);

			aabbMin = center - extent;
			aabbMax = center + extent;
		}

		public override void CalculateLocalInertia(float mass, out Vector3 inertia)
		{
			Vector3 halfExtents = HalfExtents;

			float lx = 2f * (halfExtents.X);
			float ly = 2f * (halfExtents.Y);
			float lz = 2f * (halfExtents.Z);

			inertia = new Vector3();
			inertia.X = mass / (12.0f) * (ly * ly + lz * lz);
			inertia.Y = mass / (12.0f) * (lx * lx + lz * lz);
			inertia.Z = mass / (12.0f) * (lx * lx + ly * ly);
		}
	}
}