godot_collision_solver_3d_sat.cpp 78 KB

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