CollidePointTests.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  8. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  9. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  10. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  11. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  12. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  13. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  14. #include <Jolt/Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  15. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  16. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  17. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  18. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  19. #include <Jolt/Physics/Collision/CollidePointResult.h>
  20. TEST_SUITE("CollidePointTests")
  21. {
  22. // Probe directions in the direction of the faces
  23. static Vec3 cube_probes[] = {
  24. Vec3(-1.0f, 0, 0),
  25. Vec3(1.0f, 0, 0),
  26. Vec3(0, -1.0f, 0),
  27. Vec3(0, 1.0f, 0),
  28. Vec3(0, 0, -1.0f),
  29. Vec3(0, 0, 1.0f)
  30. };
  31. // Probe directions in the direction of the faces
  32. static Vec3 cube_and_zero_probes[] = {
  33. Vec3(0, 0, 0),
  34. Vec3(-1.0f, 0, 0),
  35. Vec3(1.0f, 0, 0),
  36. Vec3(0, -1.0f, 0),
  37. Vec3(0, 1.0f, 0),
  38. Vec3(0, 0, -1.0f),
  39. Vec3(0, 0, 1.0f)
  40. };
  41. // Probes in the xy-plane
  42. static Vec3 xy_probes[] = {
  43. Vec3(-1.0f, 0, 0),
  44. Vec3(1.0f, 0, 0),
  45. Vec3(0, 0, -1.0f),
  46. Vec3(0, 0, 1.0f)
  47. };
  48. // Probes in the xy-plane and zero
  49. static Vec3 xy_and_zero_probes[] = {
  50. Vec3(0, 0, 0),
  51. Vec3(-1.0f, 0, 0),
  52. Vec3(1.0f, 0, 0),
  53. Vec3(0, 0, -1.0f),
  54. Vec3(0, 0, 1.0f)
  55. };
  56. // Vertices of a cube
  57. static Vec3 cube_vertices[] = {
  58. Vec3(-1.0f, -1.0f, -1.0f),
  59. Vec3( 1.0f, -1.0f, -1.0f),
  60. Vec3(-1.0f, -1.0f, 1.0f),
  61. Vec3( 1.0f, -1.0f, 1.0f),
  62. Vec3(-1.0f, 1.0f, -1.0f),
  63. Vec3( 1.0f, 1.0f, -1.0f),
  64. Vec3(-1.0f, 1.0f, 1.0f),
  65. Vec3( 1.0f, 1.0f, 1.0f)
  66. };
  67. static void sTestHit(const Shape *inShape, Vec3Arg inPosition)
  68. {
  69. AllHitCollisionCollector<CollidePointCollector> collector;
  70. inShape->CollidePoint(inPosition - inShape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  71. CHECK(collector.mHits.size() == 1);
  72. }
  73. static void sTestHit(const NarrowPhaseQuery &inNarrowPhase, Vec3Arg inPosition, const BodyID &inBodyID)
  74. {
  75. AllHitCollisionCollector<CollidePointCollector> collector;
  76. inNarrowPhase.CollidePoint(inPosition, collector);
  77. CHECK(collector.mHits.size() == 1);
  78. CHECK(collector.mHits[0].mBodyID == inBodyID);
  79. }
  80. static void sTestMiss(const Shape *inShape, Vec3Arg inPosition)
  81. {
  82. AllHitCollisionCollector<CollidePointCollector> collector;
  83. inShape->CollidePoint(inPosition - inShape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  84. CHECK(collector.mHits.empty());
  85. }
  86. static void sTestMiss(const NarrowPhaseQuery &inNarrowPhase, Vec3Arg inPosition)
  87. {
  88. AllHitCollisionCollector<CollidePointCollector> collector;
  89. inNarrowPhase.CollidePoint(inPosition, collector);
  90. CHECK(collector.mHits.empty());
  91. }
  92. TEST_CASE("TestCollidePointVsBox")
  93. {
  94. Vec3 half_box_size(0.1f, 0.2f, 0.3f);
  95. ShapeRefC shape = new BoxShape(half_box_size);
  96. // Hits
  97. for (Vec3 probe : cube_and_zero_probes)
  98. sTestHit(shape, 0.99f * half_box_size * probe);
  99. // Misses
  100. for (Vec3 probe : cube_probes)
  101. sTestMiss(shape, 1.01f * half_box_size * probe);
  102. }
  103. TEST_CASE("TestCollidePointVsSphere")
  104. {
  105. const float radius = 0.1f;
  106. ShapeRefC shape = new SphereShape(radius);
  107. // Hits
  108. for (Vec3 probe : cube_and_zero_probes)
  109. sTestHit(shape, 0.99f * Vec3::sReplicate(radius) * probe);
  110. // Misses
  111. for (Vec3 probe : cube_probes)
  112. sTestMiss(shape, 1.01f * Vec3::sReplicate(radius) * probe);
  113. }
  114. TEST_CASE("TestCollidePointVsCapsule")
  115. {
  116. const float half_height = 0.2f;
  117. const float radius = 0.1f;
  118. ShapeRefC shape = new CapsuleShape(half_height, radius);
  119. // Top hits
  120. for (Vec3 probe : xy_and_zero_probes)
  121. sTestHit(shape, 0.99f * radius * probe + Vec3(0, half_height, 0));
  122. // Center hit
  123. sTestHit(shape, Vec3::sZero());
  124. // Bottom hits
  125. for (Vec3 probe : xy_and_zero_probes)
  126. sTestHit(shape, 0.99f * radius * probe + Vec3(0, -half_height, 0));
  127. // Misses
  128. for (Vec3 probe : cube_probes)
  129. sTestMiss(shape, 1.01f * Vec3(radius, half_height + radius, radius) * probe);
  130. }
  131. TEST_CASE("TestCollidePointVsTaperedCapsule")
  132. {
  133. const float half_height = 0.4f;
  134. const float top_radius = 0.1f;
  135. const float bottom_radius = 0.2f;
  136. TaperedCapsuleShapeSettings settings(half_height, top_radius, bottom_radius);
  137. ShapeRefC shape = settings.Create().Get();
  138. // Top hits
  139. for (Vec3 probe : xy_and_zero_probes)
  140. sTestHit(shape, 0.99f * top_radius * probe + Vec3(0, half_height, 0));
  141. // Center hit
  142. sTestHit(shape, Vec3::sZero());
  143. // Bottom hits
  144. for (Vec3 probe : xy_and_zero_probes)
  145. sTestHit(shape, 0.99f * bottom_radius * probe + Vec3(0, -half_height, 0));
  146. // Top misses
  147. sTestMiss(shape, Vec3(0, half_height + top_radius + 0.01f, 0));
  148. for (Vec3 probe : xy_probes)
  149. sTestMiss(shape, 1.01f * top_radius * probe + Vec3(0, half_height, 0));
  150. // Bottom misses
  151. sTestMiss(shape, Vec3(0, -half_height - bottom_radius - 0.01f, 0));
  152. for (Vec3 probe : xy_probes)
  153. sTestMiss(shape, 1.01f * bottom_radius * probe + Vec3(0, -half_height, 0));
  154. }
  155. TEST_CASE("TestCollidePointVsCylinder")
  156. {
  157. const float half_height = 0.2f;
  158. const float radius = 0.1f;
  159. ShapeRefC shape = new CylinderShape(half_height, radius);
  160. // Top hits
  161. for (Vec3 probe : xy_and_zero_probes)
  162. sTestHit(shape, 0.99f * (radius * probe + Vec3(0, half_height, 0)));
  163. // Center hit
  164. sTestHit(shape, Vec3::sZero());
  165. // Bottom hits
  166. for (Vec3 probe : xy_and_zero_probes)
  167. sTestHit(shape, 0.99f * (radius * probe + Vec3(0, -half_height, 0)));
  168. // Misses
  169. for (Vec3 probe : cube_probes)
  170. sTestMiss(shape, 1.01f * Vec3(radius, half_height, radius) * probe);
  171. }
  172. TEST_CASE("TestCollidePointVsConvexHull")
  173. {
  174. Vec3 half_box_size(0.1f, 0.2f, 0.3f);
  175. Vec3 offset(10.0f, 11.0f, 12.0f);
  176. ConvexHullShapeSettings settings;
  177. for (uint i = 0; i < size(cube_vertices); ++i)
  178. settings.mPoints.push_back(offset + cube_vertices[i] * half_box_size);
  179. ShapeRefC shape = settings.Create().Get();
  180. // Hits
  181. for (Vec3 probe : cube_and_zero_probes)
  182. sTestHit(shape, offset + 0.99f * half_box_size * probe);
  183. // Misses
  184. for (Vec3 probe : cube_probes)
  185. sTestMiss(shape, offset + 1.01f * half_box_size * probe);
  186. }
  187. TEST_CASE("TestCollidePointVsRotatedTranslated")
  188. {
  189. Vec3 translation(10.0f, 11.0f, 12.0f);
  190. Quat rotation = Quat::sRotation(Vec3(1, 2, 3).Normalized(), 0.3f * JPH_PI);
  191. Mat44 transform = Mat44::sRotationTranslation(rotation, translation);
  192. Vec3 half_box_size(0.1f, 0.2f, 0.3f);
  193. RotatedTranslatedShapeSettings settings(translation, rotation, new BoxShape(half_box_size));
  194. ShapeRefC shape = settings.Create().Get();
  195. // Hits
  196. for (Vec3 probe : cube_and_zero_probes)
  197. sTestHit(shape, transform * (0.99f * half_box_size * probe));
  198. // Misses
  199. for (Vec3 probe : cube_probes)
  200. sTestMiss(shape, transform * (1.01f * half_box_size * probe));
  201. }
  202. TEST_CASE("TestCollidePointVsScaled")
  203. {
  204. Vec3 scale(2.0f, 3.0f, -4.0f);
  205. Vec3 half_box_size(0.1f, 0.2f, 0.3f);
  206. ShapeRefC shape = new ScaledShape(new BoxShape(half_box_size), scale);
  207. // Hits
  208. for (Vec3 probe : cube_and_zero_probes)
  209. sTestHit(shape, scale * (0.99f * half_box_size * probe));
  210. // Misses
  211. for (Vec3 probe : cube_probes)
  212. sTestMiss(shape, scale * (1.01f * half_box_size * probe));
  213. }
  214. TEST_CASE("TestCollidePointVsOffsetCenterOfMass")
  215. {
  216. Vec3 offset(10.0f, 11.0f, 12.0f);
  217. Vec3 half_box_size(0.1f, 0.2f, 0.3f);
  218. OffsetCenterOfMassShapeSettings settings(offset, new BoxShape(half_box_size));
  219. ShapeRefC shape = settings.Create().Get();
  220. // Hits
  221. for (Vec3 probe : cube_and_zero_probes)
  222. sTestHit(shape, 0.99f * half_box_size * probe);
  223. // Misses
  224. for (Vec3 probe : cube_probes)
  225. sTestMiss(shape, 1.01f * half_box_size * probe);
  226. }
  227. TEST_CASE("TestCollidePointVsStaticCompound")
  228. {
  229. Vec3 translation1(10.0f, 11.0f, 12.0f);
  230. Quat rotation1 = Quat::sRotation(Vec3(1, 2, 3).Normalized(), 0.3f * JPH_PI);
  231. Mat44 transform1 = Mat44::sRotationTranslation(rotation1, translation1);
  232. Vec3 translation2(-1.0f, -2.0f, -3.0f);
  233. Quat rotation2 = Quat::sRotation(Vec3(4, 5, 6).Normalized(), 0.2f * JPH_PI);
  234. Mat44 transform2 = Mat44::sRotationTranslation(rotation2, translation2);
  235. Vec3 half_box_size(0.1f, 0.2f, 0.3f);
  236. ShapeRefC box = new BoxShape(half_box_size);
  237. StaticCompoundShapeSettings settings;
  238. settings.AddShape(translation1, rotation1, box);
  239. settings.AddShape(translation2, rotation2, box);
  240. ShapeRefC shape = settings.Create().Get();
  241. // Hits
  242. for (Vec3 probe : cube_and_zero_probes)
  243. {
  244. Vec3 point = 0.99f * half_box_size * probe;
  245. sTestHit(shape, transform1 * point);
  246. sTestHit(shape, transform2 * point);
  247. }
  248. // Misses
  249. for (Vec3 probe : cube_probes)
  250. {
  251. Vec3 point = 1.01f * half_box_size * probe;
  252. sTestMiss(shape, transform1 * point);
  253. sTestMiss(shape, transform2 * point);
  254. }
  255. }
  256. TEST_CASE("TestCollidePointVsMutableCompound")
  257. {
  258. Vec3 translation1(10.0f, 11.0f, 12.0f);
  259. Quat rotation1 = Quat::sRotation(Vec3(1, 2, 3).Normalized(), 0.3f * JPH_PI);
  260. Mat44 transform1 = Mat44::sRotationTranslation(rotation1, translation1);
  261. Vec3 translation2(-1.0f, -2.0f, -3.0f);
  262. Quat rotation2 = Quat::sRotation(Vec3(4, 5, 6).Normalized(), 0.2f * JPH_PI);
  263. Mat44 transform2 = Mat44::sRotationTranslation(rotation2, translation2);
  264. Vec3 half_box_size(0.1f, 0.2f, 0.3f);
  265. ShapeRefC box = new BoxShape(half_box_size);
  266. MutableCompoundShapeSettings settings;
  267. settings.AddShape(translation1, rotation1, box);
  268. settings.AddShape(translation2, rotation2, box);
  269. ShapeRefC shape = settings.Create().Get();
  270. // Hits
  271. for (Vec3 probe : cube_and_zero_probes)
  272. {
  273. Vec3 point = 0.99f * half_box_size * probe;
  274. sTestHit(shape, transform1 * point);
  275. sTestHit(shape, transform2 * point);
  276. }
  277. // Misses
  278. for (Vec3 probe : cube_probes)
  279. {
  280. Vec3 point = 1.01f * half_box_size * probe;
  281. sTestMiss(shape, transform1 * point);
  282. sTestMiss(shape, transform2 * point);
  283. }
  284. }
  285. TEST_CASE("TestCollidePointVsMesh")
  286. {
  287. // Face indices of a cube
  288. int indices[][3] = {
  289. { 0, 1, 3 },
  290. { 0, 3, 2 },
  291. { 4, 7, 5 },
  292. { 4, 6, 7 },
  293. { 2, 3, 6 },
  294. { 3, 7, 6 },
  295. { 1, 0, 4 },
  296. { 1, 4, 5 },
  297. { 1, 7, 3 },
  298. { 1, 5, 7 },
  299. { 0, 2, 6 },
  300. { 0, 6, 4 }
  301. };
  302. const int grid_size = 2;
  303. UnitTestRandom random;
  304. uniform_real_distribution<float> range(0.1f, 0.3f);
  305. // Create a grid of closed shapes
  306. MeshShapeSettings settings;
  307. settings.SetEmbedded();
  308. int num_cubes = Cubed(2 * grid_size + 1);
  309. settings.mTriangleVertices.reserve(num_cubes * size(cube_vertices));
  310. settings.mIndexedTriangles.reserve(num_cubes * size(indices));
  311. for (int x = -grid_size; x <= grid_size; ++x)
  312. for (int y = -grid_size; y <= grid_size; ++y)
  313. for (int z = -grid_size; z <= grid_size; ++z)
  314. {
  315. Vec3 center((float)x, (float)y, (float)z);
  316. // Create vertices with randomness
  317. uint vtx = (uint)settings.mTriangleVertices.size();
  318. settings.mTriangleVertices.resize(vtx + size(cube_vertices));
  319. for (uint i = 0; i < size(cube_vertices); ++i)
  320. {
  321. Vec3 vertex(center + cube_vertices[i] * Vec3(range(random), range(random), range(random)));
  322. vertex.StoreFloat3(&settings.mTriangleVertices[vtx + i]);
  323. }
  324. // Flip inside out? (inside out shapes should act the same as normal shapes for CollidePoint)
  325. bool flip = (y & 1) == 0;
  326. // Create face indices
  327. uint idx = (uint)settings.mIndexedTriangles.size();
  328. settings.mIndexedTriangles.resize(idx + size(indices));
  329. for (uint i = 0; i < size(indices); ++i)
  330. settings.mIndexedTriangles[idx + i] = IndexedTriangle(vtx + indices[i][0], vtx + indices[i][flip? 2 : 1], vtx + indices[i][flip? 1 : 2]);
  331. }
  332. // Create body with random orientation
  333. PhysicsTestContext context;
  334. Body &mesh_body = context.CreateBody(&settings, Vec3::sRandom(random), Quat::sRandom(random), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  335. // Get the shape
  336. ShapeRefC mesh_shape = mesh_body.GetShape();
  337. // Get narrow phase
  338. const NarrowPhaseQuery &narrow_phase = context.GetSystem()->GetNarrowPhaseQuery();
  339. // Get transform
  340. Mat44 body_transform = mesh_body.GetWorldTransform();
  341. CHECK(body_transform != Mat44::sIdentity());
  342. // Test points
  343. for (int x = -grid_size; x <= grid_size; ++x)
  344. for (int y = -grid_size; y <= grid_size; ++y)
  345. for (int z = -grid_size; z <= grid_size; ++z)
  346. {
  347. Vec3 center((float)x, (float)y, (float)z);
  348. // The center point should hit
  349. sTestHit(mesh_shape, center);
  350. sTestHit(narrow_phase, body_transform * center, mesh_body.GetID());
  351. // Points outside the hull should not hit
  352. for (Vec3 probe : cube_probes)
  353. {
  354. Vec3 point = center + 0.4f * probe;
  355. sTestMiss(mesh_shape, point);
  356. sTestMiss(narrow_phase, body_transform * point);
  357. }
  358. }
  359. }
  360. }