Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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