SensorTest.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. float mTime = 0.0f; // Total elapsed time
  27. BodyID mSensorID; // Body ID of the sensor
  28. Ref<Ragdoll> mRagdoll; // Ragdoll that is falling into the sensor
  29. BodyID mKinematicBodyID; // Body ID of a kinematic body that is animating in and out of the sensor
  30. Mutex mMutex; // Mutex that protects mBodiesInSensor and mKinematicBodyInSensor
  31. // Structure that keeps track of how many contact point each body has with the sensor
  32. struct BodyAndCount
  33. {
  34. BodyID mBodyID;
  35. int mCount;
  36. bool operator < (const BodyAndCount &inRHS) const { return mBodyID < inRHS.mBodyID; }
  37. };
  38. using BodiesInSensor = vector<BodyAndCount>;
  39. BodiesInSensor mBodiesInSensor; // Dynamic bodies that are currently inside the sensor
  40. bool mKinematicBodyInSensor = false; // Keeps track if the kinematic body is in the sensor
  41. };