You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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