SensorTest.h 2.5 KB

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