collision_solver_sw.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*************************************************************************/
  2. /* collision_solver_sw.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_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. Plane p = p_transform_A.xform(plane->get_plane());
  40. static const int max_supports = 16;
  41. Vector3 supports[max_supports];
  42. int support_count;
  43. p_shape_B->get_supports(p_transform_B.basis.xform_inv(-p.normal).normalized(), max_supports, supports, support_count);
  44. bool found = false;
  45. for (int i = 0; i < support_count; i++) {
  46. supports[i] = p_transform_B.xform(supports[i]);
  47. if (p.distance_to(supports[i]) >= 0)
  48. continue;
  49. found = true;
  50. Vector3 support_A = p.project(supports[i]);
  51. if (p_result_callback) {
  52. if (p_swap_result)
  53. p_result_callback(supports[i], support_A, p_userdata);
  54. else
  55. p_result_callback(support_A, supports[i], p_userdata);
  56. }
  57. }
  58. return found;
  59. }
  60. 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) {
  61. const RayShapeSW *ray = static_cast<const RayShapeSW *>(p_shape_A);
  62. Vector3 from = p_transform_A.origin;
  63. Vector3 to = from + p_transform_A.basis.get_axis(2) * ray->get_length();
  64. Vector3 support_A = to;
  65. Transform ai = p_transform_B.affine_inverse();
  66. from = ai.xform(from);
  67. to = ai.xform(to);
  68. Vector3 p, n;
  69. if (!p_shape_B->intersect_segment(from, to, p, n))
  70. return false;
  71. Vector3 support_B = p_transform_B.xform(p);
  72. if (ray->get_slips_on_slope()) {
  73. Vector3 global_n = ai.basis.xform_inv(n).normalized();
  74. support_B = support_A + (support_B - support_A).length() * global_n;
  75. }
  76. if (p_result_callback) {
  77. if (p_swap_result)
  78. p_result_callback(support_B, support_A, p_userdata);
  79. else
  80. p_result_callback(support_A, support_B, p_userdata);
  81. }
  82. return true;
  83. }
  84. struct _ConcaveCollisionInfo {
  85. const Transform *transform_A;
  86. const ShapeSW *shape_A;
  87. const Transform *transform_B;
  88. CollisionSolverSW::CallbackResult result_callback;
  89. void *userdata;
  90. bool swap_result;
  91. bool collided;
  92. int aabb_tests;
  93. int collisions;
  94. bool tested;
  95. real_t margin_A;
  96. real_t margin_B;
  97. Vector3 close_A, close_B;
  98. };
  99. void CollisionSolverSW::concave_callback(void *p_userdata, ShapeSW *p_convex) {
  100. _ConcaveCollisionInfo &cinfo = *(_ConcaveCollisionInfo *)(p_userdata);
  101. cinfo.aabb_tests++;
  102. bool collided = collision_solver(cinfo.shape_A, *cinfo.transform_A, p_convex, *cinfo.transform_B, cinfo.result_callback, cinfo.userdata, cinfo.swap_result, NULL, cinfo.margin_A, cinfo.margin_B);
  103. if (!collided)
  104. return;
  105. cinfo.collided = true;
  106. cinfo.collisions++;
  107. }
  108. 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) {
  109. const ConcaveShapeSW *concave_B = static_cast<const ConcaveShapeSW *>(p_shape_B);
  110. _ConcaveCollisionInfo cinfo;
  111. cinfo.transform_A = &p_transform_A;
  112. cinfo.shape_A = p_shape_A;
  113. cinfo.transform_B = &p_transform_B;
  114. cinfo.result_callback = p_result_callback;
  115. cinfo.userdata = p_userdata;
  116. cinfo.swap_result = p_swap_result;
  117. cinfo.collided = false;
  118. cinfo.collisions = 0;
  119. cinfo.margin_A = p_margin_A;
  120. cinfo.margin_B = p_margin_B;
  121. cinfo.aabb_tests = 0;
  122. Transform rel_transform = p_transform_A;
  123. rel_transform.origin -= p_transform_B.origin;
  124. //quickly compute a local AABB
  125. AABB local_aabb;
  126. for (int i = 0; i < 3; i++) {
  127. Vector3 axis(p_transform_B.basis.get_axis(i));
  128. real_t axis_scale = 1.0 / axis.length();
  129. axis *= axis_scale;
  130. real_t smin, smax;
  131. p_shape_A->project_range(axis, rel_transform, smin, smax);
  132. smin -= p_margin_A;
  133. smax += p_margin_A;
  134. smin *= axis_scale;
  135. smax *= axis_scale;
  136. local_aabb.position[i] = smin;
  137. local_aabb.size[i] = smax - smin;
  138. }
  139. concave_B->cull(local_aabb, concave_callback, &cinfo);
  140. return cinfo.collided;
  141. }
  142. 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) {
  143. PhysicsServer::ShapeType type_A = p_shape_A->get_type();
  144. PhysicsServer::ShapeType type_B = p_shape_B->get_type();
  145. bool concave_A = p_shape_A->is_concave();
  146. bool concave_B = p_shape_B->is_concave();
  147. bool swap = false;
  148. if (type_A > type_B) {
  149. SWAP(type_A, type_B);
  150. SWAP(concave_A, concave_B);
  151. swap = true;
  152. }
  153. if (type_A == PhysicsServer::SHAPE_PLANE) {
  154. if (type_B == PhysicsServer::SHAPE_PLANE)
  155. return false;
  156. if (type_B == PhysicsServer::SHAPE_RAY) {
  157. return false;
  158. }
  159. if (swap) {
  160. return solve_static_plane(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  161. } else {
  162. return solve_static_plane(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  163. }
  164. } else if (type_A == PhysicsServer::SHAPE_RAY) {
  165. if (type_B == PhysicsServer::SHAPE_RAY)
  166. return false;
  167. if (swap) {
  168. return solve_ray(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  169. } else {
  170. return solve_ray(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  171. }
  172. } else if (concave_B) {
  173. if (concave_A)
  174. return false;
  175. if (!swap)
  176. 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);
  177. else
  178. 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);
  179. } else {
  180. 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);
  181. }
  182. return false;
  183. }
  184. void CollisionSolverSW::concave_distance_callback(void *p_userdata, ShapeSW *p_convex) {
  185. _ConcaveCollisionInfo &cinfo = *(_ConcaveCollisionInfo *)(p_userdata);
  186. cinfo.aabb_tests++;
  187. if (cinfo.collided)
  188. return;
  189. Vector3 close_A, close_B;
  190. cinfo.collided = !gjk_epa_calculate_distance(cinfo.shape_A, *cinfo.transform_A, p_convex, *cinfo.transform_B, close_A, close_B);
  191. if (cinfo.collided)
  192. return;
  193. if (!cinfo.tested || close_A.distance_squared_to(close_B) < cinfo.close_A.distance_squared_to(cinfo.close_B)) {
  194. cinfo.close_A = close_A;
  195. cinfo.close_B = close_B;
  196. cinfo.tested = true;
  197. }
  198. cinfo.collisions++;
  199. }
  200. 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) {
  201. const PlaneShapeSW *plane = static_cast<const PlaneShapeSW *>(p_shape_A);
  202. if (p_shape_B->get_type() == PhysicsServer::SHAPE_PLANE)
  203. return false;
  204. Plane p = p_transform_A.xform(plane->get_plane());
  205. static const int max_supports = 16;
  206. Vector3 supports[max_supports];
  207. int support_count;
  208. p_shape_B->get_supports(p_transform_B.basis.xform_inv(-p.normal).normalized(), max_supports, supports, support_count);
  209. bool collided = false;
  210. Vector3 closest;
  211. real_t closest_d = 0;
  212. for (int i = 0; i < support_count; i++) {
  213. supports[i] = p_transform_B.xform(supports[i]);
  214. real_t d = p.distance_to(supports[i]);
  215. if (i == 0 || d < closest_d) {
  216. closest = supports[i];
  217. closest_d = d;
  218. if (d <= 0)
  219. collided = true;
  220. }
  221. }
  222. r_point_A = p.project(closest);
  223. r_point_B = closest;
  224. return collided;
  225. }
  226. 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) {
  227. if (p_shape_A->is_concave())
  228. return false;
  229. if (p_shape_B->get_type() == PhysicsServer::SHAPE_PLANE) {
  230. Vector3 a, b;
  231. bool col = solve_distance_plane(p_shape_B, p_transform_B, p_shape_A, p_transform_A, a, b);
  232. r_point_A = b;
  233. r_point_B = a;
  234. return !col;
  235. } else if (p_shape_B->is_concave()) {
  236. if (p_shape_A->is_concave())
  237. return false;
  238. const ConcaveShapeSW *concave_B = static_cast<const ConcaveShapeSW *>(p_shape_B);
  239. _ConcaveCollisionInfo cinfo;
  240. cinfo.transform_A = &p_transform_A;
  241. cinfo.shape_A = p_shape_A;
  242. cinfo.transform_B = &p_transform_B;
  243. cinfo.result_callback = NULL;
  244. cinfo.userdata = NULL;
  245. cinfo.swap_result = false;
  246. cinfo.collided = false;
  247. cinfo.collisions = 0;
  248. cinfo.aabb_tests = 0;
  249. cinfo.tested = false;
  250. Transform rel_transform = p_transform_A;
  251. rel_transform.origin -= p_transform_B.origin;
  252. //quickly compute a local AABB
  253. bool use_cc_hint = p_concave_hint != AABB();
  254. AABB cc_hint_aabb;
  255. if (use_cc_hint) {
  256. cc_hint_aabb = p_concave_hint;
  257. cc_hint_aabb.position -= p_transform_B.origin;
  258. }
  259. AABB local_aabb;
  260. for (int i = 0; i < 3; i++) {
  261. Vector3 axis(p_transform_B.basis.get_axis(i));
  262. real_t axis_scale = ((real_t)1.0) / axis.length();
  263. axis *= axis_scale;
  264. real_t smin, smax;
  265. if (use_cc_hint) {
  266. cc_hint_aabb.project_range_in_plane(Plane(axis, 0), smin, smax);
  267. } else {
  268. p_shape_A->project_range(axis, rel_transform, smin, smax);
  269. }
  270. smin *= axis_scale;
  271. smax *= axis_scale;
  272. local_aabb.position[i] = smin;
  273. local_aabb.size[i] = smax - smin;
  274. }
  275. concave_B->cull(local_aabb, concave_distance_callback, &cinfo);
  276. if (!cinfo.collided) {
  277. r_point_A = cinfo.close_A;
  278. r_point_B = cinfo.close_B;
  279. }
  280. return !cinfo.collided;
  281. } else {
  282. 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..
  283. }
  284. return false;
  285. }