Back to index

supertuxkart  0.5+dfsg1
btPolyhedralConvexShape.cpp
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 #include "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h"
00017 
00018 btPolyhedralConvexShape::btPolyhedralConvexShape()
00019 :m_localAabbMin(1,1,1),
00020 m_localAabbMax(-1,-1,-1),
00021 m_isLocalAabbValid(false),
00022 m_optionalHull(0)
00023 {
00024 
00025 }
00026 
00027 
00028 
00029 btVector3     btPolyhedralConvexShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
00030 {
00031        int i;
00032        btVector3 supVec(0,0,0);
00033 
00034        btScalar maxDot(btScalar(-1e30));
00035 
00036        btVector3 vec = vec0;
00037        btScalar lenSqr = vec.length2();
00038        if (lenSqr < btScalar(0.0001))
00039        {
00040               vec.setValue(1,0,0);
00041        } else
00042        {
00043               btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
00044               vec *= rlen;
00045        }
00046 
00047        btVector3 vtx;
00048        btScalar newDot;
00049 
00050        for (i=0;i<getNumVertices();i++)
00051        {
00052               getVertex(i,vtx);
00053               newDot = vec.dot(vtx);
00054               if (newDot > maxDot)
00055               {
00056                      maxDot = newDot;
00057                      supVec = vtx;
00058               }
00059        }
00060 
00061        return supVec;
00062 
00063 }
00064 
00065 void   btPolyhedralConvexShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00066 {
00067        int i;
00068 
00069        btVector3 vtx;
00070        btScalar newDot;
00071 
00072        for (i=0;i<numVectors;i++)
00073        {
00074               supportVerticesOut[i][3] = btScalar(-1e30);
00075        }
00076 
00077        for (int j=0;j<numVectors;j++)
00078        {
00079        
00080               const btVector3& vec = vectors[j];
00081 
00082               for (i=0;i<getNumVertices();i++)
00083               {
00084                      getVertex(i,vtx);
00085                      newDot = vec.dot(vtx);
00086                      if (newDot > supportVerticesOut[j][3])
00087                      {
00088                             //WARNING: don't swap next lines, the w component would get overwritten!
00089                             supportVerticesOut[j] = vtx;
00090                             supportVerticesOut[j][3] = newDot;
00091                      }
00092               }
00093        }
00094 }
00095 
00096 
00097 
00098 void   btPolyhedralConvexShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
00099 {
00100        //not yet, return box inertia
00101 
00102        btScalar margin = getMargin();
00103 
00104        btTransform ident;
00105        ident.setIdentity();
00106        btVector3 aabbMin,aabbMax;
00107        getAabb(ident,aabbMin,aabbMax);
00108        btVector3 halfExtents = (aabbMax-aabbMin)*btScalar(0.5);
00109 
00110        btScalar lx=btScalar(2.)*(halfExtents.x()+margin);
00111        btScalar ly=btScalar(2.)*(halfExtents.y()+margin);
00112        btScalar lz=btScalar(2.)*(halfExtents.z()+margin);
00113        const btScalar x2 = lx*lx;
00114        const btScalar y2 = ly*ly;
00115        const btScalar z2 = lz*lz;
00116        const btScalar scaledmass = mass * btScalar(0.08333333);
00117 
00118        inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
00119 
00120 }
00121 
00122 
00123 
00124 void btPolyhedralConvexShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
00125 {
00126        getNonvirtualAabb(trans,aabbMin,aabbMax,getMargin());
00127 }
00128 
00129 
00130 
00131 
00132 void   btPolyhedralConvexShape::recalcLocalAabb()
00133 {
00134        m_isLocalAabbValid = true;
00135 
00136        for (int i=0;i<3;i++)
00137        {
00138               btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.));
00139               vec[i] = btScalar(1.);
00140               btVector3 tmp = localGetSupportingVertex(vec);
00141               m_localAabbMax[i] = tmp[i]+m_collisionMargin;
00142               vec[i] = btScalar(-1.);
00143               tmp = localGetSupportingVertex(vec);
00144               m_localAabbMin[i] = tmp[i]-m_collisionMargin;
00145        }
00146 }
00147 
00148