Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

150 строки
4.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2013 Cédric 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 HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include "lolbtphysicsintegration.h"
  16. #include "lolphysics.h"
  17. #include "easycharactercontroller.h"
  18. namespace lol
  19. {
  20. namespace phys
  21. {
  22. //-------------------------------------------------------------------------
  23. //EASY_CHARACTER_CONTROLLER
  24. //--
  25. //Deactivated for Character controller
  26. void EasyCharacterController::InitBodyToRigid(bool ZeroMassIsKinematic)
  27. {
  28. }
  29. //Return correct Ghost Object
  30. btGhostObject* EasyCharacterController::GetGhostObjectInstance()
  31. {
  32. return new btPairCachingGhostObject();
  33. }
  34. //Init to Pair caching ghost object, since Character uses that one.
  35. void EasyCharacterController::InitBodyToGhost()
  36. {
  37. EasyPhysic::InitBodyToGhost();
  38. m_pair_caching_object = (btPairCachingGhostObject*)m_ghost_object;
  39. m_ghost_object->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT | m_ghost_object->getCollisionFlags());
  40. }
  41. //Add Physic object to the simulation
  42. void EasyCharacterController::AddToSimulation(class Simulation* current_simulation)
  43. {
  44. EasyPhysic::AddToSimulation(current_simulation);
  45. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  46. if (dynamics_world)
  47. {
  48. if (m_character)
  49. delete m_character;
  50. //m_character = new btKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
  51. m_character = new BulletKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
  52. //Deactivate Character controller basic behaviour.
  53. //m_character->setGravity(.0f);
  54. //m_character->setFallSpeed(.0f);
  55. dynamics_world->addAction(m_character);
  56. current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CharacterController);
  57. Ticker::Ref(this);
  58. }
  59. }
  60. //Remove Physic object to the simulation
  61. void EasyCharacterController::RemoveFromSimulation(class Simulation* current_simulation)
  62. {
  63. EasyPhysic::RemoveFromSimulation(current_simulation);
  64. btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
  65. if (dynamics_world)
  66. {
  67. if (m_character)
  68. {
  69. dynamics_world->removeAction(m_character);
  70. current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CharacterController);
  71. Ticker::Unref(this);
  72. }
  73. }
  74. }
  75. void EasyCharacterController::Jump()
  76. {
  77. m_character->Jump();
  78. }
  79. //Set movement for this frame
  80. void EasyCharacterController::SetMovementForFrame(vec3 const &MoveQuantity)
  81. {
  82. m_frame_cached_movement = MoveQuantity;
  83. }
  84. //-------------------------------------------------------------------------
  85. //Base Location/Rotation setup
  86. //--
  87. void EasyCharacterController::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  88. {
  89. if (m_base_is_updating)
  90. {
  91. m_base_cached_movement = base_location - m_local_to_world[3].xyz;
  92. m_local_to_world = lol::mat4::translate(m_local_to_world[3].xyz) * lol::mat4(base_rotation);
  93. if (m_ghost_object)
  94. m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * m_local_to_world[3].xyz)));
  95. }
  96. else
  97. EasyPhysic::SetTransform(base_location, base_rotation);
  98. }
  99. //Internal callback when Base transform has changed.
  100. void EasyCharacterController::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
  101. {
  102. m_base_is_updating = true;
  103. EasyPhysic::BaseTransformChanged(PreviousMatrix, NewMatrix);
  104. m_base_is_updating = false;
  105. }
  106. //---
  107. char const *EasyCharacterController::GetName()
  108. {
  109. return "<EasyCharacterController>";
  110. }
  111. //Physic Tick
  112. void EasyCharacterController::TickGame(float seconds)
  113. {
  114. Entity::TickGame(seconds);
  115. //Send final velocity in Bullet
  116. {
  117. int IterationsNb = (int)(seconds / m_owner_simulation->m_timestep);
  118. float NewSeconds = IterationsNb * m_owner_simulation->m_timestep;
  119. m_character->SetVelocityForTimeInterval((m_base_cached_movement + m_frame_cached_movement) / NewSeconds, NewSeconds);
  120. m_base_cached_movement = vec3(.0f);
  121. }
  122. }
  123. } /* namespace phys */
  124. } /* namespace lol */