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.
 
 
 

193 lines
4.6 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::SetBaseTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  88. {
  89. if (m_motion_state)
  90. m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  91. else
  92. m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  93. }
  94. //-------------------------------------------------------------------------
  95. //Mass related functions
  96. //--
  97. //Set Shape functions
  98. void EasyPhysics::SetMass(float mass)
  99. {
  100. m_mass = mass;
  101. if (m_rigid_body)
  102. {
  103. SetLocalInertia(m_mass);
  104. m_rigid_body->setMassProps(mass, LOL2BT_VEC3(m_local_inertia));
  105. }
  106. }
  107. //-------------------------------------------------------------------------
  108. //Final conversion pass functons : Body related
  109. //--
  110. //Init to rigid body
  111. void EasyPhysics::InitBodyToRigid()
  112. {
  113. if (m_collision_object)
  114. delete m_collision_object;
  115. SetLocalInertia(m_mass);
  116. if (!m_motion_state)
  117. SetBaseTransform(vec3(.0f));
  118. btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
  119. m_rigid_body = new btRigidBody(NewInfos);
  120. m_collision_object = m_rigid_body;
  121. }
  122. void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
  123. {
  124. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  125. if (m_rigid_body)
  126. {
  127. dynamics_world->addRigidBody(m_rigid_body);
  128. if (m_mass != .0f)
  129. current_simulation->AddToDynamic(this);
  130. else
  131. current_simulation->AddToStatic(this);
  132. }
  133. else
  134. dynamics_world->addCollisionObject(m_collision_object);
  135. }
  136. //-------------------------------------------------------------------------
  137. //Getter functons
  138. //--
  139. mat4 EasyPhysics::GetTransform()
  140. {
  141. m_local_to_world = lol::mat4(1.0f);
  142. if (m_rigid_body && m_motion_state)
  143. {
  144. btTransform CurTransform;
  145. m_motion_state->getWorldTransform(CurTransform);
  146. CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
  147. }
  148. else if (m_collision_object)
  149. m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
  150. return m_local_to_world;
  151. }
  152. //Set Local Inertia
  153. void EasyPhysics::SetLocalInertia(float mass)
  154. {
  155. if (mass != .0f)
  156. m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
  157. else
  158. m_local_inertia = btVector3(.0f, .0f, .0f);
  159. }
  160. #endif
  161. } /* namespace phys */
  162. } /* namespace lol */