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.
 
 
 

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