1 | //
|
---|
2 | // LolPhysics
|
---|
3 | //
|
---|
4 | // Copyright: (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
|
---|
5 | // (c) 2012 Sam Hocevar <sam@hocevar.net>
|
---|
6 | //
|
---|
7 |
|
---|
8 | #if !defined __LOLPHYSICS_H__
|
---|
9 | #define __LOLPHYSICS_H__
|
---|
10 |
|
---|
11 | #ifdef HAVE_PHYS_USE_BULLET
|
---|
12 | #include <bullet/btBulletDynamicsCommon.h>
|
---|
13 | #include <bullet/btBulletCollisionCommon.h>
|
---|
14 | #include "LolBtPhysicsIntegration.h"
|
---|
15 | #include "EasyPhysics.h"
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | namespace lol |
---|
19 | { |
---|
20 | |
---|
21 | namespace phys |
---|
22 | { |
---|
23 | |
---|
24 | class Simulation : public Entity
|
---|
25 | {
|
---|
26 | public:
|
---|
27 | Simulation() :
|
---|
28 | m_broadphase(0),
|
---|
29 | m_collision_configuration(0),
|
---|
30 | m_dispatcher(0),
|
---|
31 | m_solver(0),
|
---|
32 | m_dynamics_world(0),
|
---|
33 | m_timestep(1.f/60.f)
|
---|
34 | {
|
---|
35 | }
|
---|
36 | ~Simulation()
|
---|
37 | {
|
---|
38 | Exit();
|
---|
39 | }
|
---|
40 |
|
---|
41 | char const *GetName() { return "<Simulation>"; }
|
---|
42 |
|
---|
43 | #ifdef HAVE_PHYS_USE_BULLET
|
---|
44 | public:
|
---|
45 | void Init()
|
---|
46 | {
|
---|
47 | // Build the broadphase
|
---|
48 | m_broadphase = new btDbvtBroadphase();
|
---|
49 |
|
---|
50 | // Set up the collision configuration and dispatcher
|
---|
51 | m_collision_configuration = new btDefaultCollisionConfiguration();
|
---|
52 | m_dispatcher = new btCollisionDispatcher(m_collision_configuration);
|
---|
53 |
|
---|
54 | // The actual physics solver
|
---|
55 | m_solver = new btSequentialImpulseConstraintSolver;
|
---|
56 |
|
---|
57 | // The world.
|
---|
58 | m_dynamics_world = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collision_configuration);
|
---|
59 | }
|
---|
60 |
|
---|
61 | virtual void TickGame(float seconds)
|
---|
62 | {
|
---|
63 | Entity::TickGame(seconds);
|
---|
64 |
|
---|
65 | //step the simulation
|
---|
66 | if (m_dynamics_world)
|
---|
67 | {
|
---|
68 | //the "+1" is to have at least one Timestep and to ensure float to int .5f conversion.
|
---|
69 | int steps = (int)(seconds / m_timestep) + 1;
|
---|
70 | m_dynamics_world->stepSimulation(seconds, steps, m_timestep);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | void Exit()
|
---|
75 | {
|
---|
76 | delete m_dynamics_world;
|
---|
77 | delete m_solver;
|
---|
78 | delete m_dispatcher;
|
---|
79 | delete m_collision_configuration;
|
---|
80 | delete m_broadphase;
|
---|
81 | }
|
---|
82 |
|
---|
83 | btDiscreteDynamicsWorld* GetWorld()
|
---|
84 | {
|
---|
85 | return m_dynamics_world;
|
---|
86 | }
|
---|
87 |
|
---|
88 | private:
|
---|
89 | void CustomSetContinuousDetection(bool ShouldUseCCD)
|
---|
90 | {
|
---|
91 | if (m_dynamics_world)
|
---|
92 | m_dynamics_world->getDispatchInfo().m_useContinuous = ShouldUseCCD;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void CustomSetGravity(vec3 &NewGravity)
|
---|
96 | {
|
---|
97 | if (m_dynamics_world)
|
---|
98 | m_dynamics_world->setGravity(LOL2BT_VEC3(NewGravity * LOL2BT_UNIT));
|
---|
99 | }
|
---|
100 |
|
---|
101 | void CustomSetTimestep(float NewTimestep) { }
|
---|
102 |
|
---|
103 | //broadphase
|
---|
104 | btBroadphaseInterface* m_broadphase;
|
---|
105 | // Set up the collision configuration and dispatc
|
---|
106 | btDefaultCollisionConfiguration* m_collision_configuration;
|
---|
107 | btCollisionDispatcher* m_dispatcher;
|
---|
108 | // The actual physics solver
|
---|
109 | btSequentialImpulseConstraintSolver* m_solver;
|
---|
110 | // The world.
|
---|
111 | btDiscreteDynamicsWorld* m_dynamics_world;
|
---|
112 |
|
---|
113 | #else // NO PHYSIC IMPLEMENTATION
|
---|
114 |
|
---|
115 | public:
|
---|
116 | void Init() { }
|
---|
117 | void TickGame(float seconds) { }
|
---|
118 | void Exit() { }
|
---|
119 | private:
|
---|
120 | void CustomSetContinuousDetection(bool ShouldUseCCD) { }
|
---|
121 | void CustomSetGravity(vec3 &NewGravity) { }
|
---|
122 | void CustomSetTimestep(float NewTimestep) { }
|
---|
123 |
|
---|
124 | #endif // PHYSIC IMPLEMENTATION
|
---|
125 |
|
---|
126 | public:
|
---|
127 | //Main logic :
|
---|
128 | //The Set*() functions do the all-lib-independent data storage.
|
---|
129 | //And then it calls the CustomSet*() which are the specialized versions.
|
---|
130 |
|
---|
131 | //Sets the continuous collision detection flag.
|
---|
132 | void SetContinuousDetection(bool ShouldUseCCD)
|
---|
133 | {
|
---|
134 | m_using_CCD = ShouldUseCCD;
|
---|
135 | CustomSetContinuousDetection(ShouldUseCCD);
|
---|
136 | }
|
---|
137 |
|
---|
138 | //Sets the simulation gravity.
|
---|
139 | void SetGravity(vec3 &NewGravity)
|
---|
140 | {
|
---|
141 | m_gravity = NewGravity;
|
---|
142 | CustomSetGravity(NewGravity);
|
---|
143 | }
|
---|
144 |
|
---|
145 | //Sets the simulation fixed timestep.
|
---|
146 | void SetTimestep(float NewTimestep)
|
---|
147 | {
|
---|
148 | if (NewTimestep > .0f)
|
---|
149 | {
|
---|
150 | m_timestep = NewTimestep;
|
---|
151 | CustomSetTimestep(NewTimestep);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | private:
|
---|
156 | friend class EasyPhysic;
|
---|
157 | friend class EasyConstraint;
|
---|
158 |
|
---|
159 | //Adds the given EasyPhysic to the correct list.
|
---|
160 | void AddToDynamic(EasyPhysic* NewEPDynamic) { m_dynamic_list << NewEPDynamic; }
|
---|
161 | void AddToStatic(EasyPhysic* NewEPStatic) { m_static_list << NewEPStatic; }
|
---|
162 | void AddToConstraint(EasyConstraint* NewEC) { m_constraint_list << NewEC; }
|
---|
163 |
|
---|
164 | //Easy Physics body List
|
---|
165 | Array<EasyPhysic*> m_dynamic_list;
|
---|
166 | Array<EasyPhysic*> m_static_list;
|
---|
167 | Array<EasyConstraint*> m_constraint_list;
|
---|
168 |
|
---|
169 | //Easy Physics data storage
|
---|
170 | float m_timestep;
|
---|
171 | bool m_using_CCD;
|
---|
172 | vec3 m_gravity;
|
---|
173 | };
|
---|
174 | |
---|
175 | } /* namespace phys */ |
---|
176 | |
---|
177 | } /* namespace lol */ |
---|
178 |
|
---|
179 | #endif // __LOLPHYSICS_H__
|
---|
180 |
|
---|