Back to index

supertuxkart  0.5+dfsg1
triangle_mesh.cpp
Go to the documentation of this file.
00001 //  $Id: triangle_mesh.cpp 839 2006-10-24 00:01:56Z hiker $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Joerg Henrichs
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty ofati
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include "triangle_mesh.hpp"
00021 #include "world.hpp"
00022 
00023 // -----------------------------------------------------------------------------
00024 TriangleMesh::~TriangleMesh()
00025 {
00026     if(m_body)
00027     {
00028         world->getPhysics()->removeBody(m_body);
00029         delete m_body;
00030         delete m_motion_state;
00031         delete m_collision_shape;
00032     }
00033 }   // ~TriangleMesh
00034 
00035 // -----------------------------------------------------------------------------
00036 void TriangleMesh::addTriangle(btVector3 t1, btVector3 t2, btVector3 t3,
00037                                const Material* m)
00038 {
00039     m_triangleIndex2Material.push_back(m);
00040     m_mesh.addTriangle(t1, t2, t3);
00041 }   // addTriangle
00042 
00043 // -----------------------------------------------------------------------------
00044 void TriangleMesh::createBody(btCollisionObject::CollisionFlags flags)
00045 {
00046     if(m_triangleIndex2Material.size()==0)
00047     {
00048         m_collision_shape = NULL;
00049         m_motion_state    = NULL;
00050         m_body            = NULL;
00051         return;
00052     }
00053     // Now convert the triangle mesh into a static rigid body
00054     m_collision_shape = new btBvhTriangleMeshShape(&m_mesh, true);
00055     btTransform startTransform;
00056     startTransform.setIdentity();
00057     m_motion_state = new btDefaultMotionState(startTransform);
00058     btRigidBody::btRigidBodyConstructionInfo info(0.0f, m_motion_state, m_collision_shape);
00059     m_body=new btRigidBody(info);
00060 
00061     world->getPhysics()->addBody(m_body);
00062     m_user_pointer.set(this);
00063     m_body->setUserPointer(&m_user_pointer);
00064     m_body->setCollisionFlags(m_body->getCollisionFlags()  | 
00065                               flags                        |
00066                               btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
00067 }   // createBody
00068 
00069 // -----------------------------------------------------------------------------