godot_collision_solver_3d_sat.cpp 78 KB

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