CharacterTest.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Character/CharacterTest.h>
  5. #include <Layers.h>
  6. #include <Renderer/DebugRendererImp.h>
  7. JPH_IMPLEMENT_RTTI_VIRTUAL(CharacterTest)
  8. {
  9. JPH_ADD_BASE_CLASS(CharacterTest, CharacterBaseTest)
  10. }
  11. static const float cCollisionTolerance = 0.05f;
  12. CharacterTest::~CharacterTest()
  13. {
  14. mCharacter->RemoveFromPhysicsSystem();
  15. }
  16. void CharacterTest::Initialize()
  17. {
  18. CharacterBaseTest::Initialize();
  19. // Create 'player' character
  20. Ref<CharacterSettings> settings = new CharacterSettings();
  21. settings->mMaxSlopeAngle = DegreesToRadians(45.0f);
  22. settings->mLayer = Layers::MOVING;
  23. settings->mShape = mStandingShape;
  24. settings->mFriction = 0.5f;
  25. settings->mSupportingVolume = Plane(Vec3::sAxisY(), -cCharacterRadiusStanding); // Accept contacts that touch the lower sphere of the capsule
  26. mCharacter = new Character(settings, Vec3::sZero(), Quat::sIdentity(), 0, mPhysicsSystem);
  27. mCharacter->AddToPhysicsSystem(EActivation::Activate);
  28. }
  29. void CharacterTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  30. {
  31. CharacterBaseTest::PrePhysicsUpdate(inParams);
  32. // Draw state of character
  33. DrawCharacterState(mCharacter, mCharacter->GetWorldTransform(), mCharacter->GetLinearVelocity());
  34. }
  35. void CharacterTest::PostPhysicsUpdate(float inDeltaTime)
  36. {
  37. // Fetch the new ground properties
  38. mCharacter->PostSimulation(cCollisionTolerance);
  39. }
  40. void CharacterTest::SaveState(StateRecorder &inStream) const
  41. {
  42. CharacterBaseTest::SaveState(inStream);
  43. mCharacter->SaveState(inStream);
  44. bool is_standing = mCharacter->GetShape() == mStandingShape;
  45. inStream.Write(is_standing);
  46. }
  47. void CharacterTest::RestoreState(StateRecorder &inStream)
  48. {
  49. CharacterBaseTest::RestoreState(inStream);
  50. mCharacter->RestoreState(inStream);
  51. bool is_standing = mCharacter->GetShape() == mStandingShape; // Initialize variable for validation mode
  52. inStream.Read(is_standing);
  53. mCharacter->SetShape(is_standing? mStandingShape : mCrouchingShape, FLT_MAX);
  54. }
  55. void CharacterTest::HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime)
  56. {
  57. // Cancel movement in opposite direction of normal when touching something we can't walk up
  58. Character::EGroundState ground_state = mCharacter->GetGroundState();
  59. if (ground_state == Character::EGroundState::OnSteepGround
  60. || ground_state == Character::EGroundState::NotSupported)
  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. // Update velocity
  69. Vec3 current_velocity = mCharacter->GetLinearVelocity();
  70. Vec3 desired_velocity = cCharacterSpeed * inMovementDirection;
  71. desired_velocity.SetY(current_velocity.GetY());
  72. Vec3 new_velocity = 0.75f * current_velocity + 0.25f * desired_velocity;
  73. // Stance switch
  74. if (inSwitchStance)
  75. mCharacter->SetShape(mCharacter->GetShape() == mStandingShape? mCrouchingShape : mStandingShape, 1.5f * mPhysicsSystem->GetPhysicsSettings().mPenetrationSlop);
  76. // Jump
  77. if (inJump && ground_state == Character::EGroundState::OnGround)
  78. new_velocity += Vec3(0, cJumpSpeed, 0);
  79. // Update the velocity
  80. mCharacter->SetLinearVelocity(new_velocity);
  81. }