Bullet Collision Detection & Physics Library: btBox2dShape.h Source File

Bullet Collision Detection & Physics Library

btBox2dShape.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #ifndef BT_OBB_BOX_2D_SHAPE_H
17 #define BT_OBB_BOX_2D_SHAPE_H
18
19 #include "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h"
20 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
21 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
22 #include "LinearMath/btVector3.h"
23 #include "LinearMath/btMinMax.h"
24
26 ATTRIBUTE_ALIGNED16(class) btBox2dShape: public btPolyhedralConvexShape
27 {
28
29 //btVector3 m_boxHalfExtents1; //use m_implicitShapeDimensions instead
30
31 btVector3 m_centroid;
32 btVector3 m_vertices[4];
33 btVector3 m_normals[4];
34
35 public:
36
37 BT_DECLARE_ALIGNED_ALLOCATOR();
38
39 btVector3 getHalfExtentsWithMargin() const
40 {
41 btVector3 halfExtents = getHalfExtentsWithoutMargin();
42 btVector3 margin(getMargin(),getMargin(),getMargin());
43 halfExtents += margin;
44 return halfExtents;
45 }
46
47 const btVector3& getHalfExtentsWithoutMargin() const
48 {
49 return m_implicitShapeDimensions;//changed in Bullet 2.63: assume the scaling and margin are included
50 }
51
52
53 virtual btVector3 localGetSupportingVertex(const btVector3& vec) const
54 {
55 btVector3 halfExtents = getHalfExtentsWithoutMargin();
56 btVector3 margin(getMargin(),getMargin(),getMargin());
57 halfExtents += margin;
58
59 return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
60 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
61 btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
62 }
63
64 SIMD_FORCE_INLINE btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec)const
65 {
66 const btVector3& halfExtents = getHalfExtentsWithoutMargin();
67
68 return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
69 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
70 btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
71 }
72
73 virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
74 {
75 const btVector3& halfExtents = getHalfExtentsWithoutMargin();
76
77 for (int i=0;i<numVectors;i++)
78 {
79 const btVector3& vec = vectors[i];
80 supportVerticesOut[i].setValue(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
81 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
82 btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
83 }
84
85 }
86
87
89 btBox2dShape( const btVector3& boxHalfExtents)
90 : btPolyhedralConvexShape(),
91 m_centroid(0,0,0)
92 {
93 m_vertices[0].setValue(-boxHalfExtents.getX(),-boxHalfExtents.getY(),0);
94 m_vertices[1].setValue(boxHalfExtents.getX(),-boxHalfExtents.getY(),0);
95 m_vertices[2].setValue(boxHalfExtents.getX(),boxHalfExtents.getY(),0);
96 m_vertices[3].setValue(-boxHalfExtents.getX(),boxHalfExtents.getY(),0);
97
98 m_normals[0].setValue(0,-1,0);
99 m_normals[1].setValue(1,0,0);
100 m_normals[2].setValue(0,1,0);
101 m_normals[3].setValue(-1,0,0);
102
103 btScalar minDimension = boxHalfExtents.getX();
104 if (minDimension>boxHalfExtents.getY())
105 minDimension = boxHalfExtents.getY();
106
107 m_shapeType = BOX_2D_SHAPE_PROXYTYPE;
108 btVector3 margin(getMargin(),getMargin(),getMargin());
109 m_implicitShapeDimensions = (boxHalfExtents * m_localScaling) - margin;
110
111 setSafeMargin(minDimension);
112 };
113
114 virtual void setMargin(btScalar collisionMargin)
115 {
116 //correct the m_implicitShapeDimensions for the margin
117 btVector3 oldMargin(getMargin(),getMargin(),getMargin());
118 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
119
120 btConvexInternalShape::setMargin(collisionMargin);
121 btVector3 newMargin(getMargin(),getMargin(),getMargin());
122 m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
123
124 }
125 virtual void setLocalScaling(const btVector3& scaling)
126 {
127 btVector3 oldMargin(getMargin(),getMargin(),getMargin());
128 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
129 btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
130
131 btConvexInternalShape::setLocalScaling(scaling);
132
133 m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
134
135 }
136
137 virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
138
139
140
141 virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const;
142
143
144
145
146
147 int getVertexCount() const
148 {
149 return 4;
150 }
151
152 virtual int getNumVertices()const
153 {
154 return 4;
155 }
156
157 const btVector3* getVertices() const
158 {
159 return &m_vertices[0];
160 }
161
162 const btVector3* getNormals() const
163 {
164 return &m_normals[0];
165 }
166
167
168
169
170
171
172
173 virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i ) const
174 {
175 //this plane might not be aligned...
176 btVector4 plane ;
177 getPlaneEquation(plane,i);
178 planeNormal = btVector3(plane.getX(),plane.getY(),plane.getZ());
179 planeSupport = localGetSupportingVertex(-planeNormal);
180 }
181
182
183 const btVector3& getCentroid() const
184 {
185 return m_centroid;
186 }
187
188 virtual int getNumPlanes() const
189 {
190 return 6;
191 }
192
193
194
195 virtual int getNumEdges() const
196 {
197 return 12;
198 }
199
200
201 virtual void getVertex(int i,btVector3& vtx) const
202 {
203 btVector3 halfExtents = getHalfExtentsWithoutMargin();
204
205 vtx = btVector3(
206 halfExtents.x() * (1-(i&1)) - halfExtents.x() * (i&1),
207 halfExtents.y() * (1-((i&2)>>1)) - halfExtents.y() * ((i&2)>>1),
208 halfExtents.z() * (1-((i&4)>>2)) - halfExtents.z() * ((i&4)>>2));
209 }
210
211
212 virtual void getPlaneEquation(btVector4& plane,int i) const
213 {
214 btVector3 halfExtents = getHalfExtentsWithoutMargin();
215
216 switch (i)
217 {
218 case 0:
219 plane.setValue(btScalar(1.),btScalar(0.),btScalar(0.),-halfExtents.x());
220 break;
221 case 1:
222 plane.setValue(btScalar(-1.),btScalar(0.),btScalar(0.),-halfExtents.x());
223 break;
224 case 2:
225 plane.setValue(btScalar(0.),btScalar(1.),btScalar(0.),-halfExtents.y());
226 break;
227 case 3:
228 plane.setValue(btScalar(0.),btScalar(-1.),btScalar(0.),-halfExtents.y());
229 break;
230 case 4:
231 plane.setValue(btScalar(0.),btScalar(0.),btScalar(1.),-halfExtents.z());
232 break;
233 case 5:
234 plane.setValue(btScalar(0.),btScalar(0.),btScalar(-1.),-halfExtents.z());
235 break;
236 default:
237 btAssert(0);
238 }
239 }
240
241
242 virtual void getEdge(int i,btVector3& pa,btVector3& pb) const
243 //virtual void getEdge(int i,Edge& edge) const
244 {
245 int edgeVert0 = 0;
246 int edgeVert1 = 0;
247
248 switch (i)
249 {
250 case 0:
251 edgeVert0 = 0;
252 edgeVert1 = 1;
253 break;
254 case 1:
255 edgeVert0 = 0;
256 edgeVert1 = 2;
257 break;
258 case 2:
259 edgeVert0 = 1;
260 edgeVert1 = 3;
261
262 break;
263 case 3:
264 edgeVert0 = 2;
265 edgeVert1 = 3;
266 break;
267 case 4:
268 edgeVert0 = 0;
269 edgeVert1 = 4;
270 break;
271 case 5:
272 edgeVert0 = 1;
273 edgeVert1 = 5;
274
275 break;
276 case 6:
277 edgeVert0 = 2;
278 edgeVert1 = 6;
279 break;
280 case 7:
281 edgeVert0 = 3;
282 edgeVert1 = 7;
283 break;
284 case 8:
285 edgeVert0 = 4;
286 edgeVert1 = 5;
287 break;
288 case 9:
289 edgeVert0 = 4;
290 edgeVert1 = 6;
291 break;
292 case 10:
293 edgeVert0 = 5;
294 edgeVert1 = 7;
295 break;
296 case 11:
297 edgeVert0 = 6;
298 edgeVert1 = 7;
299 break;
300 default:
301 btAssert(0);
302
303 }
304
305 getVertex(edgeVert0,pa );
306 getVertex(edgeVert1,pb );
307 }
308
309
310
311
312
313 virtual bool isInside(const btVector3& pt,btScalar tolerance) const
314 {
315 btVector3 halfExtents = getHalfExtentsWithoutMargin();
316
317 //btScalar minDist = 2*tolerance;
318
319 bool result = (pt.x() <= (halfExtents.x()+tolerance)) &&
320 (pt.x() >= (-halfExtents.x()-tolerance)) &&
321 (pt.y() <= (halfExtents.y()+tolerance)) &&
322 (pt.y() >= (-halfExtents.y()-tolerance)) &&
323 (pt.z() <= (halfExtents.z()+tolerance)) &&
324 (pt.z() >= (-halfExtents.z()-tolerance));
325
326 return result;
327 }
328
329
330 //debugging
331 virtual const char* getName()const
332 {
333 return "Box2d";
334 }
335
336 virtual int getNumPreferredPenetrationDirections() const
337 {
338 return 6;
339 }
340
341 virtual void getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
342 {
343 switch (index)
344 {
345 case 0:
346 penetrationVector.setValue(btScalar(1.),btScalar(0.),btScalar(0.));
347 break;
348 case 1:
349 penetrationVector.setValue(btScalar(-1.),btScalar(0.),btScalar(0.));
350 break;
351 case 2:
352 penetrationVector.setValue(btScalar(0.),btScalar(1.),btScalar(0.));
353 break;
354 case 3:
355 penetrationVector.setValue(btScalar(0.),btScalar(-1.),btScalar(0.));
356 break;
357 case 4:
358 penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(1.));
359 break;
360 case 5:
361 penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(-1.));
362 break;
363 default:
364 btAssert(0);
365 }
366 }
367
368 };
369
370 #endif //BT_OBB_BOX_2D_SHAPE_H
371
372
btBox2dShape::isInside
virtual bool isInside(const btVector3 &pt, btScalar tolerance) const
Definition: btBox2dShape.h:313
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:652
btBox2dShape::getNumPlanes
virtual int getNumPlanes() const
Definition: btBox2dShape.h:188
btVector3.h
btBox2dShape::getName
virtual const char * getName() const
Definition: btBox2dShape.h:331
btBox2dShape::localGetSupportingVertexWithoutMargin
btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
Definition: btBox2dShape.h:64
btBox2dShape::getPreferredPenetrationDirection
virtual void getPreferredPenetrationDirection(int index, btVector3 &penetrationVector) const
Definition: btBox2dShape.h:341
btBox2dShape::getVertex
virtual void getVertex(int i, btVector3 &vtx) const
Definition: btBox2dShape.h:201
btBox2dShape::getCentroid
const btVector3 & getCentroid() const
Definition: btBox2dShape.h:183
btAssert
#define btAssert(x)
Definition: btScalar.h:131
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:81
btVector3::getY
const btScalar & getY() const
Return the y value.
Definition: btVector3.h:575
btBox2dShape::getEdge
virtual void getEdge(int i, btVector3 &pa, btVector3 &pb) const
Definition: btBox2dShape.h:242
btCollisionMargin.h
btBox2dShape::m_centroid
btVector3 m_centroid
Definition: btBox2dShape.h:31
btBox2dShape::getVertexCount
int getVertexCount() const
Definition: btBox2dShape.h:147
btVector3::getZ
const btScalar & getZ() const
Return the z value.
Definition: btVector3.h:577
btVector4
Definition: btVector3.h:1089
btConvexInternalShape::setMargin
virtual void setMargin(btScalar margin)
Definition: btConvexInternalShape.h:109
btBox2dShape::getVertices
const btVector3 * getVertices() const
Definition: btBox2dShape.h:157
btVector3::x
const btScalar & x() const
Return the x value.
Definition: btVector3.h:587
btBox2dShape::getNumPreferredPenetrationDirections
virtual int getNumPreferredPenetrationDirections() const
Definition: btBox2dShape.h:336
btBox2dShape::getHalfExtentsWithoutMargin
const btVector3 & getHalfExtentsWithoutMargin() const
Definition: btBox2dShape.h:47
BOX_2D_SHAPE_PROXYTYPE
Definition: btBroadphaseProxy.h:49
btVector3::y
const btScalar & y() const
Return the y value.
Definition: btVector3.h:589
btVector3::z
const btScalar & z() const
Return the z value.
Definition: btVector3.h:591
btPolyhedralConvexShape
The btPolyhedralConvexShape is an internal interface class for polyhedral convex shapes.
Definition: btPolyhedralConvexShape.h:25
btBroadphaseProxy.h
btBox2dShape::getPlane
virtual void getPlane(btVector3 &planeNormal, btVector3 &planeSupport, int i) const
Definition: btBox2dShape.h:173
btBox2dShape::getNormals
const btVector3 * getNormals() const
Definition: btBox2dShape.h:162
btMinMax.h
btFsels
#define btFsels(a, b, c)
Definition: btScalar.h:581
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
ATTRIBUTE_ALIGNED16
#define ATTRIBUTE_ALIGNED16(a)
Definition: btScalar.h:82
btBox2dShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
Definition: btBox2dShape.h:125
btBox2dShape::getHalfExtentsWithMargin
btVector3 getHalfExtentsWithMargin() const
Definition: btBox2dShape.h:39
btBox2dShape::btBox2dShape
btBox2dShape(const btVector3 &boxHalfExtents)
a btBox2dShape is a flat 2D box in the X-Y plane (Z extents are zero)
Definition: btBox2dShape.h:89
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btBox2dShape::batchedUnitVectorGetSupportingVertexWithoutMargin
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition: btBox2dShape.h:73
BT_DECLARE_ALIGNED_ALLOCATOR
#define BT_DECLARE_ALIGNED_ALLOCATOR()
Definition: btScalar.h:403
btBox2dShape::setMargin
virtual void setMargin(btScalar collisionMargin)
Definition: btBox2dShape.h:114
btBox2dShape::getNumEdges
virtual int getNumEdges() const
Definition: btBox2dShape.h:195
btBox2dShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
Definition: btBox2dShape.h:53
btConvexInternalShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
Definition: btConvexInternalShape.cpp:28
btPolyhedralConvexShape.h
btBox2dShape::getPlaneEquation
virtual void getPlaneEquation(btVector4 &plane, int i) const
Definition: btBox2dShape.h:212
btVector3::getX
const btScalar & getX() const
Return the x value.
Definition: btVector3.h:573
btBox2dShape::getNumVertices
virtual int getNumVertices() const
Definition: btBox2dShape.h:152
btBox2dShape
The btBox2dShape is a box primitive around the origin, its sides axis aligned with length specified b...
Definition: btBox2dShape.h:26
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
btVector4::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z, const btScalar &_w)
Set x,y,z and zero w.
Definition: btVector3.h:1225