Normand Briere
2018-05-22 42107f9a01652cb2f47228d20c1148a2a22f6a63
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Java port of Bullet (c) 2008 Martin Dvorak <jezek2@advel.cz>
 *
 * Bullet Continuous Collision Detection and Physics Library
 * Copyright (c) 2003-2008 Erwin Coumans  http://www.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.
 */
 
package com.bulletphysics.collision.shapes;
 
import com.bulletphysics.collision.broadphase.BroadphaseNativeType;
import com.bulletphysics.collision.dispatch.CollisionObject;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.linearmath.AabbUtil2;
import com.bulletphysics.linearmath.ScalarUtil;
import com.bulletphysics.linearmath.Transform;
import com.bulletphysics.linearmath.VectorUtil;
import cz.advel.stack.Stack;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector4f;
 
/**
 * BoxShape is a box primitive around the origin, its sides axis aligned with length
 * specified by half extents, in local shape coordinates. When used as part of a
 * {@link CollisionObject} or {@link RigidBody} it will be an oriented box in world space.
 *
 * @author jezek2
 */
public class BoxShape extends PolyhedralConvexShape {
 
   public BoxShape(Vector3f boxHalfExtents) {
       Vector3f margin = new Vector3f(getMargin(), getMargin(), getMargin());
       VectorUtil.mul(implicitShapeDimensions, boxHalfExtents, localScaling);
       implicitShapeDimensions.sub(margin);
   }
 
   public Vector3f getHalfExtentsWithMargin(Vector3f out) {
       Vector3f halfExtents = getHalfExtentsWithoutMargin(out);
       Vector3f margin = Stack.alloc(Vector3f.class);
       margin.set(getMargin(), getMargin(), getMargin());
       halfExtents.add(margin);
       return out;
   }
 
   public Vector3f getHalfExtentsWithoutMargin(Vector3f out) {
       out.set(implicitShapeDimensions); // changed in Bullet 2.63: assume the scaling and margin are included
       return out;
   }
 
   @Override
   public BroadphaseNativeType getShapeType() {
       return BroadphaseNativeType.BOX_SHAPE_PROXYTYPE;
   }
 
   @Override
   public Vector3f localGetSupportingVertex(Vector3f vec, Vector3f out) {
       Vector3f halfExtents = getHalfExtentsWithoutMargin(out);
       
       float margin = getMargin();
       halfExtents.x += margin;
       halfExtents.y += margin;
       halfExtents.z += margin;
 
       out.set(
               ScalarUtil.fsel(vec.x, halfExtents.x, -halfExtents.x),
               ScalarUtil.fsel(vec.y, halfExtents.y, -halfExtents.y),
               ScalarUtil.fsel(vec.z, halfExtents.z, -halfExtents.z));
       return out;
   }
 
   @Override
   public Vector3f localGetSupportingVertexWithoutMargin(Vector3f vec, Vector3f out) {
       Vector3f halfExtents = getHalfExtentsWithoutMargin(out);
 
       out.set(
               ScalarUtil.fsel(vec.x, halfExtents.x, -halfExtents.x),
               ScalarUtil.fsel(vec.y, halfExtents.y, -halfExtents.y),
               ScalarUtil.fsel(vec.z, halfExtents.z, -halfExtents.z));
       return out;
   }
 
   @Override
   public void batchedUnitVectorGetSupportingVertexWithoutMargin(Vector3f[] vectors, Vector3f[] supportVerticesOut, int numVectors) {
       Vector3f halfExtents = getHalfExtentsWithoutMargin(Stack.alloc(Vector3f.class));
 
       for (int i = 0; i < numVectors; i++) {
           Vector3f vec = vectors[i];
           supportVerticesOut[i].set(ScalarUtil.fsel(vec.x, halfExtents.x, -halfExtents.x),
                   ScalarUtil.fsel(vec.y, halfExtents.y, -halfExtents.y),
                   ScalarUtil.fsel(vec.z, halfExtents.z, -halfExtents.z));
       }
   }
 
   @Override
   public void setMargin(float margin) {
       // correct the implicitShapeDimensions for the margin
       Vector3f oldMargin = Stack.alloc(Vector3f.class);
       oldMargin.set(getMargin(), getMargin(), getMargin());
       Vector3f implicitShapeDimensionsWithMargin = Stack.alloc(Vector3f.class);
       implicitShapeDimensionsWithMargin.add(implicitShapeDimensions, oldMargin);
 
       super.setMargin(margin);
       Vector3f newMargin = Stack.alloc(Vector3f.class);
       newMargin.set(getMargin(), getMargin(), getMargin());
       implicitShapeDimensions.sub(implicitShapeDimensionsWithMargin, newMargin);
   }
 
