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