Back to index

supertuxkart  0.5+dfsg1
physics.hpp
Go to the documentation of this file.
00001 //  $Id: physics.hpp 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 of
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 #ifndef HEADER_PHYSICS_H
00021 #define HEADER_PHYSICS_H
00022 
00023 #include "kart.hpp"
00024 #include "flyable.hpp"
00025 #include <plib/sg.h>
00026 #include <set>
00027 
00028 #include "btBulletDynamicsCommon.h"
00029 #include "bullet/Demos/OpenGL/GLDebugDrawer.h"
00030 #include "bullet/Demos/OpenGL/GL_ShapeDrawer.h"
00031 
00032 class Physics : public btSequentialImpulseConstraintSolver
00033 {
00034 private:
00035     btDynamicsWorld                 *m_dynamics_world;
00036     Kart                            *m_kart;
00037     GLDebugDrawer                   *m_debug_drawer;
00038     GL_ShapeDrawer                   m_shape_drawer;
00039     btCollisionDispatcher           *m_dispatcher;
00040     btBroadphaseInterface           *m_axis_sweep;
00041     btDefaultCollisionConfiguration *m_collision_conf;
00042 
00043     // Bullet can report the same collision more than once (up to 4 
00044     // contact points per collision. Additionally, more than one internal
00045     // substep might be taken, resulting in potentially even more 
00046     // duplicates. To handle this, all collisions (i.e. pair of objects)
00047     // are stored in a vector, but only one entry per collision pair
00048     // of objects.
00049     // While this is a natural application of std::set, the set has some
00050     // overhead (since it will likely use a tree to sort the entries).
00051     // Considering that the number of collisions is usually rather small
00052     // a simple list and linear search is faster is is being used here.
00053 
00054     class CollisionPair {
00055     public:
00056         const UserPointer*    a, *b;
00057        
00058         // The entries in Collision Pairs are sorted: if a projectile
00059         // is included, it's always 'a'. If only two karts are reported
00060         // the first kart pointer is the smaller one
00061         CollisionPair(const UserPointer *a1, const UserPointer *b1) {
00062             if(a1->is(UserPointer::UP_KART) && 
00063                b1->is(UserPointer::UP_KART) && a1>b1) {
00064                a=b1;b=a1;
00065            } else {
00066                a=a1; b=b1; 
00067            }
00068         };  //    CollisionPair
00069         bool operator==(const CollisionPair p) {
00070             return (p.a==a && p.b==b);
00071         }
00072     };
00073 
00074     // This class is the list of collision objects, where each collision
00075     // pair is stored as most once.
00076     class CollisionList : public std::vector<CollisionPair> {
00077     private:
00078         void push_back(CollisionPair p) {
00079             // only add a pair if it's not already in there
00080             for(iterator i=begin(); i!=end(); i++) {
00081                 if((*i)==p) return;
00082             }
00083             std::vector<CollisionPair>::push_back(p);
00084         };   // push_back
00085     public:
00086         void push_back(const UserPointer* a, const UserPointer*b) {
00087             push_back(CollisionPair(a, b));
00088         }
00089     };
00090 
00091     CollisionList m_all_collisions;
00092     void  KartKartCollision(Kart *ka, Kart *kb);
00093 
00094 public:
00095           Physics         (float gravity);
00096          ~Physics         ();
00097     void  addKart         (const Kart *k, btRaycastVehicle *v);
00098     void  addBody         (btRigidBody* b) {m_dynamics_world->addRigidBody(b);}
00099     void  removeKart      (const Kart *k);
00100     void  removeBody      (btRigidBody* b) {m_dynamics_world->removeRigidBody(b);}
00101     void  update          (float dt);
00102     void  draw            ();
00103     btDynamicsWorld*
00104           getPhysicsWorld () const {return m_dynamics_world;}
00105     void  debugDraw       (float m[16], btCollisionShape *s, const btVector3 color);
00106     virtual btScalar solveGroup(btCollisionObject** bodies, int numBodies,
00107                                 btPersistentManifold** manifold,int numManifolds,
00108                                 btTypedConstraint** constraints,int numConstraints,
00109                                 const btContactSolverInfo& info, 
00110                                 btIDebugDraw* debugDrawer, btStackAlloc* stackAlloc,
00111                                 btDispatcher* dispatcher);
00112 };
00113 #endif
00114 /* EOF */
00115