collision_solver_sw.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**************************************************************************/
  2. /* collision_solver_sw.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 "collision_solver_sw.h"
  31. #include "collision_solver_sat.h"
  32. #include "gjk_epa.h"
  33. #define collision_solver sat_calculate_penetration
  34. //#define collision_solver gjk_epa_calculate_penetration
  35. bool CollisionSolverSW::solve_static_plane(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result) {
  36. const PlaneShapeSW *plane = static_cast<const PlaneShapeSW *>(p_shape_A);
  37. if (p_shape_B->get_type() == PhysicsServer::SHAPE_PLANE) {
  38. return false;
  39. }
  40. Plane p = p_transform_A.xform(plane->get_plane());
  41. static const int max_supports = 16;
  42. Vector3 supports[max_supports];
  43. int support_count;
  44. ShapeSW::FeatureType support_type;
  45. p_shape_B->get_supports(p_transform_B.basis.xform_inv(-p.normal).normalized(), max_supports, supports, support_count, support_type);
  46. if (support_type == ShapeSW::FEATURE_CIRCLE) {
  47. ERR_FAIL_COND_V(support_count != 3, false);
  48. Vector3 circle_pos = supports[0];
  49. Vector3 circle_axis_1 = supports[1] - circle_pos;
  50. Vector3 circle_axis_2 = supports[2] - circle_pos;
  51. // Use 3 equidistant points on the circle.
  52. for (int i = 0; i < 3; ++i) {
  53. Vector3 vertex_pos = circle_pos;
  54. vertex_pos += circle_axis_1 * Math::cos(2.0 * Math_PI * i / 3.0);
  55. vertex_pos += circle_axis_2 * Math::sin(2.0 * Math_PI * i / 3.0);
  56. supports[i] = vertex_pos;
  57. }
  58. }
  59. bool found = false;
  60. for (int i = 0; i < support_count; i++) {
  61. supports[i] = p_transform_B.xform(supports[i]);
  62. if (p.distance_to(supports[i]) >= 0) {
  63. continue;
  64. }
  65. found = true;
  66. Vector3 support_A = p.project(supports[i]);
  67. if (p_result_callback) {
  68. if (p_swap_result) {
  69. p_result_callback(supports[i], support_A, p_userdata);
  70. } else {
  71. p_result_callback(support_A, supports[i], p_userdata);
  72. }
  73. }
  74. }
  75. return found;
  76. }
  77. bool CollisionSolverSW::solve_ray(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, real_t p_margin) {
  78. const RayShapeSW *ray = static_cast<const RayShapeSW *>(p_shape_A);
  79. Vector3 from = p_transform_A.origin;
  80. Vector3 to = from + p_transform_A.basis.get_axis(2) * (ray->get_length() + p_margin);
  81. Vector3 support_A = to;
  82. Transform ai = p_transform_B.affine_inverse();
  83. from = ai.xform(from);
  84. to = ai.xform(to);
  85. Vector3 p, n;
  86. if (!p_shape_B->intersect_segment(from, to, p, n)) {
  87. return false;
  88. }
  89. Vector3 support_B = p_transform_B.xform(p);
  90. if (ray->get_slips_on_slope()) {
  91. Vector3 global_n = ai.basis.xform_inv(n).normalized();
  92. support_B = support_A + (support_B - support_A).length() * global_n;
  93. }
  94. if (p_result_callback) {
  95. if (p_swap_result) {
  96. p_result_callback(support_B, support_A, p_userdata);
  97. } else {
  98. p_result_callback(support_A, support_B, p_userdata);
  99. }
  100. }
  101. return true;
  102. }
  103. struct _ConcaveCollisionInfo {
  104. const Transform *transform_A;
  105. const ShapeSW *shape_A;
  106. const Transform *transform_B;
  107. CollisionSolverSW::CallbackResult result_callback;
  108. void *userdata;
  109. bool swap_result;
  110. bool collided;
  111. int aabb_tests;
  112. int collisions;
  113. bool tested;
  114. real_t margin_A;
  115. real_t margin_B;
  116. Vector3 close_A, close_B;
  117. };
  118. bool CollisionSolverSW::concave_callback(void *p_userdata, ShapeSW *p_convex) {
  119. _ConcaveCollisionInfo &cinfo = *(_ConcaveCollisionInfo *)(p_userdata);
  120. cinfo.aabb_tests++;
  121. 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);
  122. if (!collided) {
  123. return false;
  124. }
  125. cinfo.collided = true;
  126. cinfo.collisions++;
  127. // Stop at first collision if contacts are not needed.
  128. return !cinfo.result_callback;
  129. }
  130. bool CollisionSolverSW::solve_concave(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, real_t p_margin_A, real_t p_margin_B) {
  131. const ConcaveShapeSW *concave_B = static_cast<const ConcaveShapeSW *>(p_shape_B);
  132. _ConcaveCollisionInfo cinfo;
  133. cinfo.transform_A = &p_transform_A;
  134. cinfo.shape_A = p_shape_A;
  135. cinfo.transform_B = &p_transform_B;
  136. cinfo.result_callback = p_result_callback;
  137. cinfo.userdata = p_userdata;
  138. cinfo.swap_result = p_swap_result;
  139. cinfo.collided = false;
  140. cinfo.collisions = 0;
  141. cinfo.margin_A = p_margin_A;
  142. cinfo.margin_B = p_margin_B;
  143. cinfo.aabb_tests = 0;
  144. Transform rel_transform = p_transform_A;
  145. rel_transform.origin -= p_transform_B.origin;
  146. //quickly compute a local AABB
  147. AABB local_aabb;
  148. for (int i = 0; i < 3; i++) {
  149. Vector3 axis(p_transform_B.basis.get_axis(i));
  150. real_t axis_scale = 1.0 / axis.length();
  151. axis *= axis_scale;
  152. real_t smin, smax;
  153. p_shape_A->project_range(axis, rel_transform, smin, smax);
  154. smin -= p_margin_A;
  155. smax += p_margin_A;
  156. smin *= axis_scale;
  157. smax *= axis_scale;
  158. local_aabb.position[i] = smin;
  159. local_aabb.size[i] = smax - smin;
  160. }
  161. concave_B->cull(local_aabb, concave_callback, &cinfo);
  162. return cinfo.collided;
  163. }
  164. bool CollisionSolverSW::solve_static(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, CallbackResult p_result_callback, void *p_userdata, Vector3 *r_sep_axis, real_t p_margin_A, real_t p_margin_B) {
  165. PhysicsServer::ShapeType type_A = p_shape_A->get_type();
  166. PhysicsServer::ShapeType type_B = p_shape_B->get_type();
  167. bool concave_A = p_shape_A->is_concave();
  168. bool concave_B = p_shape_B->is_concave();
  169. bool swap = false;
  170. if (type_A > type_B) {
  171. SWAP(type_A, type_B);
  172. SWAP(concave_A, concave_B);
  173. swap = true;
  174. }
  175. if (type_A == PhysicsServer::SHAPE_PLANE) {
  176. if (type_B == PhysicsServer::SHAPE_PLANE) {
  177. return false;
  178. }
  179. if (type_B == PhysicsServer::SHAPE_RAY) {
  180. return false;
  181. }
  182. if (swap) {
  183. return solve_static_plane(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  184. } else {
  185. return solve_static_plane(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  186. }
  187. } else if (type_A == PhysicsServer::SHAPE_RAY) {
  188. if (type_B == PhysicsServer::SHAPE_RAY) {
  189. return false;
  190. }
  191. if (swap) {
  192. return solve_ray(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, p_margin_B);
  193. } else {
  194. return solve_ray(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, p_margin_A);
  195. }
  196. } else if (concave_B) {
  197. if (concave_A) {
  198. return false;
  199. }
  200. if (!swap) {
  201. 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);
  202. } else {
  203. 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);
  204. }
  205. } else {
  206. 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);
  207. }
  208. }
  209. bool CollisionSolverSW::concave_distance_callback(void *p_userdata, ShapeSW *p_convex) {
  210. _ConcaveCollisionInfo &cinfo = *(_ConcaveCollisionInfo *)(p_userdata);
  211. cinfo.aabb_tests++;
  212. Vector3 close_A, close_B;
  213. cinfo.collided = !gjk_epa_calculate_distance(cinfo.shape_A, *cinfo.transform_A, p_convex, *cinfo.transform_B, close_A, close_B);
  214. if (cinfo.collided) {
  215. // No need to process any more result.
  216. return true;
  217. }
  218. if (!cinfo.tested || close_A.distance_squared_to(close_B) < cinfo.close_A.distance_squared_to(cinfo.close_B)) {
  219. cinfo.close_A = close_A;
  220. cinfo.close_B = close_B;
  221. cinfo.tested = true;
  222. }
  223. cinfo.collisions++;
  224. return false;
  225. }
  226. bool CollisionSolverSW::solve_distance_plane(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, Vector3 &r_point_A, Vector3 &r_point_B) {
  227. const PlaneShapeSW *plane = static_cast<const PlaneShapeSW *>(p_shape_A);
  228. if (p_shape_B->get_type() == PhysicsServer::SHAPE_PLANE) {
  229. return false;
  230. }
  231. Plane p = p_transform_A.xform(plane->get_plane());
  232. static const int max_supports = 16;
  233. Vector3 supports[max_supports];
  234. int support_count;
  235. ShapeSW::FeatureType support_type;
  236. p_shape_B->get_supports(p_transform_B.basis.xform_inv(-p.normal).normalized(), max_supports, supports, support_count, support_type);
  237. if (support_type == ShapeSW::FEATURE_CIRCLE) {
  238. ERR_FAIL_COND_V(support_count != 3, false);
  239. Vector3 circle_pos = supports[0];
  240. Vector3 circle_axis_1 = supports[1] - circle_pos;
  241. Vector3 circle_axis_2 = supports[2] - circle_pos;
  242. // Use 3 equidistant points on the circle.
  243. for (int i = 0; i < 3; ++i) {
  244. Vector3 vertex_pos = circle_pos;
  245. vertex_pos += circle_axis_1 * Math::cos(2.0 * Math_PI * i / 3.0);
  246. vertex_pos += circle_axis_2 * Math::sin(2.0 * Math_PI * i / 3.0);
  247. supports[i] = vertex_pos;
  248. }
  249. }
  250. bool collided = false;
  251. Vector3 closest;
  252. real_t closest_d = 0;
  253. for (int i = 0; i < support_count; i++) {
  254. supports[i] = p_transform_B.xform(supports[i]);
  255. real_t d = p.distance_to(supports[i]);
  256. if (i == 0 || d < closest_d) {
  257. closest = supports[i];
  258. closest_d = d;
  259. if (d <= 0) {
  260. collided = true;
  261. }
  262. }
  263. }
  264. r_point_A = p.project(closest);
  265. r_point_B = closest;
  266. return collided;
  267. }
  268. bool CollisionSolverSW::solve_distance(const ShapeSW *p_shape_A, const Transform &p_transform_A, const ShapeSW *p_shape_B, const Transform &p_transform_B, Vector3 &r_point_A, Vector3 &r_point_B, const AABB &p_concave_hint, Vector3 *r_sep_axis) {
  269. if (p_shape_A->is_concave()) {
  270. return false;
  271. }
  272. if (p_shape_B->get_type() == PhysicsServer::SHAPE_PLANE) {
  273. Vector3 a, b;
  274. bool col = solve_distance_plane(p_shape_B, p_transform_B, p_shape_A, p_transform_A, a, b);
  275. r_point_A = b;
  276. r_point_B = a;
  277. return !col;
  278. } else if (p_shape_B->is_concave()) {
  279. if (p_shape_A->is_concave()) {
  280. return false;
  281. }
  282. const ConcaveShapeSW *concave_B = static_cast<const ConcaveShapeSW *>(p_shape_B);
  283. _ConcaveCollisionInfo cinfo;
  284. cinfo.transform_A = &p_transform_A;
  285. cinfo.shape_A = p_shape_A;
  286. cinfo.transform_B = &p_transform_B;
  287. cinfo.result_callback = nullptr;
  288. cinfo.userdata = nullptr;
  289. cinfo.swap_result = false;
  290. cinfo.collided = false;
  291. cinfo.collisions = 0;
  292. cinfo.aabb_tests = 0;
  293. cinfo.tested = false;
  294. Transform rel_transform = p_transform_A;
  295. rel_transform.origin -= p_transform_B.origin;
  296. //quickly compute a local AABB
  297. bool use_cc_hint = p_concave_hint != AABB();
  298. AABB cc_hint_aabb;
  299. if (use_cc_hint) {
  300. cc_hint_aabb = p_concave_hint;
  301. cc_hint_aabb.position -= p_transform_B.origin;
  302. }
  303. AABB local_aabb;
  304. for (int i = 0; i < 3; i++) {
  305. Vector3 axis(p_transform_B.basis.get_axis(i));
  306. real_t axis_scale = ((real_t)1.0) / axis.length();
  307. axis *= axis_scale;
  308. real_t smin, smax;
  309. if (use_cc_hint) {
  310. cc_hint_aabb.project_range_in_plane(Plane(axis, 0), smin, smax);
  311. } else {
  312. p_shape_A->project_range(axis, rel_transform, smin, smax);
  313. }
  314. smin *= axis_scale;
  315. smax *= axis_scale;
  316. local_aabb.position[i] = smin;
  317. local_aabb.size[i] = smax - smin;
  318. }
  319. concave_B->cull(local_aabb, concave_distance_callback, &cinfo);
  320. if (!cinfo.collided) {
  321. r_point_A = cinfo.close_A;
  322. r_point_B = cinfo.close_B;
  323. }
  324. return !cinfo.collided;
  325. } else {
  326. 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..
  327. }
  328. }