2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans https://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_2D_SHAPE_H
17 #define BT_OBB_BOX_2D_SHAPE_H
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"
25 ///The btBox2dShape 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 btBox2dShape : public btPolyhedralConvexShape
29 //btVector3 m_boxHalfExtents1; //use m_implicitShapeDimensions instead
32 btVector3 m_vertices[4];
33 btVector3 m_normals[4];
36 BT_DECLARE_ALIGNED_ALLOCATOR();
38 btVector3 getHalfExtentsWithMargin() const
40 btVector3 halfExtents = getHalfExtentsWithoutMargin();
41 btVector3 margin(getMargin(), getMargin(), getMargin());
42 halfExtents += margin;
46 const btVector3& getHalfExtentsWithoutMargin() const
48 return m_implicitShapeDimensions; //changed in Bullet 2.63: assume the scaling and margin are included
51 virtual btVector3 localGetSupportingVertex(const btVector3& vec) const
53 btVector3 halfExtents = getHalfExtentsWithoutMargin();
54 btVector3 margin(getMargin(), getMargin(), getMargin());
55 halfExtents += margin;
57 return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
58 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
59 btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
62 SIMD_FORCE_INLINE btVector3 localGetSupportingVertexWithoutMargin(const btVector3& vec) const
64 const btVector3& halfExtents = getHalfExtentsWithoutMargin();
66 return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
67 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
68 btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
71 virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
73 const btVector3& halfExtents = getHalfExtentsWithoutMargin();
75 for (int i = 0; i < numVectors; i++)
77 const btVector3& vec = vectors[i];
78 supportVerticesOut[i].setValue(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
79 btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
80 btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
84 ///a btBox2dShape is a flat 2D box in the X-Y plane (Z extents are zero)
85 btBox2dShape(const btVector3& boxHalfExtents)
86 : btPolyhedralConvexShape(),
89 m_vertices[0].setValue(-boxHalfExtents.getX(), -boxHalfExtents.getY(), 0);
90 m_vertices[1].setValue(boxHalfExtents.getX(), -boxHalfExtents.getY(), 0);
91 m_vertices[2].setValue(boxHalfExtents.getX(), boxHalfExtents.getY(), 0);
92 m_vertices[3].setValue(-boxHalfExtents.getX(), boxHalfExtents.getY(), 0);
94 m_normals[0].setValue(0, -1, 0);
95 m_normals[1].setValue(1, 0, 0);
96 m_normals[2].setValue(0, 1, 0);
97 m_normals[3].setValue(-1, 0, 0);
99 btScalar minDimension = boxHalfExtents.getX();
100 if (minDimension > boxHalfExtents.getY())
101 minDimension = boxHalfExtents.getY();
103 m_shapeType = BOX_2D_SHAPE_PROXYTYPE;
104 btVector3 margin(getMargin(), getMargin(), getMargin());
105 m_implicitShapeDimensions = (boxHalfExtents * m_localScaling) - margin;
107 setSafeMargin(minDimension);
110 virtual void setMargin(btScalar collisionMargin)
112 //correct the m_implicitShapeDimensions for the margin
113 btVector3 oldMargin(getMargin(), getMargin(), getMargin());
114 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions + oldMargin;
116 btConvexInternalShape::setMargin(collisionMargin);
117 btVector3 newMargin(getMargin(), getMargin(), getMargin());
118 m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
120 virtual void setLocalScaling(const btVector3& scaling)
122 btVector3 oldMargin(getMargin(), getMargin(), getMargin());
123 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions + oldMargin;
124 btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
126 btConvexInternalShape::setLocalScaling(scaling);
128 m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
131 virtual void getAabb(const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const;
133 virtual void calculateLocalInertia(btScalar mass, btVector3 & inertia) const;
135 int getVertexCount() const
140 virtual int getNumVertices() const
145 const btVector3* getVertices() const
147 return &m_vertices[0];
150 const btVector3* getNormals() const
152 return &m_normals[0];
155 virtual void getPlane(btVector3 & planeNormal, btVector3 & planeSupport, int i) const
157 //this plane might not be aligned...
159 getPlaneEquation(plane, i);
160 planeNormal = btVector3(plane.getX(), plane.getY(), plane.getZ());
161 planeSupport = localGetSupportingVertex(-planeNormal);
164 const btVector3& getCentroid() const
169 virtual int getNumPlanes() const
174 virtual int getNumEdges() const
179 virtual void getVertex(int i, btVector3& vtx) const
181 btVector3 halfExtents = getHalfExtentsWithoutMargin();
184 halfExtents.x() * (1 - (i & 1)) - halfExtents.x() * (i & 1),
185 halfExtents.y() * (1 - ((i & 2) >> 1)) - halfExtents.y() * ((i & 2) >> 1),
186 halfExtents.z() * (1 - ((i & 4) >> 2)) - halfExtents.z() * ((i & 4) >> 2));
189 virtual void getPlaneEquation(btVector4 & plane, int i) const
191 btVector3 halfExtents = getHalfExtentsWithoutMargin();
196 plane.setValue(btScalar(1.), btScalar(0.), btScalar(0.), -halfExtents.x());
199 plane.setValue(btScalar(-1.), btScalar(0.), btScalar(0.), -halfExtents.x());
202 plane.setValue(btScalar(0.), btScalar(1.), btScalar(0.), -halfExtents.y());
205 plane.setValue(btScalar(0.), btScalar(-1.), btScalar(0.), -halfExtents.y());
208 plane.setValue(btScalar(0.), btScalar(0.), btScalar(1.), -halfExtents.z());
211 plane.setValue(btScalar(0.), btScalar(0.), btScalar(-1.), -halfExtents.z());
218 virtual void getEdge(int i, btVector3& pa, btVector3& pb) const
219 //virtual void getEdge(int i,Edge& edge) const
280 getVertex(edgeVert0, pa);
281 getVertex(edgeVert1, pb);
284 virtual bool isInside(const btVector3& pt, btScalar tolerance) const
286 btVector3 halfExtents = getHalfExtentsWithoutMargin();
288 //btScalar minDist = 2*tolerance;
290 bool result = (pt.x() <= (halfExtents.x() + tolerance)) &&
291 (pt.x() >= (-halfExtents.x() - tolerance)) &&
292 (pt.y() <= (halfExtents.y() + tolerance)) &&
293 (pt.y() >= (-halfExtents.y() - tolerance)) &&
294 (pt.z() <= (halfExtents.z() + tolerance)) &&
295 (pt.z() >= (-halfExtents.z() - tolerance));
301 virtual const char* getName() const
306 virtual int getNumPreferredPenetrationDirections() const
311 virtual void getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
316 penetrationVector.setValue(btScalar(1.), btScalar(0.), btScalar(0.));
319 penetrationVector.setValue(btScalar(-1.), btScalar(0.), btScalar(0.));
322 penetrationVector.setValue(btScalar(0.), btScalar(1.), btScalar(0.));
325 penetrationVector.setValue(btScalar(0.), btScalar(-1.), btScalar(0.));
328 penetrationVector.setValue(btScalar(0.), btScalar(0.), btScalar(1.));
331 penetrationVector.setValue(btScalar(0.), btScalar(0.), btScalar(-1.));
339 #endif //BT_OBB_BOX_2D_SHAPE_H