SuperTuxKart
physics.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2006-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_PHYSICS_HPP
20 #define HEADER_PHYSICS_HPP
21 
27 #include <set>
28 #include <vector>
29 
30 #include "btBulletDynamicsCommon.h"
31 
32 #include "physics/irr_debug_drawer.hpp"
33 #include "physics/stk_dynamics_world.hpp"
34 #include "physics/user_pointer.hpp"
35 
36 class AbstractKart;
37 class STKDynamicsWorld;
38 class Vec3;
39 
43 class Physics : public btSequentialImpulseConstraintSolver
44 {
45 private:
57  {
58  private:
60  const UserPointer *m_up[2];
61 
64  public:
68  CollisionPair(const UserPointer *a, const btVector3 &contact_point_a,
69  const UserPointer *b, const btVector3 &contact_point_b)
70  {
71  if(a->is(UserPointer::UP_KART) &&
72  b->is(UserPointer::UP_KART) && a>b) {
73  m_up[0]=b; m_contact_point[0] = contact_point_b;
74  m_up[1]=a; m_contact_point[1] = contact_point_a;
75  } else {
76  m_up[0]=a; m_contact_point[0] = contact_point_a;
77  m_up[1]=b; m_contact_point[1] = contact_point_b;
78  }
79  }; // CollisionPair
80  // --------------------------------------------------------------------
84  bool operator==(const CollisionPair &p)
85  {
86  return (p.m_up[0]==m_up[0] && p.m_up[1]==m_up[1]);
87  } // operator==
88  // --------------------------------------------------------------------
89  const UserPointer *getUserPointer(unsigned int n) const
90  {
91  assert(n<=1);
92  return m_up[n];
93  } // getUserPointer
94  // --------------------------------------------------------------------
97  const Vec3 &getContactPointCS(unsigned int n) const
98  {
99  assert(n>=0 && n<=1);
100  return m_contact_point[n];
101  } // getContactPointCS
102  }; // CollisionPair
103 
104  // ========================================================================
105  // This class is the list of collision objects, where each collision
106  // pair is stored as most once.
107  class CollisionList : public std::vector<CollisionPair>
108  {
109  private:
110  void push_back(CollisionPair p) {
111  // only add a pair if it's not already in there
112  for(iterator i=begin(); i!=end(); i++) {
113  if((*i)==p) return;
114  }
115  std::vector<CollisionPair>::push_back(p);
116  }; // push_back
117  public:
119  void push_back(const UserPointer *a, const btVector3 &contact_point_a,
120  const UserPointer *b, const btVector3 &contact_point_b)
121  {
122  push_back(CollisionPair(a, contact_point_a, b, contact_point_b));
123  }
124  }; // CollisionList
125  // ========================================================================
126 
132 
137  std::vector<const AbstractKart*> m_karts_to_delete;
138 
141 
144 
145  btCollisionDispatcher *m_dispatcher;
146  btBroadphaseInterface *m_axis_sweep;
147  btDefaultCollisionConfiguration *m_collision_conf;
148  CollisionList m_all_collisions;
149 
150  Physics();
151  virtual ~Physics();
152 
153 public:
154  // ----------------------------------------------------------------------------------------
155  static Physics* get();
156  // ----------------------------------------------------------------------------------------
157  static void create();
158  // ----------------------------------------------------------------------------------------
159  static void destroy();
160  // ----------------------------------------------------------------------------------------
161  void init (const Vec3 &min_world, const Vec3 &max_world);
162  void addKart (const AbstractKart *k);
163  void addBody (btRigidBody* b) {m_dynamics_world->addRigidBody(b);}
164  void removeKart (const AbstractKart *k);
165  void removeBody (btRigidBody* b) {m_dynamics_world->removeRigidBody(b);}
166  void KartKartCollision(AbstractKart *ka, const Vec3 &contact_point_a,
167  AbstractKart *kb, const Vec3 &contact_point_b);
168  void update (int ticks);
169  void draw ();
171  getPhysicsWorld () const {return m_dynamics_world;}
175  void setDebugMode(IrrDebugDrawer::DebugModeType mode) { m_debug_drawer->setDebugMode(mode); }
177  bool isDebug() const {return m_debug_drawer->debugEnabled(); }
178  IrrDebugDrawer* getDebugDrawer() { return m_debug_drawer; }
179  virtual btScalar solveGroup(btCollisionObject** bodies, int numBodies,
180  btPersistentManifold** manifold,int numManifolds,
181  btTypedConstraint** constraints,int numConstraints,
182  const btContactSolverInfo& info,
183  btIDebugDraw* debugDrawer, btStackAlloc* stackAlloc,
184  btDispatcher* dispatcher);
185 };
186 
187 #endif // HEADER_PHYSICS_HPP
IrrDebugDrawer::nextDebugMode
void nextDebugMode()
Activates the next debug mode, or switches the mode off again.
Definition: irr_debug_drawer.cpp:37
Physics::nextDebugMode
void nextDebugMode()
Activates the next debug mode (or switches it off again).
Definition: physics.hpp:174
Physics::CollisionList
Definition: physics.hpp:108
Physics::CollisionPair::CollisionPair
CollisionPair(const UserPointer *a, const btVector3 &contact_point_a, const UserPointer *b, const btVector3 &contact_point_b)
The entries in Collision Pairs are sorted: if a projectile is included, it's always 'a'.
Definition: physics.hpp:68
AbstractKart
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:62
IrrDebugDrawer
Definition: irr_debug_drawer.hpp:33
Physics::update
void update(int ticks)
Updates the physics simulation and handles all collisions.
Definition: physics.cpp:177
Physics::CollisionPair::m_up
const UserPointer * m_up[2]
The user pointer of the objects involved in this collision.
Definition: physics.hpp:60
Physics
Definition: physics.hpp:44
Physics::draw
void draw()
A debug draw function to show the track and all karts.
Definition: physics.cpp:765
UserPointer
A UserPointer is stored as a user pointer in all bullet bodies.
Definition: user_pointer.hpp:36
Physics::removeKart
void removeKart(const AbstractKart *k)
Removes a kart from the physics engine.
Definition: physics.cpp:150
Physics::addKart
void addKart(const AbstractKart *k)
Adds a kart to the physics engine.
Definition: physics.cpp:132
Physics::Physics
Physics()
Initialise physics.
Definition: physics.cpp:76
IrrDebugDrawer::DebugModeType
DebugModeType
The drawing mode to use: If bit 0 is set, draw the bullet collision shape of karts If bit 1 is set,...
Definition: irr_debug_drawer.hpp:39
Vec3
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35
Physics::CollisionPair::getContactPointCS
const Vec3 & getContactPointCS(unsigned int n) const
Returns the contact point of the collision in car (local) coordinates.
Definition: physics.hpp:97
Physics::KartKartCollision
void KartKartCollision(AbstractKart *ka, const Vec3 &contact_point_a, AbstractKart *kb, const Vec3 &contact_point_b)
Handles the special case of two karts colliding with each other, which means that bombs must be passe...
Definition: physics.cpp:436
Physics::CollisionPair
Bullet can report the same collision more than once (up to 4 contact points per collision.
Definition: physics.hpp:57
Physics::isDebug
bool isDebug() const
Returns true if the debug drawer is enabled.
Definition: physics.hpp:177
STKDynamicsWorld
A thin wrapper around bullet's btDiscreteDynamicsWorld.
Definition: stk_dynamics_world.hpp:29
Physics::CollisionList::push_back
void push_back(const UserPointer *a, const btVector3 &contact_point_a, const UserPointer *b, const btVector3 &contact_point_b)
Adds information about a collision to this vector.
Definition: physics.hpp:119
Physics::m_physics_loop_active
bool m_physics_loop_active
This flag is set while bullets time step processing is taking place.
Definition: physics.hpp:131
Physics::solveGroup
virtual btScalar solveGroup(btCollisionObject **bodies, int numBodies, btPersistentManifold **manifold, int numManifolds, btTypedConstraint **constraints, int numConstraints, const btContactSolverInfo &info, btIDebugDraw *debugDrawer, btStackAlloc *stackAlloc, btDispatcher *dispatcher)
This function is called at each internal bullet timestep.
Definition: physics.cpp:570
Physics::init
void init(const Vec3 &min_world, const Vec3 &max_world)
The actual initialisation of the physics, which is called after the track model is loaded.
Definition: physics.cpp:87
Physics::m_debug_drawer
IrrDebugDrawer * m_debug_drawer
Used in physics debugging to draw the physics world.
Definition: physics.hpp:143
IrrDebugDrawer::debugEnabled
bool debugEnabled() const
Returns true if debug mode is enabled.
Definition: irr_debug_drawer.hpp:73
Physics::CollisionPair::m_contact_point
Vec3 m_contact_point[2]
The contact point for each object (in local coordincates).
Definition: physics.hpp:63
Physics::m_dynamics_world
STKDynamicsWorld * m_dynamics_world
Pointer to the physics dynamics world.
Definition: physics.hpp:140
Physics::CollisionPair::operator==
bool operator==(const CollisionPair &p)
Tests if two collision pairs involve the same objects.
Definition: physics.hpp:84
Physics::m_karts_to_delete
std::vector< const AbstractKart * > m_karts_to_delete
If kart need to be removed from the physics world while physics processing is taking place,...
Definition: physics.hpp:137