2
0

CharacterTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Character::EGroundState ground_state = mCharacter->GetGroundState();
  60. if (ground_state == Character::EGroundState::OnSteepGround
  61. || ground_state == Character::EGroundState::NotSupported)
  62. {
  63. Vec3 normal = mCharacter->GetGroundNormal();
  64. normal.SetY(0.0f);
  65. float dot = normal.Dot(inMovementDirection);
  66. if (dot < 0.0f)
  67. inMovementDirection -= (dot * normal) / normal.LengthSq();
  68. }
  69. // Stance switch
  70. if (inSwitchStance)
  71. mCharacter->SetShape(mCharacter->GetShape() == mStandingShape? mCrouchingShape : mStandingShape, 1.5f * mPhysicsSystem->GetPhysicsSettings().mPenetrationSlop);
  72. if (sControlMovementDuringJump || mCharacter->IsSupported())
  73. {
  74. // Update velocity
  75. Vec3 current_velocity = mCharacter->GetLinearVelocity();
  76. Vec3 desired_velocity = sCharacterSpeed * inMovementDirection;
  77. desired_velocity.SetY(current_velocity.GetY());
  78. Vec3 new_velocity = 0.75f * current_velocity + 0.25f * desired_velocity;
  79. // Jump
  80. if (inJump && ground_state == Character::EGroundState::OnGround)
  81. new_velocity += Vec3(0, sJumpSpeed, 0);
  82. // Update the velocity
  83. mCharacter->SetLinearVelocity(new_velocity);
  84. }
  85. }
  86. void CharacterTest::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  87. {
  88. // Draw a box around the character when it enters the sensor
  89. if (inBody1.GetID() == mSensorBody)
  90. mDebugRenderer->DrawBox(inBody2.GetWorldSpaceBounds(), Color::sGreen, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  91. else if (inBody2.GetID() == mSensorBody)
  92. mDebugRenderer->DrawBox(inBody1.GetWorldSpaceBounds(), Color::sGreen, DebugRenderer::ECastShadow::Off, DebugRenderer::EDrawMode::Wireframe);
  93. }
  94. void CharacterTest::OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  95. {
  96. // Same behavior as contact added
  97. OnContactAdded(inBody1, inBody2, inManifold, ioSettings);
  98. }