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