PhysicsPlayerController.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (C) 2009-present, 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/Common.h>
  7. #include <AnKi/Util/ClassWrapper.h>
  8. namespace anki {
  9. /// @addtogroup physics
  10. /// @{
  11. /// Init info for PhysicsPlayerController.
  12. class PhysicsPlayerControllerInitInfo
  13. {
  14. public:
  15. F32 m_mass = 83.0f;
  16. F32 m_standingHeight = 1.9f;
  17. F32 m_crouchingHeight = 1.2f;
  18. F32 m_waistWidth = 0.3f;
  19. Vec3 m_initialPosition = Vec3(0.0f);
  20. Bool m_controlMovementDuringJump = true;
  21. void* m_userData = nullptr;
  22. };
  23. /// A player controller that walks the world.
  24. class PhysicsPlayerController : public PhysicsObjectBase
  25. {
  26. ANKI_PHYSICS_COMMON_FRIENDS
  27. friend class PhysicsPlayerControllerPtrDeleter;
  28. public:
  29. // Update the state machine
  30. void updateState(F32 forwardSpeed, const Vec3& forwardDir, F32 jumpSpeed, Bool crouching)
  31. {
  32. m_input.m_forwardSpeed = forwardSpeed;
  33. m_input.m_forwardDir = forwardDir;
  34. m_input.m_jumpSpeed = jumpSpeed;
  35. m_input.m_crouching = crouching;
  36. m_input.m_updated = 1;
  37. }
  38. /// This is a deferred operation, will happen on the next PhysicsWorld::update.
  39. void moveToPosition(const Vec3& position)
  40. {
  41. m_jphCharacter->SetPosition(toJPH(position));
  42. }
  43. const Vec3& getPosition(U32* version = nullptr) const
  44. {
  45. if(version)
  46. {
  47. *version = m_positionVersion;
  48. }
  49. return m_position;
  50. }
  51. private:
  52. static constexpr F32 kMaxSlopeAngle = toRad(45.0f);
  53. static constexpr F32 kMaxStrength = 100.0f;
  54. static constexpr JPH::EBackFaceMode kBackFaceMode = JPH::EBackFaceMode::CollideWithBackFaces;
  55. static constexpr F32 kCharacterPadding = 0.02f;
  56. static constexpr F32 kPenetrationRecoverySpeed = 1.0f;
  57. static constexpr F32 kPredictiveContactDistance = 0.1f;
  58. static constexpr Bool kEnhancedInternalEdgeRemoval = false;
  59. static constexpr Bool kEnableCharacterInertia = true;
  60. static constexpr F32 kInnerShapeFraction = 0.9f;
  61. static constexpr Bool kEnableStickToFloor = true;
  62. static constexpr Bool kEnableWalkStairs = true;
  63. ClassWrapper<JPH::CharacterVirtual> m_jphCharacter;
  64. PhysicsCollisionShapePtr m_standingShape;
  65. PhysicsCollisionShapePtr m_crouchingShape;
  66. PhysicsCollisionShapePtr m_innerStandingShape; ///< Inner shape to recieve collision events.
  67. PhysicsCollisionShapePtr m_innerCrouchingShape;
  68. JPH::Vec3 m_desiredVelocity = JPH::Vec3::sZero();
  69. class
  70. {
  71. public:
  72. F32 m_forwardSpeed = 0.0f;
  73. Vec3 m_forwardDir;
  74. F32 m_jumpSpeed = 0.0f;
  75. U8 m_crouching : 1 = false;
  76. U8 m_updated : 1 = false;
  77. } m_input;
  78. Vec3 m_position;
  79. U32 m_positionVersion = 0;
  80. U32 m_controlMovementDuringJump : 1 = true;
  81. U32 m_allowSliding : 1 = false;
  82. U32 m_crouching : 1 = false;
  83. PhysicsPlayerController();
  84. ~PhysicsPlayerController();
  85. void init(const PhysicsPlayerControllerInitInfo& init);
  86. void prePhysicsUpdate(Second dt);
  87. void postPhysicsUpdate();
  88. };
  89. /// @}
  90. } // end namespace anki