SensorTest.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Tests/Test.h>
  5. #include <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. BodyID mSensorID; // Body ID of the sensor
  27. Ref<Ragdoll> mRagdoll;
  28. Mutex mMutex; // Mutex that protects mBodiesInSensor
  29. // Structure that keeps track of how many contact point each body has with the sensor
  30. struct BodyAndCount
  31. {
  32. BodyID mBodyID;
  33. int mCount;
  34. bool operator < (const BodyAndCount &inRHS) const { return mBodyID < inRHS.mBodyID; }
  35. };
  36. using BodiesInSensor = vector<BodyAndCount>;
  37. BodiesInSensor mBodiesInSensor; // Bodies that are currently inside the sensor
  38. };