CharacterBaseTest.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Tests/Test.h>
  6. #include <Jolt/Physics/Character/Character.h>
  7. #include <Jolt/Physics/Character/CharacterVirtual.h>
  8. // Base class for the character tests, initializes the test scene.
  9. class CharacterBaseTest : public Test
  10. {
  11. public:
  12. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, CharacterBaseTest)
  13. // Destructor
  14. virtual ~CharacterBaseTest() override;
  15. // Number used to scale the terrain and camera movement to the scene
  16. virtual float GetWorldScale() const override { return 0.2f; }
  17. // Initialize the test
  18. virtual void Initialize() override;
  19. // Process input
  20. virtual void ProcessInput(const ProcessInputParams &inParams) override;
  21. // Update the test, called before the physics update
  22. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  23. // Override to specify the initial camera state (local to GetCameraPivot)
  24. virtual void GetInitialCamera(CameraState &ioState) const override;
  25. // Override to specify a camera pivot point and orientation (world space)
  26. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const override;
  27. // Optional settings menu
  28. virtual bool HasSettingsMenu() const override { return true; }
  29. virtual void CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu) override;
  30. // Saving / restoring state for replay
  31. virtual void SaveState(StateRecorder &inStream) const override;
  32. virtual void RestoreState(StateRecorder &inStream) override;
  33. // Saving / restoring controller input state for replay
  34. virtual void SaveInputState(StateRecorder &inStream) const override;
  35. virtual void RestoreInputState(StateRecorder &inStream) override;
  36. protected:
  37. // Get position of the character
  38. virtual RVec3 GetCharacterPosition() const = 0;
  39. // Handle user input to the character
  40. virtual void HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime) = 0;
  41. // Draw the character state
  42. void DrawCharacterState(const CharacterBase *inCharacter, RMat44Arg inCharacterTransform, Vec3Arg inCharacterVelocity);
  43. // Add character movement settings
  44. virtual void AddCharacterMovementSettings(DebugUI* inUI, UIElement* inSubMenu) { /* Nothing by default */ }
  45. // Add test configuration settings
  46. virtual void AddConfigurationSettings(DebugUI *inUI, UIElement *inSubMenu) { /* Nothing by default */ }
  47. // Character size
  48. static constexpr float cCharacterHeightStanding = 1.35f;
  49. static constexpr float cCharacterRadiusStanding = 0.3f;
  50. static constexpr float cCharacterHeightCrouching = 0.8f;
  51. static constexpr float cCharacterRadiusCrouching = 0.3f;
  52. static constexpr float cInnerShapeFraction = 0.9f;
  53. // Character movement properties
  54. inline static bool sControlMovementDuringJump = true; ///< If false the character cannot change movement direction in mid air
  55. inline static float sCharacterSpeed = 6.0f;
  56. inline static float sJumpSpeed = 4.0f;
  57. // The different stances for the character
  58. RefConst<Shape> mStandingShape;
  59. RefConst<Shape> mCrouchingShape;
  60. RefConst<Shape> mInnerCrouchingShape;
  61. RefConst<Shape> mInnerStandingShape;
  62. // List of boxes on ramp
  63. Array<BodyID> mRampBlocks;
  64. float mRampBlocksTimeLeft = 0.0f;
  65. // Conveyor belt body
  66. BodyID mConveyorBeltBody;
  67. // Sensor body
  68. BodyID mSensorBody;
  69. // List of active characters in the scene so they can collide
  70. CharacterVsCharacterCollisionSimple mCharacterVsCharacterCollision;
  71. private:
  72. // Shape types
  73. enum class EType
  74. {
  75. Capsule,
  76. Cylinder,
  77. Box
  78. };
  79. // Character shape type
  80. static inline EType sShapeType = EType::Capsule;
  81. // List of possible scene names
  82. static const char * sScenes[];
  83. // Filename of animation to load for this test
  84. static const char * sSceneName;
  85. // Scene time (for moving bodies)
  86. float mTime = 0.0f;
  87. // The camera pivot, recorded before the physics update to align with the drawn world
  88. RVec3 mCameraPivot = RVec3::sZero();
  89. // Moving bodies
  90. BodyID mRotatingBody;
  91. BodyID mRotatingWallBody;
  92. BodyID mRotatingAndTranslatingBody;
  93. BodyID mSmoothVerticallyMovingBody;
  94. BodyID mReversingVerticallyMovingBody;
  95. float mReversingVerticallyMovingVelocity = 1.0f;
  96. BodyID mHorizontallyMovingBody;
  97. // Moving characters
  98. Ref<Character> mAnimatedCharacter;
  99. Ref<CharacterVirtual> mAnimatedCharacterVirtual;
  100. Ref<CharacterVirtual> mAnimatedCharacterVirtualWithInnerBody;
  101. // Player input
  102. Vec3 mControlInput = Vec3::sZero();
  103. bool mJump = false;
  104. bool mWasJump = false;
  105. bool mSwitchStance = false;
  106. bool mWasSwitchStance = false;
  107. };