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

151 lines
4.6 KiB

  1. //
  2. // Lol Engine — Bullet physics test
  3. //
  4. // Copyright © 2009—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  5. // © 2010—2018 Sam Hocevar <sam@hocevar.net>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #if HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif
  16. #include "lolbtphysicsintegration.h"
  17. #include "lolphysics.h"
  18. #include "easycharactercontroller.h"
  19. namespace lol
  20. {
  21. namespace phys
  22. {
  23. //-------------------------------------------------------------------------
  24. //EASY_CHARACTER_CONTROLLER
  25. //--
  26. //Deactivated for Character controller
  27. void EasyCharacterController::InitBodyToRigid(bool ZeroMassIsKinematic)
  28. {
  29. }
  30. //Return correct Ghost Object
  31. btGhostObject* EasyCharacterController::GetGhostObjectInstance()
  32. {
  33. return new btPairCachingGhostObject();
  34. }
  35. //Init to Pair caching ghost object, since Character uses that one.
  36. void EasyCharacterController::InitBodyToGhost()
  37. {
  38. EasyPhysic::InitBodyToGhost();
  39. m_pair_caching_object = (btPairCachingGhostObject*)m_ghost_object;
  40. m_ghost_object->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT | m_ghost_object->getCollisionFlags());
  41. }
  42. //Add Physic object to the simulation
  43. void EasyCharacterController::AddToSimulation(class Simulation* current_simulation)
  44. {
  45. EasyPhysic::AddToSimulation(current_simulation);
  46. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  47. if (dynamics_world)
  48. {
  49. if (m_character)
  50. delete m_character;
  51. //m_character = new btKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
  52. m_character = new BulletKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
  53. //Deactivate Character controller basic behaviour.
  54. //m_character->setGravity(.0f);
  55. //m_character->setFallSpeed(.0f);
  56. dynamics_world->addAction(m_character);
  57. current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CharacterController);
  58. Ticker::Ref(this);
  59. }
  60. }
  61. //Remove Physic object to the simulation
  62. void EasyCharacterController::RemoveFromSimulation(class Simulation* current_simulation)
  63. {
  64. EasyPhysic::RemoveFromSimulation(current_simulation);
  65. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  66. if (dynamics_world)
  67. {
  68. if (m_character)
  69. {
  70. dynamics_world->removeAction(m_character);
  71. current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CharacterController);
  72. Ticker::Unref(this);
  73. }
  74. }
  75. }
  76. void EasyCharacterController::Jump()
  77. {
  78. m_character->Jump();
  79. }
  80. //Set movement for this frame
  81. void EasyCharacterController::SetMovementForFrame(vec3 const &MoveQuantity)
  82. {
  83. m_frame_cached_movement = MoveQuantity;
  84. }
  85. //-------------------------------------------------------------------------
  86. //Base Location/Rotation setup
  87. //--
  88. void EasyCharacterController::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  89. {
  90. if (m_base_is_updating)
  91. {
  92. m_base_cached_movement = base_location - m_local_to_world[3].xyz;
  93. m_local_to_world = lol::mat4::translate(m_local_to_world[3].xyz) * lol::mat4(base_rotation);
  94. if (m_ghost_object)
  95. m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * m_local_to_world[3].xyz)));
  96. }
  97. else
  98. EasyPhysic::SetTransform(base_location, base_rotation);
  99. }
  100. //Internal callback when Base transform has changed.
  101. void EasyCharacterController::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
  102. {
  103. m_base_is_updating = true;
  104. EasyPhysic::BaseTransformChanged(PreviousMatrix, NewMatrix);
  105. m_base_is_updating = false;
  106. }
  107. //---
  108. std::string EasyCharacterController::GetName()
  109. {
  110. return "<EasyCharacterController>";
  111. }
  112. //Physic Tick
  113. void EasyCharacterController::tick_game(float seconds)
  114. {
  115. Entity::tick_game(seconds);
  116. //Send final velocity in Bullet
  117. {
  118. int IterationsNb = (int)(seconds / m_owner_simulation->m_timestep);
  119. float NewSeconds = IterationsNb * m_owner_simulation->m_timestep;
  120. m_character->SetVelocityForTimeInterval((m_base_cached_movement + m_frame_cached_movement) / NewSeconds, NewSeconds);
  121. m_base_cached_movement = vec3(.0f);
  122. }
  123. }
  124. } /* namespace phys */
  125. } /* namespace lol */