btCylinderShape.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 "btCylinderShape.h"
00017 
00018 btCylinderShape::btCylinderShape (const btVector3& halfExtents)
00019 :btConvexInternalShape(),
00020 m_upAxis(1)
00021 {
00022         btVector3 margin(getMargin(),getMargin(),getMargin());
00023         m_implicitShapeDimensions = (halfExtents * m_localScaling) - margin;
00024         m_shapeType = CYLINDER_SHAPE_PROXYTYPE;
00025 }
00026 
00027 
00028 btCylinderShapeX::btCylinderShapeX (const btVector3& halfExtents)
00029 :btCylinderShape(halfExtents)
00030 {
00031         m_upAxis = 0;
00032 
00033 }
00034 
00035 
00036 btCylinderShapeZ::btCylinderShapeZ (const btVector3& halfExtents)
00037 :btCylinderShape(halfExtents)
00038 {
00039         m_upAxis = 2;
00040 
00041 }
00042 
00043 void btCylinderShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const
00044 {
00045         btTransformAabb(getHalfExtentsWithoutMargin(),getMargin(),t,aabbMin,aabbMax);
00046 }
00047 
00048 void    btCylinderShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
00049 {
00050         //approximation of box shape, todo: implement cylinder shape inertia before people notice ;-)
00051         btVector3 halfExtents = getHalfExtentsWithMargin();
00052 
00053         btScalar lx=btScalar(2.)*(halfExtents.x());
00054         btScalar ly=btScalar(2.)*(halfExtents.y());
00055         btScalar lz=btScalar(2.)*(halfExtents.z());
00056 
00057         inertia.setValue(mass/(btScalar(12.0)) * (ly*ly + lz*lz),
00058                                         mass/(btScalar(12.0)) * (lx*lx + lz*lz),
00059                                         mass/(btScalar(12.0)) * (lx*lx + ly*ly));
00060 
00061 }
00062 
00063 
00064 SIMD_FORCE_INLINE btVector3 CylinderLocalSupportX(const btVector3& halfExtents,const btVector3& v) 
00065 {
00066 const int cylinderUpAxis = 0;
00067 const int XX = 1;
00068 const int YY = 0;
00069 const int ZZ = 2;
00070 
00071         //mapping depends on how cylinder local orientation is
00072         // extents of the cylinder is: X,Y is for radius, and Z for height
00073 
00074 
00075         btScalar radius = halfExtents[XX];
00076         btScalar halfHeight = halfExtents[cylinderUpAxis];
00077 
00078 
00079     btVector3 tmp;
00080         btScalar d ;
00081 
00082     btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]);
00083     if (s != btScalar(0.0))
00084         {
00085         d = radius / s;  
00086                 tmp[XX] = v[XX] * d;
00087                 tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
00088                 tmp[ZZ] = v[ZZ] * d;
00089                 return tmp;
00090         }
00091     else
00092         {
00093             tmp[XX] = radius;
00094                 tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
00095                 tmp[ZZ] = btScalar(0.0);
00096                 return tmp;
00097     }
00098 
00099 
00100 }
00101 
00102 
00103 
00104 
00105 
00106 
00107 inline  btVector3 CylinderLocalSupportY(const btVector3& halfExtents,const btVector3& v) 
00108 {
00109 
00110 const int cylinderUpAxis = 1;
00111 const int XX = 0;
00112 const int YY = 1;
00113 const int ZZ = 2;
00114 
00115 
00116         btScalar radius = halfExtents[XX];
00117         btScalar halfHeight = halfExtents[cylinderUpAxis];
00118 
00119 
00120     btVector3 tmp;
00121         btScalar d ;
00122 
00123     btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]);
00124     if (s != btScalar(0.0))
00125         {
00126         d = radius / s;  
00127                 tmp[XX] = v[XX] * d;
00128                 tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
00129                 tmp[ZZ] = v[ZZ] * d;
00130                 return tmp;
00131         }
00132     else
00133         {
00134             tmp[XX] = radius;
00135                 tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
00136                 tmp[ZZ] = btScalar(0.0);
00137                 return tmp;
00138     }
00139 
00140 }
00141 
00142 inline btVector3 CylinderLocalSupportZ(const btVector3& halfExtents,const btVector3& v) 
00143 {
00144 const int cylinderUpAxis = 2;
00145 const int XX = 0;
00146 const int YY = 2;
00147 const int ZZ = 1;
00148 
00149         //mapping depends on how cylinder local orientation is
00150         // extents of the cylinder is: X,Y is for radius, and Z for height
00151 
00152 
00153         btScalar radius = halfExtents[XX];
00154         btScalar halfHeight = halfExtents[cylinderUpAxis];
00155 
00156 
00157     btVector3 tmp;
00158         btScalar d ;
00159 
00160     btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]);
00161     if (s != btScalar(0.0))
00162         {
00163         d = radius / s;  
00164                 tmp[XX] = v[XX] * d;
00165                 tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
00166                 tmp[ZZ] = v[ZZ] * d;
00167                 return tmp;
00168         }
00169     else
00170         {
00171             tmp[XX] = radius;
00172                 tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
00173                 tmp[ZZ] = btScalar(0.0);
00174                 return tmp;
00175     }
00176 
00177 
00178 }
00179 
00180 btVector3       btCylinderShapeX::localGetSupportingVertexWithoutMargin(const btVector3& vec)const
00181 {
00182         return CylinderLocalSupportX(getHalfExtentsWithoutMargin(),vec);
00183 }
00184 
00185 
00186 btVector3       btCylinderShapeZ::localGetSupportingVertexWithoutMargin(const btVector3& vec)const
00187 {
00188         return CylinderLocalSupportZ(getHalfExtentsWithoutMargin(),vec);
00189 }
00190 btVector3       btCylinderShape::localGetSupportingVertexWithoutMargin(const btVector3& vec)const
00191 {
00192         return CylinderLocalSupportY(getHalfExtentsWithoutMargin(),vec);
00193 }
00194 
00195 void    btCylinderShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00196 {
00197         for (int i=0;i<numVectors;i++)
00198         {
00199                 supportVerticesOut[i] = CylinderLocalSupportY(getHalfExtentsWithoutMargin(),vectors[i]);
00200         }
00201 }
00202 
00203 void    btCylinderShapeZ::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00204 {
00205         for (int i=0;i<numVectors;i++)
00206         {
00207                 supportVerticesOut[i] = CylinderLocalSupportZ(getHalfExtentsWithoutMargin(),vectors[i]);
00208         }
00209 }
00210 
00211 
00212 
00213 
00214 void    btCylinderShapeX::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00215 {
00216         for (int i=0;i<numVectors;i++)
00217         {
00218                 supportVerticesOut[i] = CylinderLocalSupportX(getHalfExtentsWithoutMargin(),vectors[i]);
00219         }
00220 }
00221 
00222 

Generated on Mon Feb 15 22:17:03 2010 for Bullet Collision Detection & Physics Library by  doxygen 1.6.1