CharacterBaseTest.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // Draw the character + padding
  48. void DrawPaddedCharacter(const Shape *inShape, float inPadding, RMat44Arg inCenterOfMass);
  49. // Character size
  50. static constexpr float cCharacterHeightStanding = 1.35f;
  51. static constexpr float cCharacterRadiusStanding = 0.3f;
  52. static constexpr float cCharacterHeightCrouching = 0.8f;
  53. static constexpr float cCharacterRadiusCrouching = 0.3f;
  54. static constexpr float cInnerShapeFraction = 0.9f;
  55. // Character movement properties
  56. inline static bool sControlMovementDuringJump = true; ///< If false the character cannot change movement direction in mid air
  57. inline static float sCharacterSpeed = 6.0f;
  58. inline static float sJumpSpeed = 4.0f;
  59. // The different stances for the character
  60. RefConst<Shape> mStandingShape;
  61. RefConst<Shape> mCrouchingShape;
  62. RefConst<Shape> mInnerCrouchingShape;
  63. RefConst<Shape> mInnerStandingShape;
  64. // List of boxes on ramp
  65. Array<BodyID> mRampBlocks;
  66. float mRampBlocksTimeLeft = 0.0f;
  67. // Conveyor belt body
  68. BodyID mConveyorBeltBody;
  69. // Sensor body
  70. BodyID mSensorBody;
  71. // List of active characters in the scene so they can collide
  72. CharacterVsCharacterCollisionSimple mCharacterVsCharacterCollision;
  73. // Shape types
  74. enum class EType
  75. {
  76. Capsule,
  77. Cylinder,
  78. Box,
  79. Compound
  80. };
  81. // Character shape type
  82. static inline EType sShapeType = EType::Capsule;
  83. private:
  84. // List of possible scene names
  85. static const char * sScenes[];
  86. // Filename of animation to load for this test
  87. static const char * sSceneName;
  88. // Scene time (for moving bodies)
  89. float mTime = 0.0f;
  90. // The camera pivot, recorded before the physics update to align with the drawn world
  91. RVec3 mCameraPivot = RVec3::sZero();
  92. // Moving bodies
  93. BodyID mRotatingBody;
  94. BodyID mRotatingWallBody;
  95. BodyID mRotatingAndTranslatingBody;
  96. BodyID mSmoothVerticallyMovingBody;
  97. BodyID mReversingVerticallyMovingBody;
  98. float mReversingVerticallyMovingVelocity = 1.0f;
  99. BodyID mHorizontallyMovingBody;
  100. // Moving characters
  101. Ref<Character> mAnimatedCharacter;
  102. Ref<CharacterVirtual> mAnimatedCharacterVirtual;
  103. Ref<CharacterVirtual> mAnimatedCharacterVirtualWithInnerBody;
  104. // Player input
  105. Vec3 mControlInput = Vec3::sZero();
  106. bool mJump = false;
  107. bool mWasJump = false;
  108. bool mSwitchStance = false;
  109. bool mWasSwitchStance = false;
  110. };