Back to index

supertuxkart  0.5+dfsg1
btTriangleShape.h
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 #ifndef OBB_TRIANGLE_MINKOWSKI_H
00017 #define OBB_TRIANGLE_MINKOWSKI_H
00018 
00019 #include "btConvexShape.h"
00020 #include "btBoxShape.h"
00021 
00022 class btTriangleShape : public btPolyhedralConvexShape
00023 {
00024 
00025 
00026 public:
00027 
00028        btVector3     m_vertices1[3];
00029 
00030 
00031        virtual int getNumVertices() const
00032        {
00033               return 3;
00034        }
00035 
00036        const btVector3& getVertexPtr(int index) const
00037        {
00038               return m_vertices1[index];
00039        }
00040        virtual void getVertex(int index,btVector3& vert) const
00041        {
00042               vert = m_vertices1[index];
00043        }
00044        virtual int   getShapeType() const
00045        {
00046               return TRIANGLE_SHAPE_PROXYTYPE;
00047        }
00048 
00049        virtual int getNumEdges() const
00050        {
00051               return 3;
00052        }
00053        
00054        virtual void getEdge(int i,btPoint3& pa,btPoint3& pb) const
00055        {
00056               getVertex(i,pa);
00057               getVertex((i+1)%3,pb);
00058        }
00059 
00060 
00061        virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax)const 
00062        {
00063 //            btAssert(0);
00064               getAabbSlow(t,aabbMin,aabbMax);
00065        }
00066 
00067        btVector3 localGetSupportingVertexWithoutMargin(const btVector3& dir)const 
00068        {
00069               btVector3 dots(dir.dot(m_vertices1[0]), dir.dot(m_vertices1[1]), dir.dot(m_vertices1[2]));
00070               return m_vertices1[dots.maxAxis()];
00071 
00072        }
00073 
00074        virtual void  batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
00075        {
00076               for (int i=0;i<numVectors;i++)
00077               {
00078                      const btVector3& dir = vectors[i];
00079                      btVector3 dots(dir.dot(m_vertices1[0]), dir.dot(m_vertices1[1]), dir.dot(m_vertices1[2]));
00080                      supportVerticesOut[i] = m_vertices1[dots.maxAxis()];
00081               }
00082 
00083        }
00084 
00085 
00086 
00087        btTriangleShape(const btVector3& p0,const btVector3& p1,const btVector3& p2)
00088        {
00089               m_vertices1[0] = p0;
00090               m_vertices1[1] = p1;
00091               m_vertices1[2] = p2;
00092        }
00093 
00094        
00095 
00096        virtual void getPlane(btVector3& planeNormal,btPoint3& planeSupport,int i) const
00097        {
00098               getPlaneEquation(i,planeNormal,planeSupport);
00099        }
00100 
00101        virtual int   getNumPlanes() const
00102        {
00103               return 1;
00104        }
00105 
00106        void calcNormal(btVector3& normal) const
00107        {
00108               normal = (m_vertices1[1]-m_vertices1[0]).cross(m_vertices1[2]-m_vertices1[0]);
00109               normal.normalize();
00110        }
00111 
00112        virtual void getPlaneEquation(int i, btVector3& planeNormal,btPoint3& planeSupport) const
00113        {
00114               (void)i;
00115               calcNormal(planeNormal);
00116               planeSupport = m_vertices1[0];
00117        }
00118 
00119        virtual void  calculateLocalInertia(btScalar mass,btVector3& inertia) const
00120        {
00121               (void)mass;
00122               btAssert(0);
00123               inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.));
00124        }
00125 
00126               virtual       bool isInside(const btPoint3& pt,btScalar tolerance) const
00127        {
00128               btVector3 normal;
00129               calcNormal(normal);
00130               //distance to plane
00131               btScalar dist = pt.dot(normal);
00132               btScalar planeconst = m_vertices1[0].dot(normal);
00133               dist -= planeconst;
00134               if (dist >= -tolerance && dist <= tolerance)
00135               {
00136                      //inside check on edge-planes
00137                      int i;
00138                      for (i=0;i<3;i++)
00139                      {
00140                             btPoint3 pa,pb;
00141                             getEdge(i,pa,pb);
00142                             btVector3 edge = pb-pa;
00143                             btVector3 edgeNormal = edge.cross(normal);
00144                             edgeNormal.normalize();
00145                             btScalar dist = pt.dot( edgeNormal);
00146                             btScalar edgeConst = pa.dot(edgeNormal);
00147                             dist -= edgeConst;
00148                             if (dist < -tolerance)
00149                                    return false;
00150                      }
00151                      
00152                      return true;
00153               }
00154 
00155               return false;
00156        }
00157               //debugging
00158               virtual const char*  getName()const
00159               {
00160                      return "Triangle";
00161               }
00162 
00163               virtual int          getNumPreferredPenetrationDirections() const
00164               {
00165                      return 2;
00166               }
00167               
00168               virtual void  getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
00169               {
00170                      calcNormal(penetrationVector);
00171                      if (index)
00172                             penetrationVector *= btScalar(-1.);
00173               }
00174 
00175 
00176 };
00177 
00178 #endif //OBB_TRIANGLE_MINKOWSKI_H
00179