SensorTests.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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("TestKinematicVsKinematicSensor")
  74. {
  75. // Same as TestKinematicVsSensor but with the sensor being an active kinematic body
  76. PhysicsTestContext c;
  77. // Register listener
  78. LoggingContactListener listener;
  79. c.GetSystem()->SetContactListener(&listener);
  80. // Kinematic sensor
  81. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(1)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Kinematic, Layers::NON_MOVING);
  82. sensor_settings.mIsSensor = true;
  83. BodyID sensor_id = c.GetBodyInterface().CreateAndAddBody(sensor_settings, EActivation::Activate);
  84. // Kinematic body moving downwards
  85. Body &kinematic = c.CreateBox(Vec3(0, 2, 0), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f));
  86. kinematic.SetLinearVelocity(Vec3(0, -1, 0));
  87. // After a single step the kinematic object should not have touched the sensor yet
  88. c.SimulateSingleStep();
  89. CHECK(listener.GetEntryCount() == 0);
  90. // After half a second we should be touching the sensor
  91. c.Simulate(0.5f);
  92. CHECK(listener.Contains(EType::Add, kinematic.GetID(), sensor_id));
  93. listener.Clear();
  94. // The next step we require that the contact persists
  95. c.SimulateSingleStep();
  96. CHECK(listener.Contains(EType::Persist, kinematic.GetID(), sensor_id));
  97. CHECK(!listener.Contains(EType::Remove, kinematic.GetID(), sensor_id));
  98. listener.Clear();
  99. // After 3 more seconds we should have left the sensor at the bottom side
  100. c.Simulate(3.0f + c.GetDeltaTime());
  101. CHECK(listener.Contains(EType::Remove, kinematic.GetID(), sensor_id));
  102. CHECK_APPROX_EQUAL(kinematic.GetPosition(), Vec3(0, -1.5f - 3.0f * c.GetDeltaTime(), 0), 1.0e-4f);
  103. }
  104. TEST_CASE("TestKinematicVsKinematicSensorReversed")
  105. {
  106. // Same as TestKinematicVsKinematicSensor but with bodies created in reverse order (this matters for Body::sFindCollidingPairsCanCollide because MotionProperties::mIndexInActiveBodies is swapped between the bodies)
  107. PhysicsTestContext c;
  108. // Register listener
  109. LoggingContactListener listener;
  110. c.GetSystem()->SetContactListener(&listener);
  111. // Kinematic body moving downwards
  112. Body &kinematic = c.CreateBox(Vec3(0, 2, 0), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f));
  113. kinematic.SetLinearVelocity(Vec3(0, -1, 0));
  114. // Kinematic sensor
  115. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(1)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Kinematic, Layers::NON_MOVING);
  116. sensor_settings.mIsSensor = true;
  117. BodyID sensor_id = c.GetBodyInterface().CreateAndAddBody(sensor_settings, EActivation::Activate);
  118. // After a single step the kinematic object should not have touched the sensor yet
  119. c.SimulateSingleStep();
  120. CHECK(listener.GetEntryCount() == 0);
  121. // After half a second we should be touching the sensor
  122. c.Simulate(0.5f);
  123. CHECK(listener.Contains(EType::Add, kinematic.GetID(), sensor_id));
  124. listener.Clear();
  125. // The next step we require that the contact persists
  126. c.SimulateSingleStep();
  127. CHECK(listener.Contains(EType::Persist, kinematic.GetID(), sensor_id));
  128. CHECK(!listener.Contains(EType::Remove, kinematic.GetID(), sensor_id));
  129. listener.Clear();
  130. // After 3 more seconds we should have left the sensor at the bottom side
  131. c.Simulate(3.0f + c.GetDeltaTime());
  132. CHECK(listener.Contains(EType::Remove, kinematic.GetID(), sensor_id));
  133. CHECK_APPROX_EQUAL(kinematic.GetPosition(), Vec3(0, -1.5f - 3.0f * c.GetDeltaTime(), 0), 1.0e-4f);
  134. }
  135. TEST_CASE("TestDynamicSleepingVsStaticSensor")
  136. {
  137. PhysicsTestContext c;
  138. // Register listener
  139. LoggingContactListener listener;
  140. c.GetSystem()->SetContactListener(&listener);
  141. // Sensor
  142. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(1)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  143. sensor_settings.mIsSensor = true;
  144. Body &sensor = *c.GetBodyInterface().CreateBody(sensor_settings);
  145. c.GetBodyInterface().AddBody(sensor.GetID(), EActivation::DontActivate);
  146. // Floor
  147. Body &floor = c.CreateFloor();
  148. // Dynamic body on floor (make them penetrate)
  149. 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);
  150. // After a single step (because the object is sleeping) there should not be a contact
  151. c.SimulateSingleStep();
  152. CHECK(listener.GetEntryCount() == 0);
  153. // The dynamic object should not be part of an island
  154. CHECK(!sensor.IsActive());
  155. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() == Body::cInactiveIndex);
  156. // Activate the body
  157. c.GetBodyInterface().ActivateBody(dynamic.GetID());
  158. // After a single step we should have detected the collision with the floor and the sensor
  159. c.SimulateSingleStep();
  160. CHECK(listener.GetEntryCount() == 4);
  161. CHECK(listener.Contains(EType::Validate, floor.GetID(), dynamic.GetID()));
  162. CHECK(listener.Contains(EType::Add, floor.GetID(), dynamic.GetID()));
  163. CHECK(listener.Contains(EType::Validate, sensor.GetID(), dynamic.GetID()));
  164. CHECK(listener.Contains(EType::Add, sensor.GetID(), dynamic.GetID()));
  165. listener.Clear();
  166. // The dynamic object should be part of an island now
  167. CHECK(!sensor.IsActive());
  168. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  169. // After a second the body should have gone to sleep and the contacts should have been removed
  170. c.Simulate(1.0f);
  171. CHECK(!dynamic.IsActive());
  172. CHECK(listener.Contains(EType::Remove, floor.GetID(), dynamic.GetID()));
  173. CHECK(listener.Contains(EType::Remove, sensor.GetID(), dynamic.GetID()));
  174. // The dynamic object should not be part of an island
  175. CHECK(!sensor.IsActive());
  176. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() == Body::cInactiveIndex);
  177. }
  178. TEST_CASE("TestDynamicSleepingVsKinematicSensor")
  179. {
  180. PhysicsTestContext c;
  181. // Register listener
  182. LoggingContactListener listener;
  183. c.GetSystem()->SetContactListener(&listener);
  184. // Kinematic sensor that is active (so will keep detecting contacts with sleeping bodies)
  185. BodyCreationSettings sensor_settings(new BoxShape(Vec3::sReplicate(1)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Kinematic, Layers::NON_MOVING);
  186. sensor_settings.mIsSensor = true;
  187. Body &sensor = *c.GetBodyInterface().CreateBody(sensor_settings);
  188. c.GetBodyInterface().AddBody(sensor.GetID(), EActivation::Activate);
  189. // Floor
  190. Body &floor = c.CreateFloor();
  191. // Dynamic body on floor (make them penetrate)
  192. 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);
  193. // After a single step, there should be a contact with the sensor only (the sensor is active)
  194. c.SimulateSingleStep();
  195. CHECK(listener.GetEntryCount() == 2);
  196. CHECK(listener.Contains(EType::Validate, sensor.GetID(), dynamic.GetID()));
  197. CHECK(listener.Contains(EType::Add, sensor.GetID(), dynamic.GetID()));
  198. listener.Clear();
  199. // The sensor should be in its own island
  200. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  201. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() == Body::cInactiveIndex);
  202. // The second step, the contact with the sensor should have persisted
  203. c.SimulateSingleStep();
  204. CHECK(listener.GetEntryCount() == 1);
  205. CHECK(listener.Contains(EType::Persist, sensor.GetID(), dynamic.GetID()));
  206. listener.Clear();
  207. // The sensor should still be in its own island
  208. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  209. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() == Body::cInactiveIndex);
  210. // Activate the body
  211. c.GetBodyInterface().ActivateBody(dynamic.GetID());
  212. // After a single step we should have detected collision with the floor and the collision with the sensor should have persisted
  213. c.SimulateSingleStep();
  214. CHECK(listener.GetEntryCount() == 3);
  215. CHECK(listener.Contains(EType::Validate, floor.GetID(), dynamic.GetID()));
  216. CHECK(listener.Contains(EType::Add, floor.GetID(), dynamic.GetID()));
  217. CHECK(listener.Contains(EType::Persist, sensor.GetID(), dynamic.GetID()));
  218. listener.Clear();
  219. // The sensor should not be part of the same island as the dynamic body (they won't interact, so this is not needed)
  220. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  221. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  222. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != dynamic.GetMotionProperties()->GetIslandIndexInternal());
  223. // After another step we should have persisted the collision with the floor and sensor
  224. c.SimulateSingleStep();
  225. CHECK(listener.GetEntryCount() >= 2); // Depending on if we used the contact cache or not there will be validate callbacks too
  226. CHECK(listener.Contains(EType::Persist, floor.GetID(), dynamic.GetID()));
  227. CHECK(!listener.Contains(EType::Remove, floor.GetID(), dynamic.GetID()));
  228. CHECK(listener.Contains(EType::Persist, sensor.GetID(), dynamic.GetID()));
  229. CHECK(!listener.Contains(EType::Remove, sensor.GetID(), dynamic.GetID()));
  230. listener.Clear();
  231. // The same islands as the previous step should have been created
  232. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  233. CHECK(dynamic.GetMotionProperties()->GetIslandIndexInternal() != Body::cInactiveIndex);
  234. CHECK(sensor.GetMotionProperties()->GetIslandIndexInternal() != dynamic.GetMotionProperties()->GetIslandIndexInternal());
  235. // 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
  236. c.Simulate(1.0f);
  237. CHECK(!dynamic.IsActive());
  238. CHECK(listener.Contains(EType::Remove, floor.GetID(), dynamic.GetID()));
  239. CHECK(!listener.Contains(EType::Remove, sensor.GetID(), dynamic.GetID()));
  240. }
  241. TEST_CASE("TestContactListenerMakesSensor")
  242. {
  243. PhysicsTestContext c;
  244. c.ZeroGravity();
  245. class SensorOverridingListener : public LoggingContactListener
  246. {
  247. public:
  248. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  249. {
  250. LoggingContactListener::OnContactAdded(inBody1, inBody2, inManifold, ioSettings);
  251. JPH_ASSERT(ioSettings.mIsSensor == false);
  252. if (inBody1.GetID() == mBodyThatSeesSensorID || inBody2.GetID() == mBodyThatSeesSensorID)
  253. ioSettings.mIsSensor = true;
  254. }
  255. virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  256. {
  257. LoggingContactListener::OnContactPersisted(inBody1, inBody2, inManifold, ioSettings);
  258. JPH_ASSERT(ioSettings.mIsSensor == false);
  259. if (inBody1.GetID() == mBodyThatSeesSensorID || inBody2.GetID() == mBodyThatSeesSensorID)
  260. ioSettings.mIsSensor = true;
  261. }
  262. BodyID mBodyThatSeesSensorID;
  263. };
  264. // Register listener
  265. SensorOverridingListener listener;
  266. c.GetSystem()->SetContactListener(&listener);
  267. // Body that will appear as a sensor to one object and as static to another
  268. BodyID static_id = c.GetBodyInterface().CreateAndAddBody(BodyCreationSettings(new BoxShape(Vec3(5, 1, 5)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  269. // Dynamic body moving down that will do a normal collision
  270. Body &dynamic1 = c.CreateBox(Vec3(-2, 2, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f));
  271. dynamic1.SetAllowSleeping(false);
  272. dynamic1.SetLinearVelocity(Vec3(0, -1, 0));
  273. // Dynamic body moving down that will only see the static object as a sensor
  274. Body &dynamic2 = c.CreateBox(Vec3(2, 2, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f));
  275. dynamic2.SetAllowSleeping(false);
  276. dynamic2.SetLinearVelocity(Vec3(0, -1, 0));
  277. listener.mBodyThatSeesSensorID = dynamic2.GetID();
  278. // After a single step the dynamic object should not have touched the sensor yet
  279. c.SimulateSingleStep();
  280. CHECK(listener.GetEntryCount() == 0);
  281. // After half a second both bodies should be touching the sensor
  282. c.Simulate(0.5f);
  283. CHECK(listener.Contains(EType::Add, dynamic1.GetID(), static_id));
  284. CHECK(listener.Contains(EType::Add, dynamic2.GetID(), static_id));
  285. listener.Clear();
  286. // The next step we require that the contact persists
  287. c.SimulateSingleStep();
  288. CHECK(listener.Contains(EType::Persist, dynamic1.GetID(), static_id));
  289. CHECK(!listener.Contains(EType::Remove, dynamic1.GetID(), static_id));
  290. CHECK(listener.Contains(EType::Persist, dynamic2.GetID(), static_id));
  291. CHECK(!listener.Contains(EType::Remove, dynamic2.GetID(), static_id));
  292. listener.Clear();
  293. // After 3 more seconds one body should be resting on the static body, the other should have fallen through
  294. c.Simulate(3.0f + c.GetDeltaTime());
  295. CHECK(listener.Contains(EType::Persist, dynamic1.GetID(), static_id));
  296. CHECK(!listener.Contains(EType::Remove, dynamic1.GetID(), static_id));
  297. CHECK(listener.Contains(EType::Remove, dynamic2.GetID(), static_id));
  298. CHECK_APPROX_EQUAL(dynamic1.GetPosition(), Vec3(-2, 1.5f, 0), 5.0e-3f);
  299. CHECK_APPROX_EQUAL(dynamic2.GetPosition(), Vec3(2, -1.5f - 3.0f * c.GetDeltaTime(), 0), 1.0e-4f);
  300. }
  301. }