SensorTest.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <Tests/Test.h>
  6. #include <Jolt/Physics/Ragdoll/Ragdoll.h>
  7. // Test that contains a sensor that will apply forces to bodies inside the sensor
  8. class SensorTest : public Test, public ContactListener
  9. {
  10. public:
  11. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, SensorTest)
  12. virtual ~SensorTest() override;
  13. // Number used to scale the terrain and camera movement to the scene
  14. virtual float GetWorldScale() const override { return 0.2f; }
  15. // See: Test
  16. virtual void Initialize() override;
  17. virtual void PrePhysicsUpdate(const PreUpdateParams &inParams) override;
  18. // If this test implements a contact listener, it should be returned here
  19. virtual ContactListener *GetContactListener() override { return this; }
  20. // See: ContactListener
  21. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  22. virtual void OnContactRemoved(const SubShapeIDPair &inSubShapePair) override;
  23. // Saving / restoring state for replay
  24. virtual void SaveState(StateRecorder &inStream) const override;
  25. virtual void RestoreState(StateRecorder &inStream) override;
  26. private:
  27. float mTime = 0.0f; // Total elapsed time
  28. enum
  29. {
  30. StaticAttractor, // A static sensor that attrects dynamic bodies that enter its area
  31. StaticSensor, // A static sensor that only detects active bodies
  32. KinematicSensor, // A kinematic sensor that also detects sleeping bodies
  33. SensorDetectingStatic, // A kinematic sensor that detects static bodies
  34. NumSensors
  35. };
  36. BodyID mSensorID[NumSensors]; // Body ID of the various sensors
  37. Ref<Ragdoll> mRagdoll; // Ragdoll that is falling into the sensor
  38. BodyID mKinematicBodyID; // Body ID of a kinematic body that is animating in and out of the sensor
  39. Mutex mMutex; // Mutex that protects mBodiesInSensor and mKinematicBodyInSensor
  40. // Structure that keeps track of how many contact point each body has with the sensor
  41. struct BodyAndCount
  42. {
  43. BodyID mBodyID;
  44. int mCount;
  45. bool operator < (const BodyAndCount &inRHS) const { return mBodyID < inRHS.mBodyID; }
  46. };
  47. using BodiesInSensor = Array<BodyAndCount>;
  48. BodiesInSensor mBodiesInSensor[NumSensors]; // Dynamic bodies that are currently inside the sensor
  49. };