SamplesApp.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Application/Application.h>
  5. #include <UI/UIManager.h>
  6. #include <Application/DebugUI.h>
  7. #include <Jolt/Physics/Collision/CollideShape.h>
  8. #include <Jolt/Skeleton/SkeletonPose.h>
  9. #include <Tests/Test.h>
  10. #include <Utils/ContactListenerImpl.h>
  11. #include <Renderer/DebugRendererImp.h>
  12. #include <Jolt/Physics/StateRecorderImpl.h>
  13. #include <Layers.h>
  14. namespace JPH {
  15. class JobSystem;
  16. class TempAllocator;
  17. };
  18. // Application class that runs the samples
  19. class SamplesApp : public Application
  20. {
  21. public:
  22. // Constructor / destructor
  23. SamplesApp();
  24. virtual ~SamplesApp() override;
  25. // Render the frame.
  26. virtual bool RenderFrame(float inDeltaTime) override;
  27. // Override to specify the initial camera state (local to GetCameraPivot)
  28. virtual void GetInitialCamera(CameraState &ioState) const override;
  29. // Override to specify a camera pivot point and orientation (world space)
  30. virtual Mat44 GetCameraPivot(float inCameraHeading, float inCameraPitch) const override;
  31. // Get scale factor for this world, used to boost camera speed and to scale detail of the shadows
  32. virtual float GetWorldScale() const override;
  33. private:
  34. // Start running a new test
  35. void StartTest(const RTTI *inRTTI);
  36. // Run all tests one by one
  37. void RunAllTests();
  38. // Run the next test. Returns false when the application should exit.
  39. bool NextTest();
  40. // Check if we've got to start the next test. Returns false when the application should exit.
  41. bool CheckNextTest();
  42. // Probing the collision world
  43. RefConst<Shape> CreateProbeShape();
  44. bool CastProbe(float inProbeLength, float &outFraction, Vec3 &outPosition, BodyID &outID);
  45. // Shooting an object
  46. RefConst<Shape> CreateShootObjectShape();
  47. void ShootObject();
  48. // Debug functionality: firing a ball, mouse dragging
  49. void UpdateDebug();
  50. // Draw the state of the physics system
  51. void DrawPhysics();
  52. // Update the physics system with a fixed delta time
  53. void StepPhysics(JobSystem *inJobSystem);
  54. // Save state of simulation
  55. void SaveState(StateRecorderImpl &inStream);
  56. // Restore state of simulation
  57. void RestoreState(StateRecorderImpl &inStream);
  58. // Compare current physics state with inExpectedState
  59. void ValidateState(StateRecorderImpl &inExpectedState);
  60. // Global settings
  61. int mMaxConcurrentJobs = thread::hardware_concurrency(); // How many jobs to run in parallel
  62. float mUpdateFrequency = 60.0f; // Physics update frequency
  63. int mCollisionSteps = 1; // How many collision detection steps per physics update
  64. int mIntegrationSubSteps = 1; // How many integration steps per physics update
  65. TempAllocator * mTempAllocator = nullptr; // Allocator for temporary allocations
  66. JobSystem * mJobSystem = nullptr; // The job system that runs physics jobs
  67. JobSystem * mJobSystemValidating = nullptr; // The job system to use when validating determinism
  68. BPLayerInterfaceImpl mBroadPhaseLayerInterface; // The broadphase layer interface that maps object layers to broadphase layers
  69. PhysicsSystem * mPhysicsSystem = nullptr; // The physics system that simulates the world
  70. ContactListenerImpl * mContactListener = nullptr; // Contact listener implementation
  71. PhysicsSettings mPhysicsSettings; // Main physics simulation settings
  72. // Drawing settings
  73. #ifdef JPH_DEBUG_RENDERER
  74. bool mDrawGetTriangles = false; // Draw all shapes using Shape::GetTrianglesStart/Next
  75. bool mDrawConstraints = false; // If the constraints should be drawn
  76. bool mDrawConstraintLimits = false; // If the constraint limits should be drawn
  77. bool mDrawConstraintReferenceFrame = false; // If the constraint reference frames should be drawn
  78. BodyManager::DrawSettings mBodyDrawSettings; // Settings for how to draw bodies from the body manager
  79. SkeletonPose::DrawSettings mPoseDrawSettings; // Settings for drawing skeletal poses
  80. #endif // JPH_DEBUG_RENDERER
  81. // Drawing using GetTriangles interface
  82. using ShapeToGeometryMap = unordered_map<RefConst<Shape>, DebugRenderer::GeometryRef>;
  83. ShapeToGeometryMap mShapeToGeometry;
  84. // The test to run
  85. const RTTI * mTestClass = nullptr; // RTTI information for the test we're currently running
  86. Test * mTest = nullptr; // The test we're currently running
  87. UITextButton * mTestSettingsButton = nullptr; // Button that activates the menu that the test uses to configure additional settings
  88. // Automatic cycling through tests
  89. vector<const RTTI *> mTestsToRun; // The list of tests that are still waiting to be run
  90. float mTestTimeLeft = -1.0f; // How many seconds the test is still supposed to run
  91. bool mExitAfterRunningTests = false; // When true, the application will quit when mTestsToRun becomes empty
  92. UITextButton * mNextTestButton = nullptr; // Button that activates the next test when we're running all tests
  93. // Test settings
  94. bool mInstallContactListener = false; // When true, the contact listener is installed the next time the test is reset
  95. // State recording and determinism checks
  96. bool mRecordState = false; // When true, the state of the physics system is recorded in mPlaybackFrames every physics update
  97. bool mCheckDeterminism = false; // When true, the physics state is rolled back after every update and run again to verify that the state is the same
  98. vector<StateRecorderImpl> mPlaybackFrames; // A list of recorded world states, one per physics simulation step
  99. enum class EPlaybackMode
  100. {
  101. Rewind,
  102. StepBack,
  103. Stop,
  104. StepForward,
  105. FastForward,
  106. Play
  107. };
  108. EPlaybackMode mPlaybackMode = EPlaybackMode::Play; // Current playback state. Indicates if we're playing or scrubbing back/forward.
  109. int mCurrentPlaybackFrame = -1; // Current playback frame
  110. // Which mode the probe is operating in.
  111. enum class EProbeMode
  112. {
  113. Pick,
  114. Ray,
  115. RayCollector,
  116. CollidePoint,
  117. CollideShape,
  118. CastShape,
  119. TransformedShape,
  120. GetTriangles,
  121. BroadPhaseRay,
  122. BroadPhaseBox,
  123. BroadPhaseSphere,
  124. BroadPhasePoint,
  125. BroadPhaseOrientedBox,
  126. BroadPhaseCastBox,
  127. };
  128. // Which probe shape to use.
  129. enum class EProbeShape
  130. {
  131. Sphere,
  132. Box,
  133. ConvexHull,
  134. Capsule,
  135. TaperedCapsule,
  136. Cylinder,
  137. Triangle,
  138. StaticCompound,
  139. StaticCompound2,
  140. MutableCompound,
  141. };
  142. // Probe settings
  143. EProbeMode mProbeMode = EProbeMode::Pick; // Mouse probe mode. Determines what happens under the crosshair.
  144. EProbeShape mProbeShape = EProbeShape::Sphere; // Shape to use for the mouse probe.
  145. bool mScaleShape = false; // If the shape is scaled or not. When true mShapeScale is taken into account.
  146. Vec3 mShapeScale = Vec3::sReplicate(1.0f); // Scale in local space for the probe shape.
  147. EBackFaceMode mBackFaceMode = EBackFaceMode::CollideWithBackFaces; // How to handle back facing triangles when doing a collision probe check.
  148. EActiveEdgeMode mActiveEdgeMode = EActiveEdgeMode::CollideOnlyWithActive; // How to handle active edges when doing a collision probe check.
  149. ECollectFacesMode mCollectFacesMode = ECollectFacesMode::NoFaces; // If we should collect colliding faces
  150. float mMaxSeparationDistance = 0.0f; // Max separation distance for collide shape test
  151. bool mTreatConvexAsSolid = true; // For ray casts if the shape should be treated as solid or if the ray should only collide with the surface
  152. bool mReturnDeepestPoint = true; // For shape casts, when true this will return the deepest point
  153. bool mUseShrunkenShapeAndConvexRadius = false; // Shrink then expand the shape by the convex radius
  154. int mMaxHits = 10; // The maximum number of hits to request for a collision probe.
  155. // Which object to shoot
  156. enum class EShootObjectShape
  157. {
  158. Sphere,
  159. ConvexHull,
  160. ThinBar,
  161. };
  162. // Shoot object settings
  163. EShootObjectShape mShootObjectShape = EShootObjectShape::Sphere; // Type of object to shoot
  164. float mShootObjectVelocity = 20.0f; // Speed at which objects are ejected
  165. EMotionQuality mShootObjectMotionQuality = EMotionQuality::Discrete; // Motion quality for the object that we're shooting
  166. float mShootObjectFriction = 0.2f; // Friction for the object that is shot
  167. float mShootObjectRestitution = 0.0f; // Restitution for the object that is shot
  168. bool mShootObjectScaleShape = false; // If the shape should be scaled
  169. Vec3 mShootObjectShapeScale = Vec3::sReplicate(1.0f); // Scale of the object to shoot
  170. // Mouse dragging
  171. Body * mDragAnchor = nullptr; // A anchor point for the distance constraint. Corresponds to the current crosshair position.
  172. BodyID mDragBody; // The body ID of the body that the user is currently dragging.
  173. Ref<Constraint> mDragConstraint; // The distance constraint that connects the body to be dragged and the anchor point.
  174. float mDragFraction; // Fraction along cDragRayLength (see cpp) where the hit occurred. This will be combined with the crosshair position to get a 3d anchor point.
  175. // Timing
  176. uint mStepNumber = 0; // Which step number we're accumulating
  177. uint64 mTotalTime = 0; // How many tick we spent
  178. };