collision_solver_3d_sat.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254
  1. /*************************************************************************/
  2. /* collision_solver_3d_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 "collision_solver_3d_sat.h"
  31. #include "core/math/geometry_3d.h"
  32. #include "gjk_epa.h"
  33. #define fallback_collision_solver gjk_epa_calculate_penetration
  34. // Cylinder SAT analytic methods and face-circle 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: [email protected] 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. CollisionSolver3DSW::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 = Geometry3D::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 = Geometry3D::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, Shape3DSW::FeatureType p_feature_type_A, const Vector3 *p_points_B, int p_point_count_B, Shape3DSW::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 treshold 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. Shape3DSW::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. Shape3DSW::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 Shape3DSW *, const Transform &, const Shape3DSW *, const Transform &, _CollectorCallback *p_callback, real_t, real_t);
  615. template <bool withMargin>
  616. static void _collision_sphere_sphere(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  617. const SphereShape3DSW *sphere_A = static_cast<const SphereShape3DSW *>(p_a);
  618. const SphereShape3DSW *sphere_B = static_cast<const SphereShape3DSW *>(p_b);
  619. SeparatorAxisTest<SphereShape3DSW, SphereShape3DSW, 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 Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  631. const SphereShape3DSW *sphere_A = static_cast<const SphereShape3DSW *>(p_a);
  632. const BoxShape3DSW *box_B = static_cast<const BoxShape3DSW *>(p_b);
  633. SeparatorAxisTest<SphereShape3DSW, BoxShape3DSW, 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 Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  666. const SphereShape3DSW *sphere_A = static_cast<const SphereShape3DSW *>(p_a);
  667. const CapsuleShape3DSW *capsule_B = static_cast<const CapsuleShape3DSW *>(p_b);
  668. SeparatorAxisTest<SphereShape3DSW, CapsuleShape3DSW, 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 Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  693. const SphereShape3DSW *sphere_A = static_cast<const SphereShape3DSW *>(p_a);
  694. const CylinderShape3DSW *cylinder_B = static_cast<const CylinderShape3DSW *>(p_b);
  695. SeparatorAxisTest<SphereShape3DSW, CylinderShape3DSW, 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 Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  738. const SphereShape3DSW *sphere_A = static_cast<const SphereShape3DSW *>(p_a);
  739. const ConvexPolygonShape3DSW *convex_polygon_B = static_cast<const ConvexPolygonShape3DSW *>(p_b);
  740. SeparatorAxisTest<SphereShape3DSW, ConvexPolygonShape3DSW, 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 Geometry3D::MeshData &mesh = convex_polygon_B->get_mesh();
  745. const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
  746. int face_count = mesh.faces.size();
  747. const Geometry3D::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. // faces of B
  752. for (int i = 0; i < face_count; i++) {
  753. Vector3 axis = p_transform_b.xform(faces[i].plane).normal;
  754. if (!separator.test_axis(axis)) {
  755. return;
  756. }
  757. }
  758. // edges of B
  759. for (int i = 0; i < edge_count; i++) {
  760. Vector3 v1 = p_transform_b.xform(vertices[edges[i].a]);
  761. Vector3 v2 = p_transform_b.xform(vertices[edges[i].b]);
  762. Vector3 v3 = p_transform_a.origin;
  763. Vector3 n1 = v2 - v1;
  764. Vector3 n2 = v2 - v3;
  765. Vector3 axis = n1.cross(n2).cross(n1).normalized();
  766. if (!separator.test_axis(axis)) {
  767. return;
  768. }
  769. }
  770. // vertices of B
  771. for (int i = 0; i < vertex_count; i++) {
  772. Vector3 v1 = p_transform_b.xform(vertices[i]);
  773. Vector3 v2 = p_transform_a.origin;
  774. Vector3 axis = (v2 - v1).normalized();
  775. if (!separator.test_axis(axis)) {
  776. return;
  777. }
  778. }
  779. separator.generate_contacts();
  780. }
  781. template <bool withMargin>
  782. static void _collision_sphere_face(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  783. const SphereShape3DSW *sphere_A = static_cast<const SphereShape3DSW *>(p_a);
  784. const FaceShape3DSW *face_B = static_cast<const FaceShape3DSW *>(p_b);
  785. SeparatorAxisTest<SphereShape3DSW, FaceShape3DSW, withMargin> separator(sphere_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  786. Vector3 vertex[3] = {
  787. p_transform_b.xform(face_B->vertex[0]),
  788. p_transform_b.xform(face_B->vertex[1]),
  789. p_transform_b.xform(face_B->vertex[2]),
  790. };
  791. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  792. return;
  793. }
  794. // edges and points of B
  795. for (int i = 0; i < 3; i++) {
  796. Vector3 n1 = vertex[i] - p_transform_a.origin;
  797. if (!separator.test_axis(n1.normalized())) {
  798. return;
  799. }
  800. Vector3 n2 = vertex[(i + 1) % 3] - vertex[i];
  801. Vector3 axis = n1.cross(n2).cross(n2).normalized();
  802. if (!separator.test_axis(axis)) {
  803. return;
  804. }
  805. }
  806. separator.generate_contacts();
  807. }
  808. template <bool withMargin>
  809. static void _collision_box_box(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  810. const BoxShape3DSW *box_A = static_cast<const BoxShape3DSW *>(p_a);
  811. const BoxShape3DSW *box_B = static_cast<const BoxShape3DSW *>(p_b);
  812. SeparatorAxisTest<BoxShape3DSW, BoxShape3DSW, withMargin> separator(box_A, p_transform_a, box_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  813. if (!separator.test_previous_axis()) {
  814. return;
  815. }
  816. // test faces of A
  817. for (int i = 0; i < 3; i++) {
  818. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  819. if (!separator.test_axis(axis)) {
  820. return;
  821. }
  822. }
  823. // test faces of B
  824. for (int i = 0; i < 3; i++) {
  825. Vector3 axis = p_transform_b.basis.get_axis(i).normalized();
  826. if (!separator.test_axis(axis)) {
  827. return;
  828. }
  829. }
  830. // test combined edges
  831. for (int i = 0; i < 3; i++) {
  832. for (int j = 0; j < 3; j++) {
  833. Vector3 axis = p_transform_a.basis.get_axis(i).cross(p_transform_b.basis.get_axis(j));
  834. if (Math::is_zero_approx(axis.length_squared())) {
  835. continue;
  836. }
  837. axis.normalize();
  838. if (!separator.test_axis(axis)) {
  839. return;
  840. }
  841. }
  842. }
  843. if (withMargin) {
  844. //add endpoint test between closest vertices and edges
  845. // calculate closest point to sphere
  846. Vector3 ab_vec = p_transform_b.origin - p_transform_a.origin;
  847. Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
  848. Vector3 support_a = p_transform_a.xform(Vector3(
  849. (cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  850. (cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  851. (cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  852. Vector3 cnormal_b = p_transform_b.basis.xform_inv(-ab_vec);
  853. Vector3 support_b = p_transform_b.xform(Vector3(
  854. (cnormal_b.x < 0) ? -box_B->get_half_extents().x : box_B->get_half_extents().x,
  855. (cnormal_b.y < 0) ? -box_B->get_half_extents().y : box_B->get_half_extents().y,
  856. (cnormal_b.z < 0) ? -box_B->get_half_extents().z : box_B->get_half_extents().z));
  857. Vector3 axis_ab = (support_a - support_b);
  858. if (!separator.test_axis(axis_ab.normalized())) {
  859. return;
  860. }
  861. //now try edges, which become cylinders!
  862. for (int i = 0; i < 3; i++) {
  863. //a ->b
  864. Vector3 axis_a = p_transform_a.basis.get_axis(i);
  865. if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized())) {
  866. return;
  867. }
  868. //b ->a
  869. Vector3 axis_b = p_transform_b.basis.get_axis(i);
  870. if (!separator.test_axis(axis_ab.cross(axis_b).cross(axis_b).normalized())) {
  871. return;
  872. }
  873. }
  874. }
  875. separator.generate_contacts();
  876. }
  877. template <bool withMargin>
  878. static void _collision_box_capsule(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  879. const BoxShape3DSW *box_A = static_cast<const BoxShape3DSW *>(p_a);
  880. const CapsuleShape3DSW *capsule_B = static_cast<const CapsuleShape3DSW *>(p_b);
  881. SeparatorAxisTest<BoxShape3DSW, CapsuleShape3DSW, withMargin> separator(box_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  882. if (!separator.test_previous_axis()) {
  883. return;
  884. }
  885. // faces of A
  886. for (int i = 0; i < 3; i++) {
  887. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  888. if (!separator.test_axis(axis)) {
  889. return;
  890. }
  891. }
  892. Vector3 cyl_axis = p_transform_b.basis.get_axis(2).normalized();
  893. // edges of A, capsule cylinder
  894. for (int i = 0; i < 3; i++) {
  895. // cylinder
  896. Vector3 box_axis = p_transform_a.basis.get_axis(i);
  897. Vector3 axis = box_axis.cross(cyl_axis);
  898. if (Math::is_zero_approx(axis.length_squared())) {
  899. continue;
  900. }
  901. if (!separator.test_axis(axis.normalized())) {
  902. return;
  903. }
  904. }
  905. // points of A, capsule cylinder
  906. // this sure could be made faster somehow..
  907. for (int i = 0; i < 2; i++) {
  908. for (int j = 0; j < 2; j++) {
  909. for (int k = 0; k < 2; k++) {
  910. Vector3 he = box_A->get_half_extents();
  911. he.x *= (i * 2 - 1);
  912. he.y *= (j * 2 - 1);
  913. he.z *= (k * 2 - 1);
  914. Vector3 point = p_transform_a.origin;
  915. for (int l = 0; l < 3; l++) {
  916. point += p_transform_a.basis.get_axis(l) * he[l];
  917. }
  918. //Vector3 axis = (point - cyl_axis * cyl_axis.dot(point)).normalized();
  919. Vector3 axis = Plane(cyl_axis, 0).project(point).normalized();
  920. if (!separator.test_axis(axis)) {
  921. return;
  922. }
  923. }
  924. }
  925. }
  926. // capsule balls, edges of A
  927. for (int i = 0; i < 2; i++) {
  928. Vector3 capsule_axis = p_transform_b.basis.get_axis(2) * (capsule_B->get_height() * 0.5);
  929. Vector3 sphere_pos = p_transform_b.origin + ((i == 0) ? capsule_axis : -capsule_axis);
  930. Vector3 cnormal = p_transform_a.xform_inv(sphere_pos);
  931. Vector3 cpoint = p_transform_a.xform(Vector3(
  932. (cnormal.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  933. (cnormal.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  934. (cnormal.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  935. // use point to test axis
  936. Vector3 point_axis = (sphere_pos - cpoint).normalized();
  937. if (!separator.test_axis(point_axis)) {
  938. return;
  939. }
  940. // test edges of A
  941. for (int j = 0; j < 3; j++) {
  942. Vector3 axis = point_axis.cross(p_transform_a.basis.get_axis(j)).cross(p_transform_a.basis.get_axis(j)).normalized();
  943. if (!separator.test_axis(axis)) {
  944. return;
  945. }
  946. }
  947. }
  948. separator.generate_contacts();
  949. }
  950. template <bool withMargin>
  951. static void _collision_box_cylinder(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  952. const BoxShape3DSW *box_A = static_cast<const BoxShape3DSW *>(p_a);
  953. const CylinderShape3DSW *cylinder_B = static_cast<const CylinderShape3DSW *>(p_b);
  954. SeparatorAxisTest<BoxShape3DSW, CylinderShape3DSW, withMargin> separator(box_A, p_transform_a, cylinder_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  955. if (!separator.test_previous_axis()) {
  956. return;
  957. }
  958. // Faces of A.
  959. for (int i = 0; i < 3; i++) {
  960. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  961. if (!separator.test_axis(axis)) {
  962. return;
  963. }
  964. }
  965. Vector3 cyl_axis = p_transform_b.basis.get_axis(1).normalized();
  966. // Cylinder end caps.
  967. {
  968. if (!separator.test_axis(cyl_axis)) {
  969. return;
  970. }
  971. }
  972. // Edges of A, cylinder lateral surface.
  973. for (int i = 0; i < 3; i++) {
  974. Vector3 box_axis = p_transform_a.basis.get_axis(i);
  975. Vector3 axis = box_axis.cross(cyl_axis);
  976. if (Math::is_zero_approx(axis.length_squared())) {
  977. continue;
  978. }
  979. if (!separator.test_axis(axis.normalized())) {
  980. return;
  981. }
  982. }
  983. // Gather points of A.
  984. Vector3 vertices_A[8];
  985. Vector3 box_extent = box_A->get_half_extents();
  986. for (int i = 0; i < 2; i++) {
  987. for (int j = 0; j < 2; j++) {
  988. for (int k = 0; k < 2; k++) {
  989. Vector3 extent = box_extent;
  990. extent.x *= (i * 2 - 1);
  991. extent.y *= (j * 2 - 1);
  992. extent.z *= (k * 2 - 1);
  993. Vector3 &point = vertices_A[i * 2 * 2 + j * 2 + k];
  994. point = p_transform_a.origin;
  995. for (int l = 0; l < 3; l++) {
  996. point += p_transform_a.basis.get_axis(l) * extent[l];
  997. }
  998. }
  999. }
  1000. }
  1001. // Points of A, cylinder lateral surface.
  1002. for (int i = 0; i < 8; i++) {
  1003. const Vector3 &point = vertices_A[i];
  1004. Vector3 axis = Plane(cyl_axis, 0).project(point).normalized();
  1005. if (!separator.test_axis(axis)) {
  1006. return;
  1007. }
  1008. }
  1009. // Edges of A, cylinder end caps rim.
  1010. int edges_start_A[12] = { 0, 2, 4, 6, 0, 1, 4, 5, 0, 1, 2, 3 };
  1011. int edges_end_A[12] = { 1, 3, 5, 7, 2, 3, 6, 7, 4, 5, 6, 7 };
  1012. Vector3 cap_axis = cyl_axis * (cylinder_B->get_height() * 0.5);
  1013. for (int i = 0; i < 2; i++) {
  1014. Vector3 cap_pos = p_transform_b.origin + ((i == 0) ? cap_axis : -cap_axis);
  1015. for (int e = 0; e < 12; e++) {
  1016. const Vector3 &edge_start = vertices_A[edges_start_A[e]];
  1017. const Vector3 &edge_end = vertices_A[edges_end_A[e]];
  1018. Vector3 edge_dir = (edge_end - edge_start);
  1019. edge_dir.normalize();
  1020. real_t edge_dot = edge_dir.dot(cyl_axis);
  1021. if (Math::is_zero_approx(edge_dot)) {
  1022. // Edge is perpendicular to cylinder axis.
  1023. continue;
  1024. }
  1025. // Calculate intersection between edge and circle plane.
  1026. Vector3 edge_diff = cap_pos - edge_start;
  1027. real_t diff_dot = edge_diff.dot(cyl_axis);
  1028. Vector3 intersection = edge_start + edge_dir * diff_dot / edge_dot;
  1029. // Calculate tangent that touches intersection.
  1030. Vector3 tangent = (cap_pos - intersection).cross(cyl_axis);
  1031. // Axis is orthogonal both to tangent and edge direction.
  1032. Vector3 axis = tangent.cross(edge_dir);
  1033. if (!separator.test_axis(axis.normalized())) {
  1034. return;
  1035. }
  1036. }
  1037. }
  1038. separator.generate_contacts();
  1039. }
  1040. template <bool withMargin>
  1041. static void _collision_box_convex_polygon(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1042. const BoxShape3DSW *box_A = static_cast<const BoxShape3DSW *>(p_a);
  1043. const ConvexPolygonShape3DSW *convex_polygon_B = static_cast<const ConvexPolygonShape3DSW *>(p_b);
  1044. SeparatorAxisTest<BoxShape3DSW, ConvexPolygonShape3DSW, withMargin> separator(box_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1045. if (!separator.test_previous_axis()) {
  1046. return;
  1047. }
  1048. const Geometry3D::MeshData &mesh = convex_polygon_B->get_mesh();
  1049. const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
  1050. int face_count = mesh.faces.size();
  1051. const Geometry3D::MeshData::Edge *edges = mesh.edges.ptr();
  1052. int edge_count = mesh.edges.size();
  1053. const Vector3 *vertices = mesh.vertices.ptr();
  1054. int vertex_count = mesh.vertices.size();
  1055. // faces of A
  1056. for (int i = 0; i < 3; i++) {
  1057. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  1058. if (!separator.test_axis(axis)) {
  1059. return;
  1060. }
  1061. }
  1062. // faces of B
  1063. for (int i = 0; i < face_count; i++) {
  1064. Vector3 axis = p_transform_b.xform(faces[i].plane).normal;
  1065. if (!separator.test_axis(axis)) {
  1066. return;
  1067. }
  1068. }
  1069. // A<->B edges
  1070. for (int i = 0; i < 3; i++) {
  1071. Vector3 e1 = p_transform_a.basis.get_axis(i);
  1072. for (int j = 0; j < edge_count; j++) {
  1073. Vector3 e2 = p_transform_b.basis.xform(vertices[edges[j].a]) - p_transform_b.basis.xform(vertices[edges[j].b]);
  1074. Vector3 axis = e1.cross(e2).normalized();
  1075. if (!separator.test_axis(axis)) {
  1076. return;
  1077. }
  1078. }
  1079. }
  1080. if (withMargin) {
  1081. // calculate closest points between vertices and box edges
  1082. for (int v = 0; v < vertex_count; v++) {
  1083. Vector3 vtxb = p_transform_b.xform(vertices[v]);
  1084. Vector3 ab_vec = vtxb - p_transform_a.origin;
  1085. Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
  1086. Vector3 support_a = p_transform_a.xform(Vector3(
  1087. (cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  1088. (cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  1089. (cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  1090. Vector3 axis_ab = support_a - vtxb;
  1091. if (!separator.test_axis(axis_ab.normalized())) {
  1092. return;
  1093. }
  1094. //now try edges, which become cylinders!
  1095. for (int i = 0; i < 3; i++) {
  1096. //a ->b
  1097. Vector3 axis_a = p_transform_a.basis.get_axis(i);
  1098. if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized())) {
  1099. return;
  1100. }
  1101. }
  1102. }
  1103. //convex edges and box points
  1104. for (int i = 0; i < 2; i++) {
  1105. for (int j = 0; j < 2; j++) {
  1106. for (int k = 0; k < 2; k++) {
  1107. Vector3 he = box_A->get_half_extents();
  1108. he.x *= (i * 2 - 1);
  1109. he.y *= (j * 2 - 1);
  1110. he.z *= (k * 2 - 1);
  1111. Vector3 point = p_transform_a.origin;
  1112. for (int l = 0; l < 3; l++) {
  1113. point += p_transform_a.basis.get_axis(l) * he[l];
  1114. }
  1115. for (int e = 0; e < edge_count; e++) {
  1116. Vector3 p1 = p_transform_b.xform(vertices[edges[e].a]);
  1117. Vector3 p2 = p_transform_b.xform(vertices[edges[e].b]);
  1118. Vector3 n = (p2 - p1);
  1119. if (!separator.test_axis((point - p2).cross(n).cross(n).normalized())) {
  1120. return;
  1121. }
  1122. }
  1123. }
  1124. }
  1125. }
  1126. }
  1127. separator.generate_contacts();
  1128. }
  1129. template <bool withMargin>
  1130. static void _collision_box_face(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1131. const BoxShape3DSW *box_A = static_cast<const BoxShape3DSW *>(p_a);
  1132. const FaceShape3DSW *face_B = static_cast<const FaceShape3DSW *>(p_b);
  1133. SeparatorAxisTest<BoxShape3DSW, FaceShape3DSW, withMargin> separator(box_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1134. Vector3 vertex[3] = {
  1135. p_transform_b.xform(face_B->vertex[0]),
  1136. p_transform_b.xform(face_B->vertex[1]),
  1137. p_transform_b.xform(face_B->vertex[2]),
  1138. };
  1139. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  1140. return;
  1141. }
  1142. // faces of A
  1143. for (int i = 0; i < 3; i++) {
  1144. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  1145. if (!separator.test_axis(axis)) {
  1146. return;
  1147. }
  1148. }
  1149. // combined edges
  1150. for (int i = 0; i < 3; i++) {
  1151. Vector3 e = vertex[i] - vertex[(i + 1) % 3];
  1152. for (int j = 0; j < 3; j++) {
  1153. Vector3 axis = p_transform_a.basis.get_axis(j);
  1154. if (!separator.test_axis(e.cross(axis).normalized())) {
  1155. return;
  1156. }
  1157. }
  1158. }
  1159. if (withMargin) {
  1160. // calculate closest points between vertices and box edges
  1161. for (int v = 0; v < 3; v++) {
  1162. Vector3 ab_vec = vertex[v] - p_transform_a.origin;
  1163. Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
  1164. Vector3 support_a = p_transform_a.xform(Vector3(
  1165. (cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  1166. (cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  1167. (cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  1168. Vector3 axis_ab = support_a - vertex[v];
  1169. if (!separator.test_axis(axis_ab.normalized())) {
  1170. return;
  1171. }
  1172. //now try edges, which become cylinders!
  1173. for (int i = 0; i < 3; i++) {
  1174. //a ->b
  1175. Vector3 axis_a = p_transform_a.basis.get_axis(i);
  1176. if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized())) {
  1177. return;
  1178. }
  1179. }
  1180. }
  1181. //convex edges and box points, there has to be a way to speed up this (get closest point?)
  1182. for (int i = 0; i < 2; i++) {
  1183. for (int j = 0; j < 2; j++) {
  1184. for (int k = 0; k < 2; k++) {
  1185. Vector3 he = box_A->get_half_extents();
  1186. he.x *= (i * 2 - 1);
  1187. he.y *= (j * 2 - 1);
  1188. he.z *= (k * 2 - 1);
  1189. Vector3 point = p_transform_a.origin;
  1190. for (int l = 0; l < 3; l++) {
  1191. point += p_transform_a.basis.get_axis(l) * he[l];
  1192. }
  1193. for (int e = 0; e < 3; e++) {
  1194. Vector3 p1 = vertex[e];
  1195. Vector3 p2 = vertex[(e + 1) % 3];
  1196. Vector3 n = (p2 - p1);
  1197. if (!separator.test_axis((point - p2).cross(n).cross(n).normalized())) {
  1198. return;
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. separator.generate_contacts();
  1206. }
  1207. template <bool withMargin>
  1208. static void _collision_capsule_capsule(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1209. const CapsuleShape3DSW *capsule_A = static_cast<const CapsuleShape3DSW *>(p_a);
  1210. const CapsuleShape3DSW *capsule_B = static_cast<const CapsuleShape3DSW *>(p_b);
  1211. SeparatorAxisTest<CapsuleShape3DSW, CapsuleShape3DSW, withMargin> separator(capsule_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1212. if (!separator.test_previous_axis()) {
  1213. return;
  1214. }
  1215. // some values
  1216. Vector3 capsule_A_axis = p_transform_a.basis.get_axis(2) * (capsule_A->get_height() * 0.5);
  1217. Vector3 capsule_B_axis = p_transform_b.basis.get_axis(2) * (capsule_B->get_height() * 0.5);
  1218. Vector3 capsule_A_ball_1 = p_transform_a.origin + capsule_A_axis;
  1219. Vector3 capsule_A_ball_2 = p_transform_a.origin - capsule_A_axis;
  1220. Vector3 capsule_B_ball_1 = p_transform_b.origin + capsule_B_axis;
  1221. Vector3 capsule_B_ball_2 = p_transform_b.origin - capsule_B_axis;
  1222. //balls-balls
  1223. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_1).normalized())) {
  1224. return;
  1225. }
  1226. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_2).normalized())) {
  1227. return;
  1228. }
  1229. if (!separator.test_axis((capsule_A_ball_2 - capsule_B_ball_1).normalized())) {
  1230. return;
  1231. }
  1232. if (!separator.test_axis((capsule_A_ball_2 - capsule_B_ball_2).normalized())) {
  1233. return;
  1234. }
  1235. // edges-balls
  1236. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_1).cross(capsule_A_axis).cross(capsule_A_axis).normalized())) {
  1237. return;
  1238. }
  1239. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_2).cross(capsule_A_axis).cross(capsule_A_axis).normalized())) {
  1240. return;
  1241. }
  1242. if (!separator.test_axis((capsule_B_ball_1 - capsule_A_ball_1).cross(capsule_B_axis).cross(capsule_B_axis).normalized())) {
  1243. return;
  1244. }
  1245. if (!separator.test_axis((capsule_B_ball_1 - capsule_A_ball_2).cross(capsule_B_axis).cross(capsule_B_axis).normalized())) {
  1246. return;
  1247. }
  1248. // edges
  1249. if (!separator.test_axis(capsule_A_axis.cross(capsule_B_axis).normalized())) {
  1250. return;
  1251. }
  1252. separator.generate_contacts();
  1253. }
  1254. template <bool withMargin>
  1255. static void _collision_capsule_cylinder(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1256. const CapsuleShape3DSW *capsule_A = static_cast<const CapsuleShape3DSW *>(p_a);
  1257. const CylinderShape3DSW *cylinder_B = static_cast<const CylinderShape3DSW *>(p_b);
  1258. SeparatorAxisTest<CapsuleShape3DSW, CylinderShape3DSW, withMargin> separator(capsule_A, p_transform_a, cylinder_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1259. CollisionSolver3DSW::CallbackResult callback = SeparatorAxisTest<CapsuleShape3DSW, CylinderShape3DSW, withMargin>::test_contact_points;
  1260. // Fallback to generic algorithm to find the best separating axis.
  1261. if (!fallback_collision_solver(p_a, p_transform_a, p_b, p_transform_b, callback, &separator)) {
  1262. return;
  1263. }
  1264. separator.generate_contacts();
  1265. }
  1266. template <bool withMargin>
  1267. static void _collision_capsule_convex_polygon(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1268. const CapsuleShape3DSW *capsule_A = static_cast<const CapsuleShape3DSW *>(p_a);
  1269. const ConvexPolygonShape3DSW *convex_polygon_B = static_cast<const ConvexPolygonShape3DSW *>(p_b);
  1270. SeparatorAxisTest<CapsuleShape3DSW, ConvexPolygonShape3DSW, withMargin> separator(capsule_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1271. if (!separator.test_previous_axis()) {
  1272. return;
  1273. }
  1274. const Geometry3D::MeshData &mesh = convex_polygon_B->get_mesh();
  1275. const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
  1276. int face_count = mesh.faces.size();
  1277. const Geometry3D::MeshData::Edge *edges = mesh.edges.ptr();
  1278. int edge_count = mesh.edges.size();
  1279. const Vector3 *vertices = mesh.vertices.ptr();
  1280. // faces of B
  1281. for (int i = 0; i < face_count; i++) {
  1282. Vector3 axis = p_transform_b.xform(faces[i].plane).normal;
  1283. if (!separator.test_axis(axis)) {
  1284. return;
  1285. }
  1286. }
  1287. // edges of B, capsule cylinder
  1288. for (int i = 0; i < edge_count; i++) {
  1289. // cylinder
  1290. Vector3 edge_axis = p_transform_b.basis.xform(vertices[edges[i].a]) - p_transform_b.basis.xform(vertices[edges[i].b]);
  1291. Vector3 axis = edge_axis.cross(p_transform_a.basis.get_axis(2)).normalized();
  1292. if (!separator.test_axis(axis)) {
  1293. return;
  1294. }
  1295. }
  1296. // capsule balls, edges of B
  1297. for (int i = 0; i < 2; i++) {
  1298. // edges of B, capsule cylinder
  1299. Vector3 capsule_axis = p_transform_a.basis.get_axis(2) * (capsule_A->get_height() * 0.5);
  1300. Vector3 sphere_pos = p_transform_a.origin + ((i == 0) ? capsule_axis : -capsule_axis);
  1301. for (int j = 0; j < edge_count; j++) {
  1302. Vector3 n1 = sphere_pos - p_transform_b.xform(vertices[edges[j].a]);
  1303. Vector3 n2 = p_transform_b.basis.xform(vertices[edges[j].a]) - p_transform_b.basis.xform(vertices[edges[j].b]);
  1304. Vector3 axis = n1.cross(n2).cross(n2).normalized();
  1305. if (!separator.test_axis(axis)) {
  1306. return;
  1307. }
  1308. }
  1309. }
  1310. separator.generate_contacts();
  1311. }
  1312. template <bool withMargin>
  1313. static void _collision_capsule_face(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1314. const CapsuleShape3DSW *capsule_A = static_cast<const CapsuleShape3DSW *>(p_a);
  1315. const FaceShape3DSW *face_B = static_cast<const FaceShape3DSW *>(p_b);
  1316. SeparatorAxisTest<CapsuleShape3DSW, FaceShape3DSW, withMargin> separator(capsule_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1317. Vector3 vertex[3] = {
  1318. p_transform_b.xform(face_B->vertex[0]),
  1319. p_transform_b.xform(face_B->vertex[1]),
  1320. p_transform_b.xform(face_B->vertex[2]),
  1321. };
  1322. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  1323. return;
  1324. }
  1325. // edges of B, capsule cylinder
  1326. Vector3 capsule_axis = p_transform_a.basis.get_axis(2) * (capsule_A->get_height() * 0.5);
  1327. for (int i = 0; i < 3; i++) {
  1328. // edge-cylinder
  1329. Vector3 edge_axis = vertex[i] - vertex[(i + 1) % 3];
  1330. Vector3 axis = edge_axis.cross(capsule_axis).normalized();
  1331. if (!separator.test_axis(axis)) {
  1332. return;
  1333. }
  1334. if (!separator.test_axis((p_transform_a.origin - vertex[i]).cross(capsule_axis).cross(capsule_axis).normalized())) {
  1335. return;
  1336. }
  1337. for (int j = 0; j < 2; j++) {
  1338. // point-spheres
  1339. Vector3 sphere_pos = p_transform_a.origin + ((j == 0) ? capsule_axis : -capsule_axis);
  1340. Vector3 n1 = sphere_pos - vertex[i];
  1341. if (!separator.test_axis(n1.normalized())) {
  1342. return;
  1343. }
  1344. Vector3 n2 = edge_axis;
  1345. axis = n1.cross(n2).cross(n2);
  1346. if (!separator.test_axis(axis.normalized())) {
  1347. return;
  1348. }
  1349. }
  1350. }
  1351. separator.generate_contacts();
  1352. }
  1353. template <bool withMargin>
  1354. static void _collision_cylinder_cylinder(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1355. const CylinderShape3DSW *cylinder_A = static_cast<const CylinderShape3DSW *>(p_a);
  1356. const CylinderShape3DSW *cylinder_B = static_cast<const CylinderShape3DSW *>(p_b);
  1357. SeparatorAxisTest<CylinderShape3DSW, CylinderShape3DSW, withMargin> separator(cylinder_A, p_transform_a, cylinder_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1358. Vector3 cylinder_A_axis = p_transform_a.basis.get_axis(1);
  1359. Vector3 cylinder_B_axis = p_transform_b.basis.get_axis(1);
  1360. if (!separator.test_previous_axis()) {
  1361. return;
  1362. }
  1363. // Cylinder A end caps.
  1364. if (!separator.test_axis(cylinder_A_axis.normalized())) {
  1365. return;
  1366. }
  1367. // Cylinder B end caps.
  1368. if (!separator.test_axis(cylinder_A_axis.normalized())) {
  1369. return;
  1370. }
  1371. Vector3 cylinder_diff = p_transform_b.origin - p_transform_a.origin;
  1372. // Cylinder A lateral surface.
  1373. if (!separator.test_axis(cylinder_A_axis.cross(cylinder_diff).cross(cylinder_A_axis).normalized())) {
  1374. return;
  1375. }
  1376. // Cylinder B lateral surface.
  1377. if (!separator.test_axis(cylinder_B_axis.cross(cylinder_diff).cross(cylinder_B_axis).normalized())) {
  1378. return;
  1379. }
  1380. real_t proj = cylinder_A_axis.cross(cylinder_B_axis).cross(cylinder_B_axis).dot(cylinder_A_axis);
  1381. if (Math::is_zero_approx(proj)) {
  1382. // Parallel cylinders, handle with specific axes only.
  1383. // Note: GJKEPA with no margin can lead to degenerate cases in this situation.
  1384. separator.generate_contacts();
  1385. return;
  1386. }
  1387. CollisionSolver3DSW::CallbackResult callback = SeparatorAxisTest<CylinderShape3DSW, CylinderShape3DSW, withMargin>::test_contact_points;
  1388. // Fallback to generic algorithm to find the best separating axis.
  1389. if (!fallback_collision_solver(p_a, p_transform_a, p_b, p_transform_b, callback, &separator)) {
  1390. return;
  1391. }
  1392. separator.generate_contacts();
  1393. }
  1394. template <bool withMargin>
  1395. static void _collision_cylinder_convex_polygon(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1396. const CylinderShape3DSW *cylinder_A = static_cast<const CylinderShape3DSW *>(p_a);
  1397. const ConvexPolygonShape3DSW *convex_polygon_B = static_cast<const ConvexPolygonShape3DSW *>(p_b);
  1398. SeparatorAxisTest<CylinderShape3DSW, ConvexPolygonShape3DSW, withMargin> separator(cylinder_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1399. CollisionSolver3DSW::CallbackResult callback = SeparatorAxisTest<CylinderShape3DSW, ConvexPolygonShape3DSW, withMargin>::test_contact_points;
  1400. // Fallback to generic algorithm to find the best separating axis.
  1401. if (!fallback_collision_solver(p_a, p_transform_a, p_b, p_transform_b, callback, &separator)) {
  1402. return;
  1403. }
  1404. separator.generate_contacts();
  1405. }
  1406. template <bool withMargin>
  1407. static void _collision_cylinder_face(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1408. const CylinderShape3DSW *cylinder_A = static_cast<const CylinderShape3DSW *>(p_a);
  1409. const FaceShape3DSW *face_B = static_cast<const FaceShape3DSW *>(p_b);
  1410. SeparatorAxisTest<CylinderShape3DSW, FaceShape3DSW, withMargin> separator(cylinder_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1411. if (!separator.test_previous_axis()) {
  1412. return;
  1413. }
  1414. Vector3 vertex[3] = {
  1415. p_transform_b.xform(face_B->vertex[0]),
  1416. p_transform_b.xform(face_B->vertex[1]),
  1417. p_transform_b.xform(face_B->vertex[2]),
  1418. };
  1419. // Face B normal.
  1420. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  1421. return;
  1422. }
  1423. Vector3 cyl_axis = p_transform_a.basis.get_axis(1).normalized();
  1424. // Cylinder end caps.
  1425. {
  1426. if (!separator.test_axis(cyl_axis)) {
  1427. return;
  1428. }
  1429. }
  1430. // Edges of B, cylinder lateral surface.
  1431. for (int i = 0; i < 3; i++) {
  1432. Vector3 edge_axis = vertex[i] - vertex[(i + 1) % 3];
  1433. Vector3 axis = edge_axis.cross(cyl_axis);
  1434. if (Math::is_zero_approx(axis.length_squared())) {
  1435. continue;
  1436. }
  1437. if (!separator.test_axis(axis.normalized())) {
  1438. return;
  1439. }
  1440. }
  1441. // Points of B, cylinder lateral surface.
  1442. for (int i = 0; i < 3; i++) {
  1443. const Vector3 &point = vertex[i];
  1444. Vector3 axis = Plane(cyl_axis, 0).project(point).normalized();
  1445. if (!separator.test_axis(axis)) {
  1446. return;
  1447. }
  1448. }
  1449. // Edges of B, cylinder end caps rim.
  1450. Vector3 cap_axis = cyl_axis * (cylinder_A->get_height() * 0.5);
  1451. for (int i = 0; i < 2; i++) {
  1452. Vector3 cap_pos = p_transform_a.origin + ((i == 0) ? cap_axis : -cap_axis);
  1453. for (int j = 0; j < 3; j++) {
  1454. const Vector3 &edge_start = vertex[j];
  1455. const Vector3 &edge_end = vertex[(j + 1) % 3];
  1456. Vector3 edge_dir = edge_end - edge_start;
  1457. edge_dir.normalize();
  1458. real_t edge_dot = edge_dir.dot(cyl_axis);
  1459. if (Math::is_zero_approx(edge_dot)) {
  1460. // Edge is perpendicular to cylinder axis.
  1461. continue;
  1462. }
  1463. // Calculate intersection between edge and circle plane.
  1464. Vector3 edge_diff = cap_pos - edge_start;
  1465. real_t diff_dot = edge_diff.dot(cyl_axis);
  1466. Vector3 intersection = edge_start + edge_dir * diff_dot / edge_dot;
  1467. // Calculate tangent that touches intersection.
  1468. Vector3 tangent = (cap_pos - intersection).cross(cyl_axis);
  1469. // Axis is orthogonal both to tangent and edge direction.
  1470. Vector3 axis = tangent.cross(edge_dir);
  1471. if (!separator.test_axis(axis.normalized())) {
  1472. return;
  1473. }
  1474. }
  1475. }
  1476. separator.generate_contacts();
  1477. }
  1478. template <bool withMargin>
  1479. static void _collision_convex_polygon_convex_polygon(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1480. const ConvexPolygonShape3DSW *convex_polygon_A = static_cast<const ConvexPolygonShape3DSW *>(p_a);
  1481. const ConvexPolygonShape3DSW *convex_polygon_B = static_cast<const ConvexPolygonShape3DSW *>(p_b);
  1482. SeparatorAxisTest<ConvexPolygonShape3DSW, ConvexPolygonShape3DSW, withMargin> separator(convex_polygon_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1483. if (!separator.test_previous_axis()) {
  1484. return;
  1485. }
  1486. const Geometry3D::MeshData &mesh_A = convex_polygon_A->get_mesh();
  1487. const Geometry3D::MeshData::Face *faces_A = mesh_A.faces.ptr();
  1488. int face_count_A = mesh_A.faces.size();
  1489. const Geometry3D::MeshData::Edge *edges_A = mesh_A.edges.ptr();
  1490. int edge_count_A = mesh_A.edges.size();
  1491. const Vector3 *vertices_A = mesh_A.vertices.ptr();
  1492. int vertex_count_A = mesh_A.vertices.size();
  1493. const Geometry3D::MeshData &mesh_B = convex_polygon_B->get_mesh();
  1494. const Geometry3D::MeshData::Face *faces_B = mesh_B.faces.ptr();
  1495. int face_count_B = mesh_B.faces.size();
  1496. const Geometry3D::MeshData::Edge *edges_B = mesh_B.edges.ptr();
  1497. int edge_count_B = mesh_B.edges.size();
  1498. const Vector3 *vertices_B = mesh_B.vertices.ptr();
  1499. int vertex_count_B = mesh_B.vertices.size();
  1500. // faces of A
  1501. for (int i = 0; i < face_count_A; i++) {
  1502. Vector3 axis = p_transform_a.xform(faces_A[i].plane).normal;
  1503. //Vector3 axis = p_transform_a.basis.xform( faces_A[i].plane.normal ).normalized();
  1504. if (!separator.test_axis(axis)) {
  1505. return;
  1506. }
  1507. }
  1508. // faces of B
  1509. for (int i = 0; i < face_count_B; i++) {
  1510. Vector3 axis = p_transform_b.xform(faces_B[i].plane).normal;
  1511. //Vector3 axis = p_transform_b.basis.xform( faces_B[i].plane.normal ).normalized();
  1512. if (!separator.test_axis(axis)) {
  1513. return;
  1514. }
  1515. }
  1516. // A<->B edges
  1517. for (int i = 0; i < edge_count_A; i++) {
  1518. Vector3 e1 = p_transform_a.basis.xform(vertices_A[edges_A[i].a]) - p_transform_a.basis.xform(vertices_A[edges_A[i].b]);
  1519. for (int j = 0; j < edge_count_B; j++) {
  1520. Vector3 e2 = p_transform_b.basis.xform(vertices_B[edges_B[j].a]) - p_transform_b.basis.xform(vertices_B[edges_B[j].b]);
  1521. Vector3 axis = e1.cross(e2).normalized();
  1522. if (!separator.test_axis(axis)) {
  1523. return;
  1524. }
  1525. }
  1526. }
  1527. if (withMargin) {
  1528. //vertex-vertex
  1529. for (int i = 0; i < vertex_count_A; i++) {
  1530. Vector3 va = p_transform_a.xform(vertices_A[i]);
  1531. for (int j = 0; j < vertex_count_B; j++) {
  1532. if (!separator.test_axis((va - p_transform_b.xform(vertices_B[j])).normalized())) {
  1533. return;
  1534. }
  1535. }
  1536. }
  1537. //edge-vertex (shell)
  1538. for (int i = 0; i < edge_count_A; i++) {
  1539. Vector3 e1 = p_transform_a.basis.xform(vertices_A[edges_A[i].a]);
  1540. Vector3 e2 = p_transform_a.basis.xform(vertices_A[edges_A[i].b]);
  1541. Vector3 n = (e2 - e1);
  1542. for (int j = 0; j < vertex_count_B; j++) {
  1543. Vector3 e3 = p_transform_b.xform(vertices_B[j]);
  1544. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
  1545. return;
  1546. }
  1547. }
  1548. }
  1549. for (int i = 0; i < edge_count_B; i++) {
  1550. Vector3 e1 = p_transform_b.basis.xform(vertices_B[edges_B[i].a]);
  1551. Vector3 e2 = p_transform_b.basis.xform(vertices_B[edges_B[i].b]);
  1552. Vector3 n = (e2 - e1);
  1553. for (int j = 0; j < vertex_count_A; j++) {
  1554. Vector3 e3 = p_transform_a.xform(vertices_A[j]);
  1555. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
  1556. return;
  1557. }
  1558. }
  1559. }
  1560. }
  1561. separator.generate_contacts();
  1562. }
  1563. template <bool withMargin>
  1564. static void _collision_convex_polygon_face(const Shape3DSW *p_a, const Transform &p_transform_a, const Shape3DSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  1565. const ConvexPolygonShape3DSW *convex_polygon_A = static_cast<const ConvexPolygonShape3DSW *>(p_a);
  1566. const FaceShape3DSW *face_B = static_cast<const FaceShape3DSW *>(p_b);
  1567. SeparatorAxisTest<ConvexPolygonShape3DSW, FaceShape3DSW, withMargin> separator(convex_polygon_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  1568. const Geometry3D::MeshData &mesh = convex_polygon_A->get_mesh();
  1569. const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
  1570. int face_count = mesh.faces.size();
  1571. const Geometry3D::MeshData::Edge *edges = mesh.edges.ptr();
  1572. int edge_count = mesh.edges.size();
  1573. const Vector3 *vertices = mesh.vertices.ptr();
  1574. int vertex_count = mesh.vertices.size();
  1575. Vector3 vertex[3] = {
  1576. p_transform_b.xform(face_B->vertex[0]),
  1577. p_transform_b.xform(face_B->vertex[1]),
  1578. p_transform_b.xform(face_B->vertex[2]),
  1579. };
  1580. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized())) {
  1581. return;
  1582. }
  1583. // faces of A
  1584. for (int i = 0; i < face_count; i++) {
  1585. //Vector3 axis = p_transform_a.xform( faces[i].plane ).normal;
  1586. Vector3 axis = p_transform_a.basis.xform(faces[i].plane.normal).normalized();
  1587. if (!separator.test_axis(axis)) {
  1588. return;
  1589. }
  1590. }
  1591. // A<->B edges
  1592. for (int i = 0; i < edge_count; i++) {
  1593. Vector3 e1 = p_transform_a.xform(vertices[edges[i].a]) - p_transform_a.xform(vertices[edges[i].b]);
  1594. for (int j = 0; j < 3; j++) {
  1595. Vector3 e2 = vertex[j] - vertex[(j + 1) % 3];
  1596. Vector3 axis = e1.cross(e2).normalized();
  1597. if (!separator.test_axis(axis)) {
  1598. return;
  1599. }
  1600. }
  1601. }
  1602. if (withMargin) {
  1603. //vertex-vertex
  1604. for (int i = 0; i < vertex_count; i++) {
  1605. Vector3 va = p_transform_a.xform(vertices[i]);
  1606. for (int j = 0; j < 3; j++) {
  1607. if (!separator.test_axis((va - vertex[j]).normalized())) {
  1608. return;
  1609. }
  1610. }
  1611. }
  1612. //edge-vertex (shell)
  1613. for (int i = 0; i < edge_count; i++) {
  1614. Vector3 e1 = p_transform_a.basis.xform(vertices[edges[i].a]);
  1615. Vector3 e2 = p_transform_a.basis.xform(vertices[edges[i].b]);
  1616. Vector3 n = (e2 - e1);
  1617. for (int j = 0; j < 3; j++) {
  1618. Vector3 e3 = vertex[j];
  1619. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
  1620. return;
  1621. }
  1622. }
  1623. }
  1624. for (int i = 0; i < 3; i++) {
  1625. Vector3 e1 = vertex[i];
  1626. Vector3 e2 = vertex[(i + 1) % 3];
  1627. Vector3 n = (e2 - e1);
  1628. for (int j = 0; j < vertex_count; j++) {
  1629. Vector3 e3 = p_transform_a.xform(vertices[j]);
  1630. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized())) {
  1631. return;
  1632. }
  1633. }
  1634. }
  1635. }
  1636. separator.generate_contacts();
  1637. }
  1638. bool sat_calculate_penetration(const Shape3DSW *p_shape_A, const Transform &p_transform_A, const Shape3DSW *p_shape_B, const Transform &p_transform_B, CollisionSolver3DSW::CallbackResult p_result_callback, void *p_userdata, bool p_swap, Vector3 *r_prev_axis, real_t p_margin_a, real_t p_margin_b) {
  1639. PhysicsServer3D::ShapeType type_A = p_shape_A->get_type();
  1640. ERR_FAIL_COND_V(type_A == PhysicsServer3D::SHAPE_PLANE, false);
  1641. ERR_FAIL_COND_V(type_A == PhysicsServer3D::SHAPE_RAY, false);
  1642. ERR_FAIL_COND_V(p_shape_A->is_concave(), false);
  1643. PhysicsServer3D::ShapeType type_B = p_shape_B->get_type();
  1644. ERR_FAIL_COND_V(type_B == PhysicsServer3D::SHAPE_PLANE, false);
  1645. ERR_FAIL_COND_V(type_B == PhysicsServer3D::SHAPE_RAY, false);
  1646. ERR_FAIL_COND_V(p_shape_B->is_concave(), false);
  1647. static const CollisionFunc collision_table[6][6] = {
  1648. { _collision_sphere_sphere<false>,
  1649. _collision_sphere_box<false>,
  1650. _collision_sphere_capsule<false>,
  1651. _collision_sphere_cylinder<false>,
  1652. _collision_sphere_convex_polygon<false>,
  1653. _collision_sphere_face<false> },
  1654. { nullptr,
  1655. _collision_box_box<false>,
  1656. _collision_box_capsule<false>,
  1657. _collision_box_cylinder<false>,
  1658. _collision_box_convex_polygon<false>,
  1659. _collision_box_face<false> },
  1660. { nullptr,
  1661. nullptr,
  1662. _collision_capsule_capsule<false>,
  1663. _collision_capsule_cylinder<false>,
  1664. _collision_capsule_convex_polygon<false>,
  1665. _collision_capsule_face<false> },
  1666. { nullptr,
  1667. nullptr,
  1668. nullptr,
  1669. _collision_cylinder_cylinder<false>,
  1670. _collision_cylinder_convex_polygon<false>,
  1671. _collision_cylinder_face<false> },
  1672. { nullptr,
  1673. nullptr,
  1674. nullptr,
  1675. nullptr,
  1676. _collision_convex_polygon_convex_polygon<false>,
  1677. _collision_convex_polygon_face<false> },
  1678. { nullptr,
  1679. nullptr,
  1680. nullptr,
  1681. nullptr,
  1682. nullptr,
  1683. nullptr },
  1684. };
  1685. static const CollisionFunc collision_table_margin[6][6] = {
  1686. { _collision_sphere_sphere<true>,
  1687. _collision_sphere_box<true>,
  1688. _collision_sphere_capsule<true>,
  1689. _collision_sphere_cylinder<true>,
  1690. _collision_sphere_convex_polygon<true>,
  1691. _collision_sphere_face<true> },
  1692. { nullptr,
  1693. _collision_box_box<true>,
  1694. _collision_box_capsule<true>,
  1695. _collision_box_cylinder<true>,
  1696. _collision_box_convex_polygon<true>,
  1697. _collision_box_face<true> },
  1698. { nullptr,
  1699. nullptr,
  1700. _collision_capsule_capsule<true>,
  1701. _collision_capsule_cylinder<true>,
  1702. _collision_capsule_convex_polygon<true>,
  1703. _collision_capsule_face<true> },
  1704. { nullptr,
  1705. nullptr,
  1706. nullptr,
  1707. _collision_cylinder_cylinder<true>,
  1708. _collision_cylinder_convex_polygon<true>,
  1709. _collision_cylinder_face<true> },
  1710. { nullptr,
  1711. nullptr,
  1712. nullptr,
  1713. nullptr,
  1714. _collision_convex_polygon_convex_polygon<true>,
  1715. _collision_convex_polygon_face<true> },
  1716. { nullptr,
  1717. nullptr,
  1718. nullptr,
  1719. nullptr,
  1720. nullptr,
  1721. nullptr },
  1722. };
  1723. _CollectorCallback callback;
  1724. callback.callback = p_result_callback;
  1725. callback.swap = p_swap;
  1726. callback.userdata = p_userdata;
  1727. callback.collided = false;
  1728. callback.prev_axis = r_prev_axis;
  1729. const Shape3DSW *A = p_shape_A;
  1730. const Shape3DSW *B = p_shape_B;
  1731. const Transform *transform_A = &p_transform_A;
  1732. const Transform *transform_B = &p_transform_B;
  1733. real_t margin_A = p_margin_a;
  1734. real_t margin_B = p_margin_b;
  1735. if (type_A > type_B) {
  1736. SWAP(A, B);
  1737. SWAP(transform_A, transform_B);
  1738. SWAP(type_A, type_B);
  1739. SWAP(margin_A, margin_B);
  1740. callback.swap = !callback.swap;
  1741. }
  1742. CollisionFunc collision_func;
  1743. if (margin_A != 0.0 || margin_B != 0.0) {
  1744. collision_func = collision_table_margin[type_A - 2][type_B - 2];
  1745. } else {
  1746. collision_func = collision_table[type_A - 2][type_B - 2];
  1747. }
  1748. ERR_FAIL_COND_V(!collision_func, false);
  1749. collision_func(A, *transform_A, B, *transform_B, &callback, margin_A, margin_B);
  1750. return callback.collided;
  1751. }