SensorTest.h 2.2 KB

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