collision_solver_2d_sat.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /*************************************************************************/
  2. /* collision_solver_2d_sat.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "collision_solver_2d_sat.h"
  31. #include "core/math/geometry.h"
  32. struct _CollectorCallback2D {
  33. CollisionSolver2DSW::CallbackResult callback;
  34. void *userdata;
  35. bool swap;
  36. bool collided;
  37. Vector2 normal;
  38. Vector2 *sep_axis;
  39. _FORCE_INLINE_ void call(const Vector2 &p_point_A, const Vector2 &p_point_B) {
  40. /*
  41. if (normal.dot(p_point_A) >= normal.dot(p_point_B))
  42. return;
  43. */
  44. if (swap) {
  45. callback(p_point_B, p_point_A, userdata);
  46. } else {
  47. callback(p_point_A, p_point_B, userdata);
  48. }
  49. }
  50. };
  51. typedef void (*GenerateContactsFunc)(const Vector2 *, int, const Vector2 *, int, _CollectorCallback2D *);
  52. _FORCE_INLINE_ static void _generate_contacts_point_point(const Vector2 *p_points_A, int p_point_count_A, const Vector2 *p_points_B, int p_point_count_B, _CollectorCallback2D *p_collector) {
  53. #ifdef DEBUG_ENABLED
  54. ERR_FAIL_COND(p_point_count_A != 1);
  55. ERR_FAIL_COND(p_point_count_B != 1);
  56. #endif
  57. p_collector->call(*p_points_A, *p_points_B);
  58. }
  59. _FORCE_INLINE_ static void _generate_contacts_point_edge(const Vector2 *p_points_A, int p_point_count_A, const Vector2 *p_points_B, int p_point_count_B, _CollectorCallback2D *p_collector) {
  60. #ifdef DEBUG_ENABLED
  61. ERR_FAIL_COND(p_point_count_A != 1);
  62. ERR_FAIL_COND(p_point_count_B != 2);
  63. #endif
  64. Vector2 closest_B = Geometry::get_closest_point_to_segment_uncapped_2d(*p_points_A, p_points_B);
  65. p_collector->call(*p_points_A, closest_B);
  66. }
  67. struct _generate_contacts_Pair {
  68. bool a;
  69. int idx;
  70. real_t d;
  71. _FORCE_INLINE_ bool operator<(const _generate_contacts_Pair &l) const { return d < l.d; }
  72. };
  73. _FORCE_INLINE_ static void _generate_contacts_edge_edge(const Vector2 *p_points_A, int p_point_count_A, const Vector2 *p_points_B, int p_point_count_B, _CollectorCallback2D *p_collector) {
  74. #ifdef DEBUG_ENABLED
  75. ERR_FAIL_COND(p_point_count_A != 2);
  76. ERR_FAIL_COND(p_point_count_B != 2); // circle is actually a 4x3 matrix
  77. #endif
  78. Vector2 n = p_collector->normal;
  79. Vector2 t = n.tangent();
  80. real_t dA = n.dot(p_points_A[0]);
  81. real_t dB = n.dot(p_points_B[0]);
  82. _generate_contacts_Pair dvec[4];
  83. dvec[0].d = t.dot(p_points_A[0]);
  84. dvec[0].a = true;
  85. dvec[0].idx = 0;
  86. dvec[1].d = t.dot(p_points_A[1]);
  87. dvec[1].a = true;
  88. dvec[1].idx = 1;
  89. dvec[2].d = t.dot(p_points_B[0]);
  90. dvec[2].a = false;
  91. dvec[2].idx = 0;
  92. dvec[3].d = t.dot(p_points_B[1]);
  93. dvec[3].a = false;
  94. dvec[3].idx = 1;
  95. SortArray<_generate_contacts_Pair> sa;
  96. sa.sort(dvec, 4);
  97. for (int i = 1; i <= 2; i++) {
  98. if (dvec[i].a) {
  99. Vector2 a = p_points_A[dvec[i].idx];
  100. Vector2 b = n.plane_project(dB, a);
  101. if (n.dot(a) > n.dot(b) - CMP_EPSILON) {
  102. continue;
  103. }
  104. p_collector->call(a, b);
  105. } else {
  106. Vector2 b = p_points_B[dvec[i].idx];
  107. Vector2 a = n.plane_project(dA, b);
  108. if (n.dot(a) > n.dot(b) - CMP_EPSILON) {
  109. continue;
  110. }
  111. p_collector->call(a, b);
  112. }
  113. }
  114. }
  115. static void _generate_contacts_from_supports(const Vector2 *p_points_A, int p_point_count_A, const Vector2 *p_points_B, int p_point_count_B, _CollectorCallback2D *p_collector) {
  116. #ifdef DEBUG_ENABLED
  117. ERR_FAIL_COND(p_point_count_A < 1);
  118. ERR_FAIL_COND(p_point_count_B < 1);
  119. #endif
  120. static const GenerateContactsFunc generate_contacts_func_table[2][2] = {
  121. {
  122. _generate_contacts_point_point,
  123. _generate_contacts_point_edge,
  124. },
  125. {
  126. nullptr,
  127. _generate_contacts_edge_edge,
  128. }
  129. };
  130. int pointcount_B;
  131. int pointcount_A;
  132. const Vector2 *points_A;
  133. const Vector2 *points_B;
  134. if (p_point_count_A > p_point_count_B) {
  135. //swap
  136. p_collector->swap = !p_collector->swap;
  137. p_collector->normal = -p_collector->normal;
  138. pointcount_B = p_point_count_A;
  139. pointcount_A = p_point_count_B;
  140. points_A = p_points_B;
  141. points_B = p_points_A;
  142. } else {
  143. pointcount_B = p_point_count_B;
  144. pointcount_A = p_point_count_A;
  145. points_A = p_points_A;
  146. points_B = p_points_B;
  147. }
  148. int version_A = (pointcount_A > 2 ? 2 : pointcount_A) - 1;
  149. int version_B = (pointcount_B > 2 ? 2 : pointcount_B) - 1;
  150. GenerateContactsFunc contacts_func = generate_contacts_func_table[version_A][version_B];
  151. ERR_FAIL_COND(!contacts_func);
  152. contacts_func(points_A, pointcount_A, points_B, pointcount_B, p_collector);
  153. }
  154. template <class ShapeA, class ShapeB, bool castA = false, bool castB = false, bool withMargin = false>
  155. class SeparatorAxisTest2D {
  156. const ShapeA *shape_A;
  157. const ShapeB *shape_B;
  158. const Transform2D *transform_A;
  159. const Transform2D *transform_B;
  160. real_t best_depth;
  161. Vector2 best_axis;
  162. int best_axis_count;
  163. int best_axis_index;
  164. Vector2 motion_A;
  165. Vector2 motion_B;
  166. real_t margin_A;
  167. real_t margin_B;
  168. _CollectorCallback2D *callback;
  169. public:
  170. _FORCE_INLINE_ bool test_previous_axis() {
  171. if (callback && callback->sep_axis && *callback->sep_axis != Vector2()) {
  172. return test_axis(*callback->sep_axis);
  173. } else {
  174. #ifdef DEBUG_ENABLED
  175. best_axis_count++;
  176. #endif
  177. }
  178. return true;
  179. }
  180. _FORCE_INLINE_ bool test_cast() {
  181. if (castA) {
  182. Vector2 na = motion_A.normalized();
  183. if (!test_axis(na)) {
  184. return false;
  185. }
  186. if (!test_axis(na.tangent())) {
  187. return false;
  188. }
  189. }
  190. if (castB) {
  191. Vector2 nb = motion_B.normalized();
  192. if (!test_axis(nb)) {
  193. return false;
  194. }
  195. if (!test_axis(nb.tangent())) {
  196. return false;
  197. }
  198. }
  199. return true;
  200. }
  201. _FORCE_INLINE_ bool test_axis(const Vector2 &p_axis) {
  202. Vector2 axis = p_axis;
  203. if (Math::is_zero_approx(axis.x) &&
  204. Math::is_zero_approx(axis.y)) {
  205. // strange case, try an upwards separator
  206. axis = Vector2(0.0, 1.0);
  207. }
  208. real_t min_A, max_A, min_B, max_B;
  209. if (castA) {
  210. shape_A->project_range_cast(motion_A, axis, *transform_A, min_A, max_A);
  211. } else {
  212. shape_A->project_range(axis, *transform_A, min_A, max_A);
  213. }
  214. if (castB) {
  215. shape_B->project_range_cast(motion_B, axis, *transform_B, min_B, max_B);
  216. } else {
  217. shape_B->project_range(axis, *transform_B, min_B, max_B);
  218. }
  219. if (withMargin) {
  220. min_A -= margin_A;
  221. max_A += margin_A;
  222. min_B -= margin_B;
  223. max_B += margin_B;
  224. }
  225. min_B -= (max_A - min_A) * 0.5;
  226. max_B += (max_A - min_A) * 0.5;
  227. real_t dmin = min_B - (min_A + max_A) * 0.5;
  228. real_t dmax = max_B - (min_A + max_A) * 0.5;
  229. if (dmin > 0.0 || dmax < 0.0) {
  230. if (callback && callback->sep_axis) {
  231. *callback->sep_axis = axis;
  232. }
  233. #ifdef DEBUG_ENABLED
  234. best_axis_count++;
  235. #endif
  236. return false; // doesn't contain 0
  237. }
  238. //use the smallest depth
  239. dmin = Math::abs(dmin);
  240. if (dmax < dmin) {
  241. if (dmax < best_depth) {
  242. best_depth = dmax;
  243. best_axis = axis;
  244. #ifdef DEBUG_ENABLED
  245. best_axis_index = best_axis_count;
  246. #endif
  247. }
  248. } else {
  249. if (dmin < best_depth) {
  250. best_depth = dmin;
  251. best_axis = -axis; // keep it as A axis
  252. #ifdef DEBUG_ENABLED
  253. best_axis_index = best_axis_count;
  254. #endif
  255. }
  256. }
  257. #ifdef DEBUG_ENABLED
  258. best_axis_count++;
  259. #endif
  260. return true;
  261. }
  262. _FORCE_INLINE_ void generate_contacts() {
  263. // nothing to do, don't generate
  264. if (best_axis == Vector2(0.0, 0.0)) {
  265. return;
  266. }
  267. if (callback) {
  268. callback->collided = true;
  269. if (!callback->callback) {
  270. return; //only collide, no callback
  271. }
  272. }
  273. static const int max_supports = 2;
  274. Vector2 supports_A[max_supports];
  275. int support_count_A;
  276. if (castA) {
  277. shape_A->get_supports_transformed_cast(motion_A, -best_axis, *transform_A, supports_A, support_count_A);
  278. } else {
  279. shape_A->get_supports(transform_A->basis_xform_inv(-best_axis).normalized(), supports_A, support_count_A);
  280. for (int i = 0; i < support_count_A; i++) {
  281. supports_A[i] = transform_A->xform(supports_A[i]);
  282. }
  283. }
  284. if (withMargin) {
  285. for (int i = 0; i < support_count_A; i++) {
  286. supports_A[i] += -best_axis * margin_A;
  287. }
  288. }
  289. Vector2 supports_B[max_supports];
  290. int support_count_B;
  291. if (castB) {
  292. shape_B->get_supports_transformed_cast(motion_B, best_axis, *transform_B, supports_B, support_count_B);
  293. } else {
  294. shape_B->get_supports(transform_B->basis_xform_inv(best_axis).normalized(), supports_B, support_count_B);
  295. for (int i = 0; i < support_count_B; i++) {
  296. supports_B[i] = transform_B->xform(supports_B[i]);
  297. }
  298. }
  299. if (withMargin) {
  300. for (int i = 0; i < support_count_B; i++) {
  301. supports_B[i] += best_axis * margin_B;
  302. }
  303. }
  304. if (callback) {
  305. callback->normal = best_axis;
  306. _generate_contacts_from_supports(supports_A, support_count_A, supports_B, support_count_B, callback);
  307. if (callback->sep_axis && *callback->sep_axis != Vector2()) {
  308. *callback->sep_axis = Vector2(); //invalidate previous axis (no test)
  309. }
  310. }
  311. }
  312. _FORCE_INLINE_ SeparatorAxisTest2D(const ShapeA *p_shape_A, const Transform2D &p_transform_a, const ShapeB *p_shape_B, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_A = Vector2(), const Vector2 &p_motion_B = Vector2(), real_t p_margin_A = 0, real_t p_margin_B = 0) {
  313. margin_A = p_margin_A;
  314. margin_B = p_margin_B;
  315. best_depth = 1e15;
  316. shape_A = p_shape_A;
  317. shape_B = p_shape_B;
  318. transform_A = &p_transform_a;
  319. transform_B = &p_transform_b;
  320. motion_A = p_motion_A;
  321. motion_B = p_motion_B;
  322. callback = p_collector;
  323. #ifdef DEBUG_ENABLED
  324. best_axis_count = 0;
  325. best_axis_index = -1;
  326. #endif
  327. }
  328. };
  329. /****** SAT TESTS *******/
  330. #define TEST_POINT(m_a, m_b) \
  331. ((!separator.test_axis(((m_a) - (m_b)).normalized())) || \
  332. (castA && !separator.test_axis(((m_a) + p_motion_a - (m_b)).normalized())) || \
  333. (castB && !separator.test_axis(((m_a) - ((m_b) + p_motion_b)).normalized())) || \
  334. (castA && castB && !separator.test_axis(((m_a) + p_motion_a - ((m_b) + p_motion_b)).normalized())))
  335. typedef void (*CollisionFunc)(const Shape2DSW *, const Transform2D &, const Shape2DSW *, const Transform2D &, _CollectorCallback2D *p_collector, const Vector2 &, const Vector2 &, real_t, real_t);
  336. template <bool castA, bool castB, bool withMargin>
  337. static void _collision_segment_segment(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  338. const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
  339. const SegmentShape2DSW *segment_B = static_cast<const SegmentShape2DSW *>(p_b);
  340. SeparatorAxisTest2D<SegmentShape2DSW, SegmentShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, segment_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  341. if (!separator.test_previous_axis()) {
  342. return;
  343. }
  344. //this collision is kind of pointless
  345. if (!separator.test_cast()) {
  346. return;
  347. }
  348. if (!separator.test_axis(segment_A->get_xformed_normal(p_transform_a))) {
  349. return;
  350. }
  351. if (!separator.test_axis(segment_B->get_xformed_normal(p_transform_b))) {
  352. return;
  353. }
  354. if (withMargin) {
  355. //points grow to circles
  356. if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), p_transform_b.xform(segment_B->get_a()))) {
  357. return;
  358. }
  359. if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), p_transform_b.xform(segment_B->get_b()))) {
  360. return;
  361. }
  362. if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), p_transform_b.xform(segment_B->get_a()))) {
  363. return;
  364. }
  365. if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), p_transform_b.xform(segment_B->get_b()))) {
  366. return;
  367. }
  368. }
  369. separator.generate_contacts();
  370. }
  371. template <bool castA, bool castB, bool withMargin>
  372. static void _collision_segment_circle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  373. const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
  374. const CircleShape2DSW *circle_B = static_cast<const CircleShape2DSW *>(p_b);
  375. SeparatorAxisTest2D<SegmentShape2DSW, CircleShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, circle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  376. if (!separator.test_previous_axis()) {
  377. return;
  378. }
  379. if (!separator.test_cast()) {
  380. return;
  381. }
  382. //segment normal
  383. if (!separator.test_axis(
  384. (p_transform_a.xform(segment_A->get_b()) - p_transform_a.xform(segment_A->get_a())).normalized().tangent())) {
  385. return;
  386. }
  387. //endpoint a vs circle
  388. if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), p_transform_b.get_origin())) {
  389. return;
  390. }
  391. //endpoint b vs circle
  392. if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), p_transform_b.get_origin())) {
  393. return;
  394. }
  395. separator.generate_contacts();
  396. }
  397. template <bool castA, bool castB, bool withMargin>
  398. static void _collision_segment_rectangle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  399. const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
  400. const RectangleShape2DSW *rectangle_B = static_cast<const RectangleShape2DSW *>(p_b);
  401. SeparatorAxisTest2D<SegmentShape2DSW, RectangleShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  402. if (!separator.test_previous_axis()) {
  403. return;
  404. }
  405. if (!separator.test_cast()) {
  406. return;
  407. }
  408. if (!separator.test_axis(segment_A->get_xformed_normal(p_transform_a))) {
  409. return;
  410. }
  411. if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
  412. return;
  413. }
  414. if (!separator.test_axis(p_transform_b.elements[1].normalized())) {
  415. return;
  416. }
  417. if (withMargin) {
  418. Transform2D inv = p_transform_b.affine_inverse();
  419. Vector2 a = p_transform_a.xform(segment_A->get_a());
  420. Vector2 b = p_transform_a.xform(segment_A->get_b());
  421. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a))) {
  422. return;
  423. }
  424. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, b))) {
  425. return;
  426. }
  427. if (castA) {
  428. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a + p_motion_a))) {
  429. return;
  430. }
  431. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, b + p_motion_a))) {
  432. return;
  433. }
  434. }
  435. if (castB) {
  436. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a - p_motion_b))) {
  437. return;
  438. }
  439. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, b - p_motion_b))) {
  440. return;
  441. }
  442. }
  443. if (castA && castB) {
  444. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, a - p_motion_b + p_motion_a))) {
  445. return;
  446. }
  447. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, inv, b - p_motion_b + p_motion_a))) {
  448. return;
  449. }
  450. }
  451. }
  452. separator.generate_contacts();
  453. }
  454. template <bool castA, bool castB, bool withMargin>
  455. static void _collision_segment_capsule(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  456. const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
  457. const CapsuleShape2DSW *capsule_B = static_cast<const CapsuleShape2DSW *>(p_b);
  458. SeparatorAxisTest2D<SegmentShape2DSW, CapsuleShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  459. if (!separator.test_previous_axis()) {
  460. return;
  461. }
  462. if (!separator.test_cast()) {
  463. return;
  464. }
  465. if (!separator.test_axis(segment_A->get_xformed_normal(p_transform_a))) {
  466. return;
  467. }
  468. if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
  469. return;
  470. }
  471. if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * 0.5))) {
  472. return;
  473. }
  474. if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * -0.5))) {
  475. return;
  476. }
  477. if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * 0.5))) {
  478. return;
  479. }
  480. if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * -0.5))) {
  481. return;
  482. }
  483. separator.generate_contacts();
  484. }
  485. template <bool castA, bool castB, bool withMargin>
  486. static void _collision_segment_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  487. const SegmentShape2DSW *segment_A = static_cast<const SegmentShape2DSW *>(p_a);
  488. const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
  489. SeparatorAxisTest2D<SegmentShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(segment_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  490. if (!separator.test_previous_axis()) {
  491. return;
  492. }
  493. if (!separator.test_cast()) {
  494. return;
  495. }
  496. if (!separator.test_axis(segment_A->get_xformed_normal(p_transform_a))) {
  497. return;
  498. }
  499. for (int i = 0; i < convex_B->get_point_count(); i++) {
  500. if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
  501. return;
  502. }
  503. if (withMargin) {
  504. if (TEST_POINT(p_transform_a.xform(segment_A->get_a()), p_transform_b.xform(convex_B->get_point(i)))) {
  505. return;
  506. }
  507. if (TEST_POINT(p_transform_a.xform(segment_A->get_b()), p_transform_b.xform(convex_B->get_point(i)))) {
  508. return;
  509. }
  510. }
  511. }
  512. separator.generate_contacts();
  513. }
  514. /////////
  515. template <bool castA, bool castB, bool withMargin>
  516. static void _collision_circle_circle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  517. const CircleShape2DSW *circle_A = static_cast<const CircleShape2DSW *>(p_a);
  518. const CircleShape2DSW *circle_B = static_cast<const CircleShape2DSW *>(p_b);
  519. SeparatorAxisTest2D<CircleShape2DSW, CircleShape2DSW, castA, castB, withMargin> separator(circle_A, p_transform_a, circle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  520. if (!separator.test_previous_axis()) {
  521. return;
  522. }
  523. if (!separator.test_cast()) {
  524. return;
  525. }
  526. if (TEST_POINT(p_transform_a.get_origin(), p_transform_b.get_origin())) {
  527. return;
  528. }
  529. separator.generate_contacts();
  530. }
  531. template <bool castA, bool castB, bool withMargin>
  532. static void _collision_circle_rectangle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  533. const CircleShape2DSW *circle_A = static_cast<const CircleShape2DSW *>(p_a);
  534. const RectangleShape2DSW *rectangle_B = static_cast<const RectangleShape2DSW *>(p_b);
  535. SeparatorAxisTest2D<CircleShape2DSW, RectangleShape2DSW, castA, castB, withMargin> separator(circle_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  536. if (!separator.test_previous_axis()) {
  537. return;
  538. }
  539. if (!separator.test_cast()) {
  540. return;
  541. }
  542. const Vector2 &sphere = p_transform_a.elements[2];
  543. const Vector2 *axis = &p_transform_b.elements[0];
  544. //const Vector2& half_extents = rectangle_B->get_half_extents();
  545. if (!separator.test_axis(axis[0].normalized())) {
  546. return;
  547. }
  548. if (!separator.test_axis(axis[1].normalized())) {
  549. return;
  550. }
  551. Transform2D binv = p_transform_b.affine_inverse();
  552. {
  553. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphere))) {
  554. return;
  555. }
  556. }
  557. if (castA) {
  558. Vector2 sphereofs = sphere + p_motion_a;
  559. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphereofs))) {
  560. return;
  561. }
  562. }
  563. if (castB) {
  564. Vector2 sphereofs = sphere - p_motion_b;
  565. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphereofs))) {
  566. return;
  567. }
  568. }
  569. if (castA && castB) {
  570. Vector2 sphereofs = sphere - p_motion_b + p_motion_a;
  571. if (!separator.test_axis(rectangle_B->get_circle_axis(p_transform_b, binv, sphereofs))) {
  572. return;
  573. }
  574. }
  575. separator.generate_contacts();
  576. }
  577. template <bool castA, bool castB, bool withMargin>
  578. static void _collision_circle_capsule(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  579. const CircleShape2DSW *circle_A = static_cast<const CircleShape2DSW *>(p_a);
  580. const CapsuleShape2DSW *capsule_B = static_cast<const CapsuleShape2DSW *>(p_b);
  581. SeparatorAxisTest2D<CircleShape2DSW, CapsuleShape2DSW, castA, castB, withMargin> separator(circle_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  582. if (!separator.test_previous_axis()) {
  583. return;
  584. }
  585. if (!separator.test_cast()) {
  586. return;
  587. }
  588. //capsule axis
  589. if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
  590. return;
  591. }
  592. //capsule endpoints
  593. if (TEST_POINT(p_transform_a.get_origin(), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * 0.5))) {
  594. return;
  595. }
  596. if (TEST_POINT(p_transform_a.get_origin(), (p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * -0.5))) {
  597. return;
  598. }
  599. separator.generate_contacts();
  600. }
  601. template <bool castA, bool castB, bool withMargin>
  602. static void _collision_circle_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  603. const CircleShape2DSW *circle_A = static_cast<const CircleShape2DSW *>(p_a);
  604. const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
  605. SeparatorAxisTest2D<CircleShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(circle_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  606. if (!separator.test_previous_axis()) {
  607. return;
  608. }
  609. if (!separator.test_cast()) {
  610. return;
  611. }
  612. //poly faces and poly points vs circle
  613. for (int i = 0; i < convex_B->get_point_count(); i++) {
  614. if (TEST_POINT(p_transform_a.get_origin(), p_transform_b.xform(convex_B->get_point(i)))) {
  615. return;
  616. }
  617. if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
  618. return;
  619. }
  620. }
  621. separator.generate_contacts();
  622. }
  623. /////////
  624. template <bool castA, bool castB, bool withMargin>
  625. static void _collision_rectangle_rectangle(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  626. const RectangleShape2DSW *rectangle_A = static_cast<const RectangleShape2DSW *>(p_a);
  627. const RectangleShape2DSW *rectangle_B = static_cast<const RectangleShape2DSW *>(p_b);
  628. SeparatorAxisTest2D<RectangleShape2DSW, RectangleShape2DSW, castA, castB, withMargin> separator(rectangle_A, p_transform_a, rectangle_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  629. if (!separator.test_previous_axis()) {
  630. return;
  631. }
  632. if (!separator.test_cast()) {
  633. return;
  634. }
  635. //box faces A
  636. if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
  637. return;
  638. }
  639. if (!separator.test_axis(p_transform_a.elements[1].normalized())) {
  640. return;
  641. }
  642. //box faces B
  643. if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
  644. return;
  645. }
  646. if (!separator.test_axis(p_transform_b.elements[1].normalized())) {
  647. return;
  648. }
  649. if (withMargin) {
  650. Transform2D invA = p_transform_a.affine_inverse();
  651. Transform2D invB = p_transform_b.affine_inverse();
  652. if (!separator.test_axis(rectangle_A->get_box_axis(p_transform_a, invA, rectangle_B, p_transform_b, invB))) {
  653. return;
  654. }
  655. if (castA || castB) {
  656. Transform2D aofs = p_transform_a;
  657. aofs.elements[2] += p_motion_a;
  658. Transform2D bofs = p_transform_b;
  659. bofs.elements[2] += p_motion_b;
  660. Transform2D aofsinv = aofs.affine_inverse();
  661. Transform2D bofsinv = bofs.affine_inverse();
  662. if (castA) {
  663. if (!separator.test_axis(rectangle_A->get_box_axis(aofs, aofsinv, rectangle_B, p_transform_b, invB))) {
  664. return;
  665. }
  666. }
  667. if (castB) {
  668. if (!separator.test_axis(rectangle_A->get_box_axis(p_transform_a, invA, rectangle_B, bofs, bofsinv))) {
  669. return;
  670. }
  671. }
  672. if (castA && castB) {
  673. if (!separator.test_axis(rectangle_A->get_box_axis(aofs, aofsinv, rectangle_B, bofs, bofsinv))) {
  674. return;
  675. }
  676. }
  677. }
  678. }
  679. separator.generate_contacts();
  680. }
  681. template <bool castA, bool castB, bool withMargin>
  682. static void _collision_rectangle_capsule(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  683. const RectangleShape2DSW *rectangle_A = static_cast<const RectangleShape2DSW *>(p_a);
  684. const CapsuleShape2DSW *capsule_B = static_cast<const CapsuleShape2DSW *>(p_b);
  685. SeparatorAxisTest2D<RectangleShape2DSW, CapsuleShape2DSW, castA, castB, withMargin> separator(rectangle_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  686. if (!separator.test_previous_axis()) {
  687. return;
  688. }
  689. if (!separator.test_cast()) {
  690. return;
  691. }
  692. //box faces
  693. if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
  694. return;
  695. }
  696. if (!separator.test_axis(p_transform_a.elements[1].normalized())) {
  697. return;
  698. }
  699. //capsule axis
  700. if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
  701. return;
  702. }
  703. //box endpoints to capsule circles
  704. Transform2D boxinv = p_transform_a.affine_inverse();
  705. for (int i = 0; i < 2; i++) {
  706. {
  707. Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (i == 0 ? 0.5 : -0.5);
  708. if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, capsule_endpoint))) {
  709. return;
  710. }
  711. }
  712. if (castA) {
  713. Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (i == 0 ? 0.5 : -0.5);
  714. capsule_endpoint -= p_motion_a;
  715. if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, capsule_endpoint))) {
  716. return;
  717. }
  718. }
  719. if (castB) {
  720. Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (i == 0 ? 0.5 : -0.5);
  721. capsule_endpoint += p_motion_b;
  722. if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, capsule_endpoint))) {
  723. return;
  724. }
  725. }
  726. if (castA && castB) {
  727. Vector2 capsule_endpoint = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (i == 0 ? 0.5 : -0.5);
  728. capsule_endpoint -= p_motion_a;
  729. capsule_endpoint += p_motion_b;
  730. if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, capsule_endpoint))) {
  731. return;
  732. }
  733. }
  734. }
  735. separator.generate_contacts();
  736. }
  737. template <bool castA, bool castB, bool withMargin>
  738. static void _collision_rectangle_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  739. const RectangleShape2DSW *rectangle_A = static_cast<const RectangleShape2DSW *>(p_a);
  740. const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
  741. SeparatorAxisTest2D<RectangleShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(rectangle_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  742. if (!separator.test_previous_axis()) {
  743. return;
  744. }
  745. if (!separator.test_cast()) {
  746. return;
  747. }
  748. //box faces
  749. if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
  750. return;
  751. }
  752. if (!separator.test_axis(p_transform_a.elements[1].normalized())) {
  753. return;
  754. }
  755. //convex faces
  756. Transform2D boxinv;
  757. if (withMargin) {
  758. boxinv = p_transform_a.affine_inverse();
  759. }
  760. for (int i = 0; i < convex_B->get_point_count(); i++) {
  761. if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
  762. return;
  763. }
  764. if (withMargin) {
  765. //all points vs all points need to be tested if margin exist
  766. if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i))))) {
  767. return;
  768. }
  769. if (castA) {
  770. if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i)) - p_motion_a))) {
  771. return;
  772. }
  773. }
  774. if (castB) {
  775. if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i)) + p_motion_b))) {
  776. return;
  777. }
  778. }
  779. if (castA && castB) {
  780. if (!separator.test_axis(rectangle_A->get_circle_axis(p_transform_a, boxinv, p_transform_b.xform(convex_B->get_point(i)) + p_motion_b - p_motion_a))) {
  781. return;
  782. }
  783. }
  784. }
  785. }
  786. separator.generate_contacts();
  787. }
  788. /////////
  789. template <bool castA, bool castB, bool withMargin>
  790. static void _collision_capsule_capsule(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  791. const CapsuleShape2DSW *capsule_A = static_cast<const CapsuleShape2DSW *>(p_a);
  792. const CapsuleShape2DSW *capsule_B = static_cast<const CapsuleShape2DSW *>(p_b);
  793. SeparatorAxisTest2D<CapsuleShape2DSW, CapsuleShape2DSW, castA, castB, withMargin> separator(capsule_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  794. if (!separator.test_previous_axis()) {
  795. return;
  796. }
  797. if (!separator.test_cast()) {
  798. return;
  799. }
  800. //capsule axis
  801. if (!separator.test_axis(p_transform_b.elements[0].normalized())) {
  802. return;
  803. }
  804. if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
  805. return;
  806. }
  807. //capsule endpoints
  808. for (int i = 0; i < 2; i++) {
  809. Vector2 capsule_endpoint_A = p_transform_a.get_origin() + p_transform_a.elements[1] * capsule_A->get_height() * (i == 0 ? 0.5 : -0.5);
  810. for (int j = 0; j < 2; j++) {
  811. Vector2 capsule_endpoint_B = p_transform_b.get_origin() + p_transform_b.elements[1] * capsule_B->get_height() * (j == 0 ? 0.5 : -0.5);
  812. if (TEST_POINT(capsule_endpoint_A, capsule_endpoint_B)) {
  813. return;
  814. }
  815. }
  816. }
  817. separator.generate_contacts();
  818. }
  819. template <bool castA, bool castB, bool withMargin>
  820. static void _collision_capsule_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  821. const CapsuleShape2DSW *capsule_A = static_cast<const CapsuleShape2DSW *>(p_a);
  822. const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
  823. SeparatorAxisTest2D<CapsuleShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(capsule_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  824. if (!separator.test_previous_axis()) {
  825. return;
  826. }
  827. if (!separator.test_cast()) {
  828. return;
  829. }
  830. //capsule axis
  831. if (!separator.test_axis(p_transform_a.elements[0].normalized())) {
  832. return;
  833. }
  834. //poly vs capsule
  835. for (int i = 0; i < convex_B->get_point_count(); i++) {
  836. Vector2 cpoint = p_transform_b.xform(convex_B->get_point(i));
  837. for (int j = 0; j < 2; j++) {
  838. Vector2 capsule_endpoint_A = p_transform_a.get_origin() + p_transform_a.elements[1] * capsule_A->get_height() * (j == 0 ? 0.5 : -0.5);
  839. if (TEST_POINT(capsule_endpoint_A, cpoint)) {
  840. return;
  841. }
  842. }
  843. if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
  844. return;
  845. }
  846. }
  847. separator.generate_contacts();
  848. }
  849. /////////
  850. template <bool castA, bool castB, bool withMargin>
  851. static void _collision_convex_polygon_convex_polygon(const Shape2DSW *p_a, const Transform2D &p_transform_a, const Shape2DSW *p_b, const Transform2D &p_transform_b, _CollectorCallback2D *p_collector, const Vector2 &p_motion_a, const Vector2 &p_motion_b, real_t p_margin_A, real_t p_margin_B) {
  852. const ConvexPolygonShape2DSW *convex_A = static_cast<const ConvexPolygonShape2DSW *>(p_a);
  853. const ConvexPolygonShape2DSW *convex_B = static_cast<const ConvexPolygonShape2DSW *>(p_b);
  854. SeparatorAxisTest2D<ConvexPolygonShape2DSW, ConvexPolygonShape2DSW, castA, castB, withMargin> separator(convex_A, p_transform_a, convex_B, p_transform_b, p_collector, p_motion_a, p_motion_b, p_margin_A, p_margin_B);
  855. if (!separator.test_previous_axis()) {
  856. return;
  857. }
  858. if (!separator.test_cast()) {
  859. return;
  860. }
  861. for (int i = 0; i < convex_A->get_point_count(); i++) {
  862. if (!separator.test_axis(convex_A->get_xformed_segment_normal(p_transform_a, i))) {
  863. return;
  864. }
  865. }
  866. for (int i = 0; i < convex_B->get_point_count(); i++) {
  867. if (!separator.test_axis(convex_B->get_xformed_segment_normal(p_transform_b, i))) {
  868. return;
  869. }
  870. }
  871. if (withMargin) {
  872. for (int i = 0; i < convex_A->get_point_count(); i++) {
  873. for (int j = 0; j < convex_B->get_point_count(); j++) {
  874. if (TEST_POINT(p_transform_a.xform(convex_A->get_point(i)), p_transform_b.xform(convex_B->get_point(j)))) {
  875. return;
  876. }
  877. }
  878. }
  879. }
  880. separator.generate_contacts();
  881. }
  882. ////////
  883. bool sat_2d_calculate_penetration(const Shape2DSW *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const Shape2DSW *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CollisionSolver2DSW::CallbackResult p_result_callback, void *p_userdata, bool p_swap, Vector2 *sep_axis, real_t p_margin_A, real_t p_margin_B) {
  884. Physics2DServer::ShapeType type_A = p_shape_A->get_type();
  885. ERR_FAIL_COND_V(type_A == Physics2DServer::SHAPE_LINE, false);
  886. //ERR_FAIL_COND_V(type_A==Physics2DServer::SHAPE_RAY,false);
  887. ERR_FAIL_COND_V(p_shape_A->is_concave(), false);
  888. Physics2DServer::ShapeType type_B = p_shape_B->get_type();
  889. ERR_FAIL_COND_V(type_B == Physics2DServer::SHAPE_LINE, false);
  890. //ERR_FAIL_COND_V(type_B==Physics2DServer::SHAPE_RAY,false);
  891. ERR_FAIL_COND_V(p_shape_B->is_concave(), false);
  892. static const CollisionFunc collision_table[5][5] = {
  893. { _collision_segment_segment<false, false, false>,
  894. _collision_segment_circle<false, false, false>,
  895. _collision_segment_rectangle<false, false, false>,
  896. _collision_segment_capsule<false, false, false>,
  897. _collision_segment_convex_polygon<false, false, false> },
  898. { nullptr,
  899. _collision_circle_circle<false, false, false>,
  900. _collision_circle_rectangle<false, false, false>,
  901. _collision_circle_capsule<false, false, false>,
  902. _collision_circle_convex_polygon<false, false, false> },
  903. { nullptr,
  904. nullptr,
  905. _collision_rectangle_rectangle<false, false, false>,
  906. _collision_rectangle_capsule<false, false, false>,
  907. _collision_rectangle_convex_polygon<false, false, false> },
  908. { nullptr,
  909. nullptr,
  910. nullptr,
  911. _collision_capsule_capsule<false, false, false>,
  912. _collision_capsule_convex_polygon<false, false, false> },
  913. { nullptr,
  914. nullptr,
  915. nullptr,
  916. nullptr,
  917. _collision_convex_polygon_convex_polygon<false, false, false> }
  918. };
  919. static const CollisionFunc collision_table_castA[5][5] = {
  920. { _collision_segment_segment<true, false, false>,
  921. _collision_segment_circle<true, false, false>,
  922. _collision_segment_rectangle<true, false, false>,
  923. _collision_segment_capsule<true, false, false>,
  924. _collision_segment_convex_polygon<true, false, false> },
  925. { nullptr,
  926. _collision_circle_circle<true, false, false>,
  927. _collision_circle_rectangle<true, false, false>,
  928. _collision_circle_capsule<true, false, false>,
  929. _collision_circle_convex_polygon<true, false, false> },
  930. { nullptr,
  931. nullptr,
  932. _collision_rectangle_rectangle<true, false, false>,
  933. _collision_rectangle_capsule<true, false, false>,
  934. _collision_rectangle_convex_polygon<true, false, false> },
  935. { nullptr,
  936. nullptr,
  937. nullptr,
  938. _collision_capsule_capsule<true, false, false>,
  939. _collision_capsule_convex_polygon<true, false, false> },
  940. { nullptr,
  941. nullptr,
  942. nullptr,
  943. nullptr,
  944. _collision_convex_polygon_convex_polygon<true, false, false> }
  945. };
  946. static const CollisionFunc collision_table_castB[5][5] = {
  947. { _collision_segment_segment<false, true, false>,
  948. _collision_segment_circle<false, true, false>,
  949. _collision_segment_rectangle<false, true, false>,
  950. _collision_segment_capsule<false, true, false>,
  951. _collision_segment_convex_polygon<false, true, false> },
  952. { nullptr,
  953. _collision_circle_circle<false, true, false>,
  954. _collision_circle_rectangle<false, true, false>,
  955. _collision_circle_capsule<false, true, false>,
  956. _collision_circle_convex_polygon<false, true, false> },
  957. { nullptr,
  958. nullptr,
  959. _collision_rectangle_rectangle<false, true, false>,
  960. _collision_rectangle_capsule<false, true, false>,
  961. _collision_rectangle_convex_polygon<false, true, false> },
  962. { nullptr,
  963. nullptr,
  964. nullptr,
  965. _collision_capsule_capsule<false, true, false>,
  966. _collision_capsule_convex_polygon<false, true, false> },
  967. { nullptr,
  968. nullptr,
  969. nullptr,
  970. nullptr,
  971. _collision_convex_polygon_convex_polygon<false, true, false> }
  972. };
  973. static const CollisionFunc collision_table_castA_castB[5][5] = {
  974. { _collision_segment_segment<true, true, false>,
  975. _collision_segment_circle<true, true, false>,
  976. _collision_segment_rectangle<true, true, false>,
  977. _collision_segment_capsule<true, true, false>,
  978. _collision_segment_convex_polygon<true, true, false> },
  979. { nullptr,
  980. _collision_circle_circle<true, true, false>,
  981. _collision_circle_rectangle<true, true, false>,
  982. _collision_circle_capsule<true, true, false>,
  983. _collision_circle_convex_polygon<true, true, false> },
  984. { nullptr,
  985. nullptr,
  986. _collision_rectangle_rectangle<true, true, false>,
  987. _collision_rectangle_capsule<true, true, false>,
  988. _collision_rectangle_convex_polygon<true, true, false> },
  989. { nullptr,
  990. nullptr,
  991. nullptr,
  992. _collision_capsule_capsule<true, true, false>,
  993. _collision_capsule_convex_polygon<true, true, false> },
  994. { nullptr,
  995. nullptr,
  996. nullptr,
  997. nullptr,
  998. _collision_convex_polygon_convex_polygon<true, true, false> }
  999. };
  1000. static const CollisionFunc collision_table_margin[5][5] = {
  1001. { _collision_segment_segment<false, false, true>,
  1002. _collision_segment_circle<false, false, true>,
  1003. _collision_segment_rectangle<false, false, true>,
  1004. _collision_segment_capsule<false, false, true>,
  1005. _collision_segment_convex_polygon<false, false, true> },
  1006. { nullptr,
  1007. _collision_circle_circle<false, false, true>,
  1008. _collision_circle_rectangle<false, false, true>,
  1009. _collision_circle_capsule<false, false, true>,
  1010. _collision_circle_convex_polygon<false, false, true> },
  1011. { nullptr,
  1012. nullptr,
  1013. _collision_rectangle_rectangle<false, false, true>,
  1014. _collision_rectangle_capsule<false, false, true>,
  1015. _collision_rectangle_convex_polygon<false, false, true> },
  1016. { nullptr,
  1017. nullptr,
  1018. nullptr,
  1019. _collision_capsule_capsule<false, false, true>,
  1020. _collision_capsule_convex_polygon<false, false, true> },
  1021. { nullptr,
  1022. nullptr,
  1023. nullptr,
  1024. nullptr,
  1025. _collision_convex_polygon_convex_polygon<false, false, true> }
  1026. };
  1027. static const CollisionFunc collision_table_castA_margin[5][5] = {
  1028. { _collision_segment_segment<true, false, true>,
  1029. _collision_segment_circle<true, false, true>,
  1030. _collision_segment_rectangle<true, false, true>,
  1031. _collision_segment_capsule<true, false, true>,
  1032. _collision_segment_convex_polygon<true, false, true> },
  1033. { nullptr,
  1034. _collision_circle_circle<true, false, true>,
  1035. _collision_circle_rectangle<true, false, true>,
  1036. _collision_circle_capsule<true, false, true>,
  1037. _collision_circle_convex_polygon<true, false, true> },
  1038. { nullptr,
  1039. nullptr,
  1040. _collision_rectangle_rectangle<true, false, true>,
  1041. _collision_rectangle_capsule<true, false, true>,
  1042. _collision_rectangle_convex_polygon<true, false, true> },
  1043. { nullptr,
  1044. nullptr,
  1045. nullptr,
  1046. _collision_capsule_capsule<true, false, true>,
  1047. _collision_capsule_convex_polygon<true, false, true> },
  1048. { nullptr,
  1049. nullptr,
  1050. nullptr,
  1051. nullptr,
  1052. _collision_convex_polygon_convex_polygon<true, false, true> }
  1053. };
  1054. static const CollisionFunc collision_table_castB_margin[5][5] = {
  1055. { _collision_segment_segment<false, true, true>,
  1056. _collision_segment_circle<false, true, true>,
  1057. _collision_segment_rectangle<false, true, true>,
  1058. _collision_segment_capsule<false, true, true>,
  1059. _collision_segment_convex_polygon<false, true, true> },
  1060. { nullptr,
  1061. _collision_circle_circle<false, true, true>,
  1062. _collision_circle_rectangle<false, true, true>,
  1063. _collision_circle_capsule<false, true, true>,
  1064. _collision_circle_convex_polygon<false, true, true> },
  1065. { nullptr,
  1066. nullptr,
  1067. _collision_rectangle_rectangle<false, true, true>,
  1068. _collision_rectangle_capsule<false, true, true>,
  1069. _collision_rectangle_convex_polygon<false, true, true> },
  1070. { nullptr,
  1071. nullptr,
  1072. nullptr,
  1073. _collision_capsule_capsule<false, true, true>,
  1074. _collision_capsule_convex_polygon<false, true, true> },
  1075. { nullptr,
  1076. nullptr,
  1077. nullptr,
  1078. nullptr,
  1079. _collision_convex_polygon_convex_polygon<false, true, true> }
  1080. };
  1081. static const CollisionFunc collision_table_castA_castB_margin[5][5] = {
  1082. { _collision_segment_segment<true, true, true>,
  1083. _collision_segment_circle<true, true, true>,
  1084. _collision_segment_rectangle<true, true, true>,
  1085. _collision_segment_capsule<true, true, true>,
  1086. _collision_segment_convex_polygon<true, true, true> },
  1087. { nullptr,
  1088. _collision_circle_circle<true, true, true>,
  1089. _collision_circle_rectangle<true, true, true>,
  1090. _collision_circle_capsule<true, true, true>,
  1091. _collision_circle_convex_polygon<true, true, true> },
  1092. { nullptr,
  1093. nullptr,
  1094. _collision_rectangle_rectangle<true, true, true>,
  1095. _collision_rectangle_capsule<true, true, true>,
  1096. _collision_rectangle_convex_polygon<true, true, true> },
  1097. { nullptr,
  1098. nullptr,
  1099. nullptr,
  1100. _collision_capsule_capsule<true, true, true>,
  1101. _collision_capsule_convex_polygon<true, true, true> },
  1102. { nullptr,
  1103. nullptr,
  1104. nullptr,
  1105. nullptr,
  1106. _collision_convex_polygon_convex_polygon<true, true, true> }
  1107. };
  1108. _CollectorCallback2D callback;
  1109. callback.callback = p_result_callback;
  1110. callback.swap = p_swap;
  1111. callback.userdata = p_userdata;
  1112. callback.collided = false;
  1113. callback.sep_axis = sep_axis;
  1114. const Shape2DSW *A = p_shape_A;
  1115. const Shape2DSW *B = p_shape_B;
  1116. const Transform2D *transform_A = &p_transform_A;
  1117. const Transform2D *transform_B = &p_transform_B;
  1118. const Vector2 *motion_A = &p_motion_A;
  1119. const Vector2 *motion_B = &p_motion_B;
  1120. real_t margin_A = p_margin_A, margin_B = p_margin_B;
  1121. if (type_A > type_B) {
  1122. SWAP(A, B);
  1123. SWAP(transform_A, transform_B);
  1124. SWAP(type_A, type_B);
  1125. SWAP(motion_A, motion_B);
  1126. SWAP(margin_A, margin_B);
  1127. callback.swap = !callback.swap;
  1128. }
  1129. CollisionFunc collision_func;
  1130. if (p_margin_A || p_margin_B) {
  1131. if (*motion_A == Vector2() && *motion_B == Vector2()) {
  1132. collision_func = collision_table_margin[type_A - 2][type_B - 2];
  1133. } else if (*motion_A != Vector2() && *motion_B == Vector2()) {
  1134. collision_func = collision_table_castA_margin[type_A - 2][type_B - 2];
  1135. } else if (*motion_A == Vector2() && *motion_B != Vector2()) {
  1136. collision_func = collision_table_castB_margin[type_A - 2][type_B - 2];
  1137. } else {
  1138. collision_func = collision_table_castA_castB_margin[type_A - 2][type_B - 2];
  1139. }
  1140. } else {
  1141. if (*motion_A == Vector2() && *motion_B == Vector2()) {
  1142. collision_func = collision_table[type_A - 2][type_B - 2];
  1143. } else if (*motion_A != Vector2() && *motion_B == Vector2()) {
  1144. collision_func = collision_table_castA[type_A - 2][type_B - 2];
  1145. } else if (*motion_A == Vector2() && *motion_B != Vector2()) {
  1146. collision_func = collision_table_castB[type_A - 2][type_B - 2];
  1147. } else {
  1148. collision_func = collision_table_castA_castB[type_A - 2][type_B - 2];
  1149. }
  1150. }
  1151. ERR_FAIL_COND_V(!collision_func, false);
  1152. collision_func(A, *transform_A, B, *transform_B, &callback, *motion_A, *motion_B, margin_A, margin_B);
  1153. return callback.collided;
  1154. }