PhysicsPlayerController.cpp 5.4 KB

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