Test.h 4.8 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 <Jolt/Physics/PhysicsSystem.h>
  6. #include <Renderer/Renderer.h>
  7. #include <Input/Keyboard.h>
  8. #include <Jolt/Skeleton/SkeletonPose.h>
  9. #include <Jolt/Core/RTTI.h>
  10. class DebugUI;
  11. class UIElement;
  12. namespace JPH {
  13. class StateRecorder;
  14. class JobSystem;
  15. class ContactListener;
  16. class DebugRenderer;
  17. }
  18. class Test
  19. {
  20. public:
  21. JPH_DECLARE_RTTI_VIRTUAL_BASE(JPH_NO_EXPORT, Test)
  22. // Destructor
  23. virtual ~Test() = default;
  24. // Set the physics system
  25. virtual void SetPhysicsSystem(PhysicsSystem *inPhysicsSystem) { mPhysicsSystem = inPhysicsSystem; mBodyInterface = &inPhysicsSystem->GetBodyInterface(); }
  26. // Set the job system
  27. void SetJobSystem(JobSystem *inJobSystem) { mJobSystem = inJobSystem; }
  28. // Set the debug renderer
  29. void SetDebugRenderer(DebugRenderer *inDebugRenderer) { mDebugRenderer = inDebugRenderer; }
  30. // Set the temp allocator
  31. void SetTempAllocator(TempAllocator *inTempAllocator) { mTempAllocator = inTempAllocator; }
  32. // Description of the test
  33. virtual const char *GetDescription() const { return nullptr; }
  34. // Initialize the test
  35. virtual void Initialize() { }
  36. // Number used to scale the terrain and camera movement to the scene
  37. virtual float GetWorldScale() const { return 1.0f; }
  38. // If this test implements a contact listener, it should be returned here
  39. virtual ContactListener *GetContactListener() { return nullptr; }
  40. class ProcessInputParams
  41. {
  42. public:
  43. float mDeltaTime;
  44. Keyboard * mKeyboard;
  45. CameraState mCameraState;
  46. };
  47. // Process input, this is called before SaveInputState is called. This allows you to determine the player input and adjust internal state accordingly.
  48. // This state should not be applied until PrePhysicsUpdate because on replay you will receive a call to RestoreInputState to restore the stored player input state before receiving another PrePhysicsUpdate.
  49. virtual void ProcessInput(const ProcessInputParams &inParams) { }
  50. class PreUpdateParams
  51. {
  52. public:
  53. float mDeltaTime;
  54. CameraState mCameraState;
  55. #ifdef JPH_DEBUG_RENDERER
  56. const SkeletonPose::DrawSettings * mPoseDrawSettings;
  57. #endif // JPH_DEBUG_RENDERER
  58. };
  59. // Update the test, called before the physics update
  60. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) { }
  61. // Update the test, called after the physics update
  62. virtual void PostPhysicsUpdate(float inDeltaTime) { }
  63. // Override to specify the initial camera state (local to GetCameraPivot)
  64. virtual void GetInitialCamera(CameraState &ioState) const { }
  65. // Override to specify a camera pivot point and orientation (world space)
  66. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const { return RMat44::sIdentity(); }
  67. // Offset around which to center drawing. This floating point accuracy issues when the camera is far from the origin.
  68. virtual RVec3 GetDrawOffset() const { return RVec3::sZero(); }
  69. // Optional settings menu
  70. virtual bool HasSettingsMenu() const { return false; }
  71. virtual void CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu) { }
  72. // Force the application to restart the test
  73. void RestartTest() { mNeedsRestart = true; }
  74. bool NeedsRestart() const { return mNeedsRestart; }
  75. // If this test is supposed to be deterministic
  76. virtual bool IsDeterministic() const { return true; }
  77. // Saving / restoring state for replay
  78. virtual void SaveState(StateRecorder &inStream) const { }
  79. virtual void RestoreState(StateRecorder &inStream) { }
  80. // Saving / restoring controller input state for replay
  81. virtual void SaveInputState(StateRecorder &inStream) const { }
  82. virtual void RestoreInputState(StateRecorder &inStream) { }
  83. // Return a string that is displayed in the top left corner of the screen
  84. virtual String GetStatusString() const { return String(); }
  85. // Draw the body labels
  86. void DrawBodyLabels();
  87. protected:
  88. // Utility function to create a static floor body
  89. Body & CreateFloor(float inSize = 200.0f);
  90. // Utility function to create a floor consisting of very large triangles
  91. Body & CreateLargeTriangleFloor();
  92. // Create an uneven terrain floor body
  93. Body & CreateMeshTerrain();
  94. Body & CreateHeightFieldTerrain();
  95. // Add a label to a body
  96. void SetBodyLabel(const BodyID &inBodyID, const String &inLabel) { mBodyLabels[inBodyID] = inLabel; }
  97. JobSystem * mJobSystem = nullptr;
  98. PhysicsSystem * mPhysicsSystem = nullptr;
  99. BodyInterface * mBodyInterface = nullptr;
  100. DebugRenderer * mDebugRenderer = nullptr;
  101. TempAllocator * mTempAllocator = nullptr;
  102. private:
  103. bool mNeedsRestart = false;
  104. using BodyLabels = unordered_map<BodyID, String>;
  105. BodyLabels mBodyLabels;
  106. };