PhysicsPlayerController.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include <AnKi/Physics2/PhysicsPlayerController.h>
  6. #include <AnKi/Physics2/PhysicsWorld.h>
  7. #include <AnKi/Physics2/PhysicsCollisionShape.h>
  8. namespace anki {
  9. namespace v2 {
  10. thread_local static StaticTempAllocator<1_MB> g_tempAllocator;
  11. PhysicsPlayerController::~PhysicsPlayerController()
  12. {
  13. m_jphCharacter.destroy();
  14. }
  15. void PhysicsPlayerController::init(const PhysicsPlayerControllerInitInfo& init)
  16. {
  17. m_standingShape = PhysicsWorld::getSingleton().newCapsuleCollisionShape(init.m_standingHeight, init.m_waistWidth);
  18. m_crouchingShape = PhysicsWorld::getSingleton().newCapsuleCollisionShape(init.m_crouchingHeight, init.m_waistWidth);
  19. m_innerStandingShape =
  20. PhysicsWorld::getSingleton().newCapsuleCollisionShape(init.m_standingHeight * kInnerShapeFraction, init.m_waistWidth * kInnerShapeFraction);
  21. m_innerCrouchingShape =
  22. PhysicsWorld::getSingleton().newCapsuleCollisionShape(init.m_crouchingHeight * kInnerShapeFraction, init.m_waistWidth * kInnerShapeFraction);
  23. JPH::CharacterVirtualSettings settings;
  24. settings.SetEmbedded();
  25. settings.mMaxSlopeAngle = kMaxSlopeAngle;
  26. settings.mMaxStrength = kMaxStrength;
  27. settings.mShape = &m_standingShape->m_capsule;
  28. settings.mBackFaceMode = kBackFaceMode;
  29. settings.mCharacterPadding = kCharacterPadding;
  30. settings.mPenetrationRecoverySpeed = kPenetrationRecoverySpeed;
  31. settings.mPredictiveContactDistance = kPredictiveContactDistance;
  32. settings.mSupportingVolume = JPH::Plane(JPH::Vec3::sAxisY(), -init.m_waistWidth); // Accept contacts that touch the lower sphere of the capsule
  33. settings.mEnhancedInternalEdgeRemoval = kEnhancedInternalEdgeRemoval;
  34. settings.mInnerBodyShape = &m_innerStandingShape->m_capsule;
  35. settings.mInnerBodyLayer = JPH::ObjectLayer(PhysicsLayer::kPlayerController);
  36. m_jphCharacter.construct(&settings, toJPH(init.m_initialPosition), JPH::Quat::sIdentity(), ptrToNumber(static_cast<PhysicsObjectBase*>(this)),
  37. &PhysicsWorld::getSingleton().m_jphPhysicsSystem);
  38. m_position = init.m_initialPosition;
  39. setUserData(init.m_userData);
  40. }
  41. void PhysicsPlayerController::prePhysicsUpdate(Second dt)
  42. {
  43. if(!m_input.m_updated)
  44. {
  45. return;
  46. }
  47. m_input.m_updated = false;
  48. const Bool bJump = m_input.m_jumpSpeed > 0.0f;
  49. const JPH::Vec3 inMovementDirection = toJPH(m_input.m_forwardDir);
  50. const Bool playerControlsHorizontalVelocity = m_controlMovementDuringJump || m_jphCharacter->IsSupported();
  51. if(playerControlsHorizontalVelocity)
  52. {
  53. // Smooth the player input
  54. m_desiredVelocity = kEnableCharacterInertia ? 0.25f * inMovementDirection * m_input.m_forwardSpeed + 0.75f * m_desiredVelocity
  55. : inMovementDirection * m_input.m_forwardSpeed;
  56. // True if the player intended to move
  57. m_allowSliding = !inMovementDirection.IsNearZero();
  58. }
  59. else
  60. {
  61. // While in air we allow sliding
  62. m_allowSliding = true;
  63. }
  64. // Update the character rotation and its up vector to match the up vector set by the user settings
  65. const JPH::Quat characterUpRotation = JPH::Quat::sEulerAngles(JPH::Vec3(0.0f, 0.0f, 0.0f));
  66. m_jphCharacter->SetUp(characterUpRotation.RotateAxisY());
  67. m_jphCharacter->SetRotation(characterUpRotation);
  68. // A cheaper way to update the character's ground velocity,
  69. // the platforms that the character is standing on may have changed velocity
  70. m_jphCharacter->UpdateGroundVelocity();
  71. // Determine new basic velocity
  72. const JPH::Vec3 currentVerticalVelocity = m_jphCharacter->GetLinearVelocity().Dot(m_jphCharacter->GetUp()) * m_jphCharacter->GetUp();
  73. const JPH::Vec3 groundVelocity = m_jphCharacter->GetGroundVelocity();
  74. JPH::Vec3 newVelocity;
  75. bool movingTowardsGround = (currentVerticalVelocity.GetY() - groundVelocity.GetY()) < 0.1f;
  76. if(m_jphCharacter->GetGroundState() == JPH::CharacterVirtual::EGroundState::OnGround // If on ground
  77. && (kEnableCharacterInertia
  78. ? movingTowardsGround // Inertia enabled: And not moving away from ground
  79. : !m_jphCharacter->IsSlopeTooSteep(m_jphCharacter->GetGroundNormal()))) // Inertia disabled: And not on a slope that is too steep
  80. {
  81. // Assume velocity of ground when on ground
  82. newVelocity = groundVelocity;
  83. // Jump
  84. if(bJump && movingTowardsGround)
  85. {
  86. newVelocity += m_input.m_jumpSpeed * m_jphCharacter->GetUp();
  87. }
  88. }
  89. else
  90. {
  91. newVelocity = currentVerticalVelocity;
  92. }
  93. // Gravity
  94. const auto& physicsSystem = *PhysicsWorld::getSingleton().m_jphPhysicsSystem;
  95. newVelocity += (characterUpRotation * physicsSystem.GetGravity()) * F32(dt);
  96. if(playerControlsHorizontalVelocity)
  97. {
  98. // Player input
  99. newVelocity += characterUpRotation * m_desiredVelocity;
  100. }
  101. else
  102. {
  103. // Preserve horizontal velocity
  104. const JPH::Vec3 currentHorizontalVelocity = m_jphCharacter->GetLinearVelocity() - currentVerticalVelocity;
  105. newVelocity += currentHorizontalVelocity;
  106. }
  107. // Update character velocity
  108. m_jphCharacter->SetLinearVelocity(newVelocity);
  109. // Stance switch
  110. if(m_crouching != m_input.m_crouching)
  111. {
  112. m_crouching = m_input.m_crouching;
  113. const Bool isStanding = m_jphCharacter->GetShape() == &m_standingShape->m_shapeBase;
  114. const JPH::Shape* shape = (isStanding) ? &m_crouchingShape->m_shapeBase : &m_standingShape->m_shapeBase;
  115. if(m_jphCharacter->SetShape(shape, 1.5f * physicsSystem.GetPhysicsSettings().mPenetrationSlop,
  116. physicsSystem.GetDefaultBroadPhaseLayerFilter(JPH::ObjectLayer(PhysicsLayer::kPlayerController)),
  117. physicsSystem.GetDefaultLayerFilter(JPH::ObjectLayer(PhysicsLayer::kPlayerController)), {}, {}, g_tempAllocator))
  118. {
  119. const JPH::Shape* innerShape = (isStanding) ? &m_innerCrouchingShape->m_capsule : &m_innerCrouchingShape->m_capsule;
  120. m_jphCharacter->SetInnerBodyShape(innerShape);
  121. }
  122. }
  123. }
  124. void PhysicsPlayerController::postPhysicsUpdate()
  125. {
  126. const Vec3 newPos = toAnKi(m_jphCharacter->GetPosition());
  127. if(newPos != m_position)
  128. {
  129. m_position = newPos;
  130. ++m_positionVersion;
  131. }
  132. }
  133. } // namespace v2
  134. } // namespace anki