btCylinderShape.h

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 #ifndef BT_CYLINDER_MINKOWSKI_H
00017 #define BT_CYLINDER_MINKOWSKI_H
00018 
00019 #include "btBoxShape.h"
00020 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" // for the types
00021 #include "LinearMath/btVector3.h"
00022 
00024 ATTRIBUTE_ALIGNED16(class) btCylinderShape : public btConvexInternalShape
00025 
00026 {
00027 
00028 protected:
00029 
00030         int     m_upAxis;
00031 
00032 public:
00033 
00034 BT_DECLARE_ALIGNED_ALLOCATOR();
00035 
00036         btVector3 getHalfExtentsWithMargin() const
00037         {
00038                 btVector3 halfExtents = getHalfExtentsWithoutMargin();
00039                 btVector3 margin(getMargin(),getMargin(),getMargin());
00040                 halfExtents += margin;
00041                 return halfExtents;
00042         }
00043         
00044         const btVector3& getHalfExtentsWithoutMargin() const
00045         {
00046                 return m_implicitShapeDimensions;//changed in Bullet 2.63: assume the scaling and margin are included
00047         }
00048 
00049         btCylinderShape (const btVector3& halfExtents);
00050         
00051         void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const;
00052 
00053         virtual void    calculateLocalInertia(btScalar mass,btVector3& inertia) const;
00054 
00055         virtual btVector3       localGetSupportingVertexWithoutMargin(const btVector3& vec)const;
00056 
00057         virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
00058 
00059         virtual void setMargin(btScalar collisionMargin)
00060         {
00061                 //correct the m_implicitShapeDimensions for the margin
00062                 btVector3 oldMargin(getMargin(),getMargin(),getMargin());
00063                 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
00064                 
00065                 btConvexInternalShape::setMargin(collisionMargin);
00066                 btVector3 newMargin(getMargin(),getMargin(),getMargin());
00067                 m_implicitShapeDimensions = implicitShapeDimensionsWithMargin - newMargin;
00068 
00069         }
00070 
00071         virtual btVector3       localGetSupportingVertex(const btVector3& vec) const
00072         {
00073 
00074                 btVector3 supVertex;
00075                 supVertex = localGetSupportingVertexWithoutMargin(vec);
00076                 
00077                 if ( getMargin()!=btScalar(0.) )
00078                 {
00079                         btVector3 vecnorm = vec;
00080                         if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
00081                         {
00082                                 vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
00083                         } 
00084                         vecnorm.normalize();
00085                         supVertex+= getMargin() * vecnorm;
00086                 }
00087                 return supVertex;
00088         }
00089 
00090 
00091         //use box inertia
00092         //      virtual void    calculateLocalInertia(btScalar mass,btVector3& inertia) const;
00093 
00094 
00095         int     getUpAxis() const
00096         {
00097                 return m_upAxis;
00098         }
00099 
00100         virtual btVector3       getAnisotropicRollingFrictionDirection() const
00101         {
00102                 btVector3 aniDir(0,0,0);
00103                 aniDir[getUpAxis()]=1;
00104                 return aniDir;
00105         }
00106 
00107         virtual btScalar getRadius() const
00108         {
00109                 return getHalfExtentsWithMargin().getX();
00110         }
00111 
00112         virtual void    setLocalScaling(const btVector3& scaling)
00113         {
00114                 btVector3 oldMargin(getMargin(),getMargin(),getMargin());
00115                 btVector3 implicitShapeDimensionsWithMargin = m_implicitShapeDimensions+oldMargin;
00116                 btVector3 unScaledImplicitShapeDimensionsWithMargin = implicitShapeDimensionsWithMargin / m_localScaling;
00117 
00118                 btConvexInternalShape::setLocalScaling(scaling);
00119 
00120                 m_implicitShapeDimensions = (unScaledImplicitShapeDimensionsWithMargin * m_localScaling) - oldMargin;
00121 
00122         }
00123 
00124         //debugging
00125         virtual const char*     getName()const
00126         {
00127                 return "CylinderY";
00128         }
00129 
00130         virtual int     calculateSerializeBufferSize() const;
00131 
00133         virtual const char*     serialize(void* dataBuffer, btSerializer* serializer) const;
00134 
00135 };
00136 
00137 class btCylinderShapeX : public btCylinderShape
00138 {
00139 public:
00140         BT_DECLARE_ALIGNED_ALLOCATOR();
00141         
00142         btCylinderShapeX (const btVector3& halfExtents);
00143 
00144         virtual btVector3       localGetSupportingVertexWithoutMargin(const btVector3& vec)const;
00145         virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
00146         
00147                 //debugging
00148         virtual const char*     getName()const
00149         {
00150                 return "CylinderX";
00151         }
00152 
00153         virtual btScalar getRadius() const
00154         {
00155                 return getHalfExtentsWithMargin().getY();
00156         }
00157 
00158 };
00159 
00160 class btCylinderShapeZ : public btCylinderShape
00161 {
00162 public:
00163         BT_DECLARE_ALIGNED_ALLOCATOR();
00164         
00165         btCylinderShapeZ (const btVector3& halfExtents);
00166 
00167         virtual btVector3       localGetSupportingVertexWithoutMargin(const btVector3& vec)const;
00168         virtual void    batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const;
00169 
00170                 //debugging
00171         virtual const char*     getName()const
00172         {
00173                 return "CylinderZ";
00174         }
00175 
00176         virtual btScalar getRadius() const
00177         {
00178                 return getHalfExtentsWithMargin().getX();
00179         }
00180 
00181 };
00182 
00184 struct  btCylinderShapeData
00185 {
00186         btConvexInternalShapeData       m_convexInternalShapeData;
00187 
00188         int     m_upAxis;
00189 
00190         char    m_padding[4];
00191 };
00192 
00193 SIMD_FORCE_INLINE       int     btCylinderShape::calculateSerializeBufferSize() const
00194 {
00195         return sizeof(btCylinderShapeData);
00196 }
00197 
00199 SIMD_FORCE_INLINE       const char*     btCylinderShape::serialize(void* dataBuffer, btSerializer* serializer) const
00200 {
00201         btCylinderShapeData* shapeData = (btCylinderShapeData*) dataBuffer;
00202         
00203         btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData,serializer);
00204 
00205         shapeData->m_upAxis = m_upAxis;
00206         
00207         return "btCylinderShapeData";
00208 }
00209 
00210 
00211 
00212 #endif //BT_CYLINDER_MINKOWSKI_H
00213