   @Override
   public void setLocalScaling(Vector3f scaling) {
       Vector3f oldMargin = Stack.alloc(Vector3f.class);
       oldMargin.set(getMargin(), getMargin(), getMargin());
       Vector3f implicitShapeDimensionsWithMargin = Stack.alloc(Vector3f.class);
       implicitShapeDimensionsWithMargin.add(implicitShapeDimensions, oldMargin);
       Vector3f unScaledImplicitShapeDimensionsWithMargin = Stack.alloc(Vector3f.class);
       VectorUtil.div(unScaledImplicitShapeDimensionsWithMargin, implicitShapeDimensionsWithMargin, localScaling);
 
       super.setLocalScaling(scaling);
 
       VectorUtil.mul(implicitShapeDimensions, unScaledImplicitShapeDimensionsWithMargin, localScaling);
       implicitShapeDimensions.sub(oldMargin);
   }
 
   @Override
   public void getAabb(Transform t, Vector3f aabbMin, Vector3f aabbMax) {
       AabbUtil2.transformAabb(getHalfExtentsWithoutMargin(Stack.alloc(Vector3f.class)), getMargin(), t, aabbMin, aabbMax);
   }
 
   @Override
   public void calculateLocalInertia(float mass, Vector3f inertia) {
       //btScalar margin = btScalar(0.);
       Vector3f halfExtents = getHalfExtentsWithMargin(Stack.alloc(Vector3f.class));
 
       float lx = 2f * halfExtents.x;
       float ly = 2f * halfExtents.y;
       float lz = 2f * halfExtents.z;
 
       inertia.set(mass / 12f * (ly * ly + lz * lz),
               mass / 12f * (lx * lx + lz * lz),
               mass / 12f * (lx * lx + ly * ly));
   }
 
   @Override
   public void getPlane(Vector3f planeNormal, Vector3f planeSupport, int i) {
       // this plane might not be aligned...
       Vector4f plane = Stack.alloc(Vector4f.class);
       getPlaneEquation(plane, i);
       planeNormal.set(plane.x, plane.y, plane.z);
       Vector3f tmp = Stack.alloc(Vector3f.class);
       tmp.negate(planeNormal);
       localGetSupportingVertex(tmp, planeSupport);
   }
 
   @Override
   public int getNumPlanes() {
       return 6;
   }
 
   @Override
   public int getNumVertices() {
       return 8;
   }
 
   @Override
   public int getNumEdges() {
       return 12;
   }
 
   @Override
   public void getVertex(int i, Vector3f vtx) {
       Vector3f halfExtents = getHalfExtentsWithoutMargin(Stack.alloc(Vector3f.class));
 
       vtx.set(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 void getPlaneEquation(Vector4f plane, int i) {
       Vector3f halfExtents = getHalfExtentsWithoutMargin(Stack.alloc(Vector3f.class));
 
       switch (i) {
           case 0:
               plane.set(1f, 0f, 0f, -halfExtents.x);
               break;
           case 1:
               plane.set(-1f, 0f, 0f, -halfExtents.x);
               break;
           case 2:
               plane.set(0f, 1f, 0f, -halfExtents.y);
               break;
           case 3:
               plane.set(0f, -1f, 0f, -halfExtents.y);
               break;
           case 4:
               plane.set(0f, 0f, 1f, -halfExtents.z);
               break;
           case 5:
               plane.set(0f, 0f, -1f, -halfExtents.z);
               break;
           default:
               assert (false);
       }
   }
 
   @Override
   public void getEdge(int i, Vector3f pa, Vector3f 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:
               assert (false);
       }
 
       getVertex(edgeVert0, pa);
       getVertex(edgeVert1, pb);
   }
 
   @Override
   public boolean isInside(Vector3f pt, float tolerance) {
       Vector3f halfExtents = getHalfExtentsWithoutMargin(Stack.alloc(Vector3f.class));
 
       //btScalar minDist = 2*tolerance;
 
       boolean 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;
   }
 
   @Override
   public String getName() {
       return "Box";
   }
 
   @Override
   public int getNumPreferredPenetrationDirections() {
       return 6;
   }
 
   @Override
   public void getPreferredPenetrationDirection(int index, Vector3f penetrationVector) {
       switch (index) {
           case 0:
               penetrationVector.set(1f, 0f, 0f);
               break;
           case 1:
               penetrationVector.set(-1f, 0f, 0f);
               break;
           case 2:
               penetrationVector.set(0f, 1f, 0f);
               break;
           case 3:
               penetrationVector.set(0f, -1f, 0f);
               break;
           case 4:
               penetrationVector.set(0f, 0f, 1f);
               break;
           case 5:
               penetrationVector.set(0f, 0f, -1f);
               break;
           default:
               assert (false);
       }
   }
 
}