PhysicsTestContext.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/PhysicsSystem.h>
  5. #include <Physics/Body/BodyCreationSettings.h>
  6. #include <Physics/Constraints/TwoBodyConstraint.h>
  7. namespace JPH {
  8. class TempAllocator;
  9. class JobSystem;
  10. };
  11. // Helper class used in test cases for creating and manipulating physics objects
  12. class PhysicsTestContext
  13. {
  14. public:
  15. // Constructor / destructor
  16. PhysicsTestContext(float inDeltaTime = 1.0f / 60.0f, int inCollisionSteps = 1, int inIntegrationSubSteps = 1, int inWorkerThreads = 0);
  17. ~PhysicsTestContext();
  18. // Set the gravity to zero
  19. void ZeroGravity();
  20. // Create a floor at Y = 0
  21. Body & CreateFloor();
  22. /// Create a body and add it to the world
  23. Body & CreateBody(const ShapeSettings *inShapeSettings, Vec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, EMotionQuality inMotionQuality, ObjectLayer inLayer, EActivation inActivation);
  24. // Create a box and add it to the world
  25. Body & CreateBox(Vec3Arg inPosition, QuatArg inRotation, EMotionType inMotionType, EMotionQuality inMotionQuality, ObjectLayer inLayer, Vec3Arg inHalfExtent, EActivation inActivation = EActivation::Activate);
  26. // Create a sphere and add it to the world
  27. Body & CreateSphere(Vec3Arg inPosition, float inRadius, EMotionType inMotionType, EMotionQuality inMotionQuality, ObjectLayer inLayer, EActivation inActivation = EActivation::Activate);
  28. // Create a constraint and add it to the world
  29. template <typename T>
  30. T & CreateConstraint(Body &inBody1, Body &inBody2, const TwoBodyConstraintSettings &inSettings)
  31. {
  32. T *constraint = static_cast<T *>(inSettings.Create(inBody1, inBody2));
  33. mSystem->AddConstraint(constraint);
  34. return *constraint;
  35. }
  36. // Simulate only for one delta time step
  37. void SimulateSingleStep();
  38. // Simulate the world for inTotalTime time
  39. void Simulate(float inTotalTime, function<void()> inPreStepCallback = []() { });
  40. // Predict position assuming ballistic motion using initial position, velocity acceleration and time
  41. Vec3 PredictPosition(Vec3Arg inPosition, Vec3Arg inVelocity, Vec3Arg inAcceleration, float inTotalTime) const;
  42. // Predict rotation assuming ballistic motion using initial orientation, angular velocity angular acceleration and time
  43. Quat PredictOrientation(QuatArg inRotation, Vec3Arg inAngularVelocity, Vec3Arg inAngularAcceleration, float inTotalTime) const;
  44. // Access to the physics system
  45. PhysicsSystem * GetSystem() const
  46. {
  47. return mSystem;
  48. }
  49. // Access to the body interface
  50. BodyInterface & GetBodyInterface() const
  51. {
  52. return mSystem->GetBodyInterface();
  53. }
  54. // Get delta time for simulation step
  55. inline float GetDeltaTime() const
  56. {
  57. return mDeltaTime;
  58. }
  59. // Get delta time for a simulation integration sub step
  60. inline float GetSubStepDeltaTime() const
  61. {
  62. return mDeltaTime / (mCollisionSteps * mIntegrationSubSteps);
  63. }
  64. private:
  65. TempAllocator * mTempAllocator;
  66. JobSystem * mJobSystem;
  67. PhysicsSystem * mSystem;
  68. float mDeltaTime;
  69. int mCollisionSteps;
  70. int mIntegrationSubSteps;
  71. };