2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
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:
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.
16 #ifndef BT_OBB_BOX_MINKOWSKI_H
17 #define BT_OBB_BOX_MINKOWSKI_H
19 #include "btPolyhedralConvexShape.h"
20 #include "btCollisionMargin.h"
21 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
22 #include "LinearMath/btVector3.h"
23 #include "LinearMath/btMinMax.h"
25 ///The btBoxShape 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 btCollisionObject or btRigidBody it will be an oriented box in world space.
26 ATTRIBUTE_ALIGNED16(class)
27 btBoxShape : public btPolyhedralConvexShape
29 //btVector3 m_boxHalfExtents1; //use m_implicitShapeDimensions instead
32 BT_DECLARE_ALIGNED_ALLOCATOR();
34 btVector3 getHalfExtentsWithMargin() const
36 btVector3 halfExtents = getHalfExtentsWithoutMargin();
37 btVector3 margin(getMargin(), getMargin(), getMargin());
38 halfExtents += margin;
42 const btVector3& getHalfExtentsWithoutMargin() const
44 return m_implicitShapeDimensions; //scaling is included, margin is not
47 virtual btVector3 localGetSupportingVertex(const btVector3& vec) const
49 btVector3 halfExtents = getHalfExtentsWithoutMargin();
50 btVector3 margin(getMargin(), getMargin(), getMargin());
51 halfExtents += margin;
53 return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
54 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
55 btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
58 SIMD_FORCE_INLINE btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec) const
60 const btVector3& halfExtents = getHalfExtentsWithoutMargin();
62 return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
63 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
64 btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
67 virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
69 const btVector3& halfExtents = getHalfExtentsWithoutMargin();
71 for (int i = 0; i < numVectors; i++)
73 const btVector3& vec = vectors[i];
74 supportVerticesOut[i].setValue(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
75 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
76 btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
80 btBoxShape(const btVector3& boxHalfExtents);
82 virtual void setMargin(btScalar collisionMargin)
84 //correct the m_implicitShapeDimensions for the margin
85 btVector3 oldMargin(getMargin(), getMargin(), getMargin());
86 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions + oldMargin;
88 btConvexInternalShape::setMargin(collisionMargin);
89 btVector3 newMargin(getMargin(), getMargin(), getMargin());
90 m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
92 virtual void setLocalScaling(const btVector3& scaling)
94 btVector3 oldMargin(getMargin(), getMargin(), getMargin());
95 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions + oldMargin;
96 btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
98 btConvexInternalShape::setLocalScaling(scaling);
100 m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
103 virtual void getAabb(const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const;
105 virtual void calculateLocalInertia(btScalar mass, btVector3 & inertia) const;
107 virtual void getPlane(btVector3 & planeNormal, btVector3 & planeSupport, int i) const
109 //this plane might not be aligned...
111 getPlaneEquation(plane, i);
112 planeNormal = btVector3(plane.getX(), plane.getY(), plane.getZ());
113 planeSupport = localGetSupportingVertex(-planeNormal);
116 virtual int getNumPlanes() const
121 virtual int getNumVertices() const
126 virtual int getNumEdges() const
131 virtual void getVertex(int i, btVector3& vtx) const
133 btVector3 halfExtents = getHalfExtentsWithMargin();
136 halfExtents.x() * (1 - (i & 1)) - halfExtents.x() * (i & 1),
137 halfExtents.y() * (1 - ((i & 2) >> 1)) - halfExtents.y() * ((i & 2) >> 1),
138 halfExtents.z() * (1 - ((i & 4) >> 2)) - halfExtents.z() * ((i & 4) >> 2));
141 virtual void getPlaneEquation(btVector4 & plane, int i) const
143 btVector3 halfExtents = getHalfExtentsWithoutMargin();
148 plane.setValue(btScalar(1.), btScalar(0.), btScalar(0.), -halfExtents.x());
151 plane.setValue(btScalar(-1.), btScalar(0.), btScalar(0.), -halfExtents.x());
154 plane.setValue(btScalar(0.), btScalar(1.), btScalar(0.), -halfExtents.y());
157 plane.setValue(btScalar(0.), btScalar(-1.), btScalar(0.), -halfExtents.y());
160 plane.setValue(btScalar(0.), btScalar(0.), btScalar(1.), -halfExtents.z());
163 plane.setValue(btScalar(0.), btScalar(0.), btScalar(-1.), -halfExtents.z());
170 virtual void getEdge(int i, btVector3& pa, btVector3& pb) const
171 //virtual void getEdge(int i,Edge& edge) const
232 getVertex(edgeVert0, pa);
233 getVertex(edgeVert1, pb);
236 virtual bool isInside(const btVector3& pt, btScalar tolerance) const
238 btVector3 halfExtents = getHalfExtentsWithoutMargin();
240 //btScalar minDist = 2*tolerance;
242 bool result = (pt.x() <= (halfExtents.x() + tolerance)) &&
243 (pt.x() >= (-halfExtents.x() - tolerance)) &&
244 (pt.y() <= (halfExtents.y() + tolerance)) &&
245 (pt.y() >= (-halfExtents.y() - tolerance)) &&
246 (pt.z() <= (halfExtents.z() + tolerance)) &&
247 (pt.z() >= (-halfExtents.z() - tolerance));
253 virtual const char* getName() const
258 virtual int getNumPreferredPenetrationDirections() const
263 virtual void getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
268 penetrationVector.setValue(btScalar(1.), btScalar(0.), btScalar(0.));
271 penetrationVector.setValue(btScalar(-1.), btScalar(0.), btScalar(0.));
274 penetrationVector.setValue(btScalar(0.), btScalar(1.), btScalar(0.));
277 penetrationVector.setValue(btScalar(0.), btScalar(-1.), btScalar(0.));
280 penetrationVector.setValue(btScalar(0.), btScalar(0.), btScalar(1.));
283 penetrationVector.setValue(btScalar(0.), btScalar(0.), btScalar(-1.));
291 #endif //BT_OBB_BOX_MINKOWSKI_H