CharacterBaseTest.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Tests/Test.h>
  5. #include <Jolt/Physics/Character/CharacterBase.h>
  6. // Base class for the character tests, initializes the test scene.
  7. class CharacterBaseTest : public Test
  8. {
  9. public:
  10. JPH_DECLARE_RTTI_VIRTUAL(CharacterBaseTest)
  11. // Number used to scale the terrain and camera movement to the scene
  12. virtual float GetWorldScale() const override { return 0.2f; }
  13. // Initialize the test
  14. virtual void Initialize() override;
  15. // Update the test, called before the physics update
  16. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  17. // Override to specify the initial camera state (local to GetCameraPivot)
  18. virtual void GetInitialCamera(CameraState &ioState) const override;
  19. // Override to specify a camera pivot point and orientation (world space)
  20. virtual Mat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const override;
  21. // Optional settings menu
  22. virtual bool HasSettingsMenu() const override { return true; }
  23. virtual void CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu) override;
  24. // Saving / restoring state for replay
  25. virtual void SaveState(StateRecorder &inStream) const override;
  26. virtual void RestoreState(StateRecorder &inStream) override;
  27. protected:
  28. // Get position of the character
  29. virtual Vec3 GetCharacterPosition() const = 0;
  30. // Handle user input to the character
  31. virtual void HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime) = 0;
  32. // Draw the character state
  33. void DrawCharacterState(const CharacterBase *inCharacter, Mat44Arg inCharacterTransform, Vec3Arg inCharacterVelocity);
  34. // Add test configuration settings
  35. virtual void AddConfigurationSettings(DebugUI *inUI, UIElement *inSubMenu) { /* Nothing by default */ }
  36. // Character size
  37. static constexpr float cCharacterHeightStanding = 1.35f;
  38. static constexpr float cCharacterRadiusStanding = 0.3f;
  39. static constexpr float cCharacterHeightCrouching = 0.8f;
  40. static constexpr float cCharacterRadiusCrouching = 0.3f;
  41. static constexpr float cCharacterSpeed = 6.0f;
  42. static constexpr float cJumpSpeed = 4.0f;
  43. // The different stances for the character
  44. RefConst<Shape> mStandingShape;
  45. RefConst<Shape> mCrouchingShape;
  46. // List of boxes on ramp
  47. Array<BodyID> mRampBlocks;
  48. float mRampBlocksTimeLeft = 0.0f;
  49. private:
  50. // Shape types
  51. enum class EType
  52. {
  53. Capsule,
  54. Cylinder,
  55. Box
  56. };
  57. // Character shape type
  58. static inline EType sShapeType = EType::Capsule;
  59. // List of possible scene names
  60. static const char * sScenes[];
  61. // Filename of animation to load for this test
  62. static const char * sSceneName;
  63. // Scene time (for moving bodies)
  64. float mTime = 0.0f;
  65. // Moving bodies
  66. BodyID mRotatingBody;
  67. BodyID mVerticallyMovingBody;
  68. BodyID mHorizontallyMovingBody;
  69. };