161 řádky
3.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. //-------------------------------------------------------------------------
  36. //Set Shape functions
  37. //--
  38. void EasyPhysics::SetShapeTo(btCollisionShape* collision_shape)
  39. {
  40. bool bReinitToRigidBody = false;
  41. if (m_rigid_body)
  42. {
  43. bReinitToRigidBody = true;
  44. delete m_rigid_body;
  45. }
  46. if (m_collision_shape)
  47. delete m_collision_shape;
  48. m_collision_shape = collision_shape;
  49. if (bReinitToRigidBody)
  50. InitBodyToRigid();
  51. }
  52. //Box Shape support
  53. void EasyPhysics::SetShapeToBox(lol::vec3& box_size)
  54. {
  55. SetShapeTo(new btBoxShape(LOL2BT_VEC3(box_size * LOL2BT_UNIT * LOL2BT_SIZE)));
  56. }
  57. //-------------------------------------------------------------------------
  58. //Base Location/Rotation setup
  59. //--
  60. void EasyPhysics::SetBaseTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  61. {
  62. if (m_motion_state)
  63. m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  64. else
  65. m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  66. }
  67. //-------------------------------------------------------------------------
  68. //Mass related functions
  69. //--
  70. //Set Shape functions
  71. void EasyPhysics::SetMass(float mass)
  72. {
  73. m_mass = mass;
  74. if (m_rigid_body)
  75. {
  76. SetLocalInertia(m_mass);
  77. m_rigid_body->setMassProps(mass, LOL2BT_VEC3(m_local_inertia));
  78. }
  79. }
  80. //-------------------------------------------------------------------------
  81. //Final conversion pass functons : Body related
  82. //--
  83. //Init to rigid body
  84. void EasyPhysics::InitBodyToRigid()
  85. {
  86. if (m_collision_object)
  87. delete m_collision_object;
  88. SetLocalInertia(m_mass);
  89. if (!m_motion_state)
  90. SetBaseTransform(vec3(.0f));
  91. btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
  92. m_rigid_body = new btRigidBody(NewInfos);
  93. m_collision_object = m_rigid_body;
  94. }
  95. void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
  96. {
  97. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  98. if (m_rigid_body)
  99. {
  100. dynamics_world->addRigidBody(m_rigid_body);
  101. if (m_mass != .0f)
  102. current_simulation->AddToDynamic(this);
  103. else
  104. current_simulation->AddToStatic(this);
  105. }
  106. else
  107. dynamics_world->addCollisionObject(m_collision_object);
  108. }
  109. //-------------------------------------------------------------------------
  110. //Getter functons
  111. //--
  112. mat4 EasyPhysics::GetTransform()
  113. {
  114. m_local_to_world = lol::mat4(1.0f);
  115. if (m_rigid_body && m_motion_state)
  116. {
  117. btTransform CurTransform;
  118. m_motion_state->getWorldTransform(CurTransform);
  119. CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
  120. }
  121. else if (m_collision_object)
  122. m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
  123. return m_local_to_world;
  124. }
  125. //Set Local Inertia
  126. void EasyPhysics::SetLocalInertia(float mass)
  127. {
  128. if (mass != .0f)
  129. m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
  130. else
  131. m_local_inertia = btVector3(.0f, .0f, .0f);
  132. }
  133. #endif
  134. } /* namespace phys */
  135. } /* namespace lol */