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.
 
 
 

200 líneas
4.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. //
  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. {
  34. }
  35. EasyPhysics::~EasyPhysics()
  36. {
  37. delete m_collision_object;
  38. delete m_collision_shape;
  39. delete m_motion_state;
  40. }
  41. //-------------------------------------------------------------------------
  42. //Set Shape functions
  43. //--
  44. void EasyPhysics::SetShapeTo(btCollisionShape* collision_shape)
  45. {
  46. bool bReinitToRigidBody = false;
  47. if (m_rigid_body)
  48. {
  49. bReinitToRigidBody = true;
  50. delete m_rigid_body;
  51. }
  52. if (m_collision_shape)
  53. delete m_collision_shape;
  54. m_collision_shape = collision_shape;
  55. if (bReinitToRigidBody)
  56. InitBodyToRigid();
  57. }
  58. //Box Shape support
  59. void EasyPhysics::SetShapeToBox(lol::vec3& box_size)
  60. {
  61. vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
  62. SetShapeTo(new btBoxShape(LOL2BT_VEC3(new_box_size)));
  63. }
  64. void EasyPhysics::SetShapeToSphere(float radius)
  65. {
  66. SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE));
  67. }
  68. void EasyPhysics::SetShapeToCone(float radius, float height)
  69. {
  70. SetShapeTo(new btConeShape( radius * LOL2BT_UNIT,
  71. height * LOL2BT_UNIT));
  72. }
  73. void EasyPhysics::SetShapeToCylinder(lol::vec3& cyl_size)
  74. {
  75. vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
  76. new_cyl_size.y *= LOL2BT_SIZE;
  77. SetShapeTo(new btCylinderShape(LOL2BT_VEC3(new_cyl_size)));
  78. }
  79. void EasyPhysics::SetShapeToCapsule(float radius, float height)
  80. {
  81. SetShapeTo(new btCapsuleShape( radius * LOL2BT_UNIT * LOL2BT_SIZE,
  82. height * LOL2BT_UNIT * LOL2BT_SIZE));
  83. }
  84. //-------------------------------------------------------------------------
  85. //Base Location/Rotation setup
  86. //--
  87. void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  88. {
  89. if (m_motion_state)
  90. {
  91. if (m_mass != .0f)
  92. m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  93. else
  94. {
  95. m_rigid_body->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  96. m_motion_state->setWorldTransform(m_rigid_body->getWorldTransform());
  97. }
  98. }
  99. else
  100. m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  101. }
  102. //-------------------------------------------------------------------------
  103. //Mass related functions
  104. //--
  105. //Set Shape functions
  106. void EasyPhysics::SetMass(float mass)
  107. {
  108. m_mass = mass;
  109. if (m_rigid_body)
  110. {
  111. SetLocalInertia(m_mass);
  112. m_rigid_body->setMassProps(mass, LOL2BT_VEC3(m_local_inertia));
  113. }
  114. }
  115. //-------------------------------------------------------------------------
  116. //Final conversion pass functons : Body related
  117. //--
  118. //Init to rigid body
  119. void EasyPhysics::InitBodyToRigid()
  120. {
  121. if (m_collision_object)
  122. delete m_collision_object;
  123. SetLocalInertia(m_mass);
  124. if (!m_motion_state)
  125. SetTransform(vec3(.0f));
  126. btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
  127. m_rigid_body = new btRigidBody(NewInfos);
  128. m_collision_object = m_rigid_body;
  129. }
  130. void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
  131. {
  132. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  133. if (m_rigid_body)
  134. {
  135. dynamics_world->addRigidBody(m_rigid_body);
  136. if (m_mass != .0f)
  137. current_simulation->AddToDynamic(this);
  138. else
  139. current_simulation->AddToStatic(this);
  140. }
  141. else
  142. dynamics_world->addCollisionObject(m_collision_object);
  143. }
  144. //-------------------------------------------------------------------------
  145. //Getter functons
  146. //--
  147. mat4 EasyPhysics::GetTransform()
  148. {
  149. m_local_to_world = lol::mat4(1.0f);
  150. if (m_rigid_body && m_motion_state)
  151. {
  152. btTransform CurTransform;
  153. m_motion_state->getWorldTransform(CurTransform);
  154. CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
  155. }
  156. else if (m_collision_object)
  157. m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
  158. return m_local_to_world;
  159. }
  160. //Set Local Inertia
  161. void EasyPhysics::SetLocalInertia(float mass)
  162. {
  163. if (mass != .0f)
  164. m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
  165. else
  166. m_local_inertia = btVector3(.0f, .0f, .0f);
  167. }
  168. #endif
  169. } /* namespace phys */
  170. } /* namespace lol */