SensorTests.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include "PhysicsTestContext.h"
  5. #include "Layers.h"
  6. #include "LoggingContactListener.h"
  7. #include <Physics/Collision/Shape/BoxShape.h>
  8. TEST_SUITE("SensorTests")
  9. {
  10. using LogEntry = LoggingContactListener::LogEntry;
  11. using EType = LoggingContactListener::EType;
  12. TEST_CASE("TestDynamicVsSensor")
  13. {
  14. PhysicsTestContext c;
  15. c.ZeroGravity();
  16. // Register listener
  17. LoggingContactListener listener;
  18. c.GetSystem()->SetContactListener(&listener);
  19. // Sensor
  20. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(1)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  21. sensor_settings.mIsSensor = true;
  22. BodyID sensor_id = c.GetBodyInterface().CreateAndAddBody(sensor_settings, EActivation::DontActivate);
  23. // Dynamic body moving downwards
  24. Body &dynamic = c.CreateBox(Vec3(0, 2, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f));
  25. dynamic.SetLinearVelocity(Vec3(0, -1, 0));
  26. // After a single step the dynamic object should not have touched the sensor yet
  27. c.SimulateSingleStep();
  28. CHECK(listener.GetEntryCount() == 0);
  29. // After half a second we should be touching the sensor
  30. c.Simulate(0.5f);
  31. CHECK(listener.Contains(EType::Add, dynamic.GetID(), sensor_id));
  32. listener.Clear();
  33. // The next step we require that the contact persists
  34. c.SimulateSingleStep();
  35. CHECK(listener.Contains(EType::Persist, dynamic.GetID(), sensor_id));
  36. listener.Clear();
  37. // After 3 more seconds we should have left the sensor at the bottom side
  38. c.Simulate(3.0f + c.GetDeltaTime());
  39. CHECK(listener.Contains(EType::Remove, dynamic.GetID(), sensor_id));
  40. }
  41. TEST_CASE("TestKinematicVsSensor")
  42. {
  43. PhysicsTestContext c;
  44. // Register listener
  45. LoggingContactListener listener;
  46. c.GetSystem()->SetContactListener(&listener);
  47. // Sensor
  48. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(1)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  49. sensor_settings.mIsSensor = true;
  50. BodyID sensor_id = c.GetBodyInterface().CreateAndAddBody(sensor_settings, EActivation::DontActivate);
  51. // Kinematic body moving downwards
  52. Body &kinematic = c.CreateBox(Vec3(0, 2, 0), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f));
  53. kinematic.SetLinearVelocity(Vec3(0, -1, 0));
  54. // After a single step the kinematic object should not have touched the sensor yet
  55. c.SimulateSingleStep();
  56. CHECK(listener.GetEntryCount() == 0);
  57. // After half a second we should be touching the sensor
  58. c.Simulate(0.5f);
  59. CHECK(listener.Contains(EType::Add, kinematic.GetID(), sensor_id));
  60. listener.Clear();
  61. // The next step we require that the contact persists
  62. c.SimulateSingleStep();
  63. CHECK(listener.Contains(EType::Persist, kinematic.GetID(), sensor_id));
  64. listener.Clear();
  65. // After 3 more seconds we should have left the sensor at the bottom side
  66. c.Simulate(3.0f + c.GetDeltaTime());
  67. CHECK(listener.Contains(EType::Remove, kinematic.GetID(), sensor_id));
  68. }
  69. }