godot_collision_solver_2d_sat.cpp 47 KB

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