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.

EasyCharacterController.cpp 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2013 Cdric Lecacheur <jordx@free.fr>
  6. // (c) 2009-2013 Benjamin "Touky" 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://www.wtfpl.net/ for more details.
  11. //
  12. #if defined HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include "../Include/LolBtPhysicsIntegration.h"
  16. #include "../Include/LolPhysics.h"
  17. #include "../Include/EasyCharacterController.h"
  18. namespace lol
  19. {
  20. namespace phys
  21. {
  22. #ifdef HAVE_PHYS_USE_BULLET
  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.v3.xyz;
  93. m_local_to_world = lol::mat4::translate(m_local_to_world.v3.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.v3.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. char const *EasyCharacterController::GetName()
  109. {
  110. return "<EasyCharacterController>";
  111. }
  112. //Physic Tick
  113. void EasyCharacterController::TickGame(float seconds)
  114. {
  115. Entity::TickGame(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. #endif // HAVE_PHYS_USE_BULLET
  125. } /* namespace phys */
  126. } /* namespace lol */