collision_solver_sat.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. /*************************************************************************/
  2. /* collision_solver_sat.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "collision_solver_sat.h"
  31. #include "geometry.h"
  32. #define _EDGE_IS_VALID_SUPPORT_THRESHOLD 0.02
  33. struct _CollectorCallback {
  34. CollisionSolverSW::CallbackResult callback;
  35. void *userdata;
  36. bool swap;
  37. bool collided;
  38. Vector3 normal;
  39. Vector3 *prev_axis;
  40. _FORCE_INLINE_ void call(const Vector3 &p_point_A, const Vector3 &p_point_B) {
  41. /*
  42. if (normal.dot(p_point_A) >= normal.dot(p_point_B))
  43. return;
  44. print_line("** A: "+p_point_A+" B: "+p_point_B+" D: "+rtos(p_point_A.distance_to(p_point_B)));
  45. */
  46. if (swap)
  47. callback(p_point_B, p_point_A, userdata);
  48. else
  49. callback(p_point_A, p_point_B, userdata);
  50. }
  51. };
  52. typedef void (*GenerateContactsFunc)(const Vector3 *, int, const Vector3 *, int, _CollectorCallback *);
  53. 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) {
  54. #ifdef DEBUG_ENABLED
  55. ERR_FAIL_COND(p_point_count_A != 1);
  56. ERR_FAIL_COND(p_point_count_B != 1);
  57. #endif
  58. p_callback->call(*p_points_A, *p_points_B);
  59. }
  60. 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) {
  61. #ifdef DEBUG_ENABLED
  62. ERR_FAIL_COND(p_point_count_A != 1);
  63. ERR_FAIL_COND(p_point_count_B != 2);
  64. #endif
  65. Vector3 closest_B = Geometry::get_closest_point_to_segment_uncapped(*p_points_A, p_points_B);
  66. p_callback->call(*p_points_A, closest_B);
  67. }
  68. 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) {
  69. #ifdef DEBUG_ENABLED
  70. ERR_FAIL_COND(p_point_count_A != 1);
  71. ERR_FAIL_COND(p_point_count_B < 3);
  72. #endif
  73. Vector3 closest_B = Plane(p_points_B[0], p_points_B[1], p_points_B[2]).project(*p_points_A);
  74. p_callback->call(*p_points_A, closest_B);
  75. }
  76. 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) {
  77. #ifdef DEBUG_ENABLED
  78. ERR_FAIL_COND(p_point_count_A != 2);
  79. ERR_FAIL_COND(p_point_count_B != 2); // circle is actually a 4x3 matrix
  80. #endif
  81. Vector3 rel_A = p_points_A[1] - p_points_A[0];
  82. Vector3 rel_B = p_points_B[1] - p_points_B[0];
  83. Vector3 c = rel_A.cross(rel_B).cross(rel_B);
  84. //if ( Math::abs(rel_A.dot(c) )<_EDGE_IS_VALID_SUPPORT_TRESHOLD ) {
  85. if (Math::abs(rel_A.dot(c)) < CMP_EPSILON) {
  86. // should handle somehow..
  87. //ERR_PRINT("TODO FIX");
  88. //return;
  89. Vector3 axis = rel_A.normalized(); //make an axis
  90. Vector3 base_A = p_points_A[0] - axis * axis.dot(p_points_A[0]);
  91. Vector3 base_B = p_points_B[0] - axis * axis.dot(p_points_B[0]);
  92. //sort all 4 points in axis
  93. 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]) };
  94. SortArray<real_t> sa;
  95. sa.sort(dvec, 4);
  96. //use the middle ones as contacts
  97. p_callback->call(base_A + axis * dvec[1], base_B + axis * dvec[1]);
  98. p_callback->call(base_A + axis * dvec[2], base_B + axis * dvec[2]);
  99. return;
  100. }
  101. real_t d = (c.dot(p_points_B[0]) - p_points_A[0].dot(c)) / rel_A.dot(c);
  102. if (d < 0.0)
  103. d = 0.0;
  104. else if (d > 1.0)
  105. d = 1.0;
  106. Vector3 closest_A = p_points_A[0] + rel_A * d;
  107. Vector3 closest_B = Geometry::get_closest_point_to_segment_uncapped(closest_A, p_points_B);
  108. p_callback->call(closest_A, closest_B);
  109. }
  110. 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) {
  111. #ifdef DEBUG_ENABLED
  112. ERR_FAIL_COND(p_point_count_A < 2);
  113. ERR_FAIL_COND(p_point_count_B < 3);
  114. #endif
  115. static const int max_clip = 32;
  116. Vector3 _clipbuf1[max_clip];
  117. Vector3 _clipbuf2[max_clip];
  118. Vector3 *clipbuf_src = _clipbuf1;
  119. Vector3 *clipbuf_dst = _clipbuf2;
  120. int clipbuf_len = p_point_count_A;
  121. // copy A points to clipbuf_src
  122. for (int i = 0; i < p_point_count_A; i++) {
  123. clipbuf_src[i] = p_points_A[i];
  124. }
  125. Plane plane_B(p_points_B[0], p_points_B[1], p_points_B[2]);
  126. // go through all of B points
  127. for (int i = 0; i < p_point_count_B; i++) {
  128. int i_n = (i + 1) % p_point_count_B;
  129. Vector3 edge0_B = p_points_B[i];
  130. Vector3 edge1_B = p_points_B[i_n];
  131. Vector3 clip_normal = (edge0_B - edge1_B).cross(plane_B.normal).normalized();
  132. // make a clip plane
  133. Plane clip(edge0_B, clip_normal);
  134. // avoid double clip if A is edge
  135. int dst_idx = 0;
  136. bool edge = clipbuf_len == 2;
  137. for (int j = 0; j < clipbuf_len; j++) {
  138. int j_n = (j + 1) % clipbuf_len;
  139. Vector3 edge0_A = clipbuf_src[j];
  140. Vector3 edge1_A = clipbuf_src[j_n];
  141. real_t dist0 = clip.distance_to(edge0_A);
  142. real_t dist1 = clip.distance_to(edge1_A);
  143. if (dist0 <= 0) { // behind plane
  144. ERR_FAIL_COND(dst_idx >= max_clip);
  145. clipbuf_dst[dst_idx++] = clipbuf_src[j];
  146. }
  147. // check for different sides and non coplanar
  148. //if ( (dist0*dist1) < -CMP_EPSILON && !(edge && j)) {
  149. if ((dist0 * dist1) < 0 && !(edge && j)) {
  150. // calculate intersection
  151. Vector3 rel = edge1_A - edge0_A;
  152. real_t den = clip.normal.dot(rel);
  153. real_t dist = -(clip.normal.dot(edge0_A) - clip.d) / den;
  154. Vector3 inters = edge0_A + rel * dist;
  155. ERR_FAIL_COND(dst_idx >= max_clip);
  156. clipbuf_dst[dst_idx] = inters;
  157. dst_idx++;
  158. }
  159. }
  160. clipbuf_len = dst_idx;
  161. SWAP(clipbuf_src, clipbuf_dst);
  162. }
  163. // generate contacts
  164. //Plane plane_A(p_points_A[0],p_points_A[1],p_points_A[2]);
  165. int added = 0;
  166. for (int i = 0; i < clipbuf_len; i++) {
  167. real_t d = plane_B.distance_to(clipbuf_src[i]);
  168. /*
  169. if (d>CMP_EPSILON)
  170. continue;
  171. */
  172. Vector3 closest_B = clipbuf_src[i] - plane_B.normal * d;
  173. if (p_callback->normal.dot(clipbuf_src[i]) >= p_callback->normal.dot(closest_B))
  174. continue;
  175. p_callback->call(clipbuf_src[i], closest_B);
  176. added++;
  177. }
  178. }
  179. static void _generate_contacts_from_supports(const Vector3 *p_points_A, int p_point_count_A, const Vector3 *p_points_B, int p_point_count_B, _CollectorCallback *p_callback) {
  180. #ifdef DEBUG_ENABLED
  181. ERR_FAIL_COND(p_point_count_A < 1);
  182. ERR_FAIL_COND(p_point_count_B < 1);
  183. #endif
  184. static const GenerateContactsFunc generate_contacts_func_table[3][3] = {
  185. {
  186. _generate_contacts_point_point,
  187. _generate_contacts_point_edge,
  188. _generate_contacts_point_face,
  189. },
  190. {
  191. 0,
  192. _generate_contacts_edge_edge,
  193. _generate_contacts_face_face,
  194. },
  195. {
  196. 0,
  197. 0,
  198. _generate_contacts_face_face,
  199. }
  200. };
  201. int pointcount_B;
  202. int pointcount_A;
  203. const Vector3 *points_A;
  204. const Vector3 *points_B;
  205. if (p_point_count_A > p_point_count_B) {
  206. //swap
  207. p_callback->swap = !p_callback->swap;
  208. p_callback->normal = -p_callback->normal;
  209. pointcount_B = p_point_count_A;
  210. pointcount_A = p_point_count_B;
  211. points_A = p_points_B;
  212. points_B = p_points_A;
  213. } else {
  214. pointcount_B = p_point_count_B;
  215. pointcount_A = p_point_count_A;
  216. points_A = p_points_A;
  217. points_B = p_points_B;
  218. }
  219. int version_A = (pointcount_A > 3 ? 3 : pointcount_A) - 1;
  220. int version_B = (pointcount_B > 3 ? 3 : pointcount_B) - 1;
  221. GenerateContactsFunc contacts_func = generate_contacts_func_table[version_A][version_B];
  222. ERR_FAIL_COND(!contacts_func);
  223. contacts_func(points_A, pointcount_A, points_B, pointcount_B, p_callback);
  224. }
  225. template <class ShapeA, class ShapeB, bool withMargin = false>
  226. class SeparatorAxisTest {
  227. const ShapeA *shape_A;
  228. const ShapeB *shape_B;
  229. const Transform *transform_A;
  230. const Transform *transform_B;
  231. real_t best_depth;
  232. Vector3 best_axis;
  233. _CollectorCallback *callback;
  234. real_t margin_A;
  235. real_t margin_B;
  236. Vector3 separator_axis;
  237. public:
  238. _FORCE_INLINE_ bool test_previous_axis() {
  239. if (callback && callback->prev_axis && *callback->prev_axis != Vector3())
  240. return test_axis(*callback->prev_axis);
  241. else
  242. return true;
  243. }
  244. _FORCE_INLINE_ bool test_axis(const Vector3 &p_axis) {
  245. Vector3 axis = p_axis;
  246. if (Math::abs(axis.x) < CMP_EPSILON &&
  247. Math::abs(axis.y) < CMP_EPSILON &&
  248. Math::abs(axis.z) < CMP_EPSILON) {
  249. // strange case, try an upwards separator
  250. axis = Vector3(0.0, 1.0, 0.0);
  251. }
  252. real_t min_A, max_A, min_B, max_B;
  253. shape_A->project_range(axis, *transform_A, min_A, max_A);
  254. shape_B->project_range(axis, *transform_B, min_B, max_B);
  255. if (withMargin) {
  256. min_A -= margin_A;
  257. max_A += margin_A;
  258. min_B -= margin_B;
  259. max_B += margin_B;
  260. }
  261. min_B -= (max_A - min_A) * 0.5;
  262. max_B += (max_A - min_A) * 0.5;
  263. min_B -= (min_A + max_A) * 0.5;
  264. max_B -= (min_A + max_A) * 0.5;
  265. if (min_B > 0.0 || max_B < 0.0) {
  266. separator_axis = axis;
  267. return false; // doesn't contain 0
  268. }
  269. //use the smallest depth
  270. if (min_B < 0.0) {
  271. min_B = -min_B;
  272. }
  273. if (max_B < min_B) {
  274. if (max_B < best_depth) {
  275. best_depth = max_B;
  276. best_axis = axis;
  277. }
  278. } else {
  279. if (min_B < best_depth) {
  280. best_depth = min_B;
  281. best_axis = -axis; // keep it as A axis
  282. }
  283. }
  284. return true;
  285. }
  286. _FORCE_INLINE_ void generate_contacts() {
  287. // nothing to do, don't generate
  288. if (best_axis == Vector3(0.0, 0.0, 0.0))
  289. return;
  290. if (!callback->callback) {
  291. //just was checking intersection?
  292. callback->collided = true;
  293. if (callback->prev_axis)
  294. *callback->prev_axis = best_axis;
  295. return;
  296. }
  297. static const int max_supports = 16;
  298. Vector3 supports_A[max_supports];
  299. int support_count_A;
  300. shape_A->get_supports(transform_A->basis.xform_inv(-best_axis).normalized(), max_supports, supports_A, support_count_A);
  301. for (int i = 0; i < support_count_A; i++) {
  302. supports_A[i] = transform_A->xform(supports_A[i]);
  303. }
  304. if (withMargin) {
  305. for (int i = 0; i < support_count_A; i++) {
  306. supports_A[i] += -best_axis * margin_A;
  307. }
  308. }
  309. Vector3 supports_B[max_supports];
  310. int support_count_B;
  311. shape_B->get_supports(transform_B->basis.xform_inv(best_axis).normalized(), max_supports, supports_B, support_count_B);
  312. for (int i = 0; i < support_count_B; i++) {
  313. supports_B[i] = transform_B->xform(supports_B[i]);
  314. }
  315. if (withMargin) {
  316. for (int i = 0; i < support_count_B; i++) {
  317. supports_B[i] += best_axis * margin_B;
  318. }
  319. }
  320. /*
  321. print_line("best depth: "+rtos(best_depth));
  322. print_line("best axis: "+(best_axis));
  323. for(int i=0;i<support_count_A;i++) {
  324. print_line("A-"+itos(i)+": "+supports_A[i]);
  325. }
  326. for(int i=0;i<support_count_B;i++) {
  327. print_line("B-"+itos(i)+": "+supports_B[i]);
  328. }
  329. */
  330. callback->normal = best_axis;
  331. if (callback->prev_axis)
  332. *callback->prev_axis = best_axis;
  333. _generate_contacts_from_supports(supports_A, support_count_A, supports_B, support_count_B, callback);
  334. callback->collided = true;
  335. //CollisionSolverSW::CallbackResult cbk=NULL;
  336. //cbk(Vector3(),Vector3(),NULL);
  337. }
  338. _FORCE_INLINE_ SeparatorAxisTest(const ShapeA *p_shape_A, const Transform &p_transform_A, const ShapeB *p_shape_B, const Transform &p_transform_B, _CollectorCallback *p_callback, real_t p_margin_A = 0, real_t p_margin_B = 0) {
  339. best_depth = 1e15;
  340. shape_A = p_shape_A;
  341. shape_B = p_shape_B;
  342. transform_A = &p_transform_A;
  343. transform_B = &p_transform_B;
  344. callback = p_callback;
  345. margin_A = p_margin_A;
  346. margin_B = p_margin_B;
  347. }
  348. };
  349. /****** SAT TESTS *******/
  350. /****** SAT TESTS *******/
  351. /****** SAT TESTS *******/
  352. /****** SAT TESTS *******/
  353. typedef void (*CollisionFunc)(const ShapeSW *, const Transform &, const ShapeSW *, const Transform &, _CollectorCallback *p_callback, real_t, real_t);
  354. template <bool withMargin>
  355. static void _collision_sphere_sphere(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  356. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  357. const SphereShapeSW *sphere_B = static_cast<const SphereShapeSW *>(p_b);
  358. SeparatorAxisTest<SphereShapeSW, SphereShapeSW, withMargin> separator(sphere_A, p_transform_a, sphere_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  359. // previous axis
  360. if (!separator.test_previous_axis())
  361. return;
  362. if (!separator.test_axis((p_transform_a.origin - p_transform_b.origin).normalized()))
  363. return;
  364. separator.generate_contacts();
  365. }
  366. template <bool withMargin>
  367. static void _collision_sphere_box(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  368. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  369. const BoxShapeSW *box_B = static_cast<const BoxShapeSW *>(p_b);
  370. SeparatorAxisTest<SphereShapeSW, BoxShapeSW, withMargin> separator(sphere_A, p_transform_a, box_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  371. if (!separator.test_previous_axis())
  372. return;
  373. // test faces
  374. for (int i = 0; i < 3; i++) {
  375. Vector3 axis = p_transform_b.basis.get_axis(i).normalized();
  376. if (!separator.test_axis(axis))
  377. return;
  378. }
  379. // calculate closest point to sphere
  380. Vector3 cnormal = p_transform_b.xform_inv(p_transform_a.origin);
  381. Vector3 cpoint = p_transform_b.xform(Vector3(
  382. (cnormal.x < 0) ? -box_B->get_half_extents().x : box_B->get_half_extents().x,
  383. (cnormal.y < 0) ? -box_B->get_half_extents().y : box_B->get_half_extents().y,
  384. (cnormal.z < 0) ? -box_B->get_half_extents().z : box_B->get_half_extents().z));
  385. // use point to test axis
  386. Vector3 point_axis = (p_transform_a.origin - cpoint).normalized();
  387. if (!separator.test_axis(point_axis))
  388. return;
  389. // test edges
  390. for (int i = 0; i < 3; i++) {
  391. Vector3 axis = point_axis.cross(p_transform_b.basis.get_axis(i)).cross(p_transform_b.basis.get_axis(i)).normalized();
  392. if (!separator.test_axis(axis))
  393. return;
  394. }
  395. separator.generate_contacts();
  396. }
  397. template <bool withMargin>
  398. static void _collision_sphere_capsule(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  399. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  400. const CapsuleShapeSW *capsule_B = static_cast<const CapsuleShapeSW *>(p_b);
  401. SeparatorAxisTest<SphereShapeSW, CapsuleShapeSW, withMargin> separator(sphere_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  402. if (!separator.test_previous_axis())
  403. return;
  404. //capsule sphere 1, sphere
  405. Vector3 capsule_axis = p_transform_b.basis.get_axis(2) * (capsule_B->get_height() * 0.5);
  406. Vector3 capsule_ball_1 = p_transform_b.origin + capsule_axis;
  407. if (!separator.test_axis((capsule_ball_1 - p_transform_a.origin).normalized()))
  408. return;
  409. //capsule sphere 2, sphere
  410. Vector3 capsule_ball_2 = p_transform_b.origin - capsule_axis;
  411. if (!separator.test_axis((capsule_ball_2 - p_transform_a.origin).normalized()))
  412. return;
  413. //capsule edge, sphere
  414. Vector3 b2a = p_transform_a.origin - p_transform_b.origin;
  415. Vector3 axis = b2a.cross(capsule_axis).cross(capsule_axis).normalized();
  416. if (!separator.test_axis(axis))
  417. return;
  418. separator.generate_contacts();
  419. }
  420. template <bool withMargin>
  421. static void _collision_sphere_convex_polygon(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  422. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  423. const ConvexPolygonShapeSW *convex_polygon_B = static_cast<const ConvexPolygonShapeSW *>(p_b);
  424. SeparatorAxisTest<SphereShapeSW, ConvexPolygonShapeSW, withMargin> separator(sphere_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  425. if (!separator.test_previous_axis())
  426. return;
  427. const Geometry::MeshData &mesh = convex_polygon_B->get_mesh();
  428. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  429. int face_count = mesh.faces.size();
  430. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  431. int edge_count = mesh.edges.size();
  432. const Vector3 *vertices = mesh.vertices.ptr();
  433. int vertex_count = mesh.vertices.size();
  434. // faces of B
  435. for (int i = 0; i < face_count; i++) {
  436. Vector3 axis = p_transform_b.xform(faces[i].plane).normal;
  437. if (!separator.test_axis(axis))
  438. return;
  439. }
  440. // edges of B
  441. for (int i = 0; i < edge_count; i++) {
  442. Vector3 v1 = p_transform_b.xform(vertices[edges[i].a]);
  443. Vector3 v2 = p_transform_b.xform(vertices[edges[i].b]);
  444. Vector3 v3 = p_transform_a.origin;
  445. Vector3 n1 = v2 - v1;
  446. Vector3 n2 = v2 - v3;
  447. Vector3 axis = n1.cross(n2).cross(n1).normalized();
  448. if (!separator.test_axis(axis))
  449. return;
  450. }
  451. // vertices of B
  452. for (int i = 0; i < vertex_count; i++) {
  453. Vector3 v1 = p_transform_b.xform(vertices[i]);
  454. Vector3 v2 = p_transform_a.origin;
  455. Vector3 axis = (v2 - v1).normalized();
  456. if (!separator.test_axis(axis))
  457. return;
  458. }
  459. separator.generate_contacts();
  460. }
  461. template <bool withMargin>
  462. static void _collision_sphere_face(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  463. const SphereShapeSW *sphere_A = static_cast<const SphereShapeSW *>(p_a);
  464. const FaceShapeSW *face_B = static_cast<const FaceShapeSW *>(p_b);
  465. SeparatorAxisTest<SphereShapeSW, FaceShapeSW, withMargin> separator(sphere_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  466. Vector3 vertex[3] = {
  467. p_transform_b.xform(face_B->vertex[0]),
  468. p_transform_b.xform(face_B->vertex[1]),
  469. p_transform_b.xform(face_B->vertex[2]),
  470. };
  471. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized()))
  472. return;
  473. // edges and points of B
  474. for (int i = 0; i < 3; i++) {
  475. Vector3 n1 = vertex[i] - p_transform_a.origin;
  476. if (!separator.test_axis(n1.normalized())) {
  477. return;
  478. }
  479. Vector3 n2 = vertex[(i + 1) % 3] - vertex[i];
  480. Vector3 axis = n1.cross(n2).cross(n2).normalized();
  481. if (!separator.test_axis(axis)) {
  482. return;
  483. }
  484. }
  485. separator.generate_contacts();
  486. }
  487. template <bool withMargin>
  488. static void _collision_box_box(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  489. const BoxShapeSW *box_A = static_cast<const BoxShapeSW *>(p_a);
  490. const BoxShapeSW *box_B = static_cast<const BoxShapeSW *>(p_b);
  491. SeparatorAxisTest<BoxShapeSW, BoxShapeSW, withMargin> separator(box_A, p_transform_a, box_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  492. if (!separator.test_previous_axis())
  493. return;
  494. // test faces of A
  495. for (int i = 0; i < 3; i++) {
  496. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  497. if (!separator.test_axis(axis))
  498. return;
  499. }
  500. // test faces of B
  501. for (int i = 0; i < 3; i++) {
  502. Vector3 axis = p_transform_b.basis.get_axis(i).normalized();
  503. if (!separator.test_axis(axis))
  504. return;
  505. }
  506. // test combined edges
  507. for (int i = 0; i < 3; i++) {
  508. for (int j = 0; j < 3; j++) {
  509. Vector3 axis = p_transform_a.basis.get_axis(i).cross(p_transform_b.basis.get_axis(j));
  510. if (axis.length_squared() < CMP_EPSILON)
  511. continue;
  512. axis.normalize();
  513. if (!separator.test_axis(axis)) {
  514. return;
  515. }
  516. }
  517. }
  518. if (withMargin) {
  519. //add endpoint test between closest vertices and edges
  520. // calculate closest point to sphere
  521. Vector3 ab_vec = p_transform_b.origin - p_transform_a.origin;
  522. Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
  523. Vector3 support_a = p_transform_a.xform(Vector3(
  524. (cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  525. (cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  526. (cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  527. Vector3 cnormal_b = p_transform_b.basis.xform_inv(-ab_vec);
  528. Vector3 support_b = p_transform_b.xform(Vector3(
  529. (cnormal_b.x < 0) ? -box_B->get_half_extents().x : box_B->get_half_extents().x,
  530. (cnormal_b.y < 0) ? -box_B->get_half_extents().y : box_B->get_half_extents().y,
  531. (cnormal_b.z < 0) ? -box_B->get_half_extents().z : box_B->get_half_extents().z));
  532. Vector3 axis_ab = (support_a - support_b);
  533. if (!separator.test_axis(axis_ab.normalized())) {
  534. return;
  535. }
  536. //now try edges, which become cylinders!
  537. for (int i = 0; i < 3; i++) {
  538. //a ->b
  539. Vector3 axis_a = p_transform_a.basis.get_axis(i);
  540. if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized()))
  541. return;
  542. //b ->a
  543. Vector3 axis_b = p_transform_b.basis.get_axis(i);
  544. if (!separator.test_axis(axis_ab.cross(axis_b).cross(axis_b).normalized()))
  545. return;
  546. }
  547. }
  548. separator.generate_contacts();
  549. }
  550. template <bool withMargin>
  551. static void _collision_box_capsule(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  552. const BoxShapeSW *box_A = static_cast<const BoxShapeSW *>(p_a);
  553. const CapsuleShapeSW *capsule_B = static_cast<const CapsuleShapeSW *>(p_b);
  554. SeparatorAxisTest<BoxShapeSW, CapsuleShapeSW, withMargin> separator(box_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  555. if (!separator.test_previous_axis())
  556. return;
  557. // faces of A
  558. for (int i = 0; i < 3; i++) {
  559. Vector3 axis = p_transform_a.basis.get_axis(i);
  560. if (!separator.test_axis(axis))
  561. return;
  562. }
  563. Vector3 cyl_axis = p_transform_b.basis.get_axis(2).normalized();
  564. // edges of A, capsule cylinder
  565. for (int i = 0; i < 3; i++) {
  566. // cylinder
  567. Vector3 box_axis = p_transform_a.basis.get_axis(i);
  568. Vector3 axis = box_axis.cross(cyl_axis);
  569. if (axis.length_squared() < CMP_EPSILON)
  570. continue;
  571. if (!separator.test_axis(axis.normalized()))
  572. return;
  573. }
  574. // points of A, capsule cylinder
  575. // this sure could be made faster somehow..
  576. for (int i = 0; i < 2; i++) {
  577. for (int j = 0; j < 2; j++) {
  578. for (int k = 0; k < 2; k++) {
  579. Vector3 he = box_A->get_half_extents();
  580. he.x *= (i * 2 - 1);
  581. he.y *= (j * 2 - 1);
  582. he.z *= (k * 2 - 1);
  583. Vector3 point = p_transform_a.origin;
  584. for (int l = 0; l < 3; l++)
  585. point += p_transform_a.basis.get_axis(l) * he[l];
  586. //Vector3 axis = (point - cyl_axis * cyl_axis.dot(point)).normalized();
  587. Vector3 axis = Plane(cyl_axis, 0).project(point).normalized();
  588. if (!separator.test_axis(axis))
  589. return;
  590. }
  591. }
  592. }
  593. // capsule balls, edges of A
  594. for (int i = 0; i < 2; i++) {
  595. Vector3 capsule_axis = p_transform_b.basis.get_axis(2) * (capsule_B->get_height() * 0.5);
  596. Vector3 sphere_pos = p_transform_b.origin + ((i == 0) ? capsule_axis : -capsule_axis);
  597. Vector3 cnormal = p_transform_a.xform_inv(sphere_pos);
  598. Vector3 cpoint = p_transform_a.xform(Vector3(
  599. (cnormal.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  600. (cnormal.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  601. (cnormal.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  602. // use point to test axis
  603. Vector3 point_axis = (sphere_pos - cpoint).normalized();
  604. if (!separator.test_axis(point_axis))
  605. return;
  606. // test edges of A
  607. for (int i = 0; i < 3; i++) {
  608. Vector3 axis = point_axis.cross(p_transform_a.basis.get_axis(i)).cross(p_transform_a.basis.get_axis(i)).normalized();
  609. if (!separator.test_axis(axis))
  610. return;
  611. }
  612. }
  613. separator.generate_contacts();
  614. }
  615. template <bool withMargin>
  616. static void _collision_box_convex_polygon(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  617. const BoxShapeSW *box_A = static_cast<const BoxShapeSW *>(p_a);
  618. const ConvexPolygonShapeSW *convex_polygon_B = static_cast<const ConvexPolygonShapeSW *>(p_b);
  619. SeparatorAxisTest<BoxShapeSW, ConvexPolygonShapeSW, withMargin> separator(box_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  620. if (!separator.test_previous_axis())
  621. return;
  622. const Geometry::MeshData &mesh = convex_polygon_B->get_mesh();
  623. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  624. int face_count = mesh.faces.size();
  625. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  626. int edge_count = mesh.edges.size();
  627. const Vector3 *vertices = mesh.vertices.ptr();
  628. int vertex_count = mesh.vertices.size();
  629. // faces of A
  630. for (int i = 0; i < 3; i++) {
  631. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  632. if (!separator.test_axis(axis))
  633. return;
  634. }
  635. // faces of B
  636. for (int i = 0; i < face_count; i++) {
  637. Vector3 axis = p_transform_b.xform(faces[i].plane).normal;
  638. if (!separator.test_axis(axis))
  639. return;
  640. }
  641. // A<->B edges
  642. for (int i = 0; i < 3; i++) {
  643. Vector3 e1 = p_transform_a.basis.get_axis(i);
  644. for (int j = 0; j < edge_count; j++) {
  645. Vector3 e2 = p_transform_b.basis.xform(vertices[edges[j].a]) - p_transform_b.basis.xform(vertices[edges[j].b]);
  646. Vector3 axis = e1.cross(e2).normalized();
  647. if (!separator.test_axis(axis))
  648. return;
  649. }
  650. }
  651. if (withMargin) {
  652. // calculate closest points between vertices and box edges
  653. for (int v = 0; v < vertex_count; v++) {
  654. Vector3 vtxb = p_transform_b.xform(vertices[v]);
  655. Vector3 ab_vec = vtxb - p_transform_a.origin;
  656. Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
  657. Vector3 support_a = p_transform_a.xform(Vector3(
  658. (cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  659. (cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  660. (cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  661. Vector3 axis_ab = support_a - vtxb;
  662. if (!separator.test_axis(axis_ab.normalized())) {
  663. return;
  664. }
  665. //now try edges, which become cylinders!
  666. for (int i = 0; i < 3; i++) {
  667. //a ->b
  668. Vector3 axis_a = p_transform_a.basis.get_axis(i);
  669. if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized()))
  670. return;
  671. }
  672. }
  673. //convex edges and box points
  674. for (int i = 0; i < 2; i++) {
  675. for (int j = 0; j < 2; j++) {
  676. for (int k = 0; k < 2; k++) {
  677. Vector3 he = box_A->get_half_extents();
  678. he.x *= (i * 2 - 1);
  679. he.y *= (j * 2 - 1);
  680. he.z *= (k * 2 - 1);
  681. Vector3 point = p_transform_a.origin;
  682. for (int l = 0; l < 3; l++)
  683. point += p_transform_a.basis.get_axis(l) * he[l];
  684. for (int e = 0; e < edge_count; e++) {
  685. Vector3 p1 = p_transform_b.xform(vertices[edges[e].a]);
  686. Vector3 p2 = p_transform_b.xform(vertices[edges[e].b]);
  687. Vector3 n = (p2 - p1);
  688. if (!separator.test_axis((point - p2).cross(n).cross(n).normalized()))
  689. return;
  690. }
  691. }
  692. }
  693. }
  694. }
  695. separator.generate_contacts();
  696. }
  697. template <bool withMargin>
  698. static void _collision_box_face(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  699. const BoxShapeSW *box_A = static_cast<const BoxShapeSW *>(p_a);
  700. const FaceShapeSW *face_B = static_cast<const FaceShapeSW *>(p_b);
  701. SeparatorAxisTest<BoxShapeSW, FaceShapeSW, withMargin> separator(box_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  702. Vector3 vertex[3] = {
  703. p_transform_b.xform(face_B->vertex[0]),
  704. p_transform_b.xform(face_B->vertex[1]),
  705. p_transform_b.xform(face_B->vertex[2]),
  706. };
  707. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized()))
  708. return;
  709. // faces of A
  710. for (int i = 0; i < 3; i++) {
  711. Vector3 axis = p_transform_a.basis.get_axis(i).normalized();
  712. if (!separator.test_axis(axis))
  713. return;
  714. }
  715. // combined edges
  716. for (int i = 0; i < 3; i++) {
  717. Vector3 e = vertex[i] - vertex[(i + 1) % 3];
  718. for (int j = 0; j < 3; j++) {
  719. Vector3 axis = p_transform_a.basis.get_axis(j);
  720. if (!separator.test_axis(e.cross(axis).normalized()))
  721. return;
  722. }
  723. }
  724. if (withMargin) {
  725. // calculate closest points between vertices and box edges
  726. for (int v = 0; v < 3; v++) {
  727. Vector3 ab_vec = vertex[v] - p_transform_a.origin;
  728. Vector3 cnormal_a = p_transform_a.basis.xform_inv(ab_vec);
  729. Vector3 support_a = p_transform_a.xform(Vector3(
  730. (cnormal_a.x < 0) ? -box_A->get_half_extents().x : box_A->get_half_extents().x,
  731. (cnormal_a.y < 0) ? -box_A->get_half_extents().y : box_A->get_half_extents().y,
  732. (cnormal_a.z < 0) ? -box_A->get_half_extents().z : box_A->get_half_extents().z));
  733. Vector3 axis_ab = support_a - vertex[v];
  734. if (!separator.test_axis(axis_ab.normalized())) {
  735. return;
  736. }
  737. //now try edges, which become cylinders!
  738. for (int i = 0; i < 3; i++) {
  739. //a ->b
  740. Vector3 axis_a = p_transform_a.basis.get_axis(i);
  741. if (!separator.test_axis(axis_ab.cross(axis_a).cross(axis_a).normalized()))
  742. return;
  743. }
  744. }
  745. //convex edges and box points, there has to be a way to speed up this (get closest point?)
  746. for (int i = 0; i < 2; i++) {
  747. for (int j = 0; j < 2; j++) {
  748. for (int k = 0; k < 2; k++) {
  749. Vector3 he = box_A->get_half_extents();
  750. he.x *= (i * 2 - 1);
  751. he.y *= (j * 2 - 1);
  752. he.z *= (k * 2 - 1);
  753. Vector3 point = p_transform_a.origin;
  754. for (int l = 0; l < 3; l++)
  755. point += p_transform_a.basis.get_axis(l) * he[l];
  756. for (int e = 0; e < 3; e++) {
  757. Vector3 p1 = vertex[e];
  758. Vector3 p2 = vertex[(e + 1) % 3];
  759. Vector3 n = (p2 - p1);
  760. if (!separator.test_axis((point - p2).cross(n).cross(n).normalized()))
  761. return;
  762. }
  763. }
  764. }
  765. }
  766. }
  767. separator.generate_contacts();
  768. }
  769. template <bool withMargin>
  770. static void _collision_capsule_capsule(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  771. const CapsuleShapeSW *capsule_A = static_cast<const CapsuleShapeSW *>(p_a);
  772. const CapsuleShapeSW *capsule_B = static_cast<const CapsuleShapeSW *>(p_b);
  773. SeparatorAxisTest<CapsuleShapeSW, CapsuleShapeSW, withMargin> separator(capsule_A, p_transform_a, capsule_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  774. if (!separator.test_previous_axis())
  775. return;
  776. // some values
  777. Vector3 capsule_A_axis = p_transform_a.basis.get_axis(2) * (capsule_A->get_height() * 0.5);
  778. Vector3 capsule_B_axis = p_transform_b.basis.get_axis(2) * (capsule_B->get_height() * 0.5);
  779. Vector3 capsule_A_ball_1 = p_transform_a.origin + capsule_A_axis;
  780. Vector3 capsule_A_ball_2 = p_transform_a.origin - capsule_A_axis;
  781. Vector3 capsule_B_ball_1 = p_transform_b.origin + capsule_B_axis;
  782. Vector3 capsule_B_ball_2 = p_transform_b.origin - capsule_B_axis;
  783. //balls-balls
  784. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_1).normalized()))
  785. return;
  786. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_2).normalized()))
  787. return;
  788. if (!separator.test_axis((capsule_A_ball_2 - capsule_B_ball_1).normalized()))
  789. return;
  790. if (!separator.test_axis((capsule_A_ball_2 - capsule_B_ball_2).normalized()))
  791. return;
  792. // edges-balls
  793. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_1).cross(capsule_A_axis).cross(capsule_A_axis).normalized()))
  794. return;
  795. if (!separator.test_axis((capsule_A_ball_1 - capsule_B_ball_2).cross(capsule_A_axis).cross(capsule_A_axis).normalized()))
  796. return;
  797. if (!separator.test_axis((capsule_B_ball_1 - capsule_A_ball_1).cross(capsule_B_axis).cross(capsule_B_axis).normalized()))
  798. return;
  799. if (!separator.test_axis((capsule_B_ball_1 - capsule_A_ball_2).cross(capsule_B_axis).cross(capsule_B_axis).normalized()))
  800. return;
  801. // edges
  802. if (!separator.test_axis(capsule_A_axis.cross(capsule_B_axis).normalized()))
  803. return;
  804. separator.generate_contacts();
  805. }
  806. template <bool withMargin>
  807. static void _collision_capsule_convex_polygon(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  808. const CapsuleShapeSW *capsule_A = static_cast<const CapsuleShapeSW *>(p_a);
  809. const ConvexPolygonShapeSW *convex_polygon_B = static_cast<const ConvexPolygonShapeSW *>(p_b);
  810. SeparatorAxisTest<CapsuleShapeSW, ConvexPolygonShapeSW, withMargin> separator(capsule_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  811. if (!separator.test_previous_axis())
  812. return;
  813. const Geometry::MeshData &mesh = convex_polygon_B->get_mesh();
  814. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  815. int face_count = mesh.faces.size();
  816. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  817. int edge_count = mesh.edges.size();
  818. const Vector3 *vertices = mesh.vertices.ptr();
  819. // faces of B
  820. for (int i = 0; i < face_count; i++) {
  821. Vector3 axis = p_transform_b.xform(faces[i].plane).normal;
  822. if (!separator.test_axis(axis))
  823. return;
  824. }
  825. // edges of B, capsule cylinder
  826. for (int i = 0; i < edge_count; i++) {
  827. // cylinder
  828. Vector3 edge_axis = p_transform_b.basis.xform(vertices[edges[i].a]) - p_transform_b.basis.xform(vertices[edges[i].b]);
  829. Vector3 axis = edge_axis.cross(p_transform_a.basis.get_axis(2)).normalized();
  830. if (!separator.test_axis(axis))
  831. return;
  832. }
  833. // capsule balls, edges of B
  834. for (int i = 0; i < 2; i++) {
  835. // edges of B, capsule cylinder
  836. Vector3 capsule_axis = p_transform_a.basis.get_axis(2) * (capsule_A->get_height() * 0.5);
  837. Vector3 sphere_pos = p_transform_a.origin + ((i == 0) ? capsule_axis : -capsule_axis);
  838. for (int j = 0; j < edge_count; j++) {
  839. Vector3 n1 = sphere_pos - p_transform_b.xform(vertices[edges[j].a]);
  840. Vector3 n2 = p_transform_b.basis.xform(vertices[edges[j].a]) - p_transform_b.basis.xform(vertices[edges[j].b]);
  841. Vector3 axis = n1.cross(n2).cross(n2).normalized();
  842. if (!separator.test_axis(axis))
  843. return;
  844. }
  845. }
  846. separator.generate_contacts();
  847. }
  848. template <bool withMargin>
  849. static void _collision_capsule_face(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  850. const CapsuleShapeSW *capsule_A = static_cast<const CapsuleShapeSW *>(p_a);
  851. const FaceShapeSW *face_B = static_cast<const FaceShapeSW *>(p_b);
  852. SeparatorAxisTest<CapsuleShapeSW, FaceShapeSW, withMargin> separator(capsule_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  853. Vector3 vertex[3] = {
  854. p_transform_b.xform(face_B->vertex[0]),
  855. p_transform_b.xform(face_B->vertex[1]),
  856. p_transform_b.xform(face_B->vertex[2]),
  857. };
  858. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized()))
  859. return;
  860. // edges of B, capsule cylinder
  861. Vector3 capsule_axis = p_transform_a.basis.get_axis(2) * (capsule_A->get_height() * 0.5);
  862. for (int i = 0; i < 3; i++) {
  863. // edge-cylinder
  864. Vector3 edge_axis = vertex[i] - vertex[(i + 1) % 3];
  865. Vector3 axis = edge_axis.cross(capsule_axis).normalized();
  866. if (!separator.test_axis(axis))
  867. return;
  868. if (!separator.test_axis((p_transform_a.origin - vertex[i]).cross(capsule_axis).cross(capsule_axis).normalized()))
  869. return;
  870. for (int j = 0; j < 2; j++) {
  871. // point-spheres
  872. Vector3 sphere_pos = p_transform_a.origin + ((j == 0) ? capsule_axis : -capsule_axis);
  873. Vector3 n1 = sphere_pos - vertex[i];
  874. if (!separator.test_axis(n1.normalized()))
  875. return;
  876. Vector3 n2 = edge_axis;
  877. axis = n1.cross(n2).cross(n2);
  878. if (!separator.test_axis(axis.normalized()))
  879. return;
  880. }
  881. }
  882. separator.generate_contacts();
  883. }
  884. template <bool withMargin>
  885. static void _collision_convex_polygon_convex_polygon(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  886. const ConvexPolygonShapeSW *convex_polygon_A = static_cast<const ConvexPolygonShapeSW *>(p_a);
  887. const ConvexPolygonShapeSW *convex_polygon_B = static_cast<const ConvexPolygonShapeSW *>(p_b);
  888. SeparatorAxisTest<ConvexPolygonShapeSW, ConvexPolygonShapeSW, withMargin> separator(convex_polygon_A, p_transform_a, convex_polygon_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  889. if (!separator.test_previous_axis())
  890. return;
  891. const Geometry::MeshData &mesh_A = convex_polygon_A->get_mesh();
  892. const Geometry::MeshData::Face *faces_A = mesh_A.faces.ptr();
  893. int face_count_A = mesh_A.faces.size();
  894. const Geometry::MeshData::Edge *edges_A = mesh_A.edges.ptr();
  895. int edge_count_A = mesh_A.edges.size();
  896. const Vector3 *vertices_A = mesh_A.vertices.ptr();
  897. int vertex_count_A = mesh_A.vertices.size();
  898. const Geometry::MeshData &mesh_B = convex_polygon_B->get_mesh();
  899. const Geometry::MeshData::Face *faces_B = mesh_B.faces.ptr();
  900. int face_count_B = mesh_B.faces.size();
  901. const Geometry::MeshData::Edge *edges_B = mesh_B.edges.ptr();
  902. int edge_count_B = mesh_B.edges.size();
  903. const Vector3 *vertices_B = mesh_B.vertices.ptr();
  904. int vertex_count_B = mesh_B.vertices.size();
  905. // faces of A
  906. for (int i = 0; i < face_count_A; i++) {
  907. Vector3 axis = p_transform_a.xform(faces_A[i].plane).normal;
  908. //Vector3 axis = p_transform_a.basis.xform( faces_A[i].plane.normal ).normalized();
  909. if (!separator.test_axis(axis))
  910. return;
  911. }
  912. // faces of B
  913. for (int i = 0; i < face_count_B; i++) {
  914. Vector3 axis = p_transform_b.xform(faces_B[i].plane).normal;
  915. //Vector3 axis = p_transform_b.basis.xform( faces_B[i].plane.normal ).normalized();
  916. if (!separator.test_axis(axis))
  917. return;
  918. }
  919. // A<->B edges
  920. for (int i = 0; i < edge_count_A; i++) {
  921. Vector3 e1 = p_transform_a.basis.xform(vertices_A[edges_A[i].a]) - p_transform_a.basis.xform(vertices_A[edges_A[i].b]);
  922. for (int j = 0; j < edge_count_B; j++) {
  923. Vector3 e2 = p_transform_b.basis.xform(vertices_B[edges_B[j].a]) - p_transform_b.basis.xform(vertices_B[edges_B[j].b]);
  924. Vector3 axis = e1.cross(e2).normalized();
  925. if (!separator.test_axis(axis))
  926. return;
  927. }
  928. }
  929. if (withMargin) {
  930. //vertex-vertex
  931. for (int i = 0; i < vertex_count_A; i++) {
  932. Vector3 va = p_transform_a.xform(vertices_A[i]);
  933. for (int j = 0; j < vertex_count_B; j++) {
  934. if (!separator.test_axis((va - p_transform_b.xform(vertices_B[j])).normalized()))
  935. return;
  936. }
  937. }
  938. //edge-vertex( hsell)
  939. for (int i = 0; i < edge_count_A; i++) {
  940. Vector3 e1 = p_transform_a.basis.xform(vertices_A[edges_A[i].a]);
  941. Vector3 e2 = p_transform_a.basis.xform(vertices_A[edges_A[i].b]);
  942. Vector3 n = (e2 - e1);
  943. for (int j = 0; j < vertex_count_B; j++) {
  944. Vector3 e3 = p_transform_b.xform(vertices_B[j]);
  945. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized()))
  946. return;
  947. }
  948. }
  949. for (int i = 0; i < edge_count_B; i++) {
  950. Vector3 e1 = p_transform_b.basis.xform(vertices_B[edges_B[i].a]);
  951. Vector3 e2 = p_transform_b.basis.xform(vertices_B[edges_B[i].b]);
  952. Vector3 n = (e2 - e1);
  953. for (int j = 0; j < vertex_count_A; j++) {
  954. Vector3 e3 = p_transform_a.xform(vertices_A[j]);
  955. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized()))
  956. return;
  957. }
  958. }
  959. }
  960. separator.generate_contacts();
  961. }
  962. template <bool withMargin>
  963. static void _collision_convex_polygon_face(const ShapeSW *p_a, const Transform &p_transform_a, const ShapeSW *p_b, const Transform &p_transform_b, _CollectorCallback *p_collector, real_t p_margin_a, real_t p_margin_b) {
  964. const ConvexPolygonShapeSW *convex_polygon_A = static_cast<const ConvexPolygonShapeSW *>(p_a);
  965. const FaceShapeSW *face_B = static_cast<const FaceShapeSW *>(p_b);
  966. SeparatorAxisTest<ConvexPolygonShapeSW, FaceShapeSW, withMargin> separator(convex_polygon_A, p_transform_a, face_B, p_transform_b, p_collector, p_margin_a, p_margin_b);
  967. const Geometry::MeshData &mesh = convex_polygon_A->get_mesh();
  968. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  969. int face_count = mesh.faces.size();
  970. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  971. int edge_count = mesh.edges.size();
  972. const Vector3 *vertices = mesh.vertices.ptr();
  973. int vertex_count = mesh.vertices.size();
  974. Vector3 vertex[3] = {
  975. p_transform_b.xform(face_B->vertex[0]),
  976. p_transform_b.xform(face_B->vertex[1]),
  977. p_transform_b.xform(face_B->vertex[2]),
  978. };
  979. if (!separator.test_axis((vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]).normalized()))
  980. return;
  981. // faces of A
  982. for (int i = 0; i < face_count; i++) {
  983. //Vector3 axis = p_transform_a.xform( faces[i].plane ).normal;
  984. Vector3 axis = p_transform_a.basis.xform(faces[i].plane.normal).normalized();
  985. if (!separator.test_axis(axis))
  986. return;
  987. }
  988. // A<->B edges
  989. for (int i = 0; i < edge_count; i++) {
  990. Vector3 e1 = p_transform_a.xform(vertices[edges[i].a]) - p_transform_a.xform(vertices[edges[i].b]);
  991. for (int j = 0; j < 3; j++) {
  992. Vector3 e2 = vertex[j] - vertex[(j + 1) % 3];
  993. Vector3 axis = e1.cross(e2).normalized();
  994. if (!separator.test_axis(axis))
  995. return;
  996. }
  997. }
  998. if (withMargin) {
  999. //vertex-vertex
  1000. for (int i = 0; i < vertex_count; i++) {
  1001. Vector3 va = p_transform_a.xform(vertices[i]);
  1002. for (int j = 0; j < 3; j++) {
  1003. if (!separator.test_axis((va - vertex[j]).normalized()))
  1004. return;
  1005. }
  1006. }
  1007. //edge-vertex( hsell)
  1008. for (int i = 0; i < edge_count; i++) {
  1009. Vector3 e1 = p_transform_a.basis.xform(vertices[edges[i].a]);
  1010. Vector3 e2 = p_transform_a.basis.xform(vertices[edges[i].b]);
  1011. Vector3 n = (e2 - e1);
  1012. for (int j = 0; j < 3; j++) {
  1013. Vector3 e3 = vertex[j];
  1014. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized()))
  1015. return;
  1016. }
  1017. }
  1018. for (int i = 0; i < 3; i++) {
  1019. Vector3 e1 = vertex[i];
  1020. Vector3 e2 = vertex[(i + 1) % 3];
  1021. Vector3 n = (e2 - e1);
  1022. for (int j = 0; j < vertex_count; j++) {
  1023. Vector3 e3 = p_transform_a.xform(vertices[j]);
  1024. if (!separator.test_axis((e1 - e3).cross(n).cross(n).normalized()))
  1025. return;
  1026. }
  1027. }
  1028. }
  1029. separator.generate_contacts();
  1030. }
  1031. bool sat_calculate_penetration(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CollisionSolverSW::CallbackResult p_result_callback, void *p_userdata, bool p_swap, Vector3 *r_prev_axis, real_t p_margin_a, real_t p_margin_b) {
  1032. PhysicsServer::ShapeType type_A = p_shape_A->get_type();
  1033. ERR_FAIL_COND_V(type_A == PhysicsServer::SHAPE_PLANE, false);
  1034. ERR_FAIL_COND_V(type_A == PhysicsServer::SHAPE_RAY, false);
  1035. ERR_FAIL_COND_V(p_shape_A->is_concave(), false);
  1036. PhysicsServer::ShapeType type_B = p_shape_B->get_type();
  1037. ERR_FAIL_COND_V(type_B == PhysicsServer::SHAPE_PLANE, false);
  1038. ERR_FAIL_COND_V(type_B == PhysicsServer::SHAPE_RAY, false);
  1039. ERR_FAIL_COND_V(p_shape_B->is_concave(), false);
  1040. static const CollisionFunc collision_table[5][5] = {
  1041. { _collision_sphere_sphere<false>,
  1042. _collision_sphere_box<false>,
  1043. _collision_sphere_capsule<false>,
  1044. _collision_sphere_convex_polygon<false>,
  1045. _collision_sphere_face<false> },
  1046. { 0,
  1047. _collision_box_box<false>,
  1048. _collision_box_capsule<false>,
  1049. _collision_box_convex_polygon<false>,
  1050. _collision_box_face<false> },
  1051. { 0,
  1052. 0,
  1053. _collision_capsule_capsule<false>,
  1054. _collision_capsule_convex_polygon<false>,
  1055. _collision_capsule_face<false> },
  1056. { 0,
  1057. 0,
  1058. 0,
  1059. _collision_convex_polygon_convex_polygon<false>,
  1060. _collision_convex_polygon_face<false> },
  1061. { 0,
  1062. 0,
  1063. 0,
  1064. 0,
  1065. 0 },
  1066. };
  1067. static const CollisionFunc collision_table_margin[5][5] = {
  1068. { _collision_sphere_sphere<true>,
  1069. _collision_sphere_box<true>,
  1070. _collision_sphere_capsule<true>,
  1071. _collision_sphere_convex_polygon<true>,
  1072. _collision_sphere_face<true> },
  1073. { 0,
  1074. _collision_box_box<true>,
  1075. _collision_box_capsule<true>,
  1076. _collision_box_convex_polygon<true>,
  1077. _collision_box_face<true> },
  1078. { 0,
  1079. 0,
  1080. _collision_capsule_capsule<true>,
  1081. _collision_capsule_convex_polygon<true>,
  1082. _collision_capsule_face<true> },
  1083. { 0,
  1084. 0,
  1085. 0,
  1086. _collision_convex_polygon_convex_polygon<true>,
  1087. _collision_convex_polygon_face<true> },
  1088. { 0,
  1089. 0,
  1090. 0,
  1091. 0,
  1092. 0 },
  1093. };
  1094. _CollectorCallback callback;
  1095. callback.callback = p_result_callback;
  1096. callback.swap = p_swap;
  1097. callback.userdata = p_userdata;
  1098. callback.collided = false;
  1099. callback.prev_axis = r_prev_axis;
  1100. const ShapeSW *A = p_shape_A;
  1101. const ShapeSW *B = p_shape_B;
  1102. const Transform *transform_A = &p_transform_A;
  1103. const Transform *transform_B = &p_transform_B;
  1104. real_t margin_A = p_margin_a;
  1105. real_t margin_B = p_margin_b;
  1106. if (type_A > type_B) {
  1107. SWAP(A, B);
  1108. SWAP(transform_A, transform_B);
  1109. SWAP(type_A, type_B);
  1110. SWAP(margin_A, margin_B);
  1111. callback.swap = !callback.swap;
  1112. }
  1113. CollisionFunc collision_func;
  1114. if (margin_A != 0.0 || margin_B != 0.0) {
  1115. collision_func = collision_table_margin[type_A - 2][type_B - 2];
  1116. } else {
  1117. collision_func = collision_table[type_A - 2][type_B - 2];
  1118. }
  1119. ERR_FAIL_COND_V(!collision_func, false);
  1120. collision_func(A, *transform_A, B, *transform_B, &callback, margin_A, margin_B);
  1121. return callback.collided;
  1122. }