godot_collision_solver_3d.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. if (!p_shape_B->intersect_segment(from, to, p, n, true)) {
  91. return false;
  92. }
  93. // Discard contacts when the ray is fully contained inside the shape.
  94. if (n == Vector3()) {
  95. return false;
  96. }
  97. // Discard contacts in the wrong direction.
  98. if (n.dot(from - to) < CMP_EPSILON) {
  99. return false;
  100. }
  101. Vector3 support_B = p_transform_B.xform(p);
  102. if (ray->get_slide_on_slope()) {
  103. Vector3 global_n = ai.basis.xform_inv(n).normalized();
  104. support_B = support_A + (support_B - support_A).length() * global_n;
  105. }
  106. if (p_result_callback) {
  107. Vector3 normal = (support_B - support_A).normalized();
  108. if (p_swap_result) {
  109. p_result_callback(support_B, 0, support_A, 0, -normal, p_userdata);
  110. } else {
  111. p_result_callback(support_A, 0, support_B, 0, normal, p_userdata);
  112. }
  113. }
  114. return true;
  115. }
  116. struct _SoftBodyContactCollisionInfo {
  117. int node_index = 0;
  118. GodotCollisionSolver3D::CallbackResult result_callback = nullptr;
  119. void *userdata = nullptr;
  120. bool swap_result = false;
  121. int contact_count = 0;
  122. };
  123. 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) {
  124. _SoftBodyContactCollisionInfo &cinfo = *(static_cast<_SoftBodyContactCollisionInfo *>(p_userdata));
  125. ++cinfo.contact_count;
  126. if (!cinfo.result_callback) {
  127. return;
  128. }
  129. if (cinfo.swap_result) {
  130. cinfo.result_callback(p_point_B, cinfo.node_index, p_point_A, p_index_A, -normal, cinfo.userdata);
  131. } else {
  132. cinfo.result_callback(p_point_A, p_index_A, p_point_B, cinfo.node_index, normal, cinfo.userdata);
  133. }
  134. }
  135. struct _SoftBodyQueryInfo {
  136. GodotSoftBody3D *soft_body = nullptr;
  137. const GodotShape3D *shape_A = nullptr;
  138. const GodotShape3D *shape_B = nullptr;
  139. Transform3D transform_A;
  140. Transform3D node_transform;
  141. _SoftBodyContactCollisionInfo contact_info;
  142. #ifdef DEBUG_ENABLED
  143. int node_query_count = 0;
  144. int convex_query_count = 0;
  145. #endif
  146. };
  147. bool GodotCollisionSolver3D::soft_body_query_callback(uint32_t p_node_index, void *p_userdata) {
  148. _SoftBodyQueryInfo &query_cinfo = *(static_cast<_SoftBodyQueryInfo *>(p_userdata));
  149. Vector3 node_position = query_cinfo.soft_body->get_node_position(p_node_index);
  150. Transform3D transform_B;
  151. transform_B.origin = query_cinfo.node_transform.xform(node_position);
  152. query_cinfo.contact_info.node_index = p_node_index;
  153. 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);
  154. #ifdef DEBUG_ENABLED
  155. ++query_cinfo.node_query_count;
  156. #endif
  157. // Stop at first collision if contacts are not needed.
  158. return (collided && !query_cinfo.contact_info.result_callback);
  159. }
  160. bool GodotCollisionSolver3D::soft_body_concave_callback(void *p_userdata, GodotShape3D *p_convex) {
  161. _SoftBodyQueryInfo &query_cinfo = *(static_cast<_SoftBodyQueryInfo *>(p_userdata));
  162. query_cinfo.shape_A = p_convex;
  163. // Calculate AABB for internal soft body query (in world space).
  164. AABB shape_aabb;
  165. for (int i = 0; i < 3; i++) {
  166. Vector3 axis;
  167. axis[i] = 1.0;
  168. real_t smin, smax;
  169. p_convex->project_range(axis, query_cinfo.transform_A, smin, smax);
  170. shape_aabb.position[i] = smin;
  171. shape_aabb.size[i] = smax - smin;
  172. }
  173. shape_aabb.grow_by(query_cinfo.soft_body->get_collision_margin());
  174. query_cinfo.soft_body->query_aabb(shape_aabb, soft_body_query_callback, &query_cinfo);
  175. bool collided = (query_cinfo.contact_info.contact_count > 0);
  176. #ifdef DEBUG_ENABLED
  177. ++query_cinfo.convex_query_count;
  178. #endif
  179. // Stop at first collision if contacts are not needed.
  180. return (collided && !query_cinfo.contact_info.result_callback);
  181. }
  182. 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) {
  183. const GodotSoftBodyShape3D *soft_body_shape_B = static_cast<const GodotSoftBodyShape3D *>(p_shape_B);
  184. GodotSoftBody3D *soft_body = soft_body_shape_B->get_soft_body();
  185. const Transform3D &world_to_local = soft_body->get_inv_transform();
  186. const real_t collision_margin = soft_body->get_collision_margin();
  187. GodotSphereShape3D sphere_shape;
  188. sphere_shape.set_data(collision_margin);
  189. _SoftBodyQueryInfo query_cinfo;
  190. query_cinfo.contact_info.result_callback = p_result_callback;
  191. query_cinfo.contact_info.userdata = p_userdata;
  192. query_cinfo.contact_info.swap_result = p_swap_result;
  193. query_cinfo.soft_body = soft_body;
  194. query_cinfo.node_transform = p_transform_B * world_to_local;
  195. query_cinfo.shape_A = p_shape_A;
  196. query_cinfo.transform_A = p_transform_A;
  197. query_cinfo.shape_B = &sphere_shape;
  198. if (p_shape_A->is_concave()) {
  199. // In case of concave shape, query convex shapes first.
  200. const GodotConcaveShape3D *concave_shape_A = static_cast<const GodotConcaveShape3D *>(p_shape_A);
  201. AABB soft_body_aabb = soft_body->get_bounds();
  202. soft_body_aabb.grow_by(collision_margin);
  203. // Calculate AABB for internal concave shape query (in local space).
  204. AABB local_aabb;
  205. for (int i = 0; i < 3; i++) {
  206. Vector3 axis(p_transform_A.basis.get_column(i));
  207. real_t axis_scale = 1.0 / axis.length();
  208. real_t smin = soft_body_aabb.position[i];
  209. real_t smax = smin + soft_body_aabb.size[i];
  210. smin *= axis_scale;
  211. smax *= axis_scale;
  212. local_aabb.position[i] = smin;
  213. local_aabb.size[i] = smax - smin;
  214. }
  215. concave_shape_A->cull(local_aabb, soft_body_concave_callback, &query_cinfo, true);
  216. } else {
  217. AABB shape_aabb = p_transform_A.xform(p_shape_A->get_aabb());
  218. shape_aabb.grow_by(collision_margin);
  219. soft_body->query_aabb(shape_aabb, soft_body_query_callback, &query_cinfo);
  220. }
  221. return (query_cinfo.contact_info.contact_count > 0);
  222. }
  223. struct _ConcaveCollisionInfo {
  224. const Transform3D *transform_A = nullptr;
  225. const GodotShape3D *shape_A = nullptr;
  226. const Transform3D *transform_B = nullptr;
  227. GodotCollisionSolver3D::CallbackResult result_callback = nullptr;
  228. void *userdata = nullptr;
  229. bool swap_result = false;
  230. bool collided = false;
  231. int aabb_tests = 0;
  232. int collisions = 0;
  233. bool tested = false;
  234. real_t margin_A = 0.0f;
  235. real_t margin_B = 0.0f;
  236. Vector3 close_A;
  237. Vector3 close_B;
  238. };
  239. bool GodotCollisionSolver3D::concave_callback(void *p_userdata, GodotShape3D *p_convex) {
  240. _ConcaveCollisionInfo &cinfo = *(static_cast<_ConcaveCollisionInfo *>(p_userdata));
  241. cinfo.aabb_tests++;
  242. 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);
  243. if (!collided) {
  244. return false;
  245. }
  246. cinfo.collided = true;
  247. cinfo.collisions++;
  248. // Stop at first collision if contacts are not needed.
  249. return !cinfo.result_callback;
  250. }
  251. 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) {
  252. const GodotConcaveShape3D *concave_B = static_cast<const GodotConcaveShape3D *>(p_shape_B);
  253. _ConcaveCollisionInfo cinfo;
  254. cinfo.transform_A = &p_transform_A;
  255. cinfo.shape_A = p_shape_A;
  256. cinfo.transform_B = &p_transform_B;
  257. cinfo.result_callback = p_result_callback;
  258. cinfo.userdata = p_userdata;
  259. cinfo.swap_result = p_swap_result;
  260. cinfo.collided = false;
  261. cinfo.collisions = 0;
  262. cinfo.margin_A = p_margin_A;
  263. cinfo.margin_B = p_margin_B;
  264. cinfo.aabb_tests = 0;
  265. Transform3D rel_transform = p_transform_A;
  266. rel_transform.origin -= p_transform_B.origin;
  267. //quickly compute a local AABB
  268. AABB local_aabb;
  269. for (int i = 0; i < 3; i++) {
  270. Vector3 axis(p_transform_B.basis.get_column(i));
  271. real_t axis_scale = 1.0 / axis.length();
  272. axis *= axis_scale;
  273. real_t smin = 0.0, smax = 0.0;
  274. p_shape_A->project_range(axis, rel_transform, smin, smax);
  275. smin -= p_margin_A;
  276. smax += p_margin_A;
  277. smin *= axis_scale;
  278. smax *= axis_scale;
  279. local_aabb.position[i] = smin;
  280. local_aabb.size[i] = smax - smin;
  281. }
  282. concave_B->cull(local_aabb, concave_callback, &cinfo, false);
  283. return cinfo.collided;
  284. }
  285. 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) {
  286. PhysicsServer3D::ShapeType type_A = p_shape_A->get_type();
  287. PhysicsServer3D::ShapeType type_B = p_shape_B->get_type();
  288. bool concave_A = p_shape_A->is_concave();
  289. bool concave_B = p_shape_B->is_concave();
  290. bool swap = false;
  291. if (type_A > type_B) {
  292. SWAP(type_A, type_B);
  293. SWAP(concave_A, concave_B);
  294. swap = true;
  295. }
  296. if (type_A == PhysicsServer3D::SHAPE_WORLD_BOUNDARY) {
  297. if (type_B == PhysicsServer3D::SHAPE_WORLD_BOUNDARY) {
  298. WARN_PRINT_ONCE("Collisions between world boundaries are not supported.");
  299. return false;
  300. }
  301. if (type_B == PhysicsServer3D::SHAPE_SEPARATION_RAY) {
  302. WARN_PRINT_ONCE("Collisions between world boundaries and rays are not supported.");
  303. return false;
  304. }
  305. if (type_B == PhysicsServer3D::SHAPE_SOFT_BODY) {
  306. WARN_PRINT_ONCE("Collisions between world boundaries and soft bodies are not supported.");
  307. return false;
  308. }
  309. if (swap) {
  310. 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);
  311. } else {
  312. 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);
  313. }
  314. } else if (type_A == PhysicsServer3D::SHAPE_SEPARATION_RAY) {
  315. if (type_B == PhysicsServer3D::SHAPE_SEPARATION_RAY) {
  316. WARN_PRINT_ONCE("Collisions between rays are not supported.");
  317. return false;
  318. }
  319. if (swap) {
  320. 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);
  321. } else {
  322. 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);
  323. }
  324. } else if (type_B == PhysicsServer3D::SHAPE_SOFT_BODY) {
  325. if (type_A == PhysicsServer3D::SHAPE_SOFT_BODY) {
  326. WARN_PRINT_ONCE("Collisions between soft bodies are not supported.");
  327. return false;
  328. }
  329. if (swap) {
  330. return solve_soft_body(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  331. } else {
  332. return solve_soft_body(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  333. }
  334. } else if (concave_B) {
  335. if (concave_A) {
  336. WARN_PRINT_ONCE("Collisions between two concave shapes are not supported.");
  337. return false;
  338. }
  339. if (!swap) {
  340. 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);
  341. } else {
  342. 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);
  343. }
  344. } else {
  345. 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);
  346. }
  347. }
  348. bool GodotCollisionSolver3D::concave_distance_callback(void *p_userdata, GodotShape3D *p_convex) {
  349. _ConcaveCollisionInfo &cinfo = *(static_cast<_ConcaveCollisionInfo *>(p_userdata));
  350. cinfo.aabb_tests++;
  351. Vector3 close_A, close_B;
  352. cinfo.collided = !gjk_epa_calculate_distance(cinfo.shape_A, *cinfo.transform_A, p_convex, *cinfo.transform_B, close_A, close_B);
  353. if (cinfo.collided) {
  354. // No need to process any more result.
  355. return true;
  356. }
  357. if (!cinfo.tested || close_A.distance_squared_to(close_B) < cinfo.close_A.distance_squared_to(cinfo.close_B)) {
  358. cinfo.close_A = close_A;
  359. cinfo.close_B = close_B;
  360. cinfo.tested = true;
  361. }
  362. cinfo.collisions++;
  363. return false;
  364. }
  365. 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) {
  366. const GodotWorldBoundaryShape3D *world_boundary = static_cast<const GodotWorldBoundaryShape3D *>(p_shape_A);
  367. if (p_shape_B->get_type() == PhysicsServer3D::SHAPE_WORLD_BOUNDARY) {
  368. return false;
  369. }
  370. Plane p = p_transform_A.xform(world_boundary->get_plane());
  371. static const int max_supports = 16;
  372. Vector3 supports[max_supports];
  373. int support_count;
  374. GodotShape3D::FeatureType support_type;
  375. Vector3 support_direction = p_transform_B.basis.xform_inv(-p.normal).normalized();
  376. p_shape_B->get_supports(support_direction, max_supports, supports, support_count, support_type);
  377. if (support_count == 0) { // This is a poor man's way to detect shapes that don't implement get_supports, such as GodotMotionShape3D.
  378. Vector3 support_B = p_transform_B.xform(p_shape_B->get_support(support_direction));
  379. r_point_A = p.project(support_B);
  380. r_point_B = support_B;
  381. bool collided = p.distance_to(support_B) <= 0;
  382. return collided;
  383. }
  384. if (support_type == GodotShape3D::FEATURE_CIRCLE) {
  385. ERR_FAIL_COND_V(support_count != 3, false);
  386. Vector3 circle_pos = supports[0];
  387. Vector3 circle_axis_1 = supports[1] - circle_pos;
  388. Vector3 circle_axis_2 = supports[2] - circle_pos;
  389. // Use 3 equidistant points on the circle.
  390. for (int i = 0; i < 3; ++i) {
  391. Vector3 vertex_pos = circle_pos;
  392. vertex_pos += circle_axis_1 * Math::cos(2.0 * Math_PI * i / 3.0);
  393. vertex_pos += circle_axis_2 * Math::sin(2.0 * Math_PI * i / 3.0);
  394. supports[i] = vertex_pos;
  395. }
  396. }
  397. bool collided = false;
  398. Vector3 closest;
  399. real_t closest_d = 0;
  400. for (int i = 0; i < support_count; i++) {
  401. supports[i] = p_transform_B.xform(supports[i]);
  402. real_t d = p.distance_to(supports[i]);
  403. if (i == 0 || d < closest_d) {
  404. closest = supports[i];
  405. closest_d = d;
  406. if (d <= 0) {
  407. collided = true;
  408. }
  409. }
  410. }
  411. r_point_A = p.project(closest);
  412. r_point_B = closest;
  413. return collided;
  414. }
  415. 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) {
  416. if (p_shape_B->get_type() == PhysicsServer3D::SHAPE_WORLD_BOUNDARY) {
  417. Vector3 a, b;
  418. bool col = solve_distance_world_boundary(p_shape_B, p_transform_B, p_shape_A, p_transform_A, a, b);
  419. r_point_A = b;
  420. r_point_B = a;
  421. return !col;
  422. } else if (p_shape_B->is_concave()) {
  423. if (p_shape_A->is_concave()) {
  424. return false;
  425. }
  426. const GodotConcaveShape3D *concave_B = static_cast<const GodotConcaveShape3D *>(p_shape_B);
  427. _ConcaveCollisionInfo cinfo;
  428. cinfo.transform_A = &p_transform_A;
  429. cinfo.shape_A = p_shape_A;
  430. cinfo.transform_B = &p_transform_B;
  431. cinfo.result_callback = nullptr;
  432. cinfo.userdata = nullptr;
  433. cinfo.swap_result = false;
  434. cinfo.collided = false;
  435. cinfo.collisions = 0;
  436. cinfo.aabb_tests = 0;
  437. cinfo.tested = false;
  438. Transform3D rel_transform = p_transform_A;
  439. rel_transform.origin -= p_transform_B.origin;
  440. //quickly compute a local AABB
  441. bool use_cc_hint = p_concave_hint != AABB();
  442. AABB cc_hint_aabb;
  443. if (use_cc_hint) {
  444. cc_hint_aabb = p_concave_hint;
  445. cc_hint_aabb.position -= p_transform_B.origin;
  446. }
  447. AABB local_aabb;
  448. for (int i = 0; i < 3; i++) {
  449. Vector3 axis(p_transform_B.basis.get_column(i));
  450. real_t axis_scale = ((real_t)1.0) / axis.length();
  451. axis *= axis_scale;
  452. real_t smin, smax;
  453. if (use_cc_hint) {
  454. cc_hint_aabb.project_range_in_plane(Plane(axis), smin, smax);
  455. } else {
  456. p_shape_A->project_range(axis, rel_transform, smin, smax);
  457. }
  458. smin *= axis_scale;
  459. smax *= axis_scale;
  460. local_aabb.position[i] = smin;
  461. local_aabb.size[i] = smax - smin;
  462. }
  463. concave_B->cull(local_aabb, concave_distance_callback, &cinfo, false);
  464. if (!cinfo.collided) {
  465. r_point_A = cinfo.close_A;
  466. r_point_B = cinfo.close_B;
  467. }
  468. return !cinfo.collided;
  469. } else {
  470. 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..
  471. }
  472. }