collision_solver_sat.cpp 47 KB

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