PhysicsTestContext.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
  8. #include "Layers.h"
  9. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  10. #include <fstream>
  11. JPH_SUPPRESS_WARNINGS_STD_END
  12. namespace JPH {
  13. class TempAllocator;
  14. class JobSystem;
  15. class DebugRendererRecorder;
  16. class StreamOutWrapper;
  17. };
  18. // Helper class used in test cases for creating and manipulating physics objects
  19. class PhysicsTestContext
  20. {
  21. public:
  22. // Constructor / destructor
  23. PhysicsTestContext(float inDeltaTime = 1.0f / 60.0f, int inCollisionSteps = 1, int inWorkerThreads = 0, uint inMaxBodies = 1024, uint inMaxBodyPairs = 4096, uint inMaxContactConstraints = 1024);
  24. ~PhysicsTestContext();
  25. // Set the gravity to zero
  26. void ZeroGravity();
  27. // Create a floor at Y = 0
  28. Body & CreateFloor();
  29. /// Create a body and add it to the world
  30. Body & CreateBody(const BodyCreationSettings &inSettings, EActivation inActivation);
  31. /// Create a body and add it to the world
  32. Body & CreateBody(const ShapeSettings *inShapeSettings, RVec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, EMotionQuality inMotionQuality, ObjectLayer inLayer, EActivation inActivation);
  33. // Create a box and add it to the world
  34. Body & CreateBox(RVec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, EMotionQuality inMotionQuality, ObjectLayer inLayer, Vec3Arg inHalfExtent, EActivation inActivation = EActivation::Activate);
  35. // Create a sphere and add it to the world
  36. Body & CreateSphere(RVec3Arg inPosition, float inRadius, EMotionType inMotionType, EMotionQuality inMotionQuality, ObjectLayer inLayer, EActivation inActivation = EActivation::Activate);
  37. // Create a constraint and add it to the world
  38. template <typename T>
  39. T & CreateConstraint(Body &inBody1, Body &inBody2, const TwoBodyConstraintSettings &inSettings)
  40. {
  41. T *constraint = static_cast<T *>(inSettings.Create(inBody1, inBody2));
  42. mSystem->AddConstraint(constraint);
  43. return *constraint;
  44. }
  45. // Call the update with zero delta time
  46. EPhysicsUpdateError SimulateNoDeltaTime();
  47. // Simulate only for one delta time step
  48. EPhysicsUpdateError SimulateSingleStep();
  49. // Simulate the world for inTotalTime time
  50. EPhysicsUpdateError Simulate(float inTotalTime, function<void()> inPreStepCallback = []() { });
  51. // Predict position assuming ballistic motion using initial position, velocity acceleration and time
  52. RVec3 PredictPosition(RVec3Arg inPosition, Vec3Arg inVelocity, Vec3Arg inAcceleration, float inTotalTime) const;
  53. // Predict rotation assuming ballistic motion using initial orientation, angular velocity angular acceleration and time
  54. Quat PredictOrientation(QuatArg inRotation, Vec3Arg inAngularVelocity, Vec3Arg inAngularAcceleration, float inTotalTime) const;
  55. // Access to the physics system
  56. PhysicsSystem * GetSystem() const
  57. {
  58. return mSystem;
  59. }
  60. // Access to the body interface
  61. BodyInterface & GetBodyInterface() const
  62. {
  63. return mSystem->GetBodyInterface();
  64. }
  65. // Get delta time for simulation step
  66. inline float GetDeltaTime() const
  67. {
  68. return mDeltaTime;
  69. }
  70. // Get delta time for a simulation collision step
  71. inline float GetStepDeltaTime() const
  72. {
  73. return mDeltaTime / mCollisionSteps;
  74. }
  75. // Get the temporary allocator
  76. TempAllocator * GetTempAllocator() const
  77. {
  78. return mTempAllocator;
  79. }
  80. // Get the job system
  81. JobSystem * GetJobSystem() const
  82. {
  83. return mJobSystem;
  84. }
  85. #ifdef JPH_DEBUG_RENDERER
  86. // Write the debug output to a file to be able to replay it with JoltViewer
  87. void RecordDebugOutput(const char *inFileName);
  88. #endif // JPH_DEBUG_RENDERER
  89. private:
  90. TempAllocator * mTempAllocator;
  91. JobSystem * mJobSystem;
  92. BPLayerInterfaceImpl mBroadPhaseLayerInterface;
  93. ObjectVsBroadPhaseLayerFilterImpl mObjectVsBroadPhaseLayerFilter;
  94. ObjectLayerPairFilterImpl mObjectVsObjectLayerFilter;
  95. PhysicsSystem * mSystem;
  96. #ifdef JPH_DEBUG_RENDERER
  97. DebugRendererRecorder *mDebugRenderer = nullptr;
  98. ofstream * mStream = nullptr;
  99. StreamOutWrapper * mStreamWrapper = nullptr;
  100. #endif // JPH_DEBUG_RENDERER
  101. float mDeltaTime;
  102. int mCollisionSteps;
  103. };