godot_collision_solver_3d.cpp 21 KB

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