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.

212 lines
5.2 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. //
  13. // The EasyMesh class
  14. // ------------------
  15. //
  16. #if defined HAVE_CONFIG_H
  17. # include "config.h"
  18. #endif
  19. #include "LolBtPhysicsIntegration.h"
  20. #include "LolPhysics.h"
  21. namespace lol
  22. {
  23. namespace phys
  24. {
  25. #ifdef HAVE_PHYS_USE_BULLET
  26. EasyPhysics::EasyPhysics() :
  27. m_collision_object(NULL),
  28. m_rigid_body(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. EasyPhysics::~EasyPhysics()
  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 EasyPhysics::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 EasyPhysics::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 EasyPhysics::SetShapeToSphere(float radius)
  68. {
  69. SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE));
  70. }
  71. void EasyPhysics::SetShapeToCone(float radius, float height)
  72. {
  73. SetShapeTo(new btConeShape( radius * LOL2BT_UNIT,
  74. height * LOL2BT_UNIT));
  75. }
  76. void EasyPhysics::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 EasyPhysics::SetShapeToCapsule(float radius, float height)
  83. {
  84. SetShapeTo(new btCapsuleShape( radius * LOL2BT_UNIT * LOL2BT_SIZE,
  85. height * LOL2BT_UNIT * LOL2BT_SIZE));
  86. }
  87. //-------------------------------------------------------------------------
  88. //Bullet collision channel setup
  89. //--
  90. void EasyPhysics::CustomSetCollisionChannel(int NewGroup, int NewMask)
  91. {
  92. if (m_rigid_body)
  93. m_rigid_body->setCollisionFlags(m_collision_mask);
  94. }
  95. //-------------------------------------------------------------------------
  96. //Base Location/Rotation setup
  97. //--
  98. void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  99. {
  100. if (m_motion_state)
  101. {
  102. if (m_mass != .0f)
  103. m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  104. else
  105. {
  106. m_rigid_body->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  107. m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  108. }
  109. }
  110. else
  111. m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  112. }
  113. //-------------------------------------------------------------------------
  114. //Mass related functions
  115. //--
  116. //Set Shape functions
  117. void EasyPhysics::SetMass(float mass)
  118. {
  119. m_mass = mass;
  120. if (m_rigid_body)
  121. {
  122. SetLocalInertia(m_mass);
  123. m_rigid_body->setMassProps(mass, m_local_inertia);
  124. }
  125. }
  126. //-------------------------------------------------------------------------
  127. //Final conversion pass functons : Body related
  128. //--
  129. //Init to rigid body
  130. void EasyPhysics::InitBodyToRigid()
  131. {
  132. if (m_collision_object)
  133. delete m_collision_object;
  134. SetLocalInertia(m_mass);
  135. if (!m_motion_state)
  136. SetTransform(vec3(.0f));
  137. btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
  138. m_rigid_body = new btRigidBody(NewInfos);
  139. m_collision_object = m_rigid_body;
  140. }
  141. void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
  142. {
  143. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  144. if (m_rigid_body)
  145. {
  146. dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
  147. if (m_mass != .0f)
  148. current_simulation->AddToDynamic(this);
  149. else
  150. current_simulation->AddToStatic(this);
  151. }
  152. else
  153. dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
  154. }
  155. //-------------------------------------------------------------------------
  156. //Getter functons
  157. //--
  158. mat4 EasyPhysics::GetTransform()
  159. {
  160. m_local_to_world = lol::mat4(1.0f);
  161. if (m_rigid_body && m_motion_state)
  162. {
  163. btTransform CurTransform;
  164. m_motion_state->getWorldTransform(CurTransform);
  165. CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
  166. }
  167. else if (m_collision_object)
  168. m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
  169. return m_local_to_world;
  170. }
  171. //Set Local Inertia
  172. void EasyPhysics::SetLocalInertia(float mass)
  173. {
  174. if (mass != .0f)
  175. m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
  176. else
  177. m_local_inertia = btVector3(.0f, .0f, .0f);
  178. }
  179. #endif
  180. } /* namespace phys */
  181. } /* namespace lol */