2
0

godot_collision_solver_2d_sat.cpp 47 KB

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