No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

265 líneas
6.8 KiB

  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. #if defined HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include "LolBtPhysicsIntegration.h"
  16. #include "LolPhysics.h"
  17. namespace lol
  18. {
  19. namespace phys
  20. {
  21. #ifdef HAVE_PHYS_USE_BULLET
  22. //-------------------------------------------------------------------------
  23. //EASY_PHYSIC
  24. //--
  25. EasyPhysic::EasyPhysic() :
  26. m_collision_object(NULL),
  27. m_rigid_body(NULL),
  28. m_ghost_object(NULL),
  29. m_collision_shape(NULL),
  30. m_motion_state(NULL),
  31. m_mass(.0f),
  32. m_local_inertia(btVector3(.0f, .0f, .0f)),
  33. m_collision_group(1),
  34. m_collision_mask(1)
  35. {
  36. }
  37. EasyPhysic::~EasyPhysic()
  38. {
  39. m_rigid_body = NULL;
  40. delete m_collision_object;
  41. delete m_collision_shape;
  42. delete m_motion_state;
  43. }
  44. //-------------------------------------------------------------------------
  45. //Set Shape functions
  46. //--
  47. void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape)
  48. {
  49. bool bReinitToRigidBody = false;
  50. if (m_rigid_body)
  51. {
  52. bReinitToRigidBody = true;
  53. delete m_rigid_body;
  54. }
  55. if (m_collision_shape)
  56. delete m_collision_shape;
  57. m_collision_shape = collision_shape;
  58. if (bReinitToRigidBody)
  59. InitBodyToRigid();
  60. }
  61. //Box Shape support
  62. void EasyPhysic::SetShapeToBox(lol::vec3& box_size)
  63. {
  64. vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
  65. SetShapeTo(new btBoxShape(LOL2BT_VEC3(new_box_size)));
  66. }
  67. void EasyPhysic::SetShapeToSphere(float radius)
  68. {
  69. SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE));
  70. }
  71. void EasyPhysic::SetShapeToCone(float radius, float height)
  72. {
  73. SetShapeTo(new btConeShape( radius * LOL2BT_UNIT,
  74. height * LOL2BT_UNIT));
  75. }
  76. void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size)
  77. {
  78. vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
  79. new_cyl_size.y *= LOL2BT_SIZE;
  80. SetShapeTo(new btCylinderShape(LOL2BT_VEC3(new_cyl_size)));
  81. }
  82. void EasyPhysic::SetShapeToCapsule(float radius, float height)
  83. {
  84. SetShapeTo(new btCapsuleShape( radius * LOL2BT_UNIT * LOL2BT_SIZE,
  85. height * LOL2BT_UNIT * LOL2BT_SIZE));
  86. }
  87. //-------------------------------------------------------------------------
  88. //Base Location/Rotation setup
  89. //--
  90. void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  91. {
  92. if (m_ghost_object)
  93. m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT)));
  94. else
  95. {
  96. if (m_motion_state)
  97. m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT)));
  98. else
  99. m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT)));
  100. }
  101. }
  102. //-------------------------------------------------------------------------
  103. //Mass related functions
  104. //--
  105. //Set Shape functions
  106. void EasyPhysic::SetMass(float mass)
  107. {
  108. m_mass = mass;
  109. if (m_rigid_body)
  110. {
  111. SetLocalInertia(m_mass);
  112. m_rigid_body->setMassProps(mass, m_local_inertia);
  113. }
  114. }
  115. //-------------------------------------------------------------------------
  116. //Final conversion pass functons : Body related
  117. //--
  118. //Init to rigid body
  119. void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
  120. {
  121. if (m_collision_object)
  122. delete m_collision_object;
  123. if (!m_motion_state)
  124. SetTransform(vec3(.0f));
  125. btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
  126. m_rigid_body = new btRigidBody(NewInfos);
  127. m_collision_object = m_rigid_body;
  128. if (m_mass == .0f && SetToKinematic)
  129. {
  130. m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
  131. m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
  132. }
  133. }
  134. //Init to Ghost object, for Overlap/Sweep Test/Touching logic
  135. void EasyPhysic::InitBodyToGhost()
  136. {
  137. if (m_collision_object)
  138. delete m_collision_object;
  139. m_ghost_object = new btGhostObject();
  140. m_ghost_object->setCollisionShape(m_collision_shape);
  141. m_collision_object = m_ghost_object;
  142. SetTransform(vec3(.0f));
  143. m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
  144. //btCollisionObject::CF_CHARACTER_OBJECT
  145. }
  146. //Add Physic object to the simulation
  147. void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
  148. {
  149. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  150. if (dynamics_world)
  151. {
  152. if (m_ghost_object)
  153. {
  154. dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
  155. current_simulation->AddToGhost(this);
  156. }
  157. else if (m_rigid_body)
  158. {
  159. dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
  160. if (m_mass != .0f)
  161. current_simulation->AddToDynamic(this);
  162. else
  163. current_simulation->AddToStatic(this);
  164. }
  165. else
  166. dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
  167. }
  168. }
  169. void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation)
  170. {
  171. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  172. if (dynamics_world)
  173. {
  174. if (m_rigid_body)
  175. dynamics_world->removeRigidBody(m_rigid_body);
  176. else
  177. dynamics_world->removeCollisionObject(m_collision_object);
  178. }
  179. }
  180. //-------------------------------------------------------------------------
  181. //Getter functons
  182. //--
  183. mat4 EasyPhysic::GetTransform()
  184. {
  185. m_local_to_world = lol::mat4(1.0f);
  186. if (m_rigid_body && m_motion_state)
  187. {
  188. btTransform CurTransform;
  189. m_motion_state->getWorldTransform(CurTransform);
  190. CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
  191. }
  192. else if (m_collision_object)
  193. m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
  194. return m_local_to_world;
  195. }
  196. //Set Local Inertia
  197. void EasyPhysic::SetLocalInertia(float mass)
  198. {
  199. if (mass != .0f)
  200. m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
  201. else
  202. m_local_inertia = btVector3(.0f, .0f, .0f);
  203. }
  204. //-------------------------------------------------------------------------
  205. //EASY_CONSTRAINT
  206. //--
  207. void EasyConstraint::AddToSimulation(class Simulation* current_simulation)
  208. {
  209. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  210. if (dynamics_world && m_typed_constraint)
  211. {
  212. dynamics_world->addConstraint(m_typed_constraint, m_disable_a2b_collision);
  213. current_simulation->AddToConstraint(this);
  214. }
  215. }
  216. void EasyConstraint::RemoveFromSimulation(class Simulation* current_simulation)
  217. {
  218. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  219. if (dynamics_world, m_typed_constraint)
  220. dynamics_world->removeConstraint(m_typed_constraint);
  221. }
  222. #endif // HAVE_PHYS_USE_BULLET
  223. } /* namespace phys */
  224. } /* namespace lol */