SensorTests.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <Jolt/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. CHECK(!listener.Contains(EType::Remove, dynamic.GetID(), sensor_id));
  37. listener.Clear();
  38. // After 3 more seconds we should have left the sensor at the bottom side
  39. c.Simulate(3.0f + c.GetDeltaTime());
  40. CHECK(listener.Contains(EType::Remove, dynamic.GetID(), sensor_id));
  41. CHECK_APPROX_EQUAL(dynamic.GetPosition(), Vec3(0, -1.5f - 3.0f * c.GetDeltaTime(), 0), 1.0e-4f);
  42. }
  43. TEST_CASE("TestKinematicVsSensor")
  44. {
  45. PhysicsTestContext c;
  46. // Register listener
  47. LoggingContactListener listener;
  48. c.GetSystem()->SetContactListener(&listener);
  49. // Sensor
  50. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(1)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  51. sensor_settings.mIsSensor = true;
  52. BodyID sensor_id = c.GetBodyInterface().CreateAndAddBody(sensor_settings, EActivation::DontActivate);
  53. // Kinematic body moving downwards
  54. Body &kinematic = c.CreateBox(Vec3(0, 2, 0), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f));
  55. kinematic.SetLinearVelocity(Vec3(0, -1, 0));
  56. // After a single step the kinematic object should not have touched the sensor yet
  57. c.SimulateSingleStep();
  58. CHECK(listener.GetEntryCount() == 0);
  59. // After half a second we should be touching the sensor
  60. c.Simulate(0.5f);
  61. CHECK(listener.Contains(EType::Add, kinematic.GetID(), sensor_id));
  62. listener.Clear();
  63. // The next step we require that the contact persists
  64. c.SimulateSingleStep();
  65. CHECK(listener.Contains(EType::Persist, kinematic.GetID(), sensor_id));
  66. CHECK(!listener.Contains(EType::Remove, kinematic.GetID(), sensor_id));
  67. listener.Clear();
  68. // After 3 more seconds we should have left the sensor at the bottom side
  69. c.Simulate(3.0f + c.GetDeltaTime());
  70. CHECK(listener.Contains(EType::Remove, kinematic.GetID(), sensor_id));
  71. CHECK_APPROX_EQUAL(kinematic.GetPosition(), Vec3(0, -1.5f - 3.0f * c.GetDeltaTime(), 0), 1.0e-4f);
  72. }
  73. TEST_CASE("TestDynamicSleepingVsStaticSensor")
  74. {
  75. PhysicsTestContext c;
  76. // Register listener
  77. LoggingContactListener listener;
  78. c.GetSystem()->SetContactListener(&listener);
  79. // Sensor
  80. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(1)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  81. sensor_settings.mIsSensor = true;
  82. Body &sensor = *c.GetBodyInterface().CreateBody(sensor_settings);
  83. c.GetBodyInterface().AddBody(sensor.GetID(), EActivation::DontActivate);
  84. // Floor
  85. Body &floor = c.CreateFloor();
  86. // Dynamic body on floor (make them penetrate)
  87. Body &dynamic = c.CreateBox(Vec3(0, 0.5f - c.GetSystem()->GetPhysicsSettings().mMaxPenetrationDistance, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f), EActivation::DontActivate);
  88. // After a single step (because the object is sleeping) there should not be a contact
  89. c.SimulateSingleStep();
  90. CHECK(listener.GetEntryCount() == 0);
  91. // The dynamic object should not be part of an island
  92. CHECK(!sensor.IsActive());
  93. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() == Body::cInactiveIndex);
  94. // Activate the body
  95. c.GetBodyInterface().ActivateBody(dynamic.GetID());
  96. // After a single step we should have detected the collision with the floor and the sensor
  97. c.SimulateSingleStep();
  98. CHECK(listener.GetEntryCount() == 4);
  99. CHECK(listener.Contains(EType::Validate, floor.GetID(), dynamic.GetID()));
  100. CHECK(listener.Contains(EType::Add, floor.GetID(), dynamic.GetID()));
  101. CHECK(listener.Contains(EType::Validate, sensor.GetID(), dynamic.GetID()));
  102. CHECK(listener.Contains(EType::Add, sensor.GetID(), dynamic.GetID()));
  103. listener.Clear();
  104. // The dynamic object should be part of an island now
  105. CHECK(!sensor.IsActive());
  106. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  107. // After a second the body should have gone to sleep and the contacts should have been removed
  108. c.Simulate(1.0f);
  109. CHECK(!dynamic.IsActive());
  110. CHECK(listener.Contains(EType::Remove, floor.GetID(), dynamic.GetID()));
  111. CHECK(listener.Contains(EType::Remove, sensor.GetID(), dynamic.GetID()));
  112. // The dynamic object should not be part of an island
  113. CHECK(!sensor.IsActive());
  114. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() == Body::cInactiveIndex);
  115. }
  116. TEST_CASE("TestDynamicSleepingVsKinematicSensor")
  117. {
  118. PhysicsTestContext c;
  119. // Register listener
  120. LoggingContactListener listener;
  121. c.GetSystem()->SetContactListener(&listener);
  122. // Kinematic sensor that is active (so will keep detecting contacts with sleeping bodies)
  123. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(1)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Kinematic, Layers::NON_MOVING);
  124. sensor_settings.mIsSensor = true;
  125. Body &sensor = *c.GetBodyInterface().CreateBody(sensor_settings);
  126. c.GetBodyInterface().AddBody(sensor.GetID(), EActivation::Activate);
  127. // Floor
  128. Body &floor = c.CreateFloor();
  129. // Dynamic body on floor (make them penetrate)
  130. Body &dynamic = c.CreateBox(Vec3(0, 0.5f - c.GetSystem()->GetPhysicsSettings().mMaxPenetrationDistance, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f), EActivation::DontActivate);
  131. // After a single step, there should be a contact with the sensor only (the sensor is active)
  132. c.SimulateSingleStep();
  133. CHECK(listener.GetEntryCount() == 2);
  134. CHECK(listener.Contains(EType::Validate, sensor.GetID(), dynamic.GetID()));
  135. CHECK(listener.Contains(EType::Add, sensor.GetID(), dynamic.GetID()));
  136. listener.Clear();
  137. // The sensor should be in its own island
  138. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  139. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() == Body::cInactiveIndex);
  140. // The second step, the contact with the sensor should have persisted
  141. c.SimulateSingleStep();
  142. CHECK(listener.GetEntryCount() == 1);
  143. CHECK(listener.Contains(EType::Persist, sensor.GetID(), dynamic.GetID()));
  144. listener.Clear();
  145. // The sensor should still be in its own island
  146. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  147. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() == Body::cInactiveIndex);
  148. // Activate the body
  149. c.GetBodyInterface().ActivateBody(dynamic.GetID());
  150. // After a single step we should have detected collision with the floor and the collision with the sensor should have persisted
  151. c.SimulateSingleStep();
  152. CHECK(listener.GetEntryCount() == 3);
  153. CHECK(listener.Contains(EType::Validate, floor.GetID(), dynamic.GetID()));
  154. CHECK(listener.Contains(EType::Add, floor.GetID(), dynamic.GetID()));
  155. CHECK(listener.Contains(EType::Persist, sensor.GetID(), dynamic.GetID()));
  156. listener.Clear();
  157. // The sensor should not be part of the same island as the dynamic body (they won't interact, so this is not needed)
  158. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  159. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  160. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != dynamic.GetMotionProperties()->GetIslandIndexInternal());
  161. // After another step we should have persisted the collision with the floor and sensor
  162. c.SimulateSingleStep();
  163. CHECK(listener.GetEntryCount() >= 2); // Depending on if we used the contact cache or not there will be validate callbacks too
  164. CHECK(listener.Contains(EType::Persist, floor.GetID(), dynamic.GetID()));
  165. CHECK(!listener.Contains(EType::Remove, floor.GetID(), dynamic.GetID()));
  166. CHECK(listener.Contains(EType::Persist, sensor.GetID(), dynamic.GetID()));
  167. CHECK(!listener.Contains(EType::Remove, sensor.GetID(), dynamic.GetID()));
  168. listener.Clear();
  169. // The same islands as the previous step should have been created
  170. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  171. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  172. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != dynamic.GetMotionProperties()->GetIslandIndexInternal());
  173. // After a second the body should have gone to sleep and the contacts with the floor should have been removed, but not with the sensor
  174. c.Simulate(1.0f);
  175. CHECK(!dynamic.IsActive());
  176. CHECK(listener.Contains(EType::Remove, floor.GetID(), dynamic.GetID()));
  177. CHECK(!listener.Contains(EType::Remove, sensor.GetID(), dynamic.GetID()));
  178. }
  179. TEST_CASE("TestContactListenerMakesSensor")
  180. {
  181. PhysicsTestContext c;
  182. c.ZeroGravity();
  183. class SensorOverridingListener : public LoggingContactListener
  184. {
  185. public:
  186. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  187. {
  188. LoggingContactListener::OnContactAdded(inBody1, inBody2, inManifold, ioSettings);
  189. JPH_ASSERT(ioSettings.mIsSensor == false);
  190. if (inBody1.GetID() == mBodyThatSeesSensorID || inBody2.GetID() == mBodyThatSeesSensorID)
  191. ioSettings.mIsSensor = true;
  192. }
  193. virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  194. {
  195. LoggingContactListener::OnContactPersisted(inBody1, inBody2, inManifold, ioSettings);
  196. JPH_ASSERT(ioSettings.mIsSensor == false);
  197. if (inBody1.GetID() == mBodyThatSeesSensorID || inBody2.GetID() == mBodyThatSeesSensorID)
  198. ioSettings.mIsSensor = true;
  199. }
  200. BodyID mBodyThatSeesSensorID;
  201. };
  202. // Register listener
  203. SensorOverridingListener listener;
  204. c.GetSystem()->SetContactListener(&listener);
  205. // Body that will appear as a sensor to one object and as static to another
  206. BodyID static_id = c.GetBodyInterface().CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(5, 1, 5)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  207. // Dynamic body moving down that will do a normal collision
  208. Body &dynamic1 = c.CreateBox(Vec3(-2, 2, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f));
  209. dynamic1.SetAllowSleeping(false);
  210. dynamic1.SetLinearVelocity(Vec3(0, -1, 0));
  211. // Dynamic body moving down that will only see the static object as a sensor
  212. Body &dynamic2 = c.CreateBox(Vec3(2, 2, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f));
  213. dynamic2.SetAllowSleeping(false);
  214. dynamic2.SetLinearVelocity(Vec3(0, -1, 0));
  215. listener.mBodyThatSeesSensorID = dynamic2.GetID();
  216. // After a single step the dynamic object should not have touched the sensor yet
  217. c.SimulateSingleStep();
  218. CHECK(listener.GetEntryCount() == 0);
  219. // After half a second both bodies should be touching the sensor
  220. c.Simulate(0.5f);
  221. CHECK(listener.Contains(EType::Add, dynamic1.GetID(), static_id));
  222. CHECK(listener.Contains(EType::Add, dynamic2.GetID(), static_id));
  223. listener.Clear();
  224. // The next step we require that the contact persists
  225. c.SimulateSingleStep();
  226. CHECK(listener.Contains(EType::Persist, dynamic1.GetID(), static_id));
  227. CHECK(!listener.Contains(EType::Remove, dynamic1.GetID(), static_id));
  228. CHECK(listener.Contains(EType::Persist, dynamic2.GetID(), static_id));
  229. CHECK(!listener.Contains(EType::Remove, dynamic2.GetID(), static_id));
  230. listener.Clear();
  231. // After 3 more seconds one body should be resting on the static body, the other should have fallen through
  232. c.Simulate(3.0f + c.GetDeltaTime());
  233. CHECK(listener.Contains(EType::Persist, dynamic1.GetID(), static_id));
  234. CHECK(!listener.Contains(EType::Remove, dynamic1.GetID(), static_id));
  235. CHECK(listener.Contains(EType::Remove, dynamic2.GetID(), static_id));
  236. CHECK_APPROX_EQUAL(dynamic1.GetPosition(), Vec3(-2, 1.5f, 0), 5.0e-3f);
  237. CHECK_APPROX_EQUAL(dynamic2.GetPosition(), Vec3(2, -1.5f - 3.0f * c.GetDeltaTime(), 0), 1.0e-4f);
  238. }
  239. }