| 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 OBB_BOX_MINKOWSKI_H | 
|---|
| 17 | #define OBB_BOX_MINKOWSKI_H | 
|---|
| 18 |  | 
|---|
| 19 | #include "btPolyhedralConvexShape.h" | 
|---|
| 20 | #include "btCollisionMargin.h" | 
|---|
| 21 | #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" | 
|---|
| 22 | #include "LinearMath/btVector3.h" | 
|---|
| 23 | #include "LinearMath/btMinMax.h" | 
|---|
| 24 |  | 
|---|
| 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 | class btBoxShape: public btPolyhedralConvexShape | 
|---|
| 27 | { | 
|---|
| 28 |  | 
|---|
| 29 | //btVector3     m_boxHalfExtents1; //use m_implicitShapeDimensions instead | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | public: | 
|---|
| 33 |  | 
|---|
| 34 | btVector3 getHalfExtentsWithMargin() const | 
|---|
| 35 | { | 
|---|
| 36 | btVector3 halfExtents = getHalfExtentsWithoutMargin(); | 
|---|
| 37 | btVector3 margin(getMargin(),getMargin(),getMargin()); | 
|---|
| 38 | halfExtents += margin; | 
|---|
| 39 | return halfExtents; | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | const btVector3& getHalfExtentsWithoutMargin() const | 
|---|
| 43 | { | 
|---|
| 44 | return m_implicitShapeDimensions;//changed in Bullet 2.63: assume the scaling and margin are included | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | virtual btVector3       localGetSupportingVertex(const btVector3& vec) const | 
|---|
| 49 | { | 
|---|
| 50 | btVector3 halfExtents = getHalfExtentsWithoutMargin(); | 
|---|
| 51 | btVector3 margin(getMargin(),getMargin(),getMargin()); | 
|---|
| 52 | halfExtents += margin; | 
|---|
| 53 |  | 
|---|
| 54 | return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()), | 
|---|
| 55 | btFsels(vec.y(), halfExtents.y(), -halfExtents.y()), | 
|---|
| 56 | btFsels(vec.z(), halfExtents.z(), -halfExtents.z())); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | SIMD_FORCE_INLINE  btVector3    localGetSupportingVertexWithoutMargin(const btVector3& vec)const | 
|---|
| 60 | { | 
|---|
| 61 | const btVector3& halfExtents = getHalfExtentsWithoutMargin(); | 
|---|
| 62 |  | 
|---|
| 63 | return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()), | 
|---|
| 64 | btFsels(vec.y(), halfExtents.y(), -halfExtents.y()), | 
|---|
| 65 | btFsels(vec.z(), halfExtents.z(), -halfExtents.z())); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const | 
|---|
| 69 | { | 
|---|
| 70 | const btVector3& halfExtents = getHalfExtentsWithoutMargin(); | 
|---|
| 71 |  | 
|---|
| 72 | for (int i=0;i<numVectors;i++) | 
|---|
| 73 | { | 
|---|
| 74 | const btVector3& vec = vectors[i]; | 
|---|
| 75 | supportVerticesOut[i].setValue(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()), | 
|---|
| 76 | btFsels(vec.y(), halfExtents.y(), -halfExtents.y()), | 
|---|
| 77 | btFsels(vec.z(), halfExtents.z(), -halfExtents.z())); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | btBoxShape( const btVector3& boxHalfExtents) | 
|---|
| 84 | : btPolyhedralConvexShape() | 
|---|
| 85 | { | 
|---|
| 86 | m_shapeType = BOX_SHAPE_PROXYTYPE; | 
|---|
| 87 | btVector3 margin(getMargin(),getMargin(),getMargin()); | 
|---|
| 88 | m_implicitShapeDimensions = (boxHalfExtents * m_localScaling) - margin; | 
|---|
| 89 | }; | 
|---|
| 90 |  | 
|---|
| 91 | virtual void setMargin(btScalar collisionMargin) | 
|---|
| 92 | { | 
|---|
| 93 | //correct the m_implicitShapeDimensions for the margin | 
|---|
| 94 | btVector3 oldMargin(getMargin(),getMargin(),getMargin()); | 
|---|
| 95 | btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin; | 
|---|
| 96 |  | 
|---|
| 97 | btConvexInternalShape::setMargin(collisionMargin); | 
|---|
| 98 | btVector3 newMargin(getMargin(),getMargin(),getMargin()); | 
|---|
| 99 | m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin; | 
|---|
| 100 |  | 
|---|
| 101 | } | 
|---|
| 102 | virtual void    setLocalScaling(const btVector3& scaling) | 
|---|
| 103 | { | 
|---|
| 104 | btVector3 oldMargin(getMargin(),getMargin(),getMargin()); | 
|---|
| 105 | btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin; | 
|---|
| 106 | btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling; | 
|---|
| 107 |  | 
|---|
| 108 | btConvexInternalShape::setLocalScaling(scaling); | 
|---|
| 109 |  | 
|---|
| 110 | m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin; | 
|---|
| 111 |  | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const; | 
|---|
| 115 |  | 
|---|
| 116 |  | 
|---|
| 117 |  | 
|---|
| 118 | virtual void    calculateLocalInertia(btScalar mass,btVector3& inertia) const; | 
|---|
| 119 |  | 
|---|
| 120 | virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i ) const | 
|---|
| 121 | { | 
|---|
| 122 | //this plane might not be aligned... | 
|---|
| 123 | btVector4 plane ; | 
|---|
| 124 | getPlaneEquation(plane,i); | 
|---|
| 125 | planeNormal = btVector3(plane.getX(),plane.getY(),plane.getZ()); | 
|---|
| 126 | planeSupport = localGetSupportingVertex(-planeNormal); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 |  | 
|---|
| 130 | virtual int getNumPlanes() const | 
|---|
| 131 | { | 
|---|
| 132 | return 6; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | virtual int     getNumVertices() const | 
|---|
| 136 | { | 
|---|
| 137 | return 8; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | virtual int getNumEdges() const | 
|---|
| 141 | { | 
|---|
| 142 | return 12; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 | virtual void getVertex(int i,btVector3& vtx) const | 
|---|
| 147 | { | 
|---|
| 148 | btVector3 halfExtents = getHalfExtentsWithoutMargin(); | 
|---|
| 149 |  | 
|---|
| 150 | vtx = btVector3( | 
|---|
| 151 | halfExtents.x() * (1-(i&1)) - halfExtents.x() * (i&1), | 
|---|
| 152 | halfExtents.y() * (1-((i&2)>>1)) - halfExtents.y() * ((i&2)>>1), | 
|---|
| 153 | halfExtents.z() * (1-((i&4)>>2)) - halfExtents.z() * ((i&4)>>2)); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 | virtual void    getPlaneEquation(btVector4& plane,int i) const | 
|---|
| 158 | { | 
|---|
| 159 | btVector3 halfExtents = getHalfExtentsWithoutMargin(); | 
|---|
| 160 |  | 
|---|
| 161 | switch (i) | 
|---|
| 162 | { | 
|---|
| 163 | case 0: | 
|---|
| 164 | plane.setValue(btScalar(1.),btScalar(0.),btScalar(0.)); | 
|---|
| 165 | plane[3] = -halfExtents.x(); | 
|---|
| 166 | break; | 
|---|
| 167 | case 1: | 
|---|
| 168 | plane.setValue(btScalar(-1.),btScalar(0.),btScalar(0.)); | 
|---|
| 169 | plane[3] = -halfExtents.x(); | 
|---|
| 170 | break; | 
|---|
| 171 | case 2: | 
|---|
| 172 | plane.setValue(btScalar(0.),btScalar(1.),btScalar(0.)); | 
|---|
| 173 | plane[3] = -halfExtents.y(); | 
|---|
| 174 | break; | 
|---|
| 175 | case 3: | 
|---|
| 176 | plane.setValue(btScalar(0.),btScalar(-1.),btScalar(0.)); | 
|---|
| 177 | plane[3] = -halfExtents.y(); | 
|---|
| 178 | break; | 
|---|
| 179 | case 4: | 
|---|
| 180 | plane.setValue(btScalar(0.),btScalar(0.),btScalar(1.)); | 
|---|
| 181 | plane[3] = -halfExtents.z(); | 
|---|
| 182 | break; | 
|---|
| 183 | case 5: | 
|---|
| 184 | plane.setValue(btScalar(0.),btScalar(0.),btScalar(-1.)); | 
|---|
| 185 | plane[3] = -halfExtents.z(); | 
|---|
| 186 | break; | 
|---|
| 187 | default: | 
|---|
| 188 | assert(0); | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 |  | 
|---|
| 193 | virtual void getEdge(int i,btVector3& pa,btVector3& pb) const | 
|---|
| 194 | //virtual void getEdge(int i,Edge& edge) const | 
|---|
| 195 | { | 
|---|
| 196 | int edgeVert0 = 0; | 
|---|
| 197 | int edgeVert1 = 0; | 
|---|
| 198 |  | 
|---|
| 199 | switch (i) | 
|---|
| 200 | { | 
|---|
| 201 | case 0: | 
|---|
| 202 | edgeVert0 = 0; | 
|---|
| 203 | edgeVert1 = 1; | 
|---|
| 204 | break; | 
|---|
| 205 | case 1: | 
|---|
| 206 | edgeVert0 = 0; | 
|---|
| 207 | edgeVert1 = 2; | 
|---|
| 208 | break; | 
|---|
| 209 | case 2: | 
|---|
| 210 | edgeVert0 = 1; | 
|---|
| 211 | edgeVert1 = 3; | 
|---|
| 212 |  | 
|---|
| 213 | break; | 
|---|
| 214 | case 3: | 
|---|
| 215 | edgeVert0 = 2; | 
|---|
| 216 | edgeVert1 = 3; | 
|---|
| 217 | break; | 
|---|
| 218 | case 4: | 
|---|
| 219 | edgeVert0 = 0; | 
|---|
| 220 | edgeVert1 = 4; | 
|---|
| 221 | break; | 
|---|
| 222 | case 5: | 
|---|
| 223 | edgeVert0 = 1; | 
|---|
| 224 | edgeVert1 = 5; | 
|---|
| 225 |  | 
|---|
| 226 | break; | 
|---|
| 227 | case 6: | 
|---|
| 228 | edgeVert0 = 2; | 
|---|
| 229 | edgeVert1 = 6; | 
|---|
| 230 | break; | 
|---|
| 231 | case 7: | 
|---|
| 232 | edgeVert0 = 3; | 
|---|
| 233 | edgeVert1 = 7; | 
|---|
| 234 | break; | 
|---|
| 235 | case 8: | 
|---|
| 236 | edgeVert0 = 4; | 
|---|
| 237 | edgeVert1 = 5; | 
|---|
| 238 | break; | 
|---|
| 239 | case 9: | 
|---|
| 240 | edgeVert0 = 4; | 
|---|
| 241 | edgeVert1 = 6; | 
|---|
| 242 | break; | 
|---|
| 243 | case 10: | 
|---|
| 244 | edgeVert0 = 5; | 
|---|
| 245 | edgeVert1 = 7; | 
|---|
| 246 | break; | 
|---|
| 247 | case 11: | 
|---|
| 248 | edgeVert0 = 6; | 
|---|
| 249 | edgeVert1 = 7; | 
|---|
| 250 | break; | 
|---|
| 251 | default: | 
|---|
| 252 | btAssert(0); | 
|---|
| 253 |  | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | getVertex(edgeVert0,pa ); | 
|---|
| 257 | getVertex(edgeVert1,pb ); | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 |  | 
|---|
| 261 |  | 
|---|
| 262 |  | 
|---|
| 263 |  | 
|---|
| 264 | virtual bool isInside(const btVector3& pt,btScalar tolerance) const | 
|---|
| 265 | { | 
|---|
| 266 | btVector3 halfExtents = getHalfExtentsWithoutMargin(); | 
|---|
| 267 |  | 
|---|
| 268 | //btScalar minDist = 2*tolerance; | 
|---|
| 269 |  | 
|---|
| 270 | bool result =   (pt.x() <= (halfExtents.x()+tolerance)) && | 
|---|
| 271 | (pt.x() >= (-halfExtents.x()-tolerance)) && | 
|---|
| 272 | (pt.y() <= (halfExtents.y()+tolerance)) && | 
|---|
| 273 | (pt.y() >= (-halfExtents.y()-tolerance)) && | 
|---|
| 274 | (pt.z() <= (halfExtents.z()+tolerance)) && | 
|---|
| 275 | (pt.z() >= (-halfExtents.z()-tolerance)); | 
|---|
| 276 |  | 
|---|
| 277 | return result; | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 |  | 
|---|
| 281 | //debugging | 
|---|
| 282 | virtual const char*     getName()const | 
|---|
| 283 | { | 
|---|
| 284 | return "Box"; | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | virtual int             getNumPreferredPenetrationDirections() const | 
|---|
| 288 | { | 
|---|
| 289 | return 6; | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | virtual void    getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const | 
|---|
| 293 | { | 
|---|
| 294 | switch (index) | 
|---|
| 295 | { | 
|---|
| 296 | case 0: | 
|---|
| 297 | penetrationVector.setValue(btScalar(1.),btScalar(0.),btScalar(0.)); | 
|---|
| 298 | break; | 
|---|
| 299 | case 1: | 
|---|
| 300 | penetrationVector.setValue(btScalar(-1.),btScalar(0.),btScalar(0.)); | 
|---|
| 301 | break; | 
|---|
| 302 | case 2: | 
|---|
| 303 | penetrationVector.setValue(btScalar(0.),btScalar(1.),btScalar(0.)); | 
|---|
| 304 | break; | 
|---|
| 305 | case 3: | 
|---|
| 306 | penetrationVector.setValue(btScalar(0.),btScalar(-1.),btScalar(0.)); | 
|---|
| 307 | break; | 
|---|
| 308 | case 4: | 
|---|
| 309 | penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(1.)); | 
|---|
| 310 | break; | 
|---|
| 311 | case 5: | 
|---|
| 312 | penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(-1.)); | 
|---|
| 313 | break; | 
|---|
| 314 | default: | 
|---|
| 315 | assert(0); | 
|---|
| 316 | } | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 | }; | 
|---|
| 320 |  | 
|---|
| 321 | #endif //OBB_BOX_MINKOWSKI_H | 
|---|
| 322 |  | 
|---|
| 323 |  | 
|---|