PhysicsPlayerController.cpp 5.9 KB

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