1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net> |
---|
5 | // (c) 2009-2012 Cédric Lecacheur <jordx@free.fr> |
---|
6 | // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com> |
---|
7 | // This program is free software; you can redistribute it and/or |
---|
8 | // modify it under the terms of the Do What The Fuck You Want To |
---|
9 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
11 | // |
---|
12 | |
---|
13 | #if defined HAVE_CONFIG_H |
---|
14 | # include "config.h" |
---|
15 | #endif |
---|
16 | |
---|
17 | #include "LolBtPhysicsIntegration.h" |
---|
18 | #include "LolPhysics.h" |
---|
19 | |
---|
20 | namespace lol |
---|
21 | { |
---|
22 | |
---|
23 | namespace phys |
---|
24 | { |
---|
25 | |
---|
26 | #ifdef HAVE_PHYS_USE_BULLET |
---|
27 | |
---|
28 | //------------------------------------------------------------------------- |
---|
29 | //EASY_PHYSIC |
---|
30 | //-- |
---|
31 | |
---|
32 | EasyPhysic::EasyPhysic() : |
---|
33 | m_collision_object(NULL), |
---|
34 | m_rigid_body(NULL), |
---|
35 | m_ghost_object(NULL), |
---|
36 | m_collision_shape(NULL), |
---|
37 | m_motion_state(NULL), |
---|
38 | m_mass(.0f), |
---|
39 | m_local_inertia(btVector3(.0f, .0f, .0f)), |
---|
40 | m_collision_group(1), |
---|
41 | m_collision_mask(1) |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | EasyPhysic::~EasyPhysic() |
---|
46 | { |
---|
47 | m_rigid_body = NULL; |
---|
48 | delete m_collision_object; |
---|
49 | delete m_collision_shape; |
---|
50 | delete m_motion_state; |
---|
51 | } |
---|
52 | |
---|
53 | //------------------------------------------------------------------------- |
---|
54 | //Set Shape functions |
---|
55 | //-- |
---|
56 | |
---|
57 | void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape) |
---|
58 | { |
---|
59 | bool bReinitToRigidBody = false; |
---|
60 | if (m_rigid_body) |
---|
61 | { |
---|
62 | bReinitToRigidBody = true; |
---|
63 | delete m_rigid_body; |
---|
64 | } |
---|
65 | if (m_collision_shape) |
---|
66 | delete m_collision_shape; |
---|
67 | |
---|
68 | m_collision_shape = collision_shape; |
---|
69 | |
---|
70 | if (bReinitToRigidBody) |
---|
71 | InitBodyToRigid(); |
---|
72 | } |
---|
73 | |
---|
74 | //Box Shape support |
---|
75 | void EasyPhysic::SetShapeToBox(lol::vec3& box_size) |
---|
76 | { |
---|
77 | vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE; |
---|
78 | m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size)); |
---|
79 | SetShapeTo(m_convex_shape); |
---|
80 | } |
---|
81 | |
---|
82 | void EasyPhysic::SetShapeToSphere(float radius) |
---|
83 | { |
---|
84 | m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE); |
---|
85 | SetShapeTo(m_convex_shape); |
---|
86 | } |
---|
87 | |
---|
88 | void EasyPhysic::SetShapeToCone(float radius, float height) |
---|
89 | { |
---|
90 | m_convex_shape = new btConeShape( radius * LOL2BT_UNIT, |
---|
91 | height * LOL2BT_UNIT); |
---|
92 | SetShapeTo(m_convex_shape); |
---|
93 | } |
---|
94 | |
---|
95 | void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size) |
---|
96 | { |
---|
97 | vec3 new_cyl_size = cyl_size * LOL2BT_UNIT; |
---|
98 | new_cyl_size.y *= LOL2BT_SIZE; |
---|
99 | m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size)); |
---|
100 | SetShapeTo(m_convex_shape); |
---|
101 | } |
---|
102 | |
---|
103 | void EasyPhysic::SetShapeToCapsule(float radius, float height) |
---|
104 | { |
---|
105 | m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE, |
---|
106 | height * LOL2BT_UNIT * LOL2BT_SIZE); |
---|
107 | SetShapeTo(m_convex_shape); |
---|
108 | } |
---|
109 | |
---|
110 | //------------------------------------------------------------------------- |
---|
111 | //Base Location/Rotation setup |
---|
112 | //-- |
---|
113 | void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation) |
---|
114 | { |
---|
115 | m_local_to_world = lol::mat4::translate(base_location) * mat4(base_rotation); |
---|
116 | |
---|
117 | if (m_ghost_object) |
---|
118 | m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT))); |
---|
119 | else |
---|
120 | { |
---|
121 | if (m_motion_state) |
---|
122 | m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT))); |
---|
123 | else |
---|
124 | m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT))); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | //------------------------------------------------------------------------- |
---|
129 | //Mass related functions |
---|
130 | //-- |
---|
131 | //Set Shape functions |
---|
132 | void EasyPhysic::SetMass(float mass) |
---|
133 | { |
---|
134 | m_mass = mass; |
---|
135 | |
---|
136 | if (m_rigid_body) |
---|
137 | { |
---|
138 | SetLocalInertia(m_mass); |
---|
139 | m_rigid_body->setMassProps(mass, m_local_inertia); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | //------------------------------------------------------------------------- |
---|
144 | //Final conversion pass functons : Body related |
---|
145 | //-- |
---|
146 | |
---|
147 | //Init to rigid body |
---|
148 | void EasyPhysic::InitBodyToRigid(bool SetToKinematic) |
---|
149 | { |
---|
150 | if (m_collision_object) |
---|
151 | delete m_collision_object; |
---|
152 | |
---|
153 | if (!m_motion_state) |
---|
154 | SetTransform(vec3(.0f)); |
---|
155 | |
---|
156 | btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia); |
---|
157 | m_rigid_body = new btRigidBody(NewInfos); |
---|
158 | m_collision_object = m_rigid_body; |
---|
159 | |
---|
160 | if (m_mass == .0f) |
---|
161 | { |
---|
162 | if (SetToKinematic) |
---|
163 | { |
---|
164 | m_rigid_body->setActivationState(DISABLE_DEACTIVATION); |
---|
165 | m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); |
---|
166 | } |
---|
167 | } |
---|
168 | else |
---|
169 | SetMass(m_mass); |
---|
170 | } |
---|
171 | |
---|
172 | //Return correct Ghost Object |
---|
173 | btGhostObject* EasyPhysic::GetGhostObject() |
---|
174 | { |
---|
175 | return new btGhostObject(); |
---|
176 | } |
---|
177 | |
---|
178 | //Init to Ghost object, for Overlap/Sweep Test/Touching logic |
---|
179 | void EasyPhysic::InitBodyToGhost() |
---|
180 | { |
---|
181 | if (m_collision_object) |
---|
182 | delete m_collision_object; |
---|
183 | |
---|
184 | m_ghost_object = GetGhostObject(); |
---|
185 | m_ghost_object->setCollisionShape(m_collision_shape); |
---|
186 | m_collision_object = m_ghost_object; |
---|
187 | |
---|
188 | SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world)); |
---|
189 | |
---|
190 | m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags()); |
---|
191 | } |
---|
192 | |
---|
193 | //Add Physic object to the simulation |
---|
194 | void EasyPhysic::AddToSimulation(class Simulation* current_simulation) |
---|
195 | { |
---|
196 | btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); |
---|
197 | if (dynamics_world) |
---|
198 | { |
---|
199 | if (m_ghost_object) |
---|
200 | { |
---|
201 | dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask); |
---|
202 | current_simulation->AddToGhost(this); |
---|
203 | } |
---|
204 | else if (m_rigid_body) |
---|
205 | { |
---|
206 | dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask); |
---|
207 | if (m_mass != .0f) |
---|
208 | current_simulation->AddToDynamic(this); |
---|
209 | else |
---|
210 | current_simulation->AddToStatic(this); |
---|
211 | } |
---|
212 | else |
---|
213 | dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | //Remove Physic object to the simulation |
---|
218 | void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation) |
---|
219 | { |
---|
220 | btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); |
---|
221 | if (dynamics_world) |
---|
222 | { |
---|
223 | if (m_rigid_body) |
---|
224 | dynamics_world->removeRigidBody(m_rigid_body); |
---|
225 | else if (m_collision_object) |
---|
226 | dynamics_world->removeCollisionObject(m_collision_object); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | //------------------------------------------------------------------------- |
---|
231 | //Getter functons |
---|
232 | //-- |
---|
233 | |
---|
234 | mat4 EasyPhysic::GetTransform() |
---|
235 | { |
---|
236 | m_local_to_world = lol::mat4(1.0f); |
---|
237 | if (m_rigid_body && m_motion_state) |
---|
238 | { |
---|
239 | btTransform CurTransform; |
---|
240 | m_motion_state->getWorldTransform(CurTransform); |
---|
241 | CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]); |
---|
242 | } |
---|
243 | else if (m_collision_object) |
---|
244 | m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]); |
---|
245 | return m_local_to_world; |
---|
246 | } |
---|
247 | |
---|
248 | //Set Local Inertia |
---|
249 | void EasyPhysic::SetLocalInertia(float mass) |
---|
250 | { |
---|
251 | if (mass != .0f) |
---|
252 | m_collision_shape->calculateLocalInertia(mass, m_local_inertia); |
---|
253 | else |
---|
254 | m_local_inertia = btVector3(.0f, .0f, .0f); |
---|
255 | } |
---|
256 | |
---|
257 | //------------------------------------------------------------------------- |
---|
258 | //EASY_CHARACTER_CONTROLLER |
---|
259 | //-- |
---|
260 | |
---|
261 | //Deactivated for Character controller |
---|
262 | void EasyCharacterController::InitBodyToRigid(bool ZeroMassIsKinematic) |
---|
263 | { |
---|
264 | } |
---|
265 | |
---|
266 | //Return correct Ghost Object |
---|
267 | btGhostObject* EasyCharacterController::GetGhostObject() |
---|
268 | { |
---|
269 | return new btPairCachingGhostObject(); |
---|
270 | } |
---|
271 | |
---|
272 | //Init to Pair caching ghost object, since Character uses that one. |
---|
273 | void EasyCharacterController::InitBodyToGhost() |
---|
274 | { |
---|
275 | EasyPhysic::InitBodyToGhost(); |
---|
276 | |
---|
277 | m_pair_caching_object = (btPairCachingGhostObject*)m_ghost_object; |
---|
278 | m_ghost_object->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT | m_ghost_object->getCollisionFlags()); |
---|
279 | } |
---|
280 | |
---|
281 | //Add Physic object to the simulation |
---|
282 | void EasyCharacterController::AddToSimulation(class Simulation* current_simulation) |
---|
283 | { |
---|
284 | EasyPhysic::AddToSimulation(current_simulation); |
---|
285 | |
---|
286 | btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); |
---|
287 | if (dynamics_world) |
---|
288 | { |
---|
289 | if (m_character) |
---|
290 | delete m_character; |
---|
291 | |
---|
292 | m_character = new btKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
|
---|
293 | dynamics_world->addAction(m_character);
|
---|
294 | }
|
---|
295 | } |
---|
296 | |
---|
297 | //Remove Physic object to the simulation |
---|
298 | void EasyCharacterController::RemoveFromSimulation(class Simulation* current_simulation) |
---|
299 | { |
---|
300 | EasyPhysic::RemoveFromSimulation(current_simulation); |
---|
301 | |
---|
302 | btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); |
---|
303 | if (dynamics_world) |
---|
304 | { |
---|
305 | if (m_character) |
---|
306 | dynamics_world->removeAction(m_character); |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | //------------------------------------------------------------------------- |
---|
311 | //EASY_CONSTRAINT |
---|
312 | //-- |
---|
313 | |
---|
314 | void EasyConstraint::AddToSimulation(class Simulation* current_simulation) |
---|
315 | { |
---|
316 | btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); |
---|
317 | if (dynamics_world && m_typed_constraint) |
---|
318 | { |
---|
319 | dynamics_world->addConstraint(m_typed_constraint, m_disable_a2b_collision); |
---|
320 | current_simulation->AddToConstraint(this); |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | void EasyConstraint::RemoveFromSimulation(class Simulation* current_simulation) |
---|
325 | { |
---|
326 | btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); |
---|
327 | if (dynamics_world, m_typed_constraint) |
---|
328 | dynamics_world->removeConstraint(m_typed_constraint); |
---|
329 | } |
---|
330 | |
---|
331 | #endif // HAVE_PHYS_USE_BULLET |
---|
332 | |
---|
333 | } /* namespace phys */ |
---|
334 | |
---|
335 | } /* namespace lol */ |
---|