PhysicsPlayerController.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #pragma once
  6. #include "anki/physics/PhysicsObject.h"
  7. namespace anki {
  8. /// @addtogroup physics
  9. /// @{
  10. /// Initializer for PhysicsPlayerController.
  11. class PhysicsPlayerControllerInitializer
  12. {
  13. public:
  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. ~PhysicsPlayerController();
  30. ANKI_USE_RESULT Error create(const Initializer& init);
  31. // Update the state machine
  32. void setVelocity(F32 forwardSpeed, F32 strafeSpeed, F32 jumpSpeed,
  33. const Vec4& forwardDir)
  34. {
  35. m_forwardSpeed = forwardSpeed;
  36. m_strafeSpeed = strafeSpeed;
  37. m_jumpSpeed = jumpSpeed;
  38. m_forwardDir = forwardDir;
  39. }
  40. void moveToPosition(const Vec4& position);
  41. const Transform& getTransform(Bool& updated)
  42. {
  43. updated = m_updated;
  44. m_updated = false;
  45. return m_trf;
  46. }
  47. static Bool classof(const PhysicsObject& c)
  48. {
  49. return c.getType() == Type::PLAYER_CONTROLLER;
  50. }
  51. /// @privatesection
  52. /// @{
  53. /// Called by Newton thread to update the controller.
  54. static void postUpdateKernelCallback(
  55. NewtonWorld* const world,
  56. void* const context,
  57. int threadIndex);
  58. /// @}
  59. private:
  60. Vec4 m_upDir;
  61. Vec4 m_frontDir;
  62. Vec4 m_groundPlane;
  63. Vec4 m_groundVelocity;
  64. F32 m_innerRadius;
  65. F32 m_outerRadius;
  66. F32 m_height;
  67. F32 m_stepHeight;
  68. F32 m_maxSlope;
  69. F32 m_sphereCastOrigin;
  70. F32 m_restrainingDistance;
  71. Bool8 m_isJumping;
  72. NewtonBody* m_body;
  73. NewtonCollision* m_castingShape;
  74. NewtonCollision* m_supportShape;
  75. NewtonCollision* m_upperBodyShape;
  76. // State
  77. F32 m_forwardSpeed = 0.0;
  78. F32 m_strafeSpeed = 0.0;
  79. F32 m_jumpSpeed = 0.0;
  80. Vec4 m_forwardDir = Vec4(0.0, 0.0, -1.0, 0.0);
  81. Vec4 m_gravity;
  82. // Motion state
  83. Bool8 m_updated = true;
  84. Transform m_trf = {Transform::getIdentity()};
  85. Mat4 m_prevTrf = {Mat4::getIdentity()};
  86. static constexpr F32 MIN_RESTRAINING_DISTANCE = 1.0e-2;
  87. static constexpr U DESCRETE_MOTION_STEPS = 8;
  88. static constexpr U MAX_CONTACTS = 32;
  89. static constexpr U MAX_INTERGRATION_STEPS = 8;
  90. static constexpr F32 CONTACT_SKIN_THICKNESS = 0.025;
  91. static constexpr U MAX_SOLVER_ITERATIONS = 16;
  92. void setClimbSlope(F32 ang)
  93. {
  94. ANKI_ASSERT(ang >= 0.0);
  95. m_maxSlope = cos(ang);
  96. }
  97. Vec4 calculateDesiredOmega(const Vec4& headingAngle, F32 dt) const;
  98. Vec4 calculateDesiredVelocity(F32 forwardSpeed, F32 strafeSpeed,
  99. F32 verticalSpeed, const Vec4& gravity, F32 dt) const;
  100. void calculateVelocity(F32 dt);
  101. F32 calculateContactKinematics(const Vec4& veloc,
  102. const NewtonWorldConvexCastReturnInfo* contactInfo) const;
  103. void updateGroundPlane(Mat4& matrix, const Mat4& castMatrix,
  104. const Vec4& dst, int threadIndex);
  105. void postUpdate(F32 dt, int threadIndex);
  106. static void onTransformCallback(
  107. const NewtonBody* const body,
  108. const dFloat* const matrix,
  109. int threadIndex);
  110. void onTransform(Mat4 matrix);
  111. };
  112. /// @}
  113. } // end namespace anki