CharacterBaseTest.h 4.1 KB

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