Blender  V2.59
btPolyhedralConvexShape.cpp
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2009 Erwin Coumans  http://bulletphysics.org
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() :btConvexInternalShape()
00019 {
00020 
00021 }
00022 
00023 
00024 btVector3       btPolyhedralConvexShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
00025 {
00026 
00027 
00028         btVector3 supVec(0,0,0);
00029 #ifndef __SPU__
00030         int i;
00031         btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
00032 
00033         btVector3 vec = vec0;
00034         btScalar lenSqr = vec.length2();
00035         if (lenSqr < btScalar(0.0001))
00036         {
00037                 vec.setValue(1,0,0);
00038         } else
00039         {
00040                 btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
00041                 vec *= rlen;
00042         }
00043 
00044         btVector3 vtx;
00045         btScalar newDot;
00046 
00047         for (i=0;i<getNumVertices();i++)
00048         {
00049                 getVertex(i,vtx);
00050                 newDot = vec.dot(vtx);
00051                 if (newDot > maxDot)
00052                 {
00053                         maxDot = newDot;
00054                         supVec = vtx;
00055                 }
00056         }
00057 
00058         
00059 #endif //__SPU__
00060         return supVec;
00061 }
00062 
00063 
00064 
00065 void    btPolyhedralConvexShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00066 {
00067 #ifndef __SPU__
00068         int i;
00069 
00070         btVector3 vtx;
00071         btScalar newDot;
00072 
00073         for (i=0;i<numVectors;i++)
00074         {
00075                 supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
00076         }
00077 
00078         for (int j=0;j<numVectors;j++)
00079         {
00080         
00081                 const btVector3& vec = vectors[j];
00082 
00083                 for (i=0;i<getNumVertices();i++)
00084                 {
00085                         getVertex(i,vtx);
00086                         newDot = vec.dot(vtx);
00087                         if (newDot > supportVerticesOut[j][3])
00088                         {
00089                                 //WARNING: don't swap next lines, the w component would get overwritten!
00090                                 supportVerticesOut[j] = vtx;
00091                                 supportVerticesOut[j][3] = newDot;
00092                         }
00093                 }
00094         }
00095 #endif //__SPU__
00096 }
00097 
00098 
00099 
00100 void    btPolyhedralConvexShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
00101 {
00102 #ifndef __SPU__
00103         //not yet, return box inertia
00104 
00105         btScalar margin = getMargin();
00106 
00107         btTransform ident;
00108         ident.setIdentity();
00109         btVector3 aabbMin,aabbMax;
00110         getAabb(ident,aabbMin,aabbMax);
00111         btVector3 halfExtents = (aabbMax-aabbMin)*btScalar(0.5);
00112 
00113         btScalar lx=btScalar(2.)*(halfExtents.x()+margin);
00114         btScalar ly=btScalar(2.)*(halfExtents.y()+margin);
00115         btScalar lz=btScalar(2.)*(halfExtents.z()+margin);
00116         const btScalar x2 = lx*lx;
00117         const btScalar y2 = ly*ly;
00118         const btScalar z2 = lz*lz;
00119         const btScalar scaledmass = mass * btScalar(0.08333333);
00120 
00121         inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
00122 #endif //__SPU__
00123 }
00124 
00125 
00126 
00127 void    btPolyhedralConvexAabbCachingShape::setLocalScaling(const btVector3& scaling)
00128 {
00129         btConvexInternalShape::setLocalScaling(scaling);
00130         recalcLocalAabb();
00131 }
00132 
00133 btPolyhedralConvexAabbCachingShape::btPolyhedralConvexAabbCachingShape()
00134 :btPolyhedralConvexShape(),
00135 m_localAabbMin(1,1,1),
00136 m_localAabbMax(-1,-1,-1),
00137 m_isLocalAabbValid(false)
00138 {
00139 }
00140 
00141 void btPolyhedralConvexAabbCachingShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
00142 {
00143         getNonvirtualAabb(trans,aabbMin,aabbMax,getMargin());
00144 }
00145 
00146 void    btPolyhedralConvexAabbCachingShape::recalcLocalAabb()
00147 {
00148         m_isLocalAabbValid = true;
00149         
00150         #if 1
00151         static const btVector3 _directions[] =
00152         {
00153                 btVector3( 1.,  0.,  0.),
00154                 btVector3( 0.,  1.,  0.),
00155                 btVector3( 0.,  0.,  1.),
00156                 btVector3( -1., 0.,  0.),
00157                 btVector3( 0., -1.,  0.),
00158                 btVector3( 0.,  0., -1.)
00159         };
00160         
00161         btVector3 _supporting[] =
00162         {
00163                 btVector3( 0., 0., 0.),
00164                 btVector3( 0., 0., 0.),
00165                 btVector3( 0., 0., 0.),
00166                 btVector3( 0., 0., 0.),
00167                 btVector3( 0., 0., 0.),
00168                 btVector3( 0., 0., 0.)
00169         };
00170         
00171         batchedUnitVectorGetSupportingVertexWithoutMargin(_directions, _supporting, 6);
00172         
00173         for ( int i = 0; i < 3; ++i )
00174         {
00175                 m_localAabbMax[i] = _supporting[i][i] + m_collisionMargin;
00176                 m_localAabbMin[i] = _supporting[i + 3][i] - m_collisionMargin;
00177         }
00178         
00179         #else
00180 
00181         for (int i=0;i<3;i++)
00182         {
00183                 btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.));
00184                 vec[i] = btScalar(1.);
00185                 btVector3 tmp = localGetSupportingVertex(vec);
00186                 m_localAabbMax[i] = tmp[i]+m_collisionMargin;
00187                 vec[i] = btScalar(-1.);
00188                 tmp = localGetSupportingVertex(vec);
00189                 m_localAabbMin[i] = tmp[i]-m_collisionMargin;
00190         }
00191         #endif
00192 }
00193