| [1963] | 1 | /* | 
|---|
 | 2 | Bullet Continuous Collision Detection and Physics Library | 
|---|
| [8351] | 3 | Copyright (c) 2003-2009 Erwin Coumans  http://bulletphysics.org | 
|---|
| [1963] | 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" | 
|---|
| [2430] | 22 | #include "LinearMath/btVector3.h" | 
|---|
| [1963] | 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 |         { | 
|---|
| [8351] | 44 |                 return m_implicitShapeDimensions;//scaling is included, margin is not | 
|---|
| [1963] | 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 |  | 
|---|
| [2430] | 120 |         virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i ) const | 
|---|
| [1963] | 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: | 
|---|
| [2882] | 164 |                         plane.setValue(btScalar(1.),btScalar(0.),btScalar(0.),-halfExtents.x()); | 
|---|
| [1963] | 165 |                         break; | 
|---|
 | 166 |                 case 1: | 
|---|
| [2882] | 167 |                         plane.setValue(btScalar(-1.),btScalar(0.),btScalar(0.),-halfExtents.x()); | 
|---|
| [1963] | 168 |                         break; | 
|---|
 | 169 |                 case 2: | 
|---|
| [2882] | 170 |                         plane.setValue(btScalar(0.),btScalar(1.),btScalar(0.),-halfExtents.y()); | 
|---|
| [1963] | 171 |                         break; | 
|---|
 | 172 |                 case 3: | 
|---|
| [2882] | 173 |                         plane.setValue(btScalar(0.),btScalar(-1.),btScalar(0.),-halfExtents.y()); | 
|---|
| [1963] | 174 |                         break; | 
|---|
 | 175 |                 case 4: | 
|---|
| [2882] | 176 |                         plane.setValue(btScalar(0.),btScalar(0.),btScalar(1.),-halfExtents.z()); | 
|---|
| [1963] | 177 |                         break; | 
|---|
 | 178 |                 case 5: | 
|---|
| [2882] | 179 |                         plane.setValue(btScalar(0.),btScalar(0.),btScalar(-1.),-halfExtents.z()); | 
|---|
| [1963] | 180 |                         break; | 
|---|
 | 181 |                 default: | 
|---|
| [2882] | 182 |                         btAssert(0); | 
|---|
| [1963] | 183 |                 } | 
|---|
 | 184 |         } | 
|---|
 | 185 |  | 
|---|
 | 186 |          | 
|---|
| [2430] | 187 |         virtual void getEdge(int i,btVector3& pa,btVector3& pb) const | 
|---|
| [1963] | 188 |         //virtual void getEdge(int i,Edge& edge) const | 
|---|
 | 189 |         { | 
|---|
 | 190 |                 int edgeVert0 = 0; | 
|---|
 | 191 |                 int edgeVert1 = 0; | 
|---|
 | 192 |  | 
|---|
 | 193 |                 switch (i) | 
|---|
 | 194 |                 { | 
|---|
 | 195 |                 case 0: | 
|---|
 | 196 |                                 edgeVert0 = 0; | 
|---|
 | 197 |                                 edgeVert1 = 1; | 
|---|
 | 198 |                         break; | 
|---|
 | 199 |                 case 1: | 
|---|
 | 200 |                                 edgeVert0 = 0; | 
|---|
 | 201 |                                 edgeVert1 = 2; | 
|---|
 | 202 |                         break; | 
|---|
 | 203 |                 case 2: | 
|---|
 | 204 |                         edgeVert0 = 1; | 
|---|
 | 205 |                         edgeVert1 = 3; | 
|---|
 | 206 |  | 
|---|
 | 207 |                         break; | 
|---|
 | 208 |                 case 3: | 
|---|
 | 209 |                         edgeVert0 = 2; | 
|---|
 | 210 |                         edgeVert1 = 3; | 
|---|
 | 211 |                         break; | 
|---|
 | 212 |                 case 4: | 
|---|
 | 213 |                         edgeVert0 = 0; | 
|---|
 | 214 |                         edgeVert1 = 4; | 
|---|
 | 215 |                         break; | 
|---|
 | 216 |                 case 5: | 
|---|
 | 217 |                         edgeVert0 = 1; | 
|---|
 | 218 |                         edgeVert1 = 5; | 
|---|
 | 219 |  | 
|---|
 | 220 |                         break; | 
|---|
 | 221 |                 case 6: | 
|---|
 | 222 |                         edgeVert0 = 2; | 
|---|
 | 223 |                         edgeVert1 = 6; | 
|---|
 | 224 |                         break; | 
|---|
 | 225 |                 case 7: | 
|---|
 | 226 |                         edgeVert0 = 3; | 
|---|
 | 227 |                         edgeVert1 = 7; | 
|---|
 | 228 |                         break; | 
|---|
 | 229 |                 case 8: | 
|---|
 | 230 |                         edgeVert0 = 4; | 
|---|
 | 231 |                         edgeVert1 = 5; | 
|---|
 | 232 |                         break; | 
|---|
 | 233 |                 case 9: | 
|---|
 | 234 |                         edgeVert0 = 4; | 
|---|
 | 235 |                         edgeVert1 = 6; | 
|---|
 | 236 |                         break; | 
|---|
 | 237 |                 case 10: | 
|---|
 | 238 |                         edgeVert0 = 5; | 
|---|
 | 239 |                         edgeVert1 = 7; | 
|---|
 | 240 |                         break; | 
|---|
 | 241 |                 case 11: | 
|---|
 | 242 |                         edgeVert0 = 6; | 
|---|
 | 243 |                         edgeVert1 = 7; | 
|---|
 | 244 |                         break; | 
|---|
 | 245 |                 default: | 
|---|
 | 246 |                         btAssert(0); | 
|---|
 | 247 |  | 
|---|
 | 248 |                 } | 
|---|
 | 249 |  | 
|---|
 | 250 |                 getVertex(edgeVert0,pa ); | 
|---|
 | 251 |                 getVertex(edgeVert1,pb ); | 
|---|
 | 252 |         } | 
|---|
 | 253 |  | 
|---|
 | 254 |  | 
|---|
 | 255 |  | 
|---|
 | 256 |  | 
|---|
 | 257 |          | 
|---|
| [2430] | 258 |         virtual bool isInside(const btVector3& pt,btScalar tolerance) const | 
|---|
| [1963] | 259 |         { | 
|---|
 | 260 |                 btVector3 halfExtents = getHalfExtentsWithoutMargin(); | 
|---|
 | 261 |  | 
|---|
 | 262 |                 //btScalar minDist = 2*tolerance; | 
|---|
 | 263 |                  | 
|---|
 | 264 |                 bool result =   (pt.x() <= (halfExtents.x()+tolerance)) && | 
|---|
 | 265 |                                                 (pt.x() >= (-halfExtents.x()-tolerance)) && | 
|---|
 | 266 |                                                 (pt.y() <= (halfExtents.y()+tolerance)) && | 
|---|
 | 267 |                                                 (pt.y() >= (-halfExtents.y()-tolerance)) && | 
|---|
 | 268 |                                                 (pt.z() <= (halfExtents.z()+tolerance)) && | 
|---|
 | 269 |                                                 (pt.z() >= (-halfExtents.z()-tolerance)); | 
|---|
 | 270 |                  | 
|---|
 | 271 |                 return result; | 
|---|
 | 272 |         } | 
|---|
 | 273 |  | 
|---|
 | 274 |  | 
|---|
 | 275 |         //debugging | 
|---|
 | 276 |         virtual const char*     getName()const | 
|---|
 | 277 |         { | 
|---|
 | 278 |                 return "Box"; | 
|---|
 | 279 |         } | 
|---|
 | 280 |  | 
|---|
 | 281 |         virtual int             getNumPreferredPenetrationDirections() const | 
|---|
 | 282 |         { | 
|---|
 | 283 |                 return 6; | 
|---|
 | 284 |         } | 
|---|
 | 285 |          | 
|---|
 | 286 |         virtual void    getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const | 
|---|
 | 287 |         { | 
|---|
 | 288 |                 switch (index) | 
|---|
 | 289 |                 { | 
|---|
 | 290 |                 case 0: | 
|---|
 | 291 |                         penetrationVector.setValue(btScalar(1.),btScalar(0.),btScalar(0.)); | 
|---|
 | 292 |                         break; | 
|---|
 | 293 |                 case 1: | 
|---|
 | 294 |                         penetrationVector.setValue(btScalar(-1.),btScalar(0.),btScalar(0.)); | 
|---|
 | 295 |                         break; | 
|---|
 | 296 |                 case 2: | 
|---|
 | 297 |                         penetrationVector.setValue(btScalar(0.),btScalar(1.),btScalar(0.)); | 
|---|
 | 298 |                         break; | 
|---|
 | 299 |                 case 3: | 
|---|
 | 300 |                         penetrationVector.setValue(btScalar(0.),btScalar(-1.),btScalar(0.)); | 
|---|
 | 301 |                         break; | 
|---|
 | 302 |                 case 4: | 
|---|
 | 303 |                         penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(1.)); | 
|---|
 | 304 |                         break; | 
|---|
 | 305 |                 case 5: | 
|---|
 | 306 |                         penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(-1.)); | 
|---|
 | 307 |                         break; | 
|---|
 | 308 |                 default: | 
|---|
| [2882] | 309 |                         btAssert(0); | 
|---|
| [1963] | 310 |                 } | 
|---|
 | 311 |         } | 
|---|
 | 312 |  | 
|---|
 | 313 | }; | 
|---|
 | 314 |  | 
|---|
| [8351] | 315 |  | 
|---|
| [1963] | 316 | #endif //OBB_BOX_MINKOWSKI_H | 
|---|
 | 317 |  | 
|---|
 | 318 |  | 
|---|