PhysicsTestContext.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 ShapeSettings *inShapeSettings, RVec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, EMotionQuality inMotionQuality, ObjectLayer inLayer, EActivation inActivation);
  31. // Create a box and add it to the world
  32. Body & CreateBox(RVec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, EMotionQuality inMotionQuality, ObjectLayer inLayer, Vec3Arg inHalfExtent, EActivation inActivation = EActivation::Activate);
  33. // Create a sphere and add it to the world
  34. Body & CreateSphere(RVec3Arg inPosition, float inRadius, EMotionType inMotionType, EMotionQuality inMotionQuality, ObjectLayer inLayer, EActivation inActivation = EActivation::Activate);
  35. // Create a constraint and add it to the world
  36. template <typename T>
  37. T & CreateConstraint(Body &inBody1, Body &inBody2, const TwoBodyConstraintSettings &inSettings)
  38. {
  39. T *constraint = static_cast<T *>(inSettings.Create(inBody1, inBody2));
  40. mSystem->AddConstraint(constraint);
  41. return *constraint;
  42. }
  43. // Call the update with zero delta time
  44. EPhysicsUpdateError SimulateNoDeltaTime();
  45. // Simulate only for one delta time step
  46. EPhysicsUpdateError SimulateSingleStep();
  47. // Simulate the world for inTotalTime time
  48. EPhysicsUpdateError Simulate(float inTotalTime, function<void()> inPreStepCallback = []() { });
  49. // Predict position assuming ballistic motion using initial position, velocity acceleration and time
  50. RVec3 PredictPosition(RVec3Arg inPosition, Vec3Arg inVelocity, Vec3Arg inAcceleration, float inTotalTime) const;
  51. // Predict rotation assuming ballistic motion using initial orientation, angular velocity angular acceleration and time
  52. Quat PredictOrientation(QuatArg inRotation, Vec3Arg inAngularVelocity, Vec3Arg inAngularAcceleration, float inTotalTime) const;
  53. // Access to the physics system
  54. PhysicsSystem * GetSystem() const
  55. {
  56. return mSystem;
  57. }
  58. // Access to the body interface
  59. BodyInterface & GetBodyInterface() const
  60. {
  61. return mSystem->GetBodyInterface();
  62. }
  63. // Get delta time for simulation step
  64. inline float GetDeltaTime() const
  65. {
  66. return mDeltaTime;
  67. }
  68. // Get delta time for a simulation collision step
  69. inline float GetStepDeltaTime() const
  70. {
  71. return mDeltaTime / mCollisionSteps;
  72. }
  73. // Get the temporary allocator
  74. TempAllocator * GetTempAllocator() const
  75. {
  76. return mTempAllocator;
  77. }
  78. // Get the job system
  79. JobSystem * GetJobSystem() const
  80. {
  81. return mJobSystem;
  82. }
  83. #ifdef JPH_DEBUG_RENDERER
  84. // Write the debug output to a file to be able to replay it with JoltViewer
  85. void RecordDebugOutput(const char *inFileName);
  86. #endif // JPH_DEBUG_RENDERER
  87. private:
  88. TempAllocator * mTempAllocator;
  89. JobSystem * mJobSystem;
  90. BPLayerInterfaceImpl mBroadPhaseLayerInterface;
  91. ObjectVsBroadPhaseLayerFilterImpl mObjectVsBroadPhaseLayerFilter;
  92. ObjectLayerPairFilterImpl mObjectVsObjectLayerFilter;
  93. PhysicsSystem * mSystem;
  94. #ifdef JPH_DEBUG_RENDERER
  95. DebugRendererRecorder *mDebugRenderer = nullptr;
  96. ofstream * mStream = nullptr;
  97. StreamOutWrapper * mStreamWrapper = nullptr;
  98. #endif // JPH_DEBUG_RENDERER
  99. float mDeltaTime;
  100. int mCollisionSteps;
  101. };