collision_solver_sat.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. /*************************************************************************/
  2. /* collision_solver_sat.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "collision_solver_sat.h"
  31. #include "core/math/geometry.h"
  32. #include "gjk_epa.h"
  33. #define fallback_collision_solver gjk_epa_calculate_penetration
  34. // Cylinder SAT analytic methods and circle-face contact points for cylinder-trimesh and cylinder-box collision are based on ODE colliders.
  35. /*
  36. * Cylinder-trimesh and Cylinder-box colliders by Alen Ladavac
  37. * Ported to ODE by Nguyen Binh
  38. */
  39. /*************************************************************************
  40. * *
  41. * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
  42. * All rights reserved. Email: russ@q12.org Web: www.q12.org *
  43. * *
  44. * This library is free software; you can redistribute it and/or *
  45. * modify it under the terms of EITHER: *
  46. * (1) The GNU Lesser General Public License as published by the Free *
  47. * Software Foundation; either version 2.1 of the License, or (at *
  48. * your option) any later version. The text of the GNU Lesser *
  49. * General Public License is included with this library in the *
  50. * file LICENSE.TXT. *
  51. * (2) The BSD-style license that is included with this library in *
  52. * the file LICENSE-BSD.TXT. *
  53. * *
  54. * This library is distributed in the hope that it will be useful, *
  55. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  56. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  57. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  58. * *
  59. *************************************************************************/
  60. struct _CollectorCallback {
  61. CollisionSolverSW::CallbackResult callback;
  62. void *userdata;
  63. bool swap;
  64. bool collided;
  65. Vector3 normal;
  66. Vector3 *prev_axis;
  67. _FORCE_INLINE_ void call(const Vector3 &p_point_A, const Vector3 &p_point_B) {
  68. if (swap) {
  69. callback(p_point_B, p_point_A, userdata);
  70. } else {
  71. callback(p_point_A, p_point_B, userdata);
  72. }
  73. }
  74. };
  75. typedef void (*GenerateContactsFunc)(const Vector3 *, int, const Vector3 *, int, _CollectorCallback *);
  76. static void _generate_contacts_point_point(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  77. #ifdef DEBUG_ENABLED
  78. ERR_FAIL_COND(p_point_count_A != 1);
  79. ERR_FAIL_COND(p_point_count_B != 1);
  80. #endif
  81. p_callback->call(*p_points_A, *p_points_B);
  82. }
  83. static void _generate_contacts_point_edge(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  84. #ifdef DEBUG_ENABLED
  85. ERR_FAIL_COND(p_point_count_A != 1);
  86. ERR_FAIL_COND(p_point_count_B != 2);
  87. #endif
  88. Vector3 closest_B = Geometry::get_closest_point_to_segment_uncapped(*p_points_A, p_points_B);
  89. p_callback->call(*p_points_A, closest_B);
  90. }
  91. static void _generate_contacts_point_face(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  92. #ifdef DEBUG_ENABLED
  93. ERR_FAIL_COND(p_point_count_A != 1);
  94. ERR_FAIL_COND(p_point_count_B < 3);
  95. #endif
  96. Vector3 closest_B = Plane(p_points_B[0], p_points_B[1], p_points_B[2]).project(*p_points_A);
  97. p_callback->call(*p_points_A, closest_B);
  98. }
  99. static void _generate_contacts_point_circle(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  100. #ifdef DEBUG_ENABLED
  101. ERR_FAIL_COND(p_point_count_A != 1);
  102. ERR_FAIL_COND(p_point_count_B != 3);
  103. #endif
  104. Vector3 closest_B = Plane(p_points_B[0], p_points_B[1], p_points_B[2]).project(*p_points_A);
  105. p_callback->call(*p_points_A, closest_B);
  106. }
  107. static void _generate_contacts_edge_edge(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  108. #ifdef DEBUG_ENABLED
  109. ERR_FAIL_COND(p_point_count_A != 2);
  110. ERR_FAIL_COND(p_point_count_B != 2); // circle is actually a 4x3 matrix
  111. #endif
  112. Vector3 rel_A = p_points_A[1] - p_points_A[0];
  113. Vector3 rel_B = p_points_B[1] - p_points_B[0];
  114. Vector3 c = rel_A.cross(rel_B).cross(rel_B);
  115. if (Math::is_zero_approx(rel_A.dot(c))) {
  116. // should handle somehow..
  117. //ERR_PRINT("TODO FIX");
  118. //return;
  119. Vector3 axis = rel_A.normalized(); //make an axis
  120. Vector3 base_A = p_points_A[0] - axis * axis.dot(p_points_A[0]);
  121. Vector3 base_B = p_points_B[0] - axis * axis.dot(p_points_B[0]);
  122. //sort all 4 points in axis
  123. real_t dvec[4] = { axis.dot(p_points_A[0]), axis.dot(p_points_A[1]), axis.dot(p_points_B[0]), axis.dot(p_points_B[1]) };
  124. SortArray<real_t> sa;
  125. sa.sort(dvec, 4);
  126. //use the middle ones as contacts
  127. p_callback->call(base_A + axis * dvec[1], base_B + axis * dvec[1]);
  128. p_callback->call(base_A + axis * dvec[2], base_B + axis * dvec[2]);
  129. return;
  130. }
  131. real_t d = (c.dot(p_points_B[0]) - p_points_A[0].dot(c)) / rel_A.dot(c);
  132. if (d < 0.0) {
  133. d = 0.0;
  134. } else if (d > 1.0) {
  135. d = 1.0;
  136. }
  137. Vector3 closest_A = p_points_A[0] + rel_A * d;
  138. Vector3 closest_B = Geometry::get_closest_point_to_segment_uncapped(closest_A, p_points_B);
  139. p_callback->call(closest_A, closest_B);
  140. }
  141. static void _generate_contacts_edge_circle(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  142. #ifdef DEBUG_ENABLED
  143. ERR_FAIL_COND(p_point_count_A != 2);
  144. ERR_FAIL_COND(p_point_count_B != 3);
  145. #endif
  146. const Vector3 &circle_B_pos = p_points_B[0];
  147. Vector3 circle_B_line_1 = p_points_B[1] - circle_B_pos;
  148. Vector3 circle_B_line_2 = p_points_B[2] - circle_B_pos;
  149. real_t circle_B_radius = circle_B_line_1.length();
  150. Vector3 circle_B_normal = circle_B_line_1.cross(circle_B_line_2).normalized();
  151. Plane circle_plane(circle_B_pos, circle_B_normal);
  152. static const int max_clip = 2;
  153. Vector3 contact_points[max_clip];
  154. int num_points = 0;
  155. // Project edge point in circle plane.
  156. const Vector3 &edge_A_1 = p_points_A[0];
  157. Vector3 proj_point_1 = circle_plane.project(edge_A_1);
  158. Vector3 dist_vec = proj_point_1 - circle_B_pos;
  159. real_t dist_sq = dist_vec.length_squared();
  160. // Point 1 is inside disk, add as contact point.
  161. if (dist_sq <= circle_B_radius * circle_B_radius) {
  162. contact_points[num_points] = edge_A_1;
  163. ++num_points;
  164. }
  165. const Vector3 &edge_A_2 = p_points_A[1];
  166. Vector3 proj_point_2 = circle_plane.project(edge_A_2);
  167. Vector3 dist_vec_2 = proj_point_2 - circle_B_pos;
  168. real_t dist_sq_2 = dist_vec_2.length_squared();
  169. // Point 2 is inside disk, add as contact point.
  170. if (dist_sq_2 <= circle_B_radius * circle_B_radius) {
  171. contact_points[num_points] = edge_A_2;
  172. ++num_points;
  173. }
  174. if (num_points < 2) {
  175. Vector3 line_vec = proj_point_2 - proj_point_1;
  176. real_t line_length_sq = line_vec.length_squared();
  177. // Create a quadratic formula of the form ax^2 + bx + c = 0
  178. real_t a, b, c;
  179. a = line_length_sq;
  180. b = 2.0 * dist_vec.dot(line_vec);
  181. c = dist_sq - circle_B_radius * circle_B_radius;
  182. // Solve for t.
  183. real_t sqrtterm = b * b - 4.0 * a * c;
  184. // If the term we intend to square root is less than 0 then the answer won't be real,
  185. // so the line doesn't intersect.
  186. if (sqrtterm >= 0) {
  187. sqrtterm = Math::sqrt(sqrtterm);
  188. Vector3 edge_dir = edge_A_2 - edge_A_1;
  189. real_t fraction_1 = (-b - sqrtterm) / (2.0 * a);
  190. if ((fraction_1 > 0.0) && (fraction_1 < 1.0)) {
  191. Vector3 face_point_1 = edge_A_1 + fraction_1 * edge_dir;
  192. ERR_FAIL_COND(num_points >= max_clip);
  193. contact_points[num_points] = face_point_1;
  194. ++num_points;
  195. }
  196. real_t fraction_2 = (-b + sqrtterm) / (2.0 * a);
  197. if ((fraction_2 > 0.0) && (fraction_2 < 1.0) && !Math::is_equal_approx(fraction_1, fraction_2)) {
  198. Vector3 face_point_2 = edge_A_1 + fraction_2 * edge_dir;
  199. ERR_FAIL_COND(num_points >= max_clip);
  200. contact_points[num_points] = face_point_2;
  201. ++num_points;
  202. }
  203. }
  204. }
  205. // Generate contact points.
  206. for (int i = 0; i < num_points; i++) {
  207. const Vector3 &contact_point_A = contact_points[i];
  208. real_t d = circle_plane.distance_to(contact_point_A);
  209. Vector3 closest_B = contact_point_A - circle_plane.normal * d;
  210. if (p_callback->normal.dot(contact_point_A) >= p_callback->normal.dot(closest_B)) {
  211. continue;
  212. }
  213. p_callback->call(contact_point_A, closest_B);
  214. }
  215. }
  216. static void _generate_contacts_face_face(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  217. #ifdef DEBUG_ENABLED
  218. ERR_FAIL_COND(p_point_count_A < 2);
  219. ERR_FAIL_COND(p_point_count_B < 3);
  220. #endif
  221. static const int max_clip = 32;
  222. Vector3 _clipbuf1[max_clip];
  223. Vector3 _clipbuf2[max_clip];
  224. Vector3 *clipbuf_src = _clipbuf1;
  225. Vector3 *clipbuf_dst = _clipbuf2;
  226. int clipbuf_len = p_point_count_A;
  227. // copy A points to clipbuf_src
  228. for (int i = 0; i < p_point_count_A; i++) {
  229. clipbuf_src[i] = p_points_A[i];
  230. }
  231. Plane plane_B(p_points_B[0], p_points_B[1], p_points_B[2]);
  232. // go through all of B points
  233. for (int i = 0; i < p_point_count_B; i++) {
  234. int i_n = (i + 1) % p_point_count_B;
  235. Vector3 edge0_B = p_points_B[i];
  236. Vector3 edge1_B = p_points_B[i_n];
  237. Vector3 clip_normal = (edge0_B - edge1_B).cross(plane_B.normal).normalized();
  238. // make a clip plane
  239. Plane clip(edge0_B, clip_normal);
  240. // avoid double clip if A is edge
  241. int dst_idx = 0;
  242. bool edge = clipbuf_len == 2;
  243. for (int j = 0; j < clipbuf_len; j++) {
  244. int j_n = (j + 1) % clipbuf_len;
  245. Vector3 edge0_A = clipbuf_src[j];
  246. Vector3 edge1_A = clipbuf_src[j_n];
  247. real_t dist0 = clip.distance_to(edge0_A);
  248. real_t dist1 = clip.distance_to(edge1_A);
  249. if (dist0 <= 0) { // behind plane
  250. ERR_FAIL_COND(dst_idx >= max_clip);
  251. clipbuf_dst[dst_idx++] = clipbuf_src[j];
  252. }
  253. // check for different sides and non coplanar
  254. //if ( (dist0*dist1) < -CMP_EPSILON && !(edge && j)) {
  255. if ((dist0 * dist1) < 0 && !(edge && j)) {
  256. // calculate intersection
  257. Vector3 rel = edge1_A - edge0_A;
  258. real_t den = clip.normal.dot(rel);
  259. real_t dist = -(clip.normal.dot(edge0_A) - clip.d) / den;
  260. Vector3 inters = edge0_A + rel * dist;
  261. ERR_FAIL_COND(dst_idx >= max_clip);
  262. clipbuf_dst[dst_idx] = inters;
  263. dst_idx++;
  264. }
  265. }
  266. clipbuf_len = dst_idx;
  267. SWAP(clipbuf_src, clipbuf_dst);
  268. }
  269. // generate contacts
  270. //Plane plane_A(p_points_A[0],p_points_A[1],p_points_A[2]);
  271. for (int i = 0; i < clipbuf_len; i++) {
  272. real_t d = plane_B.distance_to(clipbuf_src[i]);
  273. /*
  274. if (d>CMP_EPSILON)
  275. continue;
  276. */
  277. Vector3 closest_B = clipbuf_src[i] - plane_B.normal * d;
  278. if (p_callback->normal.dot(clipbuf_src[i]) >= p_callback->normal.dot(closest_B)) {
  279. continue;
  280. }
  281. p_callback->call(clipbuf_src[i], closest_B);
  282. }
  283. }
  284. static void _generate_contacts_face_circle(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  285. #ifdef DEBUG_ENABLED
  286. ERR_FAIL_COND(p_point_count_A < 3);
  287. ERR_FAIL_COND(p_point_count_B != 3);
  288. #endif
  289. const Vector3 &circle_B_pos = p_points_B[0];
  290. Vector3 circle_B_line_1 = p_points_B[1] - circle_B_pos;
  291. Vector3 circle_B_line_2 = p_points_B[2] - circle_B_pos;
  292. // Clip face with circle segments.
  293. static const int circle_segments = 8;
  294. Vector3 circle_points[circle_segments];
  295. real_t angle_delta = 2.0 * Math_PI / circle_segments;
  296. for (int i = 0; i < circle_segments; ++i) {
  297. Vector3 point_pos = circle_B_pos;
  298. point_pos += circle_B_line_1 * Math::cos(i * angle_delta);
  299. point_pos += circle_B_line_2 * Math::sin(i * angle_delta);
  300. circle_points[i] = point_pos;
  301. }
  302. _generate_contacts_face_face(p_points_A, p_point_count_A, circle_points, circle_segments, p_callback);
  303. // Clip face with circle plane.
  304. Vector3 circle_B_normal = circle_B_line_1.cross(circle_B_line_2).normalized();
  305. Plane circle_plane(circle_B_pos, circle_B_normal);
  306. static const int max_clip = 32;
  307. Vector3 contact_points[max_clip];
  308. int num_points = 0;
  309. for (int i = 0; i < p_point_count_A; i++) {
  310. int i_n = (i + 1) % p_point_count_A;
  311. const Vector3 &edge0_A = p_points_A[i];
  312. const Vector3 &edge1_A = p_points_A[i_n];
  313. real_t dist0 = circle_plane.distance_to(edge0_A);
  314. real_t dist1 = circle_plane.distance_to(edge1_A);
  315. // First point in front of plane, generate contact point.
  316. if (dist0 * circle_plane.d >= 0) {
  317. ERR_FAIL_COND(num_points >= max_clip);
  318. contact_points[num_points] = edge0_A;
  319. ++num_points;
  320. }
  321. // Points on different sides, generate contact point.
  322. if (dist0 * dist1 < 0) {
  323. // calculate intersection
  324. Vector3 rel = edge1_A - edge0_A;
  325. real_t den = circle_plane.normal.dot(rel);
  326. real_t dist = -(circle_plane.normal.dot(edge0_A) - circle_plane.d) / den;
  327. Vector3 inters = edge0_A + rel * dist;
  328. ERR_FAIL_COND(num_points >= max_clip);
  329. contact_points[num_points] = inters;
  330. ++num_points;
  331. }
  332. }
  333. // Generate contact points.
  334. for (int i = 0; i < num_points; i++) {
  335. const Vector3 &contact_point_A = contact_points[i];
  336. real_t d = circle_plane.distance_to(contact_point_A);
  337. Vector3 closest_B = contact_point_A - circle_plane.normal * d;
  338. if (p_callback->normal.dot(contact_point_A) >= p_callback->normal.dot(closest_B)) {
  339. continue;
  340. }
  341. p_callback->call(contact_point_A, closest_B);
  342. }
  343. }
  344. static void _generate_contacts_circle_circle(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  345. #ifdef DEBUG_ENABLED
  346. ERR_FAIL_COND(p_point_count_A != 3);
  347. ERR_FAIL_COND(p_point_count_B != 3);
  348. #endif
  349. const Vector3 &circle_A_pos = p_points_A[0];
  350. Vector3 circle_A_line_1 = p_points_A[1] - circle_A_pos;
  351. Vector3 circle_A_line_2 = p_points_A[2] - circle_A_pos;
  352. real_t circle_A_radius = circle_A_line_1.length();
  353. Vector3 circle_A_normal = circle_A_line_1.cross(circle_A_line_2).normalized();
  354. const Vector3 &circle_B_pos = p_points_B[0];
  355. Vector3 circle_B_line_1 = p_points_B[1] - circle_B_pos;
  356. Vector3 circle_B_line_2 = p_points_B[2] - circle_B_pos;
  357. real_t circle_B_radius = circle_B_line_1.length();
  358. Vector3 circle_B_normal = circle_B_line_1.cross(circle_B_line_2).normalized();
  359. static const int max_clip = 4;
  360. Vector3 contact_points[max_clip];
  361. int num_points = 0;
  362. Vector3 centers_diff = circle_B_pos - circle_A_pos;
  363. Vector3 norm_proj = circle_A_normal.dot(centers_diff) * circle_A_normal;
  364. Vector3 comp_proj = centers_diff - norm_proj;
  365. real_t proj_dist = comp_proj.length();
  366. if (!Math::is_zero_approx(proj_dist)) {
  367. comp_proj /= proj_dist;
  368. if ((proj_dist > circle_A_radius - circle_B_radius) && (proj_dist > circle_B_radius - circle_A_radius)) {
  369. // Circles are overlapping, use the 2 points of intersection as contacts.
  370. real_t radius_a_sqr = circle_A_radius * circle_A_radius;
  371. real_t radius_b_sqr = circle_B_radius * circle_B_radius;
  372. real_t d_sqr = proj_dist * proj_dist;
  373. real_t s = (1.0 + (radius_a_sqr - radius_b_sqr) / d_sqr) * 0.5;
  374. real_t h = Math::sqrt(MAX(radius_a_sqr - d_sqr * s * s, 0.0));
  375. Vector3 midpoint = circle_A_pos + s * comp_proj * proj_dist;
  376. Vector3 h_vec = h * circle_A_normal.cross(comp_proj);
  377. Vector3 point_A = midpoint + h_vec;
  378. contact_points[num_points] = point_A;
  379. ++num_points;
  380. point_A = midpoint - h_vec;
  381. contact_points[num_points] = point_A;
  382. ++num_points;
  383. // Add 2 points from circle A and B along the line between the centers.
  384. point_A = circle_A_pos + comp_proj * circle_A_radius;
  385. contact_points[num_points] = point_A;
  386. ++num_points;
  387. point_A = circle_B_pos - comp_proj * circle_B_radius - norm_proj;
  388. contact_points[num_points] = point_A;
  389. ++num_points;
  390. } // Otherwise one circle is inside the other one, use 3 arbitrary equidistant points.
  391. } // Otherwise circles are concentric, use 3 arbitrary equidistant points.
  392. if (num_points == 0) {
  393. // Generate equidistant points.
  394. if (circle_A_radius < circle_B_radius) {
  395. // Circle A inside circle B.
  396. for (int i = 0; i < 3; ++i) {
  397. Vector3 circle_A_point = circle_A_pos;
  398. circle_A_point += circle_A_line_1 * Math::cos(2.0 * Math_PI * i / 3.0);
  399. circle_A_point += circle_A_line_2 * Math::sin(2.0 * Math_PI * i / 3.0);
  400. contact_points[num_points] = circle_A_point;
  401. ++num_points;
  402. }
  403. } else {
  404. // Circle B inside circle A.
  405. for (int i = 0; i < 3; ++i) {
  406. Vector3 circle_B_point = circle_B_pos;
  407. circle_B_point += circle_B_line_1 * Math::cos(2.0 * Math_PI * i / 3.0);
  408. circle_B_point += circle_B_line_2 * Math::sin(2.0 * Math_PI * i / 3.0);
  409. Vector3 circle_A_point = circle_B_point - norm_proj;
  410. contact_points[num_points] = circle_A_point;
  411. ++num_points;
  412. }
  413. }
  414. }
  415. Plane circle_B_plane(circle_B_pos, circle_B_normal);
  416. // Generate contact points.
  417. for (int i = 0; i < num_points; i++) {
  418. const Vector3 &contact_point_A = contact_points[i];
  419. real_t d = circle_B_plane.distance_to(contact_point_A);
  420. Vector3 closest_B = contact_point_A - circle_B_plane.normal * d;
  421. if (p_callback->normal.dot(contact_point_A) >= p_callback->normal.dot(closest_B)) {
  422. continue;
  423. }
  424. p_callback->call(contact_point_A, closest_B);
  425. }
  426. }
  427. static void _generate_contacts_from_supports(const Vector3 *p_points_A, int p_point_count_A, ShapeSW::FeatureType p_feature_type_A, const Vector3 *p_points_B, int p_point_count_B, ShapeSW::FeatureType p_feature_type_B, _CollectorCallback *p_callback) {
  428. #ifdef DEBUG_ENABLED
  429. ERR_FAIL_COND(p_point_count_A < 1);
  430. ERR_FAIL_COND(p_point_count_B < 1);
  431. #endif
  432. static const GenerateContactsFunc generate_contacts_func_table[4][4] = {
  433. {
  434. _generate_contacts_point_point,
  435. _generate_contacts_point_edge,
  436. _generate_contacts_point_face,
  437. _generate_contacts_point_circle,
  438. },
  439. {
  440. nullptr,
  441. _generate_contacts_edge_edge,
  442. _generate_contacts_face_face,
  443. _generate_contacts_edge_circle,
  444. },
  445. {
  446. nullptr,
  447. nullptr,
  448. _generate_contacts_face_face,
  449. _generate_contacts_face_circle,
  450. },
  451. {
  452. nullptr,
  453. nullptr,
  454. nullptr,
  455. _generate_contacts_circle_circle,
  456. },
  457. };
  458. int pointcount_B;
  459. int pointcount_A;
  460. const Vector3 *points_A;
  461. const Vector3 *points_B;
  462. int version_A;
  463. int version_B;
  464. if (p_feature_type_A > p_feature_type_B) {
  465. //swap
  466. p_callback->swap = !p_callback->swap;
  467. p_callback->normal = -p_callback->normal;
  468. pointcount_B = p_point_count_A;
  469. pointcount_A = p_point_count_B;
  470. points_A = p_points_B;
  471. points_B = p_points_A;
  472. version_A = p_feature_type_B;
  473. version_B = p_feature_type_A;
  474. } else {
  475. pointcount_B = p_point_count_B;
  476. pointcount_A = p_point_count_A;
  477. points_A = p_points_A;
  478. points_B = p_points_B;
  479. version_A = p_feature_type_A;
  480. version_B = p_feature_type_B;
  481. }
  482. GenerateContactsFunc contacts_func = generate_contacts_func_table[version_A][version_B];
  483. ERR_FAIL_COND(!contacts_func);
  484. contacts_func(points_A, pointcount_A, points_B, pointcount_B, p_callback);
  485. }
  486. template <class ShapeA, class ShapeB, bool withMargin = false>
  487. class SeparatorAxisTest {
  488. const ShapeA *shape_A;
  489. const ShapeB *shape_B;
  490. const Transform *transform_A;
  491. const Transform *transform_B;
  492. real_t best_depth;
  493. Vector3 best_axis;
  494. _CollectorCallback *callback;
  495. real_t margin_A;
  496. real_t margin_B;
  497. Vector3 separator_axis;
  498. public:
  499. _FORCE_INLINE_ bool test_previous_axis() {
  500. if (callback && callback->prev_axis && *callback->prev_axis != Vector3()) {
  501. return test_axis(*callback->prev_axis);
  502. } else {
  503. return true;
  504. }
  505. }
  506. _FORCE_INLINE_ bool test_axis(const Vector3 &p_axis) {
  507. Vector3 axis = p_axis;
  508. if (Math::abs(axis.x) < CMP_EPSILON &&
  509. Math::abs(axis.y) < CMP_EPSILON &&
  510. Math::abs(axis.z) < CMP_EPSILON) {
  511. // strange case, try an upwards separator
  512. axis = Vector3(0.0, 1.0, 0.0);
  513. }
  514. real_t min_A, max_A, min_B, max_B;
  515. shape_A->project_range(axis, *transform_A, min_A, max_A);
  516. shape_B->project_range(axis, *transform_B, min_B, max_B);
  517. if (withMargin) {
  518. min_A -= margin_A;
  519. max_A += margin_A;
  520. min_B -= margin_B;
  521. max_B += margin_B;
  522. }
  523. min_B -= (max_A - min_A) * 0.5;
  524. max_B += (max_A - min_A) * 0.5;
  525. min_B -= (min_A + max_A) * 0.5;
  526. max_B -= (min_A + max_A) * 0.5;
  527. if (min_B > 0.0 || max_B < 0.0) {
  528. separator_axis = axis;
  529. return false; // doesn't contain 0
  530. }
  531. //use the smallest depth
  532. if (min_B < 0.0) { // could be +0.0, we don't want it to become -0.0
  533. min_B = -min_B;
  534. }
  535. if (max_B < min_B) {
  536. if (max_B < best_depth) {
  537. best_depth = max_B;
  538. best_axis = axis;
  539. }
  540. } else {
  541. if (min_B < best_depth) {
  542. best_depth = min_B;
  543. best_axis = -axis; // keep it as A axis
  544. }
  545. }
  546. return true;
  547. }
  548. static _FORCE_INLINE_ void test_contact_points(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
  549. SeparatorAxisTest<ShapeA, ShapeB, withMargin> *separator = (SeparatorAxisTest<ShapeA, ShapeB, withMargin> *)p_userdata;
  550. Vector3 axis = (p_point_B - p_point_A);
  551. real_t depth = axis.length();
  552. // Filter out bogus directions with a threshold and re-testing axis.
  553. if (separator->best_depth - depth > 0.001) {
  554. separator->test_axis(axis / depth);
  555. }
  556. }
  557. _FORCE_INLINE_ void generate_contacts() {
  558. // nothing to do, don't generate
  559. if (best_axis == Vector3(0.0, 0.0, 0.0)) {
  560. return;
  561. }
  562. if (!callback->callback) {
  563. //just was checking intersection?
  564. callback->collided = true;
  565. if (callback->prev_axis) {
  566. *callback->prev_axis = best_axis;
  567. }
  568. return;
  569. }
  570. static const int max_supports = 16;
  571. Vector3 supports_A[max_supports];
  572. int support_count_A;
  573. ShapeSW::FeatureType support_type_A;
  574. shape_A->get_supports(transform_A->basis.xform_inv(-best_axis).normalized(), max_supports, supports_A, support_count_A, support_type_A);
  575. for (int i = 0; i < support_count_A; i++) {
  576. supports_A[i] = transform_A->xform(supports_A[i]);
  577. }
  578. if (withMargin) {
  579. for (int i = 0; i < support_count_A; i++) {
  580. supports_A[i] += -best_axis * margin_A;
  581. }
  582. }
  583. Vector3 supports_B[max_supports];
  584. int support_count_B;
  585. ShapeSW::FeatureType support_type_B;
  586. shape_B->get_supports(transform_B->basis.xform_inv(best_axis).normalized(), max_supports, supports_B, support_count_B, support_type_B);
  587. for (int i = 0; i < support_count_B; i++) {
  588. supports_B[i] = transform_B->xform(supports_B[i]);
  589. }
  590. if (withMargin) {
  591. for (int i = 0; i < support_count_B; i++) {
  592. supports_B[i] += best_axis * margin_B;
  593. }
  594. }
  595. callback->normal = best_axis;
  596. if (callback->prev_axis) {
  597. *callback->prev_axis = best_axis;
  598. }
  599. _generate_contacts_from_supports(supports_A, support_count_A, support_type_A, supports_B, support_count_B, support_type_B, callback);
  600. callback->collided = true;
  601. }
  602. _FORCE_INLINE_ SeparatorAxisTest(const ShapeA *p_shape_A, const Transform &p_transform_A, const ShapeB *p_shape_B, const Transform &p_transform_B, _CollectorCallback *p_callback, real_t p_margin_A = 0, real_t p_margin_B = 0) {
  603. best_depth = 1e15;
  604. shape_A = p_shape_A;
  605. shape_B = p_shape_B;
  606. transform_A = &p_transform_A;
  607. transform_B = &p_transform_B;
  608. callback = p_callback;
  609. margin_A = p_margin_A;
  610. margin_B = p_margin_B;
  611. }
  612. };
  613. /****** SAT TESTS *******/
  614. typedef void (*CollisionFunc)(const ShapeSW *, const Transform &, const ShapeSW *, const Transform &, _CollectorCallback *p_callback, real_t, real_t);
  615. template <bool withMargin>
  616. static void _collision_sphere_sphere(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  617. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  618. const SphereShapeSW *sphere_B = static_cast<const SphereShapeSW *>(p_b);
  619. SeparatorAxisTest<SphereShapeSW, SphereShapeSW, withMargin> separator(sphere_A, p_transform_a, sphere_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  620. // previous axis
  621. if (!separator.test_previous_axis()) {
  622. return;
  623. }
  624. if (!separator.test_axis((p_transform_a.origin - p_transform_b.origin).normalized())) {
  625. return;
  626. }
  627. separator.generate_contacts();
  628. }
  629. template <bool withMargin>
  630. static void _collision_sphere_box(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  631. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  632. const BoxShapeSW *box_B = static_cast<const BoxShapeSW *>(p_b);
  633. SeparatorAxisTest<SphereShapeSW, BoxShapeSW, withMargin> separator(sphere_A, p_transform_a, box_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  634. if (!separator.test_previous_axis()) {
  635. return;
  636. }
  637. // test faces
  638. for (int i = 0; i < 3; i++) {
  639. Vector3 axis = p_transform_b.basis.get_axis(i).normalized();
  640. if (!separator.test_axis(axis)) {
  641. return;
  642. }
  643. }
  644. // calculate closest point to sphere
  645. Vector3 cnormal = p_transform_b.xform_inv(p_transform_a.origin);
  646. Vector3 cpoint = p_transform_b.xform(Vector3(
  647. (cnormal.x < 0) ? -box_B->get_half_extents().x : box_B->get_half_extents().x,
  648. (cnormal.y < 0) ? -box_B->get_half_extents().y : box_B->get_half_extents().y,
  649. (cnormal.z < 0) ? -box_B->get_half_extents().z : box_B->get_half_extents().z));
  650. // use point to test axis
  651. Vector3 point_axis = (p_transform_a.origin - cpoint).normalized();
  652. if (!separator.test_axis(point_axis)) {
  653. return;
  654. }
  655. // test edges
  656. for (int i = 0; i < 3; i++) {
  657. Vector3 axis = point_axis.cross(p_transform_b.basis.get_axis(i)).cross(p_transform_b.basis.get_axis(i)).normalized();
  658. if (!separator.test_axis(axis)) {
  659. return;
  660. }
  661. }
  662. separator.generate_contacts();
  663. }
  664. template <bool withMargin>
  665. static void _collision_sphere_capsule(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  666. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  667. const CapsuleShapeSW *capsule_B = static_cast<const CapsuleShapeSW *>(p_b);
  668. SeparatorAxisTest<SphereShapeSW, CapsuleShapeSW, withMargin> separator(sphere_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  669. if (!separator.test_previous_axis()) {
  670. return;
  671. }
  672. //capsule sphere 1, sphere
  673. Vector3 capsule_axis = p_transform_b.basis.get_axis(2) * (capsule_B->get_height() * 0.5);
  674. Vector3 capsule_ball_1 = p_transform_b.origin + capsule_axis;
  675. if (!separator.test_axis((capsule_ball_1 - p_transform_a.origin).normalized())) {
  676. return;
  677. }
  678. //capsule sphere 2, sphere
  679. Vector3 capsule_ball_2 = p_transform_b.origin - capsule_axis;
  680. if (!separator.test_axis((capsule_ball_2 - p_transform_a.origin).normalized())) {
  681. return;
  682. }
  683. //capsule edge, sphere
  684. Vector3 b2a = p_transform_a.origin - p_transform_b.origin;
  685. Vector3 axis = b2a.cross(capsule_axis).cross(capsule_axis).normalized();
  686. if (!separator.test_axis(axis)) {
  687. return;
  688. }
  689. separator.generate_contacts();
  690. }
  691. template <bool withMargin>
  692. static void _collision_sphere_cylinder(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  693. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  694. const CylinderShapeSW *cylinder_B = static_cast<const CylinderShapeSW *>(p_b);
  695. SeparatorAxisTest<SphereShapeSW, CylinderShapeSW, withMargin> separator(sphere_A, p_transform_a, cylinder_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  696. if (!separator.test_previous_axis()) {
  697. return;
  698. }
  699. // Cylinder B end caps.
  700. Vector3 cylinder_B_axis = p_transform_b.basis.get_axis(1).normalized();
  701. if (!separator.test_axis(cylinder_B_axis)) {
  702. return;
  703. }
  704. Vector3 cylinder_diff = p_transform_b.origin - p_transform_a.origin;
  705. // Cylinder B lateral surface.
  706. if (!separator.test_axis(cylinder_B_axis.cross(cylinder_diff).cross(cylinder_B_axis).normalized())) {
  707. return;
  708. }
  709. // Closest point to cylinder caps.
  710. const Vector3 &sphere_center = p_transform_a.origin;
  711. Vector3 cyl_axis = p_transform_b.basis.get_axis(1);
  712. Vector3 cap_axis = p_transform_b.basis.get_axis(0);
  713. real_t height_scale = cyl_axis.length();
  714. real_t cap_dist = cylinder_B->get_height() * 0.5 * height_scale;
  715. cyl_axis /= height_scale;
  716. real_t radius_scale = cap_axis.length();
  717. real_t cap_radius = cylinder_B->get_radius() * radius_scale;
  718. for (int i = 0; i < 2; i++) {
  719. Vector3 cap_dir = ((i == 0) ? cyl_axis : -cyl_axis);
  720. Vector3 cap_pos = p_transform_b.origin + cap_dir * cap_dist;
  721. Vector3 closest_point;
  722. Vector3 diff = sphere_center - cap_pos;
  723. Vector3 proj = diff - cap_dir.dot(diff) * cap_dir;
  724. real_t proj_len = proj.length();
  725. if (Math::is_zero_approx(proj_len)) {
  726. // Point is equidistant to all circle points.
  727. continue;
  728. }
  729. closest_point = cap_pos + (cap_radius / proj_len) * proj;
  730. if (!separator.test_axis((closest_point - sphere_center).normalized())) {
  731. return;
  732. }
  733. }
  734. separator.generate_contacts();
  735. }
  736. template <bool withMargin>
  737. static void _collision_sphere_convex_polygon(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  738. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  739. const ConvexPolygonShapeSW *convex_polygon_B = static_cast<const ConvexPolygonShapeSW *>(p_b);
  740. SeparatorAxisTest<SphereShapeSW, ConvexPolygonShapeSW, withMargin> separator(sphere_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  741. if (!separator.test_previous_axis()) {
  742. return;
  743. }
  744. const Geometry::MeshData &mesh = convex_polygon_B->get_mesh();
  745. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  746. int face_count = mesh.faces.size();
  747. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  748. int edge_count = mesh.edges.size();
  749. const Vector3 *vertices = mesh.vertices.ptr();
  750. int vertex_count = mesh.vertices.size();
  751. // Precalculating this makes the transforms faster.
  752. Basis b_xform_normal = p_transform_b.basis.inverse().transposed();
  753. // faces of B
  754. for (int i = 0; i < face_count; i++) {
  755. Vector3 axis = b_xform_normal.xform(faces[i].plane.normal).normalized();
  756. if (!separator.test_axis(axis)) {
  757. return;
  758. }
  759. }
  760. // edges of B
  761. for (int i = 0; i < edge_count; i++) {
  762. Vector3 v1 = p_transform_b.xform(vertices[edges[i].a]);
  763. Vector3 v2 = p_transform_b.xform(vertices[edges[i].b]);
  764. Vector3 v3 = p_transform_a.origin;
  765. Vector3 n1 = v2 - v1;
  766. Vector3 n2 = v2 - v3;
  767. Vector3 axis = n1.cross(n2).cross(n1).normalized();
  768. if (!separator.test_axis(axis)) {
  769. return;
  770. }
  771. }
  772. // vertices of B
  773. for (int i = 0; i < vertex_count; i++) {
  774. Vector3 v1 = p_transform_b.xform(vertices[i]);
  775. Vector3 v2 = p_transform_a.origin;
  776. Vector3 axis = (v2 - v1).normalized();
  777. if (!separator.test_axis(axis)) {
  778. return;
  779. }
  780. }
  781. separator.generate_contacts();
  782. }
  783. template <bool withMargin>
  784. static void _collision_sphere_face(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  785. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  786. const FaceShapeSW *face_B = static_cast<const FaceShapeSW *>(p_b);
  787. SeparatorAxisTest<SphereShapeSW, FaceShapeSW, withMargin> separator(sphere_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  788. Vector3 vertex[3] = {
  789. p_transform_b.xform(face_B->vertex[0]),
  790. p_transform_b.xform(face_B->vertex[1]),
  791. p_transform_b.xform(face_B->vertex[2]),
  792. };
  793. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  794. return;
  795. }
  796. // edges and points of B
  797. for (int i = 0; i < 3; i++) {
  798. Vector3 n1 = vertex[i] - p_transform_a.origin;
  799. if (!separator.test_axis(n1.normalized())) {
  800. return;
  801. }
  802. Vector3 n2 = vertex[(i + 1) % 3] - vertex[i];
  803. Vector3 axis = n1.cross(n2).cross(n2).normalized();
  804. if (!separator.test_axis(axis)) {
  805. return;
  806. }
  807. }
  808. separator.generate_contacts();
  809. }
  810. template <bool withMargin>
  811. static void _collision_box_box(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  812. const BoxShapeSW *box_A = static_cast<const BoxShapeSW *>(p_a);
  813. const BoxShapeSW *box_B = static_cast<const BoxShapeSW *>(p_b);
  814. SeparatorAxisTest<BoxShapeSW, BoxShapeSW, withMargin> separator(box_A, p_transform_a, box_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  815. if (!separator.test_previous_axis()) {
  816. return;
  817. }
  818. // test faces of A
  819. for (int i = 0; i < 3; i++) {
  820. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  821. if (!separator.test_axis(axis)) {
  822. return;
  823. }
  824. }
  825. // test faces of B
  826. for (int i = 0; i < 3; i++) {
  827. Vector3 axis = p_transform_b.basis.get_axis(i).normalized();
  828. if (!separator.test_axis(axis)) {
  829. return;
  830. }
  831. }
  832. // test combined edges
  833. for (int i = 0; i < 3; i++) {
  834. for (int j = 0; j < 3; j++) {
  835. Vector3 axis = p_transform_a.basis.get_axis(i).cross(p_transform_b.basis.get_axis(j));
  836. if (Math::is_zero_approx(axis.length_squared())) {
  837. continue;
  838. }
  839. axis.normalize();
  840. if (!separator.test_axis(axis)) {
  841. return;
  842. }
  843. }
  844. }
  845. if (withMargin) {
  846. //add endpoint test between closest vertices and edges
  847. // calculate closest point to sphere
  848. Vector3 ab_vec = p_transform_b.origin - p_transform_a.origin;
  849. Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
  850. Vector3 support_a = p_transform_a.xform(Vector3(
  851. (cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  852. (cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  853. (cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  854. Vector3 cnormal_b = p_transform_b.basis.xform_inv(-ab_vec);
  855. Vector3 support_b = p_transform_b.xform(Vector3(
  856. (cnormal_b.x < 0) ? -box_B->get_half_extents().x : box_B->get_half_extents().x,
  857. (cnormal_b.y < 0) ? -box_B->get_half_extents().y : box_B->get_half_extents().y,
  858. (cnormal_b.z < 0) ? -box_B->get_half_extents().z : box_B->get_half_extents().z));
  859. Vector3 axis_ab = (support_a - support_b);
  860. if (!separator.test_axis(axis_ab.normalized())) {
  861. return;
  862. }
  863. //now try edges, which become cylinders!
  864. for (int i = 0; i < 3; i++) {
  865. //a ->b
  866. Vector3 axis_a = p_transform_a.basis.get_axis(i);
  867. if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized())) {
  868. return;
  869. }
  870. //b ->a
  871. Vector3 axis_b = p_transform_b.basis.get_axis(i);
  872. if (!separator.test_axis(axis_ab.cross(axis_b).cross(axis_b).normalized())) {
  873. return;
  874. }
  875. }
  876. }
  877. separator.generate_contacts();
  878. }
  879. template <bool withMargin>
  880. static void _collision_box_capsule(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  881. const BoxShapeSW *box_A = static_cast<const BoxShapeSW *>(p_a);
  882. const CapsuleShapeSW *capsule_B = static_cast<const CapsuleShapeSW *>(p_b);
  883. SeparatorAxisTest<BoxShapeSW, CapsuleShapeSW, withMargin> separator(box_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  884. if (!separator.test_previous_axis()) {
  885. return;
  886. }
  887. // faces of A
  888. for (int i = 0; i < 3; i++) {
  889. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  890. if (!separator.test_axis(axis)) {
  891. return;
  892. }
  893. }
  894. Vector3 cyl_axis = p_transform_b.basis.get_axis(2).normalized();
  895. // edges of A, capsule cylinder
  896. for (int i = 0; i < 3; i++) {
  897. // cylinder
  898. Vector3 box_axis = p_transform_a.basis.get_axis(i);
  899. Vector3 axis = box_axis.cross(cyl_axis);
  900. if (Math::is_zero_approx(axis.length_squared())) {
  901. continue;
  902. }
  903. if (!separator.test_axis(axis.normalized())) {
  904. return;
  905. }
  906. }
  907. // points of A, capsule cylinder
  908. // this sure could be made faster somehow..
  909. for (int i = 0; i < 2; i++) {
  910. for (int j = 0; j < 2; j++) {
  911. for (int k = 0; k < 2; k++) {
  912. Vector3 he = box_A->get_half_extents();
  913. he.x *= (i * 2 - 1);
  914. he.y *= (j * 2 - 1);
  915. he.z *= (k * 2 - 1);
  916. Vector3 point = p_transform_a.origin;
  917. for (int l = 0; l < 3; l++) {
  918. point += p_transform_a.basis.get_axis(l) * he[l];
  919. }
  920. //Vector3 axis = (point - cyl_axis * cyl_axis.dot(point)).normalized();
  921. Vector3 axis = Plane(cyl_axis, 0).project(point).normalized();
  922. if (!separator.test_axis(axis)) {
  923. return;
  924. }
  925. }
  926. }
  927. }
  928. // capsule balls, edges of A
  929. Transform transform_a_inverse = p_transform_a.affine_inverse();
  930. for (int i = 0; i < 2; i++) {
  931. Vector3 capsule_axis = p_transform_b.basis.get_axis(2) * (capsule_B->get_height() * 0.5);
  932. Vector3 sphere_pos = p_transform_b.origin + ((i == 0) ? capsule_axis : -capsule_axis);
  933. Vector3 cnormal = transform_a_inverse.xform(sphere_pos);
  934. Vector3 cpoint = p_transform_a.xform(Vector3(
  935. (cnormal.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  936. (cnormal.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  937. (cnormal.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  938. // use point to test axis
  939. Vector3 point_axis = (sphere_pos - cpoint).normalized();
  940. if (!separator.test_axis(point_axis)) {
  941. return;
  942. }
  943. // test edges of A
  944. for (int j = 0; j < 3; j++) {
  945. Vector3 axis = point_axis.cross(p_transform_a.basis.get_axis(j)).cross(p_transform_a.basis.get_axis(j)).normalized();
  946. if (!separator.test_axis(axis)) {
  947. return;
  948. }
  949. }
  950. }
  951. separator.generate_contacts();
  952. }
  953. template <bool withMargin>
  954. static void _collision_box_cylinder(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  955. const BoxShapeSW *box_A = static_cast<const BoxShapeSW *>(p_a);
  956. const CylinderShapeSW *cylinder_B = static_cast<const CylinderShapeSW *>(p_b);
  957. SeparatorAxisTest<BoxShapeSW, CylinderShapeSW, withMargin> separator(box_A, p_transform_a, cylinder_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  958. if (!separator.test_previous_axis()) {
  959. return;
  960. }
  961. // Faces of A.
  962. for (int i = 0; i < 3; i++) {
  963. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  964. if (!separator.test_axis(axis)) {
  965. return;
  966. }
  967. }
  968. Vector3 cyl_axis = p_transform_b.basis.get_axis(1).normalized();
  969. // Cylinder end caps.
  970. {
  971. if (!separator.test_axis(cyl_axis)) {
  972. return;
  973. }
  974. }
  975. // Edges of A, cylinder lateral surface.
  976. for (int i = 0; i < 3; i++) {
  977. Vector3 box_axis = p_transform_a.basis.get_axis(i);
  978. Vector3 axis = box_axis.cross(cyl_axis);
  979. if (Math::is_zero_approx(axis.length_squared())) {
  980. continue;
  981. }
  982. if (!separator.test_axis(axis.normalized())) {
  983. return;
  984. }
  985. }
  986. // Gather points of A.
  987. Vector3 vertices_A[8];
  988. Vector3 box_extent = box_A->get_half_extents();
  989. for (int i = 0; i < 2; i++) {
  990. for (int j = 0; j < 2; j++) {
  991. for (int k = 0; k < 2; k++) {
  992. Vector3 extent = box_extent;
  993. extent.x *= (i * 2 - 1);
  994. extent.y *= (j * 2 - 1);
  995. extent.z *= (k * 2 - 1);
  996. Vector3 &point = vertices_A[i * 2 * 2 + j * 2 + k];
  997. point = p_transform_a.origin;
  998. for (int l = 0; l < 3; l++) {
  999. point += p_transform_a.basis.get_axis(l) * extent[l];
  1000. }
  1001. }
  1002. }
  1003. }
  1004. // Points of A, cylinder lateral surface.
  1005. for (int i = 0; i < 8; i++) {
  1006. const Vector3 &point = vertices_A[i];
  1007. Vector3 axis = Plane(cyl_axis, 0).project(point).normalized();
  1008. if (!separator.test_axis(axis)) {
  1009. return;
  1010. }
  1011. }
  1012. // Edges of A, cylinder end caps rim.
  1013. int edges_start_A[12] = { 0, 2, 4, 6, 0, 1, 4, 5, 0, 1, 2, 3 };
  1014. int edges_end_A[12] = { 1, 3, 5, 7, 2, 3, 6, 7, 4, 5, 6, 7 };
  1015. Vector3 cap_axis = cyl_axis * (cylinder_B->get_height() * 0.5);
  1016. for (int i = 0; i < 2; i++) {
  1017. Vector3 cap_pos = p_transform_b.origin + ((i == 0) ? cap_axis : -cap_axis);
  1018. for (int e = 0; e < 12; e++) {
  1019. const Vector3 &edge_start = vertices_A[edges_start_A[e]];
  1020. const Vector3 &edge_end = vertices_A[edges_end_A[e]];
  1021. Vector3 edge_dir = (edge_end - edge_start);
  1022. edge_dir.normalize();
  1023. real_t edge_dot = edge_dir.dot(cyl_axis);
  1024. if (Math::is_zero_approx(edge_dot)) {
  1025. // Edge is perpendicular to cylinder axis.
  1026. continue;
  1027. }
  1028. // Calculate intersection between edge and circle plane.
  1029. Vector3 edge_diff = cap_pos - edge_start;
  1030. real_t diff_dot = edge_diff.dot(cyl_axis);
  1031. Vector3 intersection = edge_start + edge_dir * diff_dot / edge_dot;
  1032. // Calculate tangent that touches intersection.
  1033. Vector3 tangent = (cap_pos - intersection).cross(cyl_axis);
  1034. // Axis is orthogonal both to tangent and edge direction.
  1035. Vector3 axis = tangent.cross(edge_dir);
  1036. if (!separator.test_axis(axis.normalized())) {
  1037. return;
  1038. }
  1039. }
  1040. }
  1041. separator.generate_contacts();
  1042. }
  1043. template <bool withMargin>
  1044. static void _collision_box_convex_polygon(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1045. const BoxShapeSW *box_A = static_cast<const BoxShapeSW *>(p_a);
  1046. const ConvexPolygonShapeSW *convex_polygon_B = static_cast<const ConvexPolygonShapeSW *>(p_b);
  1047. SeparatorAxisTest<BoxShapeSW, ConvexPolygonShapeSW, withMargin> separator(box_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1048. if (!separator.test_previous_axis()) {
  1049. return;
  1050. }
  1051. const Geometry::MeshData &mesh = convex_polygon_B->get_mesh();
  1052. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  1053. int face_count = mesh.faces.size();
  1054. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  1055. int edge_count = mesh.edges.size();
  1056. const Vector3 *vertices = mesh.vertices.ptr();
  1057. int vertex_count = mesh.vertices.size();
  1058. // faces of A
  1059. for (int i = 0; i < 3; i++) {
  1060. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  1061. if (!separator.test_axis(axis)) {
  1062. return;
  1063. }
  1064. }
  1065. // Precalculating this makes the transforms faster.
  1066. Basis b_xform_normal = p_transform_b.basis.inverse().transposed();
  1067. // faces of B
  1068. for (int i = 0; i < face_count; i++) {
  1069. Vector3 axis = b_xform_normal.xform(faces[i].plane.normal).normalized();
  1070. if (!separator.test_axis(axis)) {
  1071. return;
  1072. }
  1073. }
  1074. // A<->B edges
  1075. for (int i = 0; i < 3; i++) {
  1076. Vector3 e1 = p_transform_a.basis.get_axis(i);
  1077. for (int j = 0; j < edge_count; j++) {
  1078. Vector3 e2 = p_transform_b.basis.xform(vertices[edges[j].a]) - p_transform_b.basis.xform(vertices[edges[j].b]);
  1079. Vector3 axis = e1.cross(e2).normalized();
  1080. if (!separator.test_axis(axis)) {
  1081. return;
  1082. }
  1083. }
  1084. }
  1085. if (withMargin) {
  1086. // calculate closest points between vertices and box edges
  1087. for (int v = 0; v < vertex_count; v++) {
  1088. Vector3 vtxb = p_transform_b.xform(vertices[v]);
  1089. Vector3 ab_vec = vtxb - p_transform_a.origin;
  1090. Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
  1091. Vector3 support_a = p_transform_a.xform(Vector3(
  1092. (cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  1093. (cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  1094. (cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  1095. Vector3 axis_ab = support_a - vtxb;
  1096. if (!separator.test_axis(axis_ab.normalized())) {
  1097. return;
  1098. }
  1099. //now try edges, which become cylinders!
  1100. for (int i = 0; i < 3; i++) {
  1101. //a ->b
  1102. Vector3 axis_a = p_transform_a.basis.get_axis(i);
  1103. if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized())) {
  1104. return;
  1105. }
  1106. }
  1107. }
  1108. //convex edges and box points
  1109. for (int i = 0; i < 2; i++) {
  1110. for (int j = 0; j < 2; j++) {
  1111. for (int k = 0; k < 2; k++) {
  1112. Vector3 he = box_A->get_half_extents();
  1113. he.x *= (i * 2 - 1);
  1114. he.y *= (j * 2 - 1);
  1115. he.z *= (k * 2 - 1);
  1116. Vector3 point = p_transform_a.origin;
  1117. for (int l = 0; l < 3; l++) {
  1118. point += p_transform_a.basis.get_axis(l) * he[l];
  1119. }
  1120. for (int e = 0; e < edge_count; e++) {
  1121. Vector3 p1 = p_transform_b.xform(vertices[edges[e].a]);
  1122. Vector3 p2 = p_transform_b.xform(vertices[edges[e].b]);
  1123. Vector3 n = (p2 - p1);
  1124. if (!separator.test_axis((point - p2).cross(n).cross(n).normalized())) {
  1125. return;
  1126. }
  1127. }
  1128. }
  1129. }
  1130. }
  1131. }
  1132. separator.generate_contacts();
  1133. }
  1134. template <bool withMargin>
  1135. static void _collision_box_face(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1136. const BoxShapeSW *box_A = static_cast<const BoxShapeSW *>(p_a);
  1137. const FaceShapeSW *face_B = static_cast<const FaceShapeSW *>(p_b);
  1138. SeparatorAxisTest<BoxShapeSW, FaceShapeSW, withMargin> separator(box_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1139. Vector3 vertex[3] = {
  1140. p_transform_b.xform(face_B->vertex[0]),
  1141. p_transform_b.xform(face_B->vertex[1]),
  1142. p_transform_b.xform(face_B->vertex[2]),
  1143. };
  1144. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  1145. return;
  1146. }
  1147. // faces of A
  1148. for (int i = 0; i < 3; i++) {
  1149. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  1150. if (!separator.test_axis(axis)) {
  1151. return;
  1152. }
  1153. }
  1154. // combined edges
  1155. for (int i = 0; i < 3; i++) {
  1156. Vector3 e = vertex[i] - vertex[(i + 1) % 3];
  1157. for (int j = 0; j < 3; j++) {
  1158. Vector3 axis = p_transform_a.basis.get_axis(j);
  1159. if (!separator.test_axis(e.cross(axis).normalized())) {
  1160. return;
  1161. }
  1162. }
  1163. }
  1164. if (withMargin) {
  1165. // calculate closest points between vertices and box edges
  1166. for (int v = 0; v < 3; v++) {
  1167. Vector3 ab_vec = vertex[v] - p_transform_a.origin;
  1168. Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
  1169. Vector3 support_a = p_transform_a.xform(Vector3(
  1170. (cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  1171. (cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  1172. (cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  1173. Vector3 axis_ab = support_a - vertex[v];
  1174. if (!separator.test_axis(axis_ab.normalized())) {
  1175. return;
  1176. }
  1177. //now try edges, which become cylinders!
  1178. for (int i = 0; i < 3; i++) {
  1179. //a ->b
  1180. Vector3 axis_a = p_transform_a.basis.get_axis(i);
  1181. if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized())) {
  1182. return;
  1183. }
  1184. }
  1185. }
  1186. //convex edges and box points, there has to be a way to speed up this (get closest point?)
  1187. for (int i = 0; i < 2; i++) {
  1188. for (int j = 0; j < 2; j++) {
  1189. for (int k = 0; k < 2; k++) {
  1190. Vector3 he = box_A->get_half_extents();
  1191. he.x *= (i * 2 - 1);
  1192. he.y *= (j * 2 - 1);
  1193. he.z *= (k * 2 - 1);
  1194. Vector3 point = p_transform_a.origin;
  1195. for (int l = 0; l < 3; l++) {
  1196. point += p_transform_a.basis.get_axis(l) * he[l];
  1197. }
  1198. for (int e = 0; e < 3; e++) {
  1199. Vector3 p1 = vertex[e];
  1200. Vector3 p2 = vertex[(e + 1) % 3];
  1201. Vector3 n = (p2 - p1);
  1202. if (!separator.test_axis((point - p2).cross(n).cross(n).normalized())) {
  1203. return;
  1204. }
  1205. }
  1206. }
  1207. }
  1208. }
  1209. }
  1210. separator.generate_contacts();
  1211. }
  1212. template <bool withMargin>
  1213. static void _collision_capsule_capsule(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1214. const CapsuleShapeSW *capsule_A = static_cast<const CapsuleShapeSW *>(p_a);
  1215. const CapsuleShapeSW *capsule_B = static_cast<const CapsuleShapeSW *>(p_b);
  1216. SeparatorAxisTest<CapsuleShapeSW, CapsuleShapeSW, withMargin> separator(capsule_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1217. if (!separator.test_previous_axis()) {
  1218. return;
  1219. }
  1220. // some values
  1221. Vector3 capsule_A_axis = p_transform_a.basis.get_axis(2) * (capsule_A->get_height() * 0.5);
  1222. Vector3 capsule_B_axis = p_transform_b.basis.get_axis(2) * (capsule_B->get_height() * 0.5);
  1223. Vector3 capsule_A_ball_1 = p_transform_a.origin + capsule_A_axis;
  1224. Vector3 capsule_A_ball_2 = p_transform_a.origin - capsule_A_axis;
  1225. Vector3 capsule_B_ball_1 = p_transform_b.origin + capsule_B_axis;
  1226. Vector3 capsule_B_ball_2 = p_transform_b.origin - capsule_B_axis;
  1227. //balls-balls
  1228. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_1).normalized())) {
  1229. return;
  1230. }
  1231. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_2).normalized())) {
  1232. return;
  1233. }
  1234. if (!separator.test_axis((capsule_A_ball_2 - capsule_B_ball_1).normalized())) {
  1235. return;
  1236. }
  1237. if (!separator.test_axis((capsule_A_ball_2 - capsule_B_ball_2).normalized())) {
  1238. return;
  1239. }
  1240. // edges-balls
  1241. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_1).cross(capsule_A_axis).cross(capsule_A_axis).normalized())) {
  1242. return;
  1243. }
  1244. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_2).cross(capsule_A_axis).cross(capsule_A_axis).normalized())) {
  1245. return;
  1246. }
  1247. if (!separator.test_axis((capsule_B_ball_1 - capsule_A_ball_1).cross(capsule_B_axis).cross(capsule_B_axis).normalized())) {
  1248. return;
  1249. }
  1250. if (!separator.test_axis((capsule_B_ball_1 - capsule_A_ball_2).cross(capsule_B_axis).cross(capsule_B_axis).normalized())) {
  1251. return;
  1252. }
  1253. // edges
  1254. if (!separator.test_axis(capsule_A_axis.cross(capsule_B_axis).normalized())) {
  1255. return;
  1256. }
  1257. separator.generate_contacts();
  1258. }
  1259. template <bool withMargin>
  1260. static void _collision_capsule_cylinder(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1261. const CapsuleShapeSW *capsule_A = static_cast<const CapsuleShapeSW *>(p_a);
  1262. const CylinderShapeSW *cylinder_B = static_cast<const CylinderShapeSW *>(p_b);
  1263. SeparatorAxisTest<CapsuleShapeSW, CylinderShapeSW, withMargin> separator(capsule_A, p_transform_a, cylinder_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1264. if (!separator.test_previous_axis()) {
  1265. return;
  1266. }
  1267. // Cylinder B end caps.
  1268. Vector3 cylinder_B_axis = p_transform_b.basis.get_axis(1).normalized();
  1269. if (!separator.test_axis(cylinder_B_axis)) {
  1270. return;
  1271. }
  1272. // Cylinder edge against capsule balls.
  1273. Vector3 capsule_A_axis = p_transform_a.basis.get_axis(2);
  1274. Vector3 capsule_A_ball_1 = p_transform_a.origin + capsule_A_axis * (capsule_A->get_height() * 0.5);
  1275. Vector3 capsule_A_ball_2 = p_transform_a.origin - capsule_A_axis * (capsule_A->get_height() * 0.5);
  1276. if (!separator.test_axis((p_transform_b.origin - capsule_A_ball_1).cross(cylinder_B_axis).cross(cylinder_B_axis).normalized())) {
  1277. return;
  1278. }
  1279. if (!separator.test_axis((p_transform_b.origin - capsule_A_ball_2).cross(cylinder_B_axis).cross(cylinder_B_axis).normalized())) {
  1280. return;
  1281. }
  1282. // Cylinder edge against capsule edge.
  1283. Vector3 center_diff = p_transform_b.origin - p_transform_a.origin;
  1284. if (!separator.test_axis(capsule_A_axis.cross(center_diff).cross(capsule_A_axis).normalized())) {
  1285. return;
  1286. }
  1287. if (!separator.test_axis(cylinder_B_axis.cross(center_diff).cross(cylinder_B_axis).normalized())) {
  1288. return;
  1289. }
  1290. real_t proj = capsule_A_axis.cross(cylinder_B_axis).cross(cylinder_B_axis).dot(capsule_A_axis);
  1291. if (Math::is_zero_approx(proj)) {
  1292. // Parallel capsule and cylinder axes, handle with specific axes only.
  1293. // Note: GJKEPA with no margin can lead to degenerate cases in this situation.
  1294. separator.generate_contacts();
  1295. return;
  1296. }
  1297. CollisionSolverSW::CallbackResult callback = SeparatorAxisTest<CapsuleShapeSW, CylinderShapeSW, withMargin>::test_contact_points;
  1298. // Fallback to generic algorithm to find the best separating axis.
  1299. if (!fallback_collision_solver(p_a, p_transform_a, p_b, p_transform_b, callback, &separator, false, p_margin_a, p_margin_b)) {
  1300. return;
  1301. }
  1302. separator.generate_contacts();
  1303. }
  1304. template <bool withMargin>
  1305. static void _collision_capsule_convex_polygon(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1306. const CapsuleShapeSW *capsule_A = static_cast<const CapsuleShapeSW *>(p_a);
  1307. const ConvexPolygonShapeSW *convex_polygon_B = static_cast<const ConvexPolygonShapeSW *>(p_b);
  1308. SeparatorAxisTest<CapsuleShapeSW, ConvexPolygonShapeSW, withMargin> separator(capsule_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1309. if (!separator.test_previous_axis()) {
  1310. return;
  1311. }
  1312. const Geometry::MeshData &mesh = convex_polygon_B->get_mesh();
  1313. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  1314. int face_count = mesh.faces.size();
  1315. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  1316. int edge_count = mesh.edges.size();
  1317. const Vector3 *vertices = mesh.vertices.ptr();
  1318. // Precalculating this makes the transforms faster.
  1319. Basis b_xform_normal = p_transform_b.basis.inverse().transposed();
  1320. // faces of B
  1321. for (int i = 0; i < face_count; i++) {
  1322. Vector3 axis = b_xform_normal.xform(faces[i].plane.normal).normalized();
  1323. if (!separator.test_axis(axis)) {
  1324. return;
  1325. }
  1326. }
  1327. // edges of B, capsule cylinder
  1328. for (int i = 0; i < edge_count; i++) {
  1329. // cylinder
  1330. Vector3 edge_axis = p_transform_b.basis.xform(vertices[edges[i].a]) - p_transform_b.basis.xform(vertices[edges[i].b]);
  1331. Vector3 axis = edge_axis.cross(p_transform_a.basis.get_axis(2)).normalized();
  1332. if (!separator.test_axis(axis)) {
  1333. return;
  1334. }
  1335. }
  1336. // capsule balls, edges of B
  1337. for (int i = 0; i < 2; i++) {
  1338. // edges of B, capsule cylinder
  1339. Vector3 capsule_axis = p_transform_a.basis.get_axis(2) * (capsule_A->get_height() * 0.5);
  1340. Vector3 sphere_pos = p_transform_a.origin + ((i == 0) ? capsule_axis : -capsule_axis);
  1341. for (int j = 0; j < edge_count; j++) {
  1342. Vector3 n1 = sphere_pos - p_transform_b.xform(vertices[edges[j].a]);
  1343. Vector3 n2 = p_transform_b.basis.xform(vertices[edges[j].a]) - p_transform_b.basis.xform(vertices[edges[j].b]);
  1344. Vector3 axis = n1.cross(n2).cross(n2).normalized();
  1345. if (!separator.test_axis(axis)) {
  1346. return;
  1347. }
  1348. }
  1349. }
  1350. separator.generate_contacts();
  1351. }
  1352. template <bool withMargin>
  1353. static void _collision_capsule_face(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1354. const CapsuleShapeSW *capsule_A = static_cast<const CapsuleShapeSW *>(p_a);
  1355. const FaceShapeSW *face_B = static_cast<const FaceShapeSW *>(p_b);
  1356. SeparatorAxisTest<CapsuleShapeSW, FaceShapeSW, withMargin> separator(capsule_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1357. Vector3 vertex[3] = {
  1358. p_transform_b.xform(face_B->vertex[0]),
  1359. p_transform_b.xform(face_B->vertex[1]),
  1360. p_transform_b.xform(face_B->vertex[2]),
  1361. };
  1362. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  1363. return;
  1364. }
  1365. // edges of B, capsule cylinder
  1366. Vector3 capsule_axis = p_transform_a.basis.get_axis(2) * (capsule_A->get_height() * 0.5);
  1367. for (int i = 0; i < 3; i++) {
  1368. // edge-cylinder
  1369. Vector3 edge_axis = vertex[i] - vertex[(i + 1) % 3];
  1370. Vector3 axis = edge_axis.cross(capsule_axis).normalized();
  1371. if (!separator.test_axis(axis)) {
  1372. return;
  1373. }
  1374. if (!separator.test_axis((p_transform_a.origin - vertex[i]).cross(capsule_axis).cross(capsule_axis).normalized())) {
  1375. return;
  1376. }
  1377. for (int j = 0; j < 2; j++) {
  1378. // point-spheres
  1379. Vector3 sphere_pos = p_transform_a.origin + ((j == 0) ? capsule_axis : -capsule_axis);
  1380. Vector3 n1 = sphere_pos - vertex[i];
  1381. if (!separator.test_axis(n1.normalized())) {
  1382. return;
  1383. }
  1384. Vector3 n2 = edge_axis;
  1385. axis = n1.cross(n2).cross(n2);
  1386. if (!separator.test_axis(axis.normalized())) {
  1387. return;
  1388. }
  1389. }
  1390. }
  1391. separator.generate_contacts();
  1392. }
  1393. template <bool withMargin>
  1394. static void _collision_cylinder_cylinder(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1395. const CylinderShapeSW *cylinder_A = static_cast<const CylinderShapeSW *>(p_a);
  1396. const CylinderShapeSW *cylinder_B = static_cast<const CylinderShapeSW *>(p_b);
  1397. SeparatorAxisTest<CylinderShapeSW, CylinderShapeSW, withMargin> separator(cylinder_A, p_transform_a, cylinder_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1398. Vector3 cylinder_A_axis = p_transform_a.basis.get_axis(1);
  1399. Vector3 cylinder_B_axis = p_transform_b.basis.get_axis(1);
  1400. if (!separator.test_previous_axis()) {
  1401. return;
  1402. }
  1403. // Cylinder A end caps.
  1404. if (!separator.test_axis(cylinder_A_axis.normalized())) {
  1405. return;
  1406. }
  1407. // Cylinder B end caps.
  1408. if (!separator.test_axis(cylinder_A_axis.normalized())) {
  1409. return;
  1410. }
  1411. Vector3 cylinder_diff = p_transform_b.origin - p_transform_a.origin;
  1412. // Cylinder A lateral surface.
  1413. if (!separator.test_axis(cylinder_A_axis.cross(cylinder_diff).cross(cylinder_A_axis).normalized())) {
  1414. return;
  1415. }
  1416. // Cylinder B lateral surface.
  1417. if (!separator.test_axis(cylinder_B_axis.cross(cylinder_diff).cross(cylinder_B_axis).normalized())) {
  1418. return;
  1419. }
  1420. real_t proj = cylinder_A_axis.cross(cylinder_B_axis).cross(cylinder_B_axis).dot(cylinder_A_axis);
  1421. if (Math::is_zero_approx(proj)) {
  1422. // Parallel cylinders, handle with specific axes only.
  1423. // Note: GJKEPA with no margin can lead to degenerate cases in this situation.
  1424. separator.generate_contacts();
  1425. return;
  1426. }
  1427. CollisionSolverSW::CallbackResult callback = SeparatorAxisTest<CylinderShapeSW, CylinderShapeSW, withMargin>::test_contact_points;
  1428. // Fallback to generic algorithm to find the best separating axis.
  1429. if (!fallback_collision_solver(p_a, p_transform_a, p_b, p_transform_b, callback, &separator, false, p_margin_a, p_margin_b)) {
  1430. return;
  1431. }
  1432. separator.generate_contacts();
  1433. }
  1434. template <bool withMargin>
  1435. static void _collision_cylinder_convex_polygon(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1436. const CylinderShapeSW *cylinder_A = static_cast<const CylinderShapeSW *>(p_a);
  1437. const ConvexPolygonShapeSW *convex_polygon_B = static_cast<const ConvexPolygonShapeSW *>(p_b);
  1438. SeparatorAxisTest<CylinderShapeSW, ConvexPolygonShapeSW, withMargin> separator(cylinder_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1439. CollisionSolverSW::CallbackResult callback = SeparatorAxisTest<CylinderShapeSW, ConvexPolygonShapeSW, withMargin>::test_contact_points;
  1440. // Fallback to generic algorithm to find the best separating axis.
  1441. if (!fallback_collision_solver(p_a, p_transform_a, p_b, p_transform_b, callback, &separator, false, p_margin_a, p_margin_b)) {
  1442. return;
  1443. }
  1444. separator.generate_contacts();
  1445. }
  1446. template <bool withMargin>
  1447. static void _collision_cylinder_face(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1448. const CylinderShapeSW *cylinder_A = static_cast<const CylinderShapeSW *>(p_a);
  1449. const FaceShapeSW *face_B = static_cast<const FaceShapeSW *>(p_b);
  1450. SeparatorAxisTest<CylinderShapeSW, FaceShapeSW, withMargin> separator(cylinder_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1451. if (!separator.test_previous_axis()) {
  1452. return;
  1453. }
  1454. Vector3 vertex[3] = {
  1455. p_transform_b.xform(face_B->vertex[0]),
  1456. p_transform_b.xform(face_B->vertex[1]),
  1457. p_transform_b.xform(face_B->vertex[2]),
  1458. };
  1459. // Face B normal.
  1460. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  1461. return;
  1462. }
  1463. Vector3 cyl_axis = p_transform_a.basis.get_axis(1).normalized();
  1464. // Cylinder end caps.
  1465. {
  1466. if (!separator.test_axis(cyl_axis)) {
  1467. return;
  1468. }
  1469. }
  1470. // Edges of B, cylinder lateral surface.
  1471. for (int i = 0; i < 3; i++) {
  1472. Vector3 edge_axis = vertex[i] - vertex[(i + 1) % 3];
  1473. Vector3 axis = edge_axis.cross(cyl_axis);
  1474. if (Math::is_zero_approx(axis.length_squared())) {
  1475. continue;
  1476. }
  1477. if (!separator.test_axis(axis.normalized())) {
  1478. return;
  1479. }
  1480. }
  1481. // Points of B, cylinder lateral surface.
  1482. for (int i = 0; i < 3; i++) {
  1483. const Vector3 &point = vertex[i];
  1484. Vector3 axis = Plane(cyl_axis, 0).project(point).normalized();
  1485. if (!separator.test_axis(axis)) {
  1486. return;
  1487. }
  1488. }
  1489. // Edges of B, cylinder end caps rim.
  1490. Vector3 cap_axis = cyl_axis * (cylinder_A->get_height() * 0.5);
  1491. for (int i = 0; i < 2; i++) {
  1492. Vector3 cap_pos = p_transform_a.origin + ((i == 0) ? cap_axis : -cap_axis);
  1493. for (int j = 0; j < 3; j++) {
  1494. const Vector3 &edge_start = vertex[j];
  1495. const Vector3 &edge_end = vertex[(j + 1) % 3];
  1496. Vector3 edge_dir = edge_end - edge_start;
  1497. edge_dir.normalize();
  1498. real_t edge_dot = edge_dir.dot(cyl_axis);
  1499. if (Math::is_zero_approx(edge_dot)) {
  1500. // Edge is perpendicular to cylinder axis.
  1501. continue;
  1502. }
  1503. // Calculate intersection between edge and circle plane.
  1504. Vector3 edge_diff = cap_pos - edge_start;
  1505. real_t diff_dot = edge_diff.dot(cyl_axis);
  1506. Vector3 intersection = edge_start + edge_dir * diff_dot / edge_dot;
  1507. // Calculate tangent that touches intersection.
  1508. Vector3 tangent = (cap_pos - intersection).cross(cyl_axis);
  1509. // Axis is orthogonal both to tangent and edge direction.
  1510. Vector3 axis = tangent.cross(edge_dir);
  1511. if (!separator.test_axis(axis.normalized())) {
  1512. return;
  1513. }
  1514. }
  1515. }
  1516. separator.generate_contacts();
  1517. }
  1518. template <bool withMargin>
  1519. static void _collision_convex_polygon_convex_polygon(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1520. const ConvexPolygonShapeSW *convex_polygon_A = static_cast<const ConvexPolygonShapeSW *>(p_a);
  1521. const ConvexPolygonShapeSW *convex_polygon_B = static_cast<const ConvexPolygonShapeSW *>(p_b);
  1522. SeparatorAxisTest<ConvexPolygonShapeSW, ConvexPolygonShapeSW, withMargin> separator(convex_polygon_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1523. if (!separator.test_previous_axis()) {
  1524. return;
  1525. }
  1526. const Geometry::MeshData &mesh_A = convex_polygon_A->get_mesh();
  1527. const Geometry::MeshData::Face *faces_A = mesh_A.faces.ptr();
  1528. int face_count_A = mesh_A.faces.size();
  1529. const Geometry::MeshData::Edge *edges_A = mesh_A.edges.ptr();
  1530. int edge_count_A = mesh_A.edges.size();
  1531. const Vector3 *vertices_A = mesh_A.vertices.ptr();
  1532. int vertex_count_A = mesh_A.vertices.size();
  1533. const Geometry::MeshData &mesh_B = convex_polygon_B->get_mesh();
  1534. const Geometry::MeshData::Face *faces_B = mesh_B.faces.ptr();
  1535. int face_count_B = mesh_B.faces.size();
  1536. const Geometry::MeshData::Edge *edges_B = mesh_B.edges.ptr();
  1537. int edge_count_B = mesh_B.edges.size();
  1538. const Vector3 *vertices_B = mesh_B.vertices.ptr();
  1539. int vertex_count_B = mesh_B.vertices.size();
  1540. // Precalculating this makes the transforms faster.
  1541. Basis a_xform_normal = p_transform_a.basis.inverse().transposed();
  1542. // faces of A
  1543. for (int i = 0; i < face_count_A; i++) {
  1544. Vector3 axis = a_xform_normal.xform(faces_A[i].plane.normal).normalized();
  1545. if (!separator.test_axis(axis)) {
  1546. return;
  1547. }
  1548. }
  1549. // Precalculating this makes the transforms faster.
  1550. Basis b_xform_normal = p_transform_b.basis.inverse().transposed();
  1551. // faces of B
  1552. for (int i = 0; i < face_count_B; i++) {
  1553. Vector3 axis = b_xform_normal.xform(faces_B[i].plane.normal).normalized();
  1554. if (!separator.test_axis(axis)) {
  1555. return;
  1556. }
  1557. }
  1558. // A<->B edges
  1559. for (int i = 0; i < edge_count_A; i++) {
  1560. Vector3 e1 = p_transform_a.basis.xform(vertices_A[edges_A[i].a]) - p_transform_a.basis.xform(vertices_A[edges_A[i].b]);
  1561. for (int j = 0; j < edge_count_B; j++) {
  1562. Vector3 e2 = p_transform_b.basis.xform(vertices_B[edges_B[j].a]) - p_transform_b.basis.xform(vertices_B[edges_B[j].b]);
  1563. Vector3 axis = e1.cross(e2).normalized();
  1564. if (!separator.test_axis(axis)) {
  1565. return;
  1566. }
  1567. }
  1568. }
  1569. if (withMargin) {
  1570. //vertex-vertex
  1571. for (int i = 0; i < vertex_count_A; i++) {
  1572. Vector3 va = p_transform_a.xform(vertices_A[i]);
  1573. for (int j = 0; j < vertex_count_B; j++) {
  1574. if (!separator.test_axis((va - p_transform_b.xform(vertices_B[j])).normalized())) {
  1575. return;
  1576. }
  1577. }
  1578. }
  1579. //edge-vertex (shell)
  1580. for (int i = 0; i < edge_count_A; i++) {
  1581. Vector3 e1 = p_transform_a.basis.xform(vertices_A[edges_A[i].a]);
  1582. Vector3 e2 = p_transform_a.basis.xform(vertices_A[edges_A[i].b]);
  1583. Vector3 n = (e2 - e1);
  1584. for (int j = 0; j < vertex_count_B; j++) {
  1585. Vector3 e3 = p_transform_b.xform(vertices_B[j]);
  1586. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
  1587. return;
  1588. }
  1589. }
  1590. }
  1591. for (int i = 0; i < edge_count_B; i++) {
  1592. Vector3 e1 = p_transform_b.basis.xform(vertices_B[edges_B[i].a]);
  1593. Vector3 e2 = p_transform_b.basis.xform(vertices_B[edges_B[i].b]);
  1594. Vector3 n = (e2 - e1);
  1595. for (int j = 0; j < vertex_count_A; j++) {
  1596. Vector3 e3 = p_transform_a.xform(vertices_A[j]);
  1597. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
  1598. return;
  1599. }
  1600. }
  1601. }
  1602. }
  1603. separator.generate_contacts();
  1604. }
  1605. template <bool withMargin>
  1606. static void _collision_convex_polygon_face(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1607. const ConvexPolygonShapeSW *convex_polygon_A = static_cast<const ConvexPolygonShapeSW *>(p_a);
  1608. const FaceShapeSW *face_B = static_cast<const FaceShapeSW *>(p_b);
  1609. SeparatorAxisTest<ConvexPolygonShapeSW, FaceShapeSW, withMargin> separator(convex_polygon_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1610. const Geometry::MeshData &mesh = convex_polygon_A->get_mesh();
  1611. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  1612. int face_count = mesh.faces.size();
  1613. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  1614. int edge_count = mesh.edges.size();
  1615. const Vector3 *vertices = mesh.vertices.ptr();
  1616. int vertex_count = mesh.vertices.size();
  1617. Vector3 vertex[3] = {
  1618. p_transform_b.xform(face_B->vertex[0]),
  1619. p_transform_b.xform(face_B->vertex[1]),
  1620. p_transform_b.xform(face_B->vertex[2]),
  1621. };
  1622. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  1623. return;
  1624. }
  1625. // faces of A
  1626. for (int i = 0; i < face_count; i++) {
  1627. //Vector3 axis = p_transform_a.xform( faces[i].plane ).normal;
  1628. Vector3 axis = p_transform_a.basis.xform(faces[i].plane.normal).normalized();
  1629. if (!separator.test_axis(axis)) {
  1630. return;
  1631. }
  1632. }
  1633. // A<->B edges
  1634. for (int i = 0; i < edge_count; i++) {
  1635. Vector3 e1 = p_transform_a.xform(vertices[edges[i].a]) - p_transform_a.xform(vertices[edges[i].b]);
  1636. for (int j = 0; j < 3; j++) {
  1637. Vector3 e2 = vertex[j] - vertex[(j + 1) % 3];
  1638. Vector3 axis = e1.cross(e2).normalized();
  1639. if (!separator.test_axis(axis)) {
  1640. return;
  1641. }
  1642. }
  1643. }
  1644. if (withMargin) {
  1645. //vertex-vertex
  1646. for (int i = 0; i < vertex_count; i++) {
  1647. Vector3 va = p_transform_a.xform(vertices[i]);
  1648. for (int j = 0; j < 3; j++) {
  1649. if (!separator.test_axis((va - vertex[j]).normalized())) {
  1650. return;
  1651. }
  1652. }
  1653. }
  1654. //edge-vertex (shell)
  1655. for (int i = 0; i < edge_count; i++) {
  1656. Vector3 e1 = p_transform_a.basis.xform(vertices[edges[i].a]);
  1657. Vector3 e2 = p_transform_a.basis.xform(vertices[edges[i].b]);
  1658. Vector3 n = (e2 - e1);
  1659. for (int j = 0; j < 3; j++) {
  1660. Vector3 e3 = vertex[j];
  1661. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
  1662. return;
  1663. }
  1664. }
  1665. }
  1666. for (int i = 0; i < 3; i++) {
  1667. Vector3 e1 = vertex[i];
  1668. Vector3 e2 = vertex[(i + 1) % 3];
  1669. Vector3 n = (e2 - e1);
  1670. for (int j = 0; j < vertex_count; j++) {
  1671. Vector3 e3 = p_transform_a.xform(vertices[j]);
  1672. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
  1673. return;
  1674. }
  1675. }
  1676. }
  1677. }
  1678. separator.generate_contacts();
  1679. }
  1680. bool sat_calculate_penetration(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CollisionSolverSW::CallbackResult p_result_callback, void *p_userdata, bool p_swap, Vector3 *r_prev_axis, real_t p_margin_a, real_t p_margin_b) {
  1681. PhysicsServer::ShapeType type_A = p_shape_A->get_type();
  1682. ERR_FAIL_COND_V(type_A == PhysicsServer::SHAPE_PLANE, false);
  1683. ERR_FAIL_COND_V(type_A == PhysicsServer::SHAPE_RAY, false);
  1684. ERR_FAIL_COND_V(p_shape_A->is_concave(), false);
  1685. PhysicsServer::ShapeType type_B = p_shape_B->get_type();
  1686. ERR_FAIL_COND_V(type_B == PhysicsServer::SHAPE_PLANE, false);
  1687. ERR_FAIL_COND_V(type_B == PhysicsServer::SHAPE_RAY, false);
  1688. ERR_FAIL_COND_V(p_shape_B->is_concave(), false);
  1689. static const CollisionFunc collision_table[6][6] = {
  1690. { _collision_sphere_sphere<false>,
  1691. _collision_sphere_box<false>,
  1692. _collision_sphere_capsule<false>,
  1693. _collision_sphere_cylinder<false>,
  1694. _collision_sphere_convex_polygon<false>,
  1695. _collision_sphere_face<false> },
  1696. { nullptr,
  1697. _collision_box_box<false>,
  1698. _collision_box_capsule<false>,
  1699. _collision_box_cylinder<false>,
  1700. _collision_box_convex_polygon<false>,
  1701. _collision_box_face<false> },
  1702. { nullptr,
  1703. nullptr,
  1704. _collision_capsule_capsule<false>,
  1705. _collision_capsule_cylinder<false>,
  1706. _collision_capsule_convex_polygon<false>,
  1707. _collision_capsule_face<false> },
  1708. { nullptr,
  1709. nullptr,
  1710. nullptr,
  1711. _collision_cylinder_cylinder<false>,
  1712. _collision_cylinder_convex_polygon<false>,
  1713. _collision_cylinder_face<false> },
  1714. { nullptr,
  1715. nullptr,
  1716. nullptr,
  1717. nullptr,
  1718. _collision_convex_polygon_convex_polygon<false>,
  1719. _collision_convex_polygon_face<false> },
  1720. { nullptr,
  1721. nullptr,
  1722. nullptr,
  1723. nullptr,
  1724. nullptr,
  1725. nullptr },
  1726. };
  1727. static const CollisionFunc collision_table_margin[6][6] = {
  1728. { _collision_sphere_sphere<true>,
  1729. _collision_sphere_box<true>,
  1730. _collision_sphere_capsule<true>,
  1731. _collision_sphere_cylinder<true>,
  1732. _collision_sphere_convex_polygon<true>,
  1733. _collision_sphere_face<true> },
  1734. { nullptr,
  1735. _collision_box_box<true>,
  1736. _collision_box_capsule<true>,
  1737. _collision_box_cylinder<true>,
  1738. _collision_box_convex_polygon<true>,
  1739. _collision_box_face<true> },
  1740. { nullptr,
  1741. nullptr,
  1742. _collision_capsule_capsule<true>,
  1743. _collision_capsule_cylinder<true>,
  1744. _collision_capsule_convex_polygon<true>,
  1745. _collision_capsule_face<true> },
  1746. { nullptr,
  1747. nullptr,
  1748. nullptr,
  1749. _collision_cylinder_cylinder<true>,
  1750. _collision_cylinder_convex_polygon<true>,
  1751. _collision_cylinder_face<true> },
  1752. { nullptr,
  1753. nullptr,
  1754. nullptr,
  1755. nullptr,
  1756. _collision_convex_polygon_convex_polygon<true>,
  1757. _collision_convex_polygon_face<true> },
  1758. { nullptr,
  1759. nullptr,
  1760. nullptr,
  1761. nullptr,
  1762. nullptr,
  1763. nullptr },
  1764. };
  1765. _CollectorCallback callback;
  1766. callback.callback = p_result_callback;
  1767. callback.swap = p_swap;
  1768. callback.userdata = p_userdata;
  1769. callback.collided = false;
  1770. callback.prev_axis = r_prev_axis;
  1771. const ShapeSW *A = p_shape_A;
  1772. const ShapeSW *B = p_shape_B;
  1773. const Transform *transform_A = &p_transform_A;
  1774. const Transform *transform_B = &p_transform_B;
  1775. real_t margin_A = p_margin_a;
  1776. real_t margin_B = p_margin_b;
  1777. if (type_A > type_B) {
  1778. SWAP(A, B);
  1779. SWAP(transform_A, transform_B);
  1780. SWAP(type_A, type_B);
  1781. SWAP(margin_A, margin_B);
  1782. callback.swap = !callback.swap;
  1783. }
  1784. CollisionFunc collision_func;
  1785. if (margin_A != 0.0 || margin_B != 0.0) {
  1786. collision_func = collision_table_margin[type_A - 2][type_B - 2];
  1787. } else {
  1788. collision_func = collision_table[type_A - 2][type_B - 2];
  1789. }
  1790. ERR_FAIL_COND_V(!collision_func, false);
  1791. collision_func(A, *transform_A, B, *transform_B, &callback, margin_A, margin_B);
  1792. return callback.collided;
  1793. }