collision_solver_sat.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  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. }