25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

173 lines
4.1 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. vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
  56. SetShapeTo(new btBoxShape(LOL2BT_VEC3(new_box_size)));
  57. }
  58. void EasyPhysics::SetShapeToSphere(float radius)
  59. {
  60. SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE));
  61. }
  62. void EasyPhysics::SetShapeToCone(float radius, float height)
  63. {
  64. SetShapeTo(new btConeShape( radius * LOL2BT_UNIT,
  65. height * LOL2BT_UNIT));
  66. }
  67. //-------------------------------------------------------------------------
  68. //Base Location/Rotation setup
  69. //--
  70. void EasyPhysics::SetBaseTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  71. {
  72. if (m_motion_state)
  73. m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  74. else
  75. m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
  76. }
  77. //-------------------------------------------------------------------------
  78. //Mass related functions
  79. //--
  80. //Set Shape functions
  81. void EasyPhysics::SetMass(float mass)
  82. {
  83. m_mass = mass;
  84. if (m_rigid_body)
  85. {
  86. SetLocalInertia(m_mass);
  87. m_rigid_body->setMassProps(mass, LOL2BT_VEC3(m_local_inertia));
  88. }
  89. }
  90. //-------------------------------------------------------------------------
  91. //Final conversion pass functons : Body related
  92. //--
  93. //Init to rigid body
  94. void EasyPhysics::InitBodyToRigid()
  95. {
  96. if (m_collision_object)
  97. delete m_collision_object;
  98. SetLocalInertia(m_mass);
  99. if (!m_motion_state)
  100. SetBaseTransform(vec3(.0f));
  101. btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
  102. m_rigid_body = new btRigidBody(NewInfos);
  103. m_collision_object = m_rigid_body;
  104. }
  105. void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
  106. {
  107. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  108. if (m_rigid_body)
  109. {
  110. dynamics_world->addRigidBody(m_rigid_body);
  111. if (m_mass != .0f)
  112. current_simulation->AddToDynamic(this);
  113. else
  114. current_simulation->AddToStatic(this);
  115. }
  116. else
  117. dynamics_world->addCollisionObject(m_collision_object);
  118. }
  119. //-------------------------------------------------------------------------
  120. //Getter functons
  121. //--
  122. mat4 EasyPhysics::GetTransform()
  123. {
  124. m_local_to_world = lol::mat4(1.0f);
  125. if (m_rigid_body && m_motion_state)
  126. {
  127. btTransform CurTransform;
  128. m_motion_state->getWorldTransform(CurTransform);
  129. CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
  130. }
  131. else if (m_collision_object)
  132. m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
  133. return m_local_to_world;
  134. }
  135. //Set Local Inertia
  136. void EasyPhysics::SetLocalInertia(float mass)
  137. {
  138. if (mass != .0f)
  139. m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
  140. else
  141. m_local_inertia = btVector3(.0f, .0f, .0f);
  142. }
  143. #endif
  144. } /* namespace phys */
  145. } /* namespace lol */