2007-05-27 12:01:53 -04:00
|
|
|
// $Id: physics.hpp 839 2006-10-24 00:01:56Z hiker $
|
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2006 Joerg Henrichs
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
2008-06-12 20:53:52 -04:00
|
|
|
// as published by the Free Software Foundation; either version 3
|
2007-05-27 12:01:53 -04:00
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#ifndef HEADER_PHYSICS_H
|
|
|
|
#define HEADER_PHYSICS_H
|
|
|
|
|
2008-09-07 10:55:05 -04:00
|
|
|
#include <set>
|
|
|
|
#define _WINSOCKAPI_
|
|
|
|
#include <plib/sg.h>
|
2007-05-27 12:01:53 -04:00
|
|
|
#include "kart.hpp"
|
2007-12-08 08:04:56 -05:00
|
|
|
#include "flyable.hpp"
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
#include "btBulletDynamicsCommon.h"
|
2007-11-03 09:13:26 -04:00
|
|
|
#include "bullet/Demos/OpenGL/GLDebugDrawer.h"
|
2008-02-14 22:53:41 -05:00
|
|
|
#include "bullet/Demos/OpenGL/GL_ShapeDrawer.h"
|
|
|
|
|
2007-12-08 08:04:56 -05:00
|
|
|
class Physics : public btSequentialImpulseConstraintSolver
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2007-12-08 08:04:56 -05:00
|
|
|
private:
|
2007-11-08 07:31:54 -05:00
|
|
|
btDynamicsWorld *m_dynamics_world;
|
|
|
|
Kart *m_kart;
|
|
|
|
GLDebugDrawer *m_debug_drawer;
|
2008-02-14 22:53:41 -05:00
|
|
|
GL_ShapeDrawer m_shape_drawer;
|
2007-11-08 07:31:54 -05:00
|
|
|
btCollisionDispatcher *m_dispatcher;
|
|
|
|
btBroadphaseInterface *m_axis_sweep;
|
|
|
|
btDefaultCollisionConfiguration *m_collision_conf;
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2007-12-08 08:04:56 -05:00
|
|
|
// Bullet can report the same collision more than once (up to 4
|
|
|
|
// contact points per collision. Additionally, more than one internal
|
|
|
|
// substep might be taken, resulting in potentially even more
|
|
|
|
// duplicates. To handle this, all collisions (i.e. pair of objects)
|
|
|
|
// are stored in a vector, but only one entry per collision pair
|
|
|
|
// of objects.
|
|
|
|
// While this is a natural application of std::set, the set has some
|
|
|
|
// overhead (since it will likely use a tree to sort the entries).
|
|
|
|
// Considering that the number of collisions is usually rather small
|
2008-01-30 00:30:10 -05:00
|
|
|
// a simple list and linear search is faster is is being used here.
|
2007-12-08 08:04:56 -05:00
|
|
|
|
|
|
|
class CollisionPair {
|
|
|
|
public:
|
2008-01-30 00:30:10 -05:00
|
|
|
const UserPointer* a, *b;
|
|
|
|
|
2007-12-08 08:04:56 -05:00
|
|
|
// The entries in Collision Pairs are sorted: if a projectile
|
|
|
|
// is included, it's always 'a'. If only two karts are reported
|
|
|
|
// the first kart pointer is the smaller one
|
2008-01-30 00:30:10 -05:00
|
|
|
CollisionPair(const UserPointer *a1, const UserPointer *b1) {
|
|
|
|
if(a1->is(UserPointer::UP_KART) &&
|
|
|
|
b1->is(UserPointer::UP_KART) && a1>b1) {
|
|
|
|
a=b1;b=a1;
|
2007-12-08 08:04:56 -05:00
|
|
|
} else {
|
2008-01-30 00:30:10 -05:00
|
|
|
a=a1; b=b1;
|
2007-12-08 08:04:56 -05:00
|
|
|
}
|
|
|
|
}; // CollisionPair
|
|
|
|
bool operator==(const CollisionPair p) {
|
|
|
|
return (p.a==a && p.b==b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class is the list of collision objects, where each collision
|
|
|
|
// pair is stored as most once.
|
|
|
|
class CollisionList : public std::vector<CollisionPair> {
|
2008-01-30 00:30:10 -05:00
|
|
|
private:
|
|
|
|
void push_back(CollisionPair p) {
|
2007-12-08 08:04:56 -05:00
|
|
|
// only add a pair if it's not already in there
|
|
|
|
for(iterator i=begin(); i!=end(); i++) {
|
|
|
|
if((*i)==p) return;
|
|
|
|
}
|
|
|
|
std::vector<CollisionPair>::push_back(p);
|
|
|
|
}; // push_back
|
2008-01-30 00:30:10 -05:00
|
|
|
public:
|
|
|
|
void push_back(const UserPointer* a, const UserPointer*b) {
|
|
|
|
push_back(CollisionPair(a, b));
|
|
|
|
}
|
2007-12-08 08:04:56 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
CollisionList m_all_collisions;
|
|
|
|
void KartKartCollision(Kart *ka, Kart *kb);
|
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
public:
|
2007-11-03 09:13:26 -04:00
|
|
|
Physics (float gravity);
|
|
|
|
~Physics ();
|
|
|
|
void addKart (const Kart *k, btRaycastVehicle *v);
|
|
|
|
void addBody (btRigidBody* b) {m_dynamics_world->addRigidBody(b);}
|
|
|
|
void removeKart (const Kart *k);
|
|
|
|
void removeBody (btRigidBody* b) {m_dynamics_world->removeRigidBody(b);}
|
|
|
|
void update (float dt);
|
|
|
|
void draw ();
|
|
|
|
btDynamicsWorld*
|
|
|
|
getPhysicsWorld () const {return m_dynamics_world;}
|
|
|
|
void debugDraw (float m[16], btCollisionShape *s, const btVector3 color);
|
2007-12-08 08:04:56 -05:00
|
|
|
virtual btScalar solveGroup(btCollisionObject** bodies, int numBodies,
|
|
|
|
btPersistentManifold** manifold,int numManifolds,
|
|
|
|
btTypedConstraint** constraints,int numConstraints,
|
|
|
|
const btContactSolverInfo& info,
|
|
|
|
btIDebugDraw* debugDrawer, btStackAlloc* stackAlloc,
|
|
|
|
btDispatcher* dispatcher);
|
2007-05-27 12:01:53 -04:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
/* EOF */
|
|
|
|
|