SensorTest.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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(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. NumSensors
  34. };
  35. BodyID mSensorID[NumSensors]; // Body ID of the various sensors
  36. Ref<Ragdoll> mRagdoll; // Ragdoll that is falling into the sensor
  37. BodyID mKinematicBodyID; // Body ID of a kinematic body that is animating in and out of the sensor
  38. Mutex mMutex; // Mutex that protects mBodiesInSensor and mKinematicBodyInSensor
  39. // Structure that keeps track of how many contact point each body has with the sensor
  40. struct BodyAndCount
  41. {
  42. BodyID mBodyID;
  43. int mCount;
  44. bool operator < (const BodyAndCount &inRHS) const { return mBodyID < inRHS.mBodyID; }
  45. };
  46. using BodiesInSensor = Array<BodyAndCount>;
  47. BodiesInSensor mBodiesInSensor[NumSensors]; // Dynamic bodies that are currently inside the sensor
  48. };