CharacterVirtualTest.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Character/CharacterVirtualTest.h>
  5. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  6. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  7. #include <Layers.h>
  8. #include <Renderer/DebugRendererImp.h>
  9. #include <Application/DebugUI.h>
  10. JPH_IMPLEMENT_RTTI_VIRTUAL(CharacterVirtualTest)
  11. {
  12. JPH_ADD_BASE_CLASS(CharacterVirtualTest, CharacterBaseTest)
  13. }
  14. void CharacterVirtualTest::Initialize()
  15. {
  16. CharacterBaseTest::Initialize();
  17. // Create 'player' character
  18. Ref<CharacterVirtualSettings> settings = new CharacterVirtualSettings();
  19. settings->mMaxSlopeAngle = sMaxSlopeAngle;
  20. settings->mMaxStrength = sMaxStrength;
  21. settings->mShape = mStandingShape;
  22. settings->mCharacterPadding = sCharacterPadding;
  23. settings->mPenetrationRecoverySpeed = sPenetrationRecoverySpeed;
  24. settings->mPredictiveContactDistance = sPredictiveContactDistance;
  25. mCharacter = new CharacterVirtual(settings, Vec3::sZero(), Quat::sIdentity(), mPhysicsSystem);
  26. mCharacter->SetListener(this);
  27. }
  28. void CharacterVirtualTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  29. {
  30. CharacterBaseTest::PrePhysicsUpdate(inParams);
  31. // Draw character pre update (the sim is also drawn pre update)
  32. Mat44 com = mCharacter->GetCenterOfMassTransform();
  33. if (mCharacter->GetShape() == mStandingShape)
  34. {
  35. mDebugRenderer->DrawCapsule(com, 0.5f * cCharacterHeightStanding, cCharacterRadiusStanding, Color::sGreen, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  36. mDebugRenderer->DrawCapsule(com, 0.5f * cCharacterHeightStanding, cCharacterRadiusStanding + mCharacter->GetCharacterPadding(), Color::sGrey, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  37. }
  38. else
  39. {
  40. mDebugRenderer->DrawCapsule(com, 0.5f * cCharacterHeightCrouching, cCharacterRadiusCrouching, Color::sGreen, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  41. mDebugRenderer->DrawCapsule(com, 0.5f * cCharacterHeightCrouching, cCharacterRadiusCrouching + mCharacter->GetCharacterPadding(), Color::sGrey, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  42. }
  43. // Remember old position
  44. Vec3 old_position = mCharacter->GetPosition();
  45. // Update the character position (instant, do not have to wait for physics update)
  46. mCharacter->Update(inParams.mDeltaTime, mPhysicsSystem->GetGravity(), mPhysicsSystem->GetDefaultBroadPhaseLayerFilter(Layers::MOVING), mPhysicsSystem->GetDefaultLayerFilter(Layers::MOVING), { }, *mTempAllocator);
  47. // Calculate effective velocity
  48. Vec3 new_position = mCharacter->GetPosition();
  49. Vec3 velocity = (new_position - old_position) / inParams.mDeltaTime;
  50. // Draw state of character
  51. DrawCharacterState(mCharacter, mCharacter->GetWorldTransform(), velocity);
  52. // Draw labels on ramp blocks
  53. for (size_t i = 0; i < mRampBlocks.size(); ++i)
  54. mDebugRenderer->DrawText3D(mBodyInterface->GetPosition(mRampBlocks[i]), StringFormat("PushesPlayer: %s\nPushable: %s", (i & 1) != 0? "True" : "False", (i & 2) != 0? "True" : "False"), Color::sWhite, 0.25f);
  55. }
  56. void CharacterVirtualTest::HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime)
  57. {
  58. // Cancel movement in opposite direction of normal when sliding
  59. CharacterVirtual::EGroundState ground_state = mCharacter->GetGroundState();
  60. if (ground_state == CharacterVirtual::EGroundState::Sliding)
  61. {
  62. Vec3 normal = mCharacter->GetGroundNormal();
  63. normal.SetY(0.0f);
  64. float dot = normal.Dot(inMovementDirection);
  65. if (dot < 0.0f)
  66. inMovementDirection -= (dot * normal) / normal.LengthSq();
  67. }
  68. // Smooth the player input
  69. mSmoothMovementDirection = 0.25f * inMovementDirection + 0.75f * mSmoothMovementDirection;
  70. Vec3 current_vertical_velocity = Vec3(0, mCharacter->GetLinearVelocity().GetY(), 0);
  71. Vec3 ground_velocity = mCharacter->GetGroundVelocity();
  72. Vec3 new_velocity;
  73. if (ground_state == CharacterVirtual::EGroundState::OnGround // If on ground
  74. && (current_vertical_velocity.GetY() - ground_velocity.GetY()) < 0.1f) // And not moving away from ground
  75. {
  76. // Assume velocity of ground when on ground
  77. new_velocity = ground_velocity;
  78. // Jump
  79. if (inJump)
  80. new_velocity += Vec3(0, cJumpSpeed, 0);
  81. }
  82. else
  83. new_velocity = current_vertical_velocity;
  84. // Gravity
  85. new_velocity += mPhysicsSystem->GetGravity() * inDeltaTime;
  86. // Player input
  87. new_velocity += mSmoothMovementDirection * cCharacterSpeed;
  88. // Update the velocity
  89. mCharacter->SetLinearVelocity(new_velocity);
  90. // Stance switch
  91. if (inSwitchStance)
  92. mCharacter->SetShape(mCharacter->GetShape() == mStandingShape? mCrouchingShape : mStandingShape, 1.5f * mPhysicsSystem->GetPhysicsSettings().mPenetrationSlop, mPhysicsSystem->GetDefaultBroadPhaseLayerFilter(Layers::MOVING), mPhysicsSystem->GetDefaultLayerFilter(Layers::MOVING), { }, *mTempAllocator);
  93. }
  94. void CharacterVirtualTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  95. {
  96. CharacterBaseTest::CreateSettingsMenu(inUI, inSubMenu);
  97. inUI->CreateTextButton(inSubMenu, "Configuration Settings", [=]() {
  98. UIElement *configuration_settings = inUI->CreateMenu();
  99. inUI->CreateSlider(configuration_settings, "Max Slope Angle (degrees)", RadiansToDegrees(sMaxSlopeAngle), 0.0f, 90.0f, 1.0f, [=](float inValue) { sMaxSlopeAngle = DegreesToRadians(inValue); });
  100. inUI->CreateSlider(configuration_settings, "Max Strength (N)", sMaxStrength, 0.0f, 500.0f, 1.0f, [=](float inValue) { sMaxStrength = inValue; });
  101. inUI->CreateSlider(configuration_settings, "Character Padding", sCharacterPadding, 0.01f, 0.5f, 0.01f, [=](float inValue) { sCharacterPadding = inValue; });
  102. inUI->CreateSlider(configuration_settings, "Penetration Recovery Speed", sPenetrationRecoverySpeed, 0.0f, 1.0f, 0.05f, [=](float inValue) { sPenetrationRecoverySpeed = inValue; });
  103. inUI->CreateSlider(configuration_settings, "Predictive Contact Distance", sPredictiveContactDistance, 0.01f, 1.0f, 0.01f, [=](float inValue) { sPredictiveContactDistance = inValue; });
  104. inUI->CreateTextButton(configuration_settings, "Accept Changes", [=]() { RestartTest(); });
  105. inUI->ShowMenu(configuration_settings);
  106. });
  107. }
  108. void CharacterVirtualTest::OnContactAdded(const CharacterVirtual *inCharacter, const BodyID &inBodyID2, const SubShapeID &inSubShapeID2, Vec3Arg inContactPosition, Vec3Arg inContactNormal, CharacterContactSettings &ioSettings)
  109. {
  110. // Dynamic boxes on the ramp go through all permutations
  111. vector<BodyID>::const_iterator i = find(mRampBlocks.begin(), mRampBlocks.end(), inBodyID2);
  112. if (i != mRampBlocks.end())
  113. {
  114. size_t index = i - mRampBlocks.begin();
  115. ioSettings.mCanPushCharacter = (index & 1) != 0;
  116. ioSettings.mCanReceiveImpulses = (index & 2) != 0;
  117. }
  118. }