2
0

PhysicsTestContext.h 3.1 KB

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