Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2009-07-07 13:43:05 +0000
committerStijn Buys <ingar@osirion.org>2009-07-07 13:43:05 +0000
commit7e15b99c01616999496155fe5d2ce89d7608932b (patch)
tree226d5c23b0a931ccbbf490aaf2da647ed6ebe5d4 /src/game/base/physics.h
parent1f71cc5e127f6163e9163afd42453fe145defbeb (diff)
Initial bullet physics support
Diffstat (limited to 'src/game/base/physics.h')
-rw-r--r--src/game/base/physics.h166
1 files changed, 166 insertions, 0 deletions
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 <vector>
+
+#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<CollisionShape *> 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__
+