PhysicsPlayerController.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_PHYSICS_PLAYER_CONTROLLER_H
  6. #define ANKI_PHYSICS_PLAYER_CONTROLLER_H
  7. #include "anki/physics/PhysicsObject.h"
  8. namespace anki {
  9. /// @addtogroup physics
  10. /// @{
  11. /// Initializer for PhysicsPlayerController.
  12. struct PhysicsPlayerControllerInitializer
  13. {
  14. F32 m_mass = 83.0;
  15. F32 m_innerRadius = 0.30;
  16. F32 m_outerRadius = 0.50;
  17. F32 m_height = 1.9;
  18. F32 m_stepHeight = 1.9 * 0.33;
  19. Vec4 m_position = Vec4(0.0);
  20. };
  21. /// A player controller that walks the world.
  22. class PhysicsPlayerController final: public PhysicsObject
  23. {
  24. public:
  25. using Initializer = PhysicsPlayerControllerInitializer;
  26. PhysicsPlayerController(PhysicsWorld* world)
  27. : PhysicsObject(Type::PLAYER_CONTROLLER, world)
  28. {}
  29. ANKI_USE_RESULT Error create(const Initializer& init);
  30. // Update the state machine
  31. void setVelocity(F32 forwardSpeed, F32 strafeSpeed, F32 jumpSpeed,
  32. const Vec4& forwardDir)
  33. {
  34. m_forwardSpeed = forwardSpeed;
  35. m_strafeSpeed = strafeSpeed;
  36. m_jumpSpeed = jumpSpeed;
  37. m_forwardDir = forwardDir;
  38. }
  39. void moveToPosition(const Vec4& position);
  40. const Transform& getTransform(Bool& updated)
  41. {
  42. updated = m_updated;
  43. m_updated = false;
  44. return m_trf;
  45. }
  46. /// @privatesection
  47. /// @{
  48. /// Called by Newton thread to update the controller.
  49. static void postUpdateKernelCallback(
  50. NewtonWorld* const world,
  51. void* const context,
  52. int threadIndex);
  53. /// @}
  54. private:
  55. Vec4 m_upDir;
  56. Vec4 m_frontDir;
  57. Vec4 m_groundPlane;
  58. Vec4 m_groundVelocity;
  59. F32 m_innerRadius;
  60. F32 m_outerRadius;
  61. F32 m_height;
  62. F32 m_stepHeight;
  63. F32 m_maxSlope;
  64. F32 m_sphereCastOrigin;
  65. F32 m_restrainingDistance;
  66. Bool8 m_isJumping;
  67. NewtonCollision* m_castingShape;
  68. NewtonCollision* m_supportShape;
  69. NewtonCollision* m_upperBodyShape;
  70. NewtonBody* m_body;
  71. // State
  72. F32 m_forwardSpeed = 0.0;
  73. F32 m_strafeSpeed = 0.0;
  74. F32 m_jumpSpeed = 0.0;
  75. Vec4 m_forwardDir = Vec4(0.0, 0.0, -1.0, 0.0);
  76. Vec4 m_gravity;
  77. // Motion state
  78. Bool8 m_updated = true;
  79. Transform m_trf = {Transform::getIdentity()};
  80. Mat4 m_prevTrf = {Mat4::getIdentity()};
  81. static constexpr F32 MIN_RESTRAINING_DISTANCE = 1.0e-2;
  82. static constexpr U DESCRETE_MOTION_STEPS = 8;
  83. static constexpr U MAX_CONTACTS = 32;
  84. static constexpr U MAX_INTERGRATION_STEPS = 8;
  85. static constexpr F32 CONTACT_SKIN_THICKNESS = 0.025;
  86. static constexpr U MAX_SOLVER_ITERATIONS = 16;
  87. void setClimbSlope(F32 ang)
  88. {
  89. ANKI_ASSERT(ang >= 0.0);
  90. m_maxSlope = cos(ang);
  91. }
  92. Vec4 calculateDesiredOmega(const Vec4& headingAngle, F32 dt) const;
  93. Vec4 calculateDesiredVelocity(F32 forwardSpeed, F32 strafeSpeed,
  94. F32 verticalSpeed, const Vec4& gravity, F32 dt) const;
  95. void calculateVelocity(F32 dt);
  96. F32 calculateContactKinematics(const Vec4& veloc,
  97. const NewtonWorldConvexCastReturnInfo* contactInfo) const;
  98. void updateGroundPlane(Mat4& matrix, const Mat4& castMatrix,
  99. const Vec4& dst, int threadIndex);
  100. void postUpdate(F32 dt, int threadIndex);
  101. static void onTransformCallback(
  102. const NewtonBody* const body,
  103. const dFloat* const matrix,
  104. int threadIndex);
  105. void onTransform(Mat4 matrix);
  106. };
  107. /// @}
  108. } // end namespace anki
  109. #endif