Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 3bbd7490c488cceb495f415eded33485b1f53064 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
   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__