PhysicsPlayerController.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/physics/PhysicsObject.h>
  7. namespace anki
  8. {
  9. /// @addtogroup physics
  10. /// @{
  11. /// The implementation of the Newton manager.
  12. class CharacterControllerManager : public dCustomPlayerControllerManager
  13. {
  14. public:
  15. PhysicsWorld* m_world;
  16. CharacterControllerManager(PhysicsWorld* world);
  17. ~CharacterControllerManager()
  18. {
  19. }
  20. void ApplyPlayerMove(dCustomPlayerController* const controller, dFloat timestep);
  21. };
  22. /// Init info for PhysicsPlayerController.
  23. class PhysicsPlayerControllerInitInfo
  24. {
  25. public:
  26. F32 m_mass = 83.0f;
  27. F32 m_innerRadius = 0.30f;
  28. F32 m_outerRadius = 0.50f;
  29. F32 m_height = 1.9f;
  30. F32 m_stepHeight = 1.9f * 0.33f;
  31. Vec4 m_position = Vec4(0.0f);
  32. };
  33. /// A player controller that walks the world.
  34. class PhysicsPlayerController final : public PhysicsObject
  35. {
  36. friend class CharacterControllerManager;
  37. public:
  38. PhysicsPlayerController(PhysicsWorld* world)
  39. : PhysicsObject(PhysicsObjectType::PLAYER_CONTROLLER, world)
  40. {
  41. }
  42. ~PhysicsPlayerController();
  43. ANKI_USE_RESULT Error create(const PhysicsPlayerControllerInitInfo& init);
  44. // Update the state machine
  45. void setVelocity(F32 forwardSpeed, F32 strafeSpeed, F32 jumpSpeed, const Vec4& forwardDir)
  46. {
  47. m_forwardSpeed = forwardSpeed;
  48. m_strafeSpeed = strafeSpeed;
  49. m_jumpSpeed = jumpSpeed;
  50. m_forwardDir = forwardDir;
  51. }
  52. void moveToPosition(const Vec4& position);
  53. const Transform& getTransform(Bool& updated)
  54. {
  55. updated = m_updated;
  56. m_updated = false;
  57. return m_trf;
  58. }
  59. private:
  60. dCustomPlayerController* m_newtonPlayer = nullptr;
  61. Transform m_trf = Transform::getIdentity();
  62. Bool m_updated = true;
  63. // State
  64. F32 m_forwardSpeed = 0.0;
  65. F32 m_strafeSpeed = 0.0;
  66. F32 m_jumpSpeed = 0.0;
  67. Vec4 m_forwardDir = Vec4(0.0, 0.0, -1.0, 0.0);
  68. static void onTransformUpdateCallback(const NewtonBody* body, const dFloat* matrix, int threadIndex)
  69. {
  70. ANKI_ASSERT(body && matrix);
  71. Transform trf = Transform(toAnki(dMatrix(matrix)));
  72. void* ud = NewtonBodyGetUserData(body);
  73. ANKI_ASSERT(ud);
  74. static_cast<PhysicsPlayerController*>(ud)->onTransformUpdate(trf);
  75. }
  76. void onTransformUpdate(const Transform& trf)
  77. {
  78. if(trf != m_trf)
  79. {
  80. m_updated = true;
  81. m_trf = trf;
  82. }
  83. }
  84. };
  85. /// @}
  86. } // end namespace anki