godot_collision_solver_3d.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*************************************************************************/
  2. /* godot_collision_solver_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "godot_collision_solver_3d.h"
  31. #include "godot_collision_solver_3d_sat.h"
  32. #include "godot_soft_body_3d.h"
  33. #include "gjk_epa.h"
  34. #define collision_solver sat_calculate_penetration
  35. //#define collision_solver gjk_epa_calculate_penetration
  36. bool GodotCollisionSolver3D::solve_static_world_boundary(const GodotShape3D *p_shape_A, const Transform3D &p_transform_A, const GodotShape3D *p_shape_B, const Transform3D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result) {
  37. const GodotWorldBoundaryShape3D *world_boundary = static_cast<const GodotWorldBoundaryShape3D *>(p_shape_A);
  38. if (p_shape_B->get_type() == PhysicsServer3D::SHAPE_WORLD_BOUNDARY) {
  39. return false;
  40. }
  41. Plane p = p_transform_A.xform(world_boundary->get_plane());
  42. static const int max_supports = 16;
  43. Vector3 supports[max_supports];
  44. int support_count;
  45. GodotShape3D::FeatureType support_type;
  46. p_shape_B->get_supports(p_transform_B.basis.xform_inv(-p.normal).normalized(), max_supports, supports, support_count, support_type);
  47. if (support_type == GodotShape3D::FEATURE_CIRCLE) {
  48. ERR_FAIL_COND_V(support_count != 3, false);
  49. Vector3 circle_pos = supports[0];
  50. Vector3 circle_axis_1 = supports[1] - circle_pos;
  51. Vector3 circle_axis_2 = supports[2] - circle_pos;
  52. // Use 3 equidistant points on the circle.
  53. for (int i = 0; i < 3; ++i) {
  54. Vector3 vertex_pos = circle_pos;
  55. vertex_pos += circle_axis_1 * Math::cos(2.0 * Math_PI * i / 3.0);
  56. vertex_pos += circle_axis_2 * Math::sin(2.0 * Math_PI * i / 3.0);
  57. supports[i] = vertex_pos;
  58. }
  59. }
  60. bool found = false;
  61. for (int i = 0; i < support_count; i++) {
  62. supports[i] = p_transform_B.xform(supports[i]);
  63. if (p.distance_to(supports[i]) >= 0) {
  64. continue;
  65. }
  66. found = true;
  67. Vector3 support_A = p.project(supports[i]);
  68. if (p_result_callback) {
  69. if (p_swap_result) {
  70. p_result_callback(supports[i], 0, support_A, 0, p_userdata);
  71. } else {
  72. p_result_callback(support_A, 0, supports[i], 0, p_userdata);
  73. }
  74. }
  75. }
  76. return found;
  77. }
  78. bool GodotCollisionSolver3D::solve_separation_ray(const GodotShape3D *p_shape_A, const Transform3D &p_transform_A, const GodotShape3D *p_shape_B, const Transform3D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, real_t p_margin) {
  79. const GodotSeparationRayShape3D *ray = static_cast<const GodotSeparationRayShape3D *>(p_shape_A);
  80. Vector3 from = p_transform_A.origin;
  81. Vector3 to = from + p_transform_A.basis.get_axis(2) * (ray->get_length() + p_margin);
  82. Vector3 support_A = to;
  83. Transform3D ai = p_transform_B.affine_inverse();
  84. from = ai.xform(from);
  85. to = ai.xform(to);
  86. Vector3 p, n;
  87. if (!p_shape_B->intersect_segment(from, to, p, n)) {
  88. return false;
  89. }
  90. // Discard contacts when the ray is fully contained inside the shape.
  91. if (n == Vector3()) {
  92. return false;
  93. }
  94. // Discard contacts in the wrong direction.
  95. if (n.dot(from - to) < CMP_EPSILON) {
  96. return false;
  97. }
  98. Vector3 support_B = p_transform_B.xform(p);
  99. if (ray->get_slide_on_slope()) {
  100. Vector3 global_n = ai.basis.xform_inv(n).normalized();
  101. support_B = support_A + (support_B - support_A).length() * global_n;
  102. }
  103. if (p_result_callback) {
  104. if (p_swap_result) {
  105. p_result_callback(support_B, 0, support_A, 0, p_userdata);
  106. } else {
  107. p_result_callback(support_A, 0, support_B, 0, p_userdata);
  108. }
  109. }
  110. return true;
  111. }
  112. struct _SoftBodyContactCollisionInfo {
  113. int node_index = 0;
  114. GodotCollisionSolver3D::CallbackResult result_callback = nullptr;
  115. void *userdata = nullptr;
  116. bool swap_result = false;
  117. int contact_count = 0;
  118. };
  119. void GodotCollisionSolver3D::soft_body_contact_callback(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, void *p_userdata) {
  120. _SoftBodyContactCollisionInfo &cinfo = *(_SoftBodyContactCollisionInfo *)(p_userdata);
  121. ++cinfo.contact_count;
  122. if (!cinfo.result_callback) {
  123. return;
  124. }
  125. if (cinfo.swap_result) {
  126. cinfo.result_callback(p_point_B, cinfo.node_index, p_point_A, p_index_A, cinfo.userdata);
  127. } else {
  128. cinfo.result_callback(p_point_A, p_index_A, p_point_B, cinfo.node_index, cinfo.userdata);
  129. }
  130. }
  131. struct _SoftBodyQueryInfo {
  132. GodotSoftBody3D *soft_body = nullptr;
  133. const GodotShape3D *shape_A = nullptr;
  134. const GodotShape3D *shape_B = nullptr;
  135. Transform3D transform_A;
  136. Transform3D node_transform;
  137. _SoftBodyContactCollisionInfo contact_info;
  138. #ifdef DEBUG_ENABLED
  139. int node_query_count = 0;
  140. int convex_query_count = 0;
  141. #endif
  142. };
  143. bool GodotCollisionSolver3D::soft_body_query_callback(uint32_t p_node_index, void *p_userdata) {
  144. _SoftBodyQueryInfo &query_cinfo = *(_SoftBodyQueryInfo *)(p_userdata);
  145. Vector3 node_position = query_cinfo.soft_body->get_node_position(p_node_index);
  146. Transform3D transform_B;
  147. transform_B.origin = query_cinfo.node_transform.xform(node_position);
  148. query_cinfo.contact_info.node_index = p_node_index;
  149. bool collided = solve_static(query_cinfo.shape_A, query_cinfo.transform_A, query_cinfo.shape_B, transform_B, soft_body_contact_callback, &query_cinfo.contact_info);
  150. #ifdef DEBUG_ENABLED
  151. ++query_cinfo.node_query_count;
  152. #endif
  153. // Stop at first collision if contacts are not needed.
  154. return (collided && !query_cinfo.contact_info.result_callback);
  155. }
  156. bool GodotCollisionSolver3D::soft_body_concave_callback(void *p_userdata, GodotShape3D *p_convex) {
  157. _SoftBodyQueryInfo &query_cinfo = *(_SoftBodyQueryInfo *)(p_userdata);
  158. query_cinfo.shape_A = p_convex;
  159. // Calculate AABB for internal soft body query (in world space).
  160. AABB shape_aabb;
  161. for (int i = 0; i < 3; i++) {
  162. Vector3 axis;
  163. axis[i] = 1.0;
  164. real_t smin, smax;
  165. p_convex->project_range(axis, query_cinfo.transform_A, smin, smax);
  166. shape_aabb.position[i] = smin;
  167. shape_aabb.size[i] = smax - smin;
  168. }
  169. shape_aabb.grow_by(query_cinfo.soft_body->get_collision_margin());
  170. query_cinfo.soft_body->query_aabb(shape_aabb, soft_body_query_callback, &query_cinfo);
  171. bool collided = (query_cinfo.contact_info.contact_count > 0);
  172. #ifdef DEBUG_ENABLED
  173. ++query_cinfo.convex_query_count;
  174. #endif
  175. // Stop at first collision if contacts are not needed.
  176. return (collided && !query_cinfo.contact_info.result_callback);
  177. }
  178. bool GodotCollisionSolver3D::solve_soft_body(const GodotShape3D *p_shape_A, const Transform3D &p_transform_A, const GodotShape3D *p_shape_B, const Transform3D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result) {
  179. const GodotSoftBodyShape3D *soft_body_shape_B = static_cast<const GodotSoftBodyShape3D *>(p_shape_B);
  180. GodotSoftBody3D *soft_body = soft_body_shape_B->get_soft_body();
  181. const Transform3D &world_to_local = soft_body->get_inv_transform();
  182. const real_t collision_margin = soft_body->get_collision_margin();
  183. GodotSphereShape3D sphere_shape;
  184. sphere_shape.set_data(collision_margin);
  185. _SoftBodyQueryInfo query_cinfo;
  186. query_cinfo.contact_info.result_callback = p_result_callback;
  187. query_cinfo.contact_info.userdata = p_userdata;
  188. query_cinfo.contact_info.swap_result = p_swap_result;
  189. query_cinfo.soft_body = soft_body;
  190. query_cinfo.node_transform = p_transform_B * world_to_local;
  191. query_cinfo.shape_A = p_shape_A;
  192. query_cinfo.transform_A = p_transform_A;
  193. query_cinfo.shape_B = &sphere_shape;
  194. if (p_shape_A->is_concave()) {
  195. // In case of concave shape, query convex shapes first.
  196. const GodotConcaveShape3D *concave_shape_A = static_cast<const GodotConcaveShape3D *>(p_shape_A);
  197. AABB soft_body_aabb = soft_body->get_bounds();
  198. soft_body_aabb.grow_by(collision_margin);
  199. // Calculate AABB for internal concave shape query (in local space).
  200. AABB local_aabb;
  201. for (int i = 0; i < 3; i++) {
  202. Vector3 axis(p_transform_A.basis.get_axis(i));
  203. real_t axis_scale = 1.0 / axis.length();
  204. real_t smin = soft_body_aabb.position[i];
  205. real_t smax = smin + soft_body_aabb.size[i];
  206. smin *= axis_scale;
  207. smax *= axis_scale;
  208. local_aabb.position[i] = smin;
  209. local_aabb.size[i] = smax - smin;
  210. }
  211. concave_shape_A->cull(local_aabb, soft_body_concave_callback, &query_cinfo);
  212. } else {
  213. AABB shape_aabb = p_transform_A.xform(p_shape_A->get_aabb());
  214. shape_aabb.grow_by(collision_margin);
  215. soft_body->query_aabb(shape_aabb, soft_body_query_callback, &query_cinfo);
  216. }
  217. return (query_cinfo.contact_info.contact_count > 0);
  218. }
  219. struct _ConcaveCollisionInfo {
  220. const Transform3D *transform_A;
  221. const GodotShape3D *shape_A;
  222. const Transform3D *transform_B;
  223. GodotCollisionSolver3D::CallbackResult result_callback;
  224. void *userdata;
  225. bool swap_result;
  226. bool collided;
  227. int aabb_tests;
  228. int collisions;
  229. bool tested;
  230. real_t margin_A;
  231. real_t margin_B;
  232. Vector3 close_A, close_B;
  233. };
  234. bool GodotCollisionSolver3D::concave_callback(void *p_userdata, GodotShape3D *p_convex) {
  235. _ConcaveCollisionInfo &cinfo = *(_ConcaveCollisionInfo *)(p_userdata);
  236. cinfo.aabb_tests++;
  237. bool collided = collision_solver(cinfo.shape_A, *cinfo.transform_A, p_convex, *cinfo.transform_B, cinfo.result_callback, cinfo.userdata, cinfo.swap_result, nullptr, cinfo.margin_A, cinfo.margin_B);
  238. if (!collided) {
  239. return false;
  240. }
  241. cinfo.collided = true;
  242. cinfo.collisions++;
  243. // Stop at first collision if contacts are not needed.
  244. return !cinfo.result_callback;
  245. }
  246. bool GodotCollisionSolver3D::solve_concave(const GodotShape3D *p_shape_A, const Transform3D &p_transform_A, const GodotShape3D *p_shape_B, const Transform3D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, real_t p_margin_A, real_t p_margin_B) {
  247. const GodotConcaveShape3D *concave_B = static_cast<const GodotConcaveShape3D *>(p_shape_B);
  248. _ConcaveCollisionInfo cinfo;
  249. cinfo.transform_A = &p_transform_A;
  250. cinfo.shape_A = p_shape_A;
  251. cinfo.transform_B = &p_transform_B;
  252. cinfo.result_callback = p_result_callback;
  253. cinfo.userdata = p_userdata;
  254. cinfo.swap_result = p_swap_result;
  255. cinfo.collided = false;
  256. cinfo.collisions = 0;
  257. cinfo.margin_A = p_margin_A;
  258. cinfo.margin_B = p_margin_B;
  259. cinfo.aabb_tests = 0;
  260. Transform3D rel_transform = p_transform_A;
  261. rel_transform.origin -= p_transform_B.origin;
  262. //quickly compute a local AABB
  263. AABB local_aabb;
  264. for (int i = 0; i < 3; i++) {
  265. Vector3 axis(p_transform_B.basis.get_axis(i));
  266. real_t axis_scale = 1.0 / axis.length();
  267. axis *= axis_scale;
  268. real_t smin, smax;
  269. p_shape_A->project_range(axis, rel_transform, smin, smax);
  270. smin -= p_margin_A;
  271. smax += p_margin_A;
  272. smin *= axis_scale;
  273. smax *= axis_scale;
  274. local_aabb.position[i] = smin;
  275. local_aabb.size[i] = smax - smin;
  276. }
  277. concave_B->cull(local_aabb, concave_callback, &cinfo);
  278. return cinfo.collided;
  279. }
  280. bool GodotCollisionSolver3D::solve_static(const GodotShape3D *p_shape_A, const Transform3D &p_transform_A, const GodotShape3D *p_shape_B, const Transform3D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, Vector3 *r_sep_axis, real_t p_margin_A, real_t p_margin_B) {
  281. PhysicsServer3D::ShapeType type_A = p_shape_A->get_type();
  282. PhysicsServer3D::ShapeType type_B = p_shape_B->get_type();
  283. bool concave_A = p_shape_A->is_concave();
  284. bool concave_B = p_shape_B->is_concave();
  285. bool swap = false;
  286. if (type_A > type_B) {
  287. SWAP(type_A, type_B);
  288. SWAP(concave_A, concave_B);
  289. swap = true;
  290. }
  291. if (type_A == PhysicsServer3D::SHAPE_WORLD_BOUNDARY) {
  292. if (type_B == PhysicsServer3D::SHAPE_WORLD_BOUNDARY) {
  293. return false;
  294. }
  295. if (type_B == PhysicsServer3D::SHAPE_SEPARATION_RAY) {
  296. return false;
  297. }
  298. if (type_B == PhysicsServer3D::SHAPE_SOFT_BODY) {
  299. return false;
  300. }
  301. if (swap) {
  302. return solve_static_world_boundary(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  303. } else {
  304. return solve_static_world_boundary(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  305. }
  306. } else if (type_A == PhysicsServer3D::SHAPE_SEPARATION_RAY) {
  307. if (type_B == PhysicsServer3D::SHAPE_SEPARATION_RAY) {
  308. return false;
  309. }
  310. if (swap) {
  311. return solve_separation_ray(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, p_margin_B);
  312. } else {
  313. return solve_separation_ray(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, p_margin_A);
  314. }
  315. } else if (type_B == PhysicsServer3D::SHAPE_SOFT_BODY) {
  316. if (type_A == PhysicsServer3D::SHAPE_SOFT_BODY) {
  317. // Soft Body / Soft Body not supported.
  318. return false;
  319. }
  320. if (swap) {
  321. return solve_soft_body(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  322. } else {
  323. return solve_soft_body(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  324. }
  325. } else if (concave_B) {
  326. if (concave_A) {
  327. return false;
  328. }
  329. if (!swap) {
  330. return solve_concave(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, p_margin_A, p_margin_B);
  331. } else {
  332. return solve_concave(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, p_margin_A, p_margin_B);
  333. }
  334. } else {
  335. return collision_solver(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, r_sep_axis, p_margin_A, p_margin_B);
  336. }
  337. }
  338. bool GodotCollisionSolver3D::concave_distance_callback(void *p_userdata, GodotShape3D *p_convex) {
  339. _ConcaveCollisionInfo &cinfo = *(_ConcaveCollisionInfo *)(p_userdata);
  340. cinfo.aabb_tests++;
  341. Vector3 close_A, close_B;
  342. cinfo.collided = !gjk_epa_calculate_distance(cinfo.shape_A, *cinfo.transform_A, p_convex, *cinfo.transform_B, close_A, close_B);
  343. if (cinfo.collided) {
  344. // No need to process any more result.
  345. return true;
  346. }
  347. if (!cinfo.tested || close_A.distance_squared_to(close_B) < cinfo.close_A.distance_squared_to(cinfo.close_B)) {
  348. cinfo.close_A = close_A;
  349. cinfo.close_B = close_B;
  350. cinfo.tested = true;
  351. }
  352. cinfo.collisions++;
  353. return false;
  354. }
  355. bool GodotCollisionSolver3D::solve_distance_world_boundary(const GodotShape3D *p_shape_A, const Transform3D &p_transform_A, const GodotShape3D *p_shape_B, const Transform3D &p_transform_B, Vector3 &r_point_A, Vector3 &r_point_B) {
  356. const GodotWorldBoundaryShape3D *world_boundary = static_cast<const GodotWorldBoundaryShape3D *>(p_shape_A);
  357. if (p_shape_B->get_type() == PhysicsServer3D::SHAPE_WORLD_BOUNDARY) {
  358. return false;
  359. }
  360. Plane p = p_transform_A.xform(world_boundary->get_plane());
  361. static const int max_supports = 16;
  362. Vector3 supports[max_supports];
  363. int support_count;
  364. GodotShape3D::FeatureType support_type;
  365. p_shape_B->get_supports(p_transform_B.basis.xform_inv(-p.normal).normalized(), max_supports, supports, support_count, support_type);
  366. if (support_type == GodotShape3D::FEATURE_CIRCLE) {
  367. ERR_FAIL_COND_V(support_count != 3, false);
  368. Vector3 circle_pos = supports[0];
  369. Vector3 circle_axis_1 = supports[1] - circle_pos;
  370. Vector3 circle_axis_2 = supports[2] - circle_pos;
  371. // Use 3 equidistant points on the circle.
  372. for (int i = 0; i < 3; ++i) {
  373. Vector3 vertex_pos = circle_pos;
  374. vertex_pos += circle_axis_1 * Math::cos(2.0 * Math_PI * i / 3.0);
  375. vertex_pos += circle_axis_2 * Math::sin(2.0 * Math_PI * i / 3.0);
  376. supports[i] = vertex_pos;
  377. }
  378. }
  379. bool collided = false;
  380. Vector3 closest;
  381. real_t closest_d = 0;
  382. for (int i = 0; i < support_count; i++) {
  383. supports[i] = p_transform_B.xform(supports[i]);
  384. real_t d = p.distance_to(supports[i]);
  385. if (i == 0 || d < closest_d) {
  386. closest = supports[i];
  387. closest_d = d;
  388. if (d <= 0) {
  389. collided = true;
  390. }
  391. }
  392. }
  393. r_point_A = p.project(closest);
  394. r_point_B = closest;
  395. return collided;
  396. }
  397. bool GodotCollisionSolver3D::solve_distance(const GodotShape3D *p_shape_A, const Transform3D &p_transform_A, const GodotShape3D *p_shape_B, const Transform3D &p_transform_B, Vector3 &r_point_A, Vector3 &r_point_B, const AABB &p_concave_hint, Vector3 *r_sep_axis) {
  398. if (p_shape_A->is_concave()) {
  399. return false;
  400. }
  401. if (p_shape_B->get_type() == PhysicsServer3D::SHAPE_WORLD_BOUNDARY) {
  402. Vector3 a, b;
  403. bool col = solve_distance_world_boundary(p_shape_B, p_transform_B, p_shape_A, p_transform_A, a, b);
  404. r_point_A = b;
  405. r_point_B = a;
  406. return !col;
  407. } else if (p_shape_B->is_concave()) {
  408. if (p_shape_A->is_concave()) {
  409. return false;
  410. }
  411. const GodotConcaveShape3D *concave_B = static_cast<const GodotConcaveShape3D *>(p_shape_B);
  412. _ConcaveCollisionInfo cinfo;
  413. cinfo.transform_A = &p_transform_A;
  414. cinfo.shape_A = p_shape_A;
  415. cinfo.transform_B = &p_transform_B;
  416. cinfo.result_callback = nullptr;
  417. cinfo.userdata = nullptr;
  418. cinfo.swap_result = false;
  419. cinfo.collided = false;
  420. cinfo.collisions = 0;
  421. cinfo.aabb_tests = 0;
  422. cinfo.tested = false;
  423. Transform3D rel_transform = p_transform_A;
  424. rel_transform.origin -= p_transform_B.origin;
  425. //quickly compute a local AABB
  426. bool use_cc_hint = p_concave_hint != AABB();
  427. AABB cc_hint_aabb;
  428. if (use_cc_hint) {
  429. cc_hint_aabb = p_concave_hint;
  430. cc_hint_aabb.position -= p_transform_B.origin;
  431. }
  432. AABB local_aabb;
  433. for (int i = 0; i < 3; i++) {
  434. Vector3 axis(p_transform_B.basis.get_axis(i));
  435. real_t axis_scale = ((real_t)1.0) / axis.length();
  436. axis *= axis_scale;
  437. real_t smin, smax;
  438. if (use_cc_hint) {
  439. cc_hint_aabb.project_range_in_plane(Plane(axis), smin, smax);
  440. } else {
  441. p_shape_A->project_range(axis, rel_transform, smin, smax);
  442. }
  443. smin *= axis_scale;
  444. smax *= axis_scale;
  445. local_aabb.position[i] = smin;
  446. local_aabb.size[i] = smax - smin;
  447. }
  448. concave_B->cull(local_aabb, concave_distance_callback, &cinfo);
  449. if (!cinfo.collided) {
  450. r_point_A = cinfo.close_A;
  451. r_point_B = cinfo.close_B;
  452. }
  453. return !cinfo.collided;
  454. } else {
  455. return gjk_epa_calculate_distance(p_shape_A, p_transform_A, p_shape_B, p_transform_B, r_point_A, r_point_B); //should pass sepaxis..
  456. }
  457. }