godot_collision_solver_3d_sat.cpp 76 KB

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