You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EasyCharacterController.cpp 4.1 KiB

преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
преди 12 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2012 Cdric 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. #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. //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. //Set movement for this frame
  76. void EasyCharacterController::SetMovementForFrame(vec3 const &MoveQuantity)
  77. {
  78. m_frame_cached_movement = MoveQuantity;
  79. }
  80. //-------------------------------------------------------------------------
  81. //Base Location/Rotation setup
  82. //--
  83. void EasyCharacterController::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
  84. {
  85. if (m_base_is_updating)
  86. {
  87. m_base_cached_movement = base_location - m_local_to_world.v3.xyz;
  88. m_local_to_world = lol::mat4::translate(m_local_to_world.v3.xyz) * lol::mat4(base_rotation);
  89. if (m_ghost_object)
  90. m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * m_local_to_world.v3.xyz)));
  91. }
  92. else
  93. EasyPhysic::SetTransform(base_location, base_rotation);
  94. }
  95. //Internal callback when Base transform has changed.
  96. void EasyCharacterController::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
  97. {
  98. m_base_is_updating = true;
  99. EasyPhysic::BaseTransformChanged(PreviousMatrix, NewMatrix);
  100. m_base_is_updating = false;
  101. }
  102. //Physic Tick
  103. void EasyCharacterController::TickGame(float seconds)
  104. {
  105. Entity::TickGame(seconds);
  106. int IterationsNb = (int)(seconds / m_owner_simulation->m_timestep);
  107. float NewSeconds = IterationsNb * m_owner_simulation->m_timestep;
  108. m_character->setVelocityForTimeInterval(LOL2BT_VEC3(LOL2BT_UNIT * (m_base_cached_movement + m_frame_cached_movement)) / NewSeconds, NewSeconds);
  109. m_base_cached_movement = vec3(.0f);
  110. }
  111. #endif // HAVE_PHYS_USE_BULLET
  112. } /* namespace phys */
  113. } /* namespace lol */