Back to index

supertuxkart  0.5+dfsg1
btCapsuleShape.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 
00017 #include "btCapsuleShape.h"
00018 
00019 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
00020 #include "LinearMath/btQuaternion.h"
00021 
00022 btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height)
00023 {
00024        m_upAxis = 1;
00025        m_implicitShapeDimensions.setValue(radius,0.5f*height,radius);
00026 }
00027 
00028  
00029  btVector3    btCapsuleShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
00030 {
00031 
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        btScalar radius = getRadius();
00051 
00052 
00053        {
00054               btVector3 pos(0,0,0);
00055               pos[getUpAxis()] = getHalfHeight();
00056 
00057               vtx = pos +vec*m_localScaling*(radius) - vec * getMargin();
00058               newDot = vec.dot(vtx);
00059               if (newDot > maxDot)
00060               {
00061                      maxDot = newDot;
00062                      supVec = vtx;
00063               }
00064        }
00065        {
00066               btVector3 pos(0,0,0);
00067               pos[getUpAxis()] = -getHalfHeight();
00068 
00069               vtx = pos +vec*m_localScaling*(radius) - vec * getMargin();
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  btCapsuleShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00083 {
00084 
00085        
00086        btScalar radius = getRadius();
00087 
00088        for (int j=0;j<numVectors;j++)
00089        {
00090               btScalar maxDot(btScalar(-1e30));
00091               const btVector3& vec = vectors[j];
00092 
00093               btVector3 vtx;
00094               btScalar newDot;
00095               {
00096                      btVector3 pos(0,0,0);
00097                      pos[getUpAxis()] = getHalfHeight();
00098                      vtx = pos +vec*m_localScaling*(radius) - vec * getMargin();
00099                      newDot = vec.dot(vtx);
00100                      if (newDot > maxDot)
00101                      {
00102                             maxDot = newDot;
00103                             supportVerticesOut[j] = vtx;
00104                      }
00105               }
00106               {
00107                      btVector3 pos(0,0,0);
00108                      pos[getUpAxis()] = -getHalfHeight();
00109                      vtx = pos +vec*m_localScaling*(radius) - vec * getMargin();
00110                      newDot = vec.dot(vtx);
00111                      if (newDot > maxDot)
00112                      {
00113                             maxDot = newDot;
00114                             supportVerticesOut[j] = vtx;
00115                      }
00116               }
00117               
00118        }
00119 }
00120 
00121 
00122 void   btCapsuleShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
00123 {
00124        //as an approximation, take the inertia of the box that bounds the spheres
00125 
00126        btTransform ident;
00127        ident.setIdentity();
00128 
00129        
00130        btScalar radius = getRadius();
00131 
00132        btVector3 halfExtents(radius,radius,radius);
00133        halfExtents[getUpAxis()]+=getHalfHeight();
00134 
00135        btScalar margin = CONVEX_DISTANCE_MARGIN;
00136 
00137        btScalar lx=btScalar(2.)*(halfExtents[0]+margin);
00138        btScalar ly=btScalar(2.)*(halfExtents[1]+margin);
00139        btScalar lz=btScalar(2.)*(halfExtents[2]+margin);
00140        const btScalar x2 = lx*lx;
00141        const btScalar y2 = ly*ly;
00142        const btScalar z2 = lz*lz;
00143        const btScalar scaledmass = mass * btScalar(.08333333);
00144 
00145        inertia[0] = scaledmass * (y2+z2);
00146        inertia[1] = scaledmass * (x2+z2);
00147        inertia[2] = scaledmass * (x2+y2);
00148 
00149 }
00150 
00151 btCapsuleShapeX::btCapsuleShapeX(btScalar radius,btScalar height)
00152 {
00153        m_upAxis = 0;
00154        m_implicitShapeDimensions.setValue(0.5f*height, radius,radius);
00155 }
00156 
00157 
00158 
00159 
00160 
00161 
00162 btCapsuleShapeZ::btCapsuleShapeZ(btScalar radius,btScalar height)
00163 {
00164        m_upAxis = 2;
00165        m_implicitShapeDimensions.setValue(radius,radius,0.5f*height);
00166 }
00167 
00168 
00169 
00170