From 7e15b99c01616999496155fe5d2ce89d7608932b Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 7 Jul 2009 13:43:05 +0000 Subject: Initial bullet physics support --- src/game/base/physics.h | 166 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 src/game/base/physics.h (limited to 'src/game/base/physics.h') diff --git a/src/game/base/physics.h b/src/game/base/physics.h new file mode 100644 index 0000000..37ed1cf --- /dev/null +++ b/src/game/base/physics.h @@ -0,0 +1,166 @@ +/* + base/physics.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_BASE_PHYSICS_H__ +#define __INCLUDED_BASE_PHYSICS_H__ + +#include "sys/sys.h" +#include "core/zone.h" +#include + +#ifdef HAVE_BULLET +#include "btBulletDynamicsCommon.h" +#include "BulletCollision/CollisionShapes/btTriangleMesh.h" +#endif + +/* + + This file provides the interface between the osirion game library + and the bullet physics library + +*/ + +namespace game { + +#ifdef HAVE_BULLET +/// helper function to convert math:Vector3f to btVector3 +inline btVector3 to_btVector3(const math::Vector3f & v) +{ + return btVector3(v[0], v[1], v[2]); +} + +/// helper function to conver math::Axis to btMatrix3x3 +inline btMatrix3x3 to_btMatrix3x3(const math::Axis &a) +{ + return btMatrix3x3(a[0][0], a[0][1], a[0][2], + a[1][0], a[1][1], a[1][2], + a[2][0], a[2][1], a[2][2]); +}; +#endif + +/* ---- class CollisionShape --------------------------------------- */ + +/// a bullet collision shape +class CollisionShape { +public: + CollisionShape(model::Model *model, const bool moving); + + ~CollisionShape(); + + inline const std::string &label() const { return shape_label; } + + inline const bool moving() const { return shape_moving; } + +#ifdef HAVE_BULLET + inline btCollisionShape *bullet_shape() { return shape_bulletshape; } +#endif + +/* ----- static functions for the shape registry ------------------- */ + + /// tpye definition for the collision shape registry + typedef std::vector Registry; + + static void clear(); + + static CollisionShape *add(model::Model *model, const bool moving); + + static CollisionShape *find(model::Model *model, const bool moving); + +private: + std::string shape_label; + bool shape_moving; + +#ifdef HAVE_BULLET + btCollisionShape *shape_bulletshape; + btTriangleMesh *shape_mesh; +#endif + + /// collision shape registry + static Registry shape_registry; +}; + +/* ---- class Physics ---------------------------------------------- */ + +/// main physics functions +class Physics { +public: + /// intialize world physics + static void init(); + + /// run a world physics frame + static void frame(const float elapsed); + + /// shutdown world physics + static void shutdown(); + +#ifdef HAVE_BULLET + static btDefaultCollisionConfiguration *configuration; + + static btCollisionDispatcher *dispatcher; + + static btSequentialImpulseConstraintSolver *solver; +#endif +}; + +/* ---- class PhysicsZone ------------------------------------------ */ + +/// a zone containing collision objects +class PhysicsZone : public core::Zone { +public: + PhysicsZone(const std::string &label); + virtual ~PhysicsZone(); + +#ifdef HAVE_BULLET + inline btDiscreteDynamicsWorld *dynamics_world() { return zone_dynamics_world; } + +private: + btAxisSweep3 *zone_cache; + btDiscreteDynamicsWorld *zone_dynamics_world; +#endif +}; + +/* ---- class PhysicsBody ------------------------------------------ */ + +/// an object that is capable of colliding with other objects +class PhysicsBody { +public: + /// initialize a collider attached to an entity + /** + * a PhysicsBody with zero mass is considered non-moving + */ + PhysicsBody(core::Entity *entity); + ~PhysicsBody(); + + /// initialize the collision body + void init_physics(const float mass); + + /// shutdown the collision body + void shutdown_physics(); + + /// collider mass + inline const float mass() const { return collider_mass; } + + /// the entity the collider is attached to + inline core::Entity *entity() { return collider_entity; } + +#ifdef HAVE_BULLET + /// the bullet rigid body associated with this collider + inline btRigidBody *body() { return collider_body; } +#endif + +private: + CollisionShape *collider_shape; + core::Entity *collider_entity; +#ifdef HAVE_BULLET + btRigidBody *collider_body; +#endif + float collider_mass; +}; + +} + +#endif // __INCLUDED_BASE_PHYSICS_H__ + -- cgit v1.2.3