Back to index

supertuxkart  0.5+dfsg1
btMultiSphereShape.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 "btMultiSphereShape.h"
00017 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
00018 #include "LinearMath/btQuaternion.h"
00019 
00020 btMultiSphereShape::btMultiSphereShape (const btVector3& inertiaHalfExtents,const btVector3* positions,const btScalar* radi,int numSpheres)
00021 :m_inertiaHalfExtents(inertiaHalfExtents)
00022 {
00023        btScalar startMargin = btScalar(1e30);
00024 
00025        m_numSpheres = numSpheres;
00026        for (int i=0;i<m_numSpheres;i++)
00027        {
00028               m_localPositions[i] = positions[i];
00029               m_radi[i] = radi[i];
00030               if (radi[i] < startMargin)
00031                      startMargin = radi[i];
00032        }
00033        setMargin(startMargin);
00034 
00035 }
00036 
00037 
00038 
00039  
00040  btVector3    btMultiSphereShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
00041 {
00042        int i;
00043        btVector3 supVec(0,0,0);
00044 
00045        btScalar maxDot(btScalar(-1e30));
00046 
00047 
00048        btVector3 vec = vec0;
00049        btScalar lenSqr = vec.length2();
00050        if (lenSqr < (SIMD_EPSILON*SIMD_EPSILON))
00051        {
00052               vec.setValue(1,0,0);
00053        } else
00054        {
00055               btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
00056               vec *= rlen;
00057        }
00058 
00059        btVector3 vtx;
00060        btScalar newDot;
00061 
00062        const btVector3* pos = &m_localPositions[0];
00063        const btScalar* rad = &m_radi[0];
00064 
00065        for (i=0;i<m_numSpheres;i++)
00066        {
00067               vtx = (*pos) +vec*m_localScaling*(*rad) - vec * getMargin();
00068               pos++;
00069               rad++;
00070               newDot = vec.dot(vtx);
00071               if (newDot > maxDot)
00072               {
00073                      maxDot = newDot;
00074                      supVec = vtx;
00075               }
00076        }
00077 
00078        return supVec;
00079 
00080 }
00081 
00082  void  btMultiSphereShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00083 {
00084 
00085        for (int j=0;j<numVectors;j++)
00086        {
00087               btScalar maxDot(btScalar(-1e30));
00088 
00089               const btVector3& vec = vectors[j];
00090 
00091               btVector3 vtx;
00092               btScalar newDot;
00093 
00094               const btVector3* pos = &m_localPositions[0];
00095               const btScalar* rad = &m_radi[0];
00096 
00097               for (int i=0;i<m_numSpheres;i++)
00098               {
00099                      vtx = (*pos) +vec*m_localScaling*(*rad) - vec * getMargin();
00100                      pos++;
00101                      rad++;
00102                      newDot = vec.dot(vtx);
00103                      if (newDot > maxDot)
00104                      {
00105                             maxDot = newDot;
00106                             supportVerticesOut[j] = vtx;
00107                      }
00108               }
00109        }
00110 }
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 void   btMultiSphereShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
00120 {
00121        //as an approximation, take the inertia of the box that bounds the spheres
00122 
00123        btTransform ident;
00124        ident.setIdentity();
00125 //     btVector3 aabbMin,aabbMax;
00126 
00127 //     getAabb(ident,aabbMin,aabbMax);
00128 
00129        btVector3 halfExtents = m_inertiaHalfExtents;//(aabbMax - aabbMin)* btScalar(0.5);
00130 
00131        btScalar margin = CONVEX_DISTANCE_MARGIN;
00132 
00133        btScalar lx=btScalar(2.)*(halfExtents[0]+margin);
00134        btScalar ly=btScalar(2.)*(halfExtents[1]+margin);
00135        btScalar lz=btScalar(2.)*(halfExtents[2]+margin);
00136        const btScalar x2 = lx*lx;
00137        const btScalar y2 = ly*ly;
00138        const btScalar z2 = lz*lz;
00139        const btScalar scaledmass = mass * btScalar(.08333333);
00140 
00141        inertia[0] = scaledmass * (y2+z2);
00142        inertia[1] = scaledmass * (x2+z2);
00143        inertia[2] = scaledmass * (x2+y2);
00144 
00145 }
00146 
00147 
00148