PhysicsPlayerController.cpp 6.9 KB

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