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