Back to index

supertuxkart  0.5+dfsg1
btBoxShape.h
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #ifndef OBB_BOX_MINKOWSKI_H
00017 #define OBB_BOX_MINKOWSKI_H
00018 
00019 #include "btPolyhedralConvexShape.h"
00020 #include "btCollisionMargin.h"
00021 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
00022 #include "LinearMath/btPoint3.h"
00023 #include "LinearMath/btMinMax.h"
00024 
00026 class btBoxShape: public btPolyhedralConvexShape
00027 {
00028 
00029        //btVector3   m_boxHalfExtents1; //use m_implicitShapeDimensions instead
00030 
00031 
00032 public:
00033 
00034        btVector3 getHalfExtentsWithMargin() const
00035        {
00036               btVector3 halfExtents = getHalfExtentsWithoutMargin();
00037               btVector3 margin(getMargin(),getMargin(),getMargin());
00038               halfExtents += margin;
00039               return halfExtents;
00040        }
00041        
00042        const btVector3& getHalfExtentsWithoutMargin() const
00043        {
00044               return m_implicitShapeDimensions;//changed in Bullet 2.63: assume the scaling and margin are included
00045        }
00046        
00047 
00048        virtual int   getShapeType() const { return BOX_SHAPE_PROXYTYPE;}
00049 
00050        virtual btVector3    localGetSupportingVertex(const btVector3& vec) const
00051        {
00052               btVector3 halfExtents = getHalfExtentsWithoutMargin();
00053               btVector3 margin(getMargin(),getMargin(),getMargin());
00054               halfExtents += margin;
00055               
00056               return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
00057                      btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
00058                      btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
00059        }
00060 
00061        SIMD_FORCE_INLINE  btVector3       localGetSupportingVertexWithoutMargin(const btVector3& vec)const
00062        {
00063               const btVector3& halfExtents = getHalfExtentsWithoutMargin();
00064               
00065               return btVector3(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
00066                      btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
00067                      btFsels(vec.z(), halfExtents.z(), -halfExtents.z()));
00068        }
00069 
00070        virtual void  batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00071        {
00072               const btVector3& halfExtents = getHalfExtentsWithoutMargin();
00073        
00074               for (int i=0;i<numVectors;i++)
00075               {
00076                      const btVector3& vec = vectors[i];
00077                      supportVerticesOut[i].setValue(btFsels(vec.x(), halfExtents.x(), -halfExtents.x()),
00078                             btFsels(vec.y(), halfExtents.y(), -halfExtents.y()),
00079                             btFsels(vec.z(), halfExtents.z(), -halfExtents.z())); 
00080               }
00081 
00082        }
00083 
00084 
00085        btBoxShape( const btVector3& boxHalfExtents)
00086        {
00087               btVector3 margin(getMargin(),getMargin(),getMargin());
00088               m_implicitShapeDimensions = (boxHalfExtents * m_localScaling) - margin;
00089        };
00090 
00091        virtual void setMargin(btScalar collisionMargin)
00092        {
00093               //correct the m_implicitShapeDimensions for the margin
00094               btVector3 oldMargin(getMargin(),getMargin(),getMargin());
00095               btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
00096               
00097               btConvexInternalShape::setMargin(collisionMargin);
00098               btVector3 newMargin(getMargin(),getMargin(),getMargin());
00099               m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
00100 
00101        }
00102        virtual void  setLocalScaling(const btVector3& scaling)
00103        {
00104               btVector3 oldMargin(getMargin(),getMargin(),getMargin());
00105               btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
00106               btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
00107 
00108               btConvexInternalShape::setLocalScaling(scaling);
00109 
00110               m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
00111 
00112        }
00113 
00114        virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
00115 
00116        
00117 
00118        virtual void  calculateLocalInertia(btScalar mass,btVector3& inertia) const;
00119 
00120        virtual void getPlane(btVector3& planeNormal,btPoint3& planeSupport,int i ) const
00121        {
00122               //this plane might not be aligned...
00123               btVector4 plane ;
00124               getPlaneEquation(plane,i);
00125               planeNormal = btVector3(plane.getX(),plane.getY(),plane.getZ());
00126               planeSupport = localGetSupportingVertex(-planeNormal);
00127        }
00128 
00129        
00130        virtual int getNumPlanes() const
00131        {
00132               return 6;
00133        }      
00134        
00135        virtual int   getNumVertices() const 
00136        {
00137               return 8;
00138        }
00139 
00140        virtual int getNumEdges() const
00141        {
00142               return 12;
00143        }
00144 
00145 
00146        virtual void getVertex(int i,btVector3& vtx) const
00147        {
00148               btVector3 halfExtents = getHalfExtentsWithoutMargin();
00149 
00150               vtx = btVector3(
00151                             halfExtents.x() * (1-(i&1)) - halfExtents.x() * (i&1),
00152                             halfExtents.y() * (1-((i&2)>>1)) - halfExtents.y() * ((i&2)>>1),
00153                             halfExtents.z() * (1-((i&4)>>2)) - halfExtents.z() * ((i&4)>>2));
00154        }
00155        
00156 
00157        virtual void  getPlaneEquation(btVector4& plane,int i) const
00158        {
00159               btVector3 halfExtents = getHalfExtentsWithoutMargin();
00160 
00161               switch (i)
00162               {
00163               case 0:
00164                      plane.setValue(btScalar(1.),btScalar(0.),btScalar(0.));
00165                      plane[3] = -halfExtents.x();
00166                      break;
00167               case 1:
00168                      plane.setValue(btScalar(-1.),btScalar(0.),btScalar(0.));
00169                      plane[3] = -halfExtents.x();
00170                      break;
00171               case 2:
00172                      plane.setValue(btScalar(0.),btScalar(1.),btScalar(0.));
00173                      plane[3] = -halfExtents.y();
00174                      break;
00175               case 3:
00176                      plane.setValue(btScalar(0.),btScalar(-1.),btScalar(0.));
00177                      plane[3] = -halfExtents.y();
00178                      break;
00179               case 4:
00180                      plane.setValue(btScalar(0.),btScalar(0.),btScalar(1.));
00181                      plane[3] = -halfExtents.z();
00182                      break;
00183               case 5:
00184                      plane.setValue(btScalar(0.),btScalar(0.),btScalar(-1.));
00185                      plane[3] = -halfExtents.z();
00186                      break;
00187               default:
00188                      assert(0);
00189               }
00190        }
00191 
00192        
00193        virtual void getEdge(int i,btPoint3& pa,btPoint3& pb) const
00194        //virtual void getEdge(int i,Edge& edge) const
00195        {
00196               int edgeVert0 = 0;
00197               int edgeVert1 = 0;
00198 
00199               switch (i)
00200               {
00201               case 0:
00202                             edgeVert0 = 0;
00203                             edgeVert1 = 1;
00204                      break;
00205               case 1:
00206                             edgeVert0 = 0;
00207                             edgeVert1 = 2;
00208                      break;
00209               case 2:
00210                      edgeVert0 = 1;
00211                      edgeVert1 = 3;
00212 
00213                      break;
00214               case 3:
00215                      edgeVert0 = 2;
00216                      edgeVert1 = 3;
00217                      break;
00218               case 4:
00219                      edgeVert0 = 0;
00220                      edgeVert1 = 4;
00221                      break;
00222               case 5:
00223                      edgeVert0 = 1;
00224                      edgeVert1 = 5;
00225 
00226                      break;
00227               case 6:
00228                      edgeVert0 = 2;
00229                      edgeVert1 = 6;
00230                      break;
00231               case 7:
00232                      edgeVert0 = 3;
00233                      edgeVert1 = 7;
00234                      break;
00235               case 8:
00236                      edgeVert0 = 4;
00237                      edgeVert1 = 5;
00238                      break;
00239               case 9:
00240                      edgeVert0 = 4;
00241                      edgeVert1 = 6;
00242                      break;
00243               case 10:
00244                      edgeVert0 = 5;
00245                      edgeVert1 = 7;
00246                      break;
00247               case 11:
00248                      edgeVert0 = 6;
00249                      edgeVert1 = 7;
00250                      break;
00251               default:
00252                      btAssert(0);
00253 
00254               }
00255 
00256               getVertex(edgeVert0,pa );
00257               getVertex(edgeVert1,pb );
00258        }
00259 
00260 
00261 
00262 
00263        
00264        virtual       bool isInside(const btPoint3& pt,btScalar tolerance) const
00265        {
00266               btVector3 halfExtents = getHalfExtentsWithoutMargin();
00267 
00268               //btScalar minDist = 2*tolerance;
00269               
00270               bool result = (pt.x() <= (halfExtents.x()+tolerance)) &&
00271                                           (pt.x() >= (-halfExtents.x()-tolerance)) &&
00272                                           (pt.y() <= (halfExtents.y()+tolerance)) &&
00273                                           (pt.y() >= (-halfExtents.y()-tolerance)) &&
00274                                           (pt.z() <= (halfExtents.z()+tolerance)) &&
00275                                           (pt.z() >= (-halfExtents.z()-tolerance));
00276               
00277               return result;
00278        }
00279 
00280 
00281        //debugging
00282        virtual const char*  getName()const
00283        {
00284               return "Box";
00285        }
00286 
00287        virtual int          getNumPreferredPenetrationDirections() const
00288        {
00289               return 6;
00290        }
00291        
00292        virtual void  getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
00293        {
00294               switch (index)
00295               {
00296               case 0:
00297                      penetrationVector.setValue(btScalar(1.),btScalar(0.),btScalar(0.));
00298                      break;
00299               case 1:
00300                      penetrationVector.setValue(btScalar(-1.),btScalar(0.),btScalar(0.));
00301                      break;
00302               case 2:
00303                      penetrationVector.setValue(btScalar(0.),btScalar(1.),btScalar(0.));
00304                      break;
00305               case 3:
00306                      penetrationVector.setValue(btScalar(0.),btScalar(-1.),btScalar(0.));
00307                      break;
00308               case 4:
00309                      penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(1.));
00310                      break;
00311               case 5:
00312                      penetrationVector.setValue(btScalar(0.),btScalar(0.),btScalar(-1.));
00313                      break;
00314               default:
00315                      assert(0);
00316               }
00317        }
00318 
00319 };
00320 
00321 #endif //OBB_BOX_MINKOWSKI_H
00322 
00323