Test.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // Initialize the test
  33. virtual void Initialize() { }
  34. // Number used to scale the terrain and camera movement to the scene
  35. virtual float GetWorldScale() const { return 1.0f; }
  36. // If this test implements a contact listener, it should be returned here
  37. virtual ContactListener *GetContactListener() { return nullptr; }
  38. class ProcessInputParams
  39. {
  40. public:
  41. float mDeltaTime;
  42. Keyboard * mKeyboard;
  43. CameraState mCameraState;
  44. };
  45. // Process input, this is called before SaveInputState is called. This allows you to determine the player input and adjust internal state accordingly.
  46. // 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.
  47. virtual void ProcessInput(const ProcessInputParams &inParams) { }
  48. class PreUpdateParams
  49. {
  50. public:
  51. float mDeltaTime;
  52. CameraState mCameraState;
  53. #ifdef JPH_DEBUG_RENDERER
  54. const SkeletonPose::DrawSettings * mPoseDrawSettings;
  55. #endif // JPH_DEBUG_RENDERER
  56. };
  57. // Update the test, called before the physics update
  58. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) { }
  59. // Update the test, called after the physics update
  60. virtual void PostPhysicsUpdate(float inDeltaTime) { }
  61. // Override to specify the initial camera state (local to GetCameraPivot)
  62. virtual void GetInitialCamera(CameraState &ioState) const { }
  63. // Override to specify a camera pivot point and orientation (world space)
  64. virtual RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const { return RMat44::sIdentity(); }
  65. // Offset around which to center drawing. This floating point accuracy issues when the camera is far from the origin.
  66. virtual RVec3 GetDrawOffset() const { return RVec3::sZero(); }
  67. // Optional settings menu
  68. virtual bool HasSettingsMenu() const { return false; }
  69. virtual void CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu) { }
  70. // Force the application to restart the test
  71. void RestartTest() { mNeedsRestart = true; }
  72. bool NeedsRestart() const { return mNeedsRestart; }
  73. // If this test is supposed to be deterministic
  74. virtual bool IsDeterministic() const { return true; }
  75. // Saving / restoring state for replay
  76. virtual void SaveState(StateRecorder &inStream) const { }
  77. virtual void RestoreState(StateRecorder &inStream) { }
  78. // Saving / restoring controller input state for replay
  79. virtual void SaveInputState(StateRecorder &inStream) const { }
  80. virtual void RestoreInputState(StateRecorder &inStream) { }
  81. // Return a string that is displayed in the top left corner of the screen
  82. virtual String GetStatusString() const { return String(); }
  83. protected:
  84. // Utility function to create a static floor body
  85. Body & CreateFloor(float inSize = 200.0f);
  86. // Utility function to create a floor consisting of very large triangles
  87. Body & CreateLargeTriangleFloor();
  88. // Create an uneven terrain floor body
  89. Body & CreateMeshTerrain();
  90. Body & CreateHeightFieldTerrain();
  91. JobSystem * mJobSystem = nullptr;
  92. PhysicsSystem * mPhysicsSystem = nullptr;
  93. BodyInterface * mBodyInterface = nullptr;
  94. DebugRenderer * mDebugRenderer = nullptr;
  95. TempAllocator * mTempAllocator = nullptr;
  96. private:
  97. bool mNeedsRestart = false;
  98. };