CharacterTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. mCharacter = new Character(settings, Vec3::sZero(), Quat::sIdentity(), 0, mPhysicsSystem);
  26. mCharacter->AddToPhysicsSystem(EActivation::Activate);
  27. }
  28. void CharacterTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  29. {
  30. CharacterBaseTest::PrePhysicsUpdate(inParams);
  31. // Draw state of character
  32. DrawCharacterState(mCharacter, mCharacter->GetWorldTransform(), mCharacter->GetLinearVelocity());
  33. }
  34. void CharacterTest::PostPhysicsUpdate(float inDeltaTime)
  35. {
  36. // Fetch the new ground properties
  37. mCharacter->PostSimulation(cCollisionTolerance);
  38. }
  39. void CharacterTest::SaveState(StateRecorder &inStream) const
  40. {
  41. CharacterBaseTest::SaveState(inStream);
  42. mCharacter->SaveState(inStream);
  43. bool is_standing = mCharacter->GetShape() == mStandingShape;
  44. inStream.Write(is_standing);
  45. }
  46. void CharacterTest::RestoreState(StateRecorder &inStream)
  47. {
  48. CharacterBaseTest::RestoreState(inStream);
  49. mCharacter->RestoreState(inStream);
  50. bool is_standing = mCharacter->GetShape() == mStandingShape; // Initialize variable for validation mode
  51. inStream.Read(is_standing);
  52. mCharacter->SetShape(is_standing? mStandingShape : mCrouchingShape, FLT_MAX);
  53. }
  54. void CharacterTest::HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime)
  55. {
  56. // Cancel movement in opposite direction of normal when sliding
  57. Character::EGroundState ground_state = mCharacter->GetGroundState();
  58. if (ground_state == Character::EGroundState::OnSteepGround)
  59. {
  60. Vec3 normal = mCharacter->GetGroundNormal();
  61. normal.SetY(0.0f);
  62. float dot = normal.Dot(inMovementDirection);
  63. if (dot < 0.0f)
  64. inMovementDirection -= (dot * normal) / normal.LengthSq();
  65. }
  66. // Update velocity
  67. Vec3 current_velocity = mCharacter->GetLinearVelocity();
  68. Vec3 desired_velocity = cCharacterSpeed * inMovementDirection;
  69. desired_velocity.SetY(current_velocity.GetY());
  70. Vec3 new_velocity = 0.75f * current_velocity + 0.25f * desired_velocity;
  71. // Stance switch
  72. if (inSwitchStance)
  73. mCharacter->SetShape(mCharacter->GetShape() == mStandingShape? mCrouchingShape : mStandingShape, 1.5f * mPhysicsSystem->GetPhysicsSettings().mPenetrationSlop);
  74. // Jump
  75. if (inJump && ground_state == Character::EGroundState::OnGround)
  76. new_velocity += Vec3(0, cJumpSpeed, 0);
  77. // Update the velocity
  78. mCharacter->SetLinearVelocity(new_velocity);
  79. }