CharacterTest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/Character/CharacterTest.h>
  6. #include <Layers.h>
  7. #include <Renderer/DebugRendererImp.h>
  8. JPH_IMPLEMENT_RTTI_VIRTUAL(CharacterTest)
  9. {
  10. JPH_ADD_BASE_CLASS(CharacterTest, CharacterBaseTest)
  11. }
  12. static const float cCollisionTolerance = 0.05f;
  13. CharacterTest::~CharacterTest()
  14. {
  15. mCharacter->RemoveFromPhysicsSystem();
  16. }
  17. void CharacterTest::Initialize()
  18. {
  19. CharacterBaseTest::Initialize();
  20. // Create 'player' character
  21. Ref<CharacterSettings> settings = new CharacterSettings();
  22. settings->mMaxSlopeAngle = DegreesToRadians(45.0f);
  23. settings->mLayer = Layers::MOVING;
  24. settings->mShape = mStandingShape;
  25. settings->mFriction = 0.5f;
  26. settings->mSupportingVolume = Plane(Vec3::sAxisY(), -cCharacterRadiusStanding); // Accept contacts that touch the lower sphere of the capsule
  27. mCharacter = new Character(settings, RVec3::sZero(), Quat::sIdentity(), 0, mPhysicsSystem);
  28. mCharacter->AddToPhysicsSystem(EActivation::Activate);
  29. }
  30. void CharacterTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  31. {
  32. CharacterBaseTest::PrePhysicsUpdate(inParams);
  33. // Draw state of character
  34. DrawCharacterState(mCharacter, mCharacter->GetWorldTransform(), mCharacter->GetLinearVelocity());
  35. }
  36. void CharacterTest::PostPhysicsUpdate(float inDeltaTime)
  37. {
  38. // Fetch the new ground properties
  39. mCharacter->PostSimulation(cCollisionTolerance);
  40. }
  41. void CharacterTest::SaveState(StateRecorder &inStream) const
  42. {
  43. CharacterBaseTest::SaveState(inStream);
  44. mCharacter->SaveState(inStream);
  45. bool is_standing = mCharacter->GetShape() == mStandingShape;
  46. inStream.Write(is_standing);
  47. }
  48. void CharacterTest::RestoreState(StateRecorder &inStream)
  49. {
  50. CharacterBaseTest::RestoreState(inStream);
  51. mCharacter->RestoreState(inStream);
  52. bool is_standing = mCharacter->GetShape() == mStandingShape; // Initialize variable for validation mode
  53. inStream.Read(is_standing);
  54. mCharacter->SetShape(is_standing? mStandingShape : mCrouchingShape, FLT_MAX);
  55. }
  56. void CharacterTest::HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime)
  57. {
  58. // Cancel movement in opposite direction of normal when touching something we can't walk up
  59. Vec3 movement_direction = inMovementDirection;
  60. Character::EGroundState ground_state = mCharacter->GetGroundState();
  61. if (ground_state == Character::EGroundState::OnSteepGround
  62. || ground_state == Character::EGroundState::NotSupported)
  63. {
  64. Vec3 normal = mCharacter->GetGroundNormal();
  65. normal.SetY(0.0f);
  66. float dot = normal.Dot(movement_direction);
  67. if (dot < 0.0f)
  68. movement_direction -= (dot * normal) / normal.LengthSq();
  69. }
  70. // Stance switch
  71. if (inSwitchStance)
  72. mCharacter->SetShape(mCharacter->GetShape() == mStandingShape? mCrouchingShape : mStandingShape, 1.5f * mPhysicsSystem->GetPhysicsSettings().mPenetrationSlop);
  73. if (sControlMovementDuringJump || mCharacter->IsSupported())
  74. {
  75. // Update velocity
  76. Vec3 current_velocity = mCharacter->GetLinearVelocity();
  77. Vec3 desired_velocity = sCharacterSpeed * movement_direction;
  78. if (!desired_velocity.IsNearZero() || !mCharacter->IsSupported())
  79. desired_velocity.SetY(current_velocity.GetY());
  80. Vec3 new_velocity = 0.75f * current_velocity + 0.25f * desired_velocity;
  81. // Jump
  82. if (inJump && ground_state == Character::EGroundState::OnGround)
  83. new_velocity += Vec3(0, sJumpSpeed, 0);
  84. // Update the velocity
  85. mCharacter->SetLinearVelocity(new_velocity);
  86. }
  87. }
  88. void CharacterTest::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  89. {
  90. // Draw a box around the character when it enters the sensor
  91. if (inBody1.GetID() == mSensorBody)
  92. mDebugRenderer->DrawBox(inBody2.GetWorldSpaceBounds(), Color::sGreen, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  93. else if (inBody2.GetID() == mSensorBody)
  94. mDebugRenderer->DrawBox(inBody1.GetWorldSpaceBounds(), Color::sGreen, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  95. }
  96. void CharacterTest::OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  97. {
  98. // Same behavior as contact added
  99. OnContactAdded(inBody1, inBody2, inManifold, ioSettings);
  100. }