collision_solver_2d_sw.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*************************************************************************/
  2. /* collision_solver_2d_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_2d_sw.h"
  31. #include "collision_solver_2d_sat.h"
  32. #define collision_solver sat_2d_calculate_penetration
  33. //#define collision_solver gjk_epa_calculate_penetration
  34. bool CollisionSolver2DSW::solve_static_line(const Shape2DSW *p_shape_A, const Transform2D &p_transform_A, const Shape2DSW *p_shape_B, const Transform2D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result) {
  35. const LineShape2DSW *line = static_cast<const LineShape2DSW *>(p_shape_A);
  36. if (p_shape_B->get_type() == Physics2DServer::SHAPE_LINE)
  37. return false;
  38. Vector2 n = p_transform_A.basis_xform(line->get_normal()).normalized();
  39. Vector2 p = p_transform_A.xform(line->get_normal() * line->get_d());
  40. real_t d = n.dot(p);
  41. Vector2 supports[2];
  42. int support_count;
  43. p_shape_B->get_supports(p_transform_A.affine_inverse().basis_xform(-n).normalized(), 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. real_t pd = n.dot(supports[i]);
  48. if (pd >= d)
  49. continue;
  50. found = true;
  51. Vector2 support_A = supports[i] - n * (pd - d);
  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 CollisionSolver2DSW::solve_raycast(const Shape2DSW *p_shape_A, const Transform2D &p_transform_A, const Shape2DSW *p_shape_B, const Transform2D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, Vector2 *sep_axis) {
  62. const RayShape2DSW *ray = static_cast<const RayShape2DSW *>(p_shape_A);
  63. if (p_shape_B->get_type() == Physics2DServer::SHAPE_RAY)
  64. return false;
  65. Vector2 from = p_transform_A.get_origin();
  66. Vector2 to = from + p_transform_A[1] * ray->get_length();
  67. Vector2 support_A = to;
  68. Transform2D invb = p_transform_B.affine_inverse();
  69. from = invb.xform(from);
  70. to = invb.xform(to);
  71. Vector2 p, n;
  72. if (!p_shape_B->intersect_segment(from, to, p, n)) {
  73. if (sep_axis)
  74. *sep_axis = p_transform_A[1].normalized();
  75. return false;
  76. }
  77. Vector2 support_B = p_transform_B.xform(p);
  78. if (p_result_callback) {
  79. if (p_swap_result)
  80. p_result_callback(support_B, support_A, p_userdata);
  81. else
  82. p_result_callback(support_A, support_B, p_userdata);
  83. }
  84. return true;
  85. }
  86. /*
  87. bool CollisionSolver2DSW::solve_ray(const Shape2DSW *p_shape_A,const Matrix32& p_transform_A,const Shape2DSW *p_shape_B,const Matrix32& p_transform_B,const Matrix32& p_inverse_B,CallbackResult p_result_callback,void *p_userdata,bool p_swap_result) {
  88. const RayShape2DSW *ray = static_cast<const RayShape2DSW*>(p_shape_A);
  89. Vector2 from = p_transform_A.origin;
  90. Vector2 to = from+p_transform_A.basis.get_axis(2)*ray->get_length();
  91. Vector2 support_A=to;
  92. from = p_inverse_B.xform(from);
  93. to = p_inverse_B.xform(to);
  94. Vector2 p,n;
  95. if (!p_shape_B->intersect_segment(from,to,&p,&n))
  96. return false;
  97. Vector2 support_B=p_transform_B.xform(p);
  98. if (p_result_callback) {
  99. if (p_swap_result)
  100. p_result_callback(support_B,support_A,p_userdata);
  101. else
  102. p_result_callback(support_A,support_B,p_userdata);
  103. }
  104. return true;
  105. }
  106. */
  107. struct _ConcaveCollisionInfo2D {
  108. const Transform2D *transform_A;
  109. const Shape2DSW *shape_A;
  110. const Transform2D *transform_B;
  111. Vector2 motion_A;
  112. Vector2 motion_B;
  113. real_t margin_A;
  114. real_t margin_B;
  115. CollisionSolver2DSW::CallbackResult result_callback;
  116. void *userdata;
  117. bool swap_result;
  118. bool collided;
  119. int aabb_tests;
  120. int collisions;
  121. Vector2 *sep_axis;
  122. };
  123. void CollisionSolver2DSW::concave_callback(void *p_userdata, Shape2DSW *p_convex) {
  124. _ConcaveCollisionInfo2D &cinfo = *(_ConcaveCollisionInfo2D *)(p_userdata);
  125. cinfo.aabb_tests++;
  126. if (!cinfo.result_callback && cinfo.collided)
  127. return; //already collided and no contacts requested, don't test anymore
  128. bool collided = collision_solver(cinfo.shape_A, *cinfo.transform_A, cinfo.motion_A, p_convex, *cinfo.transform_B, cinfo.motion_B, cinfo.result_callback, cinfo.userdata, cinfo.swap_result, cinfo.sep_axis, cinfo.margin_A, cinfo.margin_B);
  129. if (!collided)
  130. return;
  131. cinfo.collided = true;
  132. cinfo.collisions++;
  133. }
  134. bool CollisionSolver2DSW::solve_concave(const Shape2DSW *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const Shape2DSW *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, Vector2 *sep_axis, real_t p_margin_A, real_t p_margin_B) {
  135. const ConcaveShape2DSW *concave_B = static_cast<const ConcaveShape2DSW *>(p_shape_B);
  136. _ConcaveCollisionInfo2D cinfo;
  137. cinfo.transform_A = &p_transform_A;
  138. cinfo.shape_A = p_shape_A;
  139. cinfo.transform_B = &p_transform_B;
  140. cinfo.motion_A = p_motion_A;
  141. cinfo.result_callback = p_result_callback;
  142. cinfo.userdata = p_userdata;
  143. cinfo.swap_result = p_swap_result;
  144. cinfo.collided = false;
  145. cinfo.collisions = 0;
  146. cinfo.sep_axis = sep_axis;
  147. cinfo.margin_A = p_margin_A;
  148. cinfo.margin_B = p_margin_B;
  149. cinfo.aabb_tests = 0;
  150. Transform2D rel_transform = p_transform_A;
  151. rel_transform.elements[2] -= p_transform_B.get_origin();
  152. //quickly compute a local Rect2
  153. Rect2 local_aabb;
  154. for (int i = 0; i < 2; i++) {
  155. Vector2 axis(p_transform_B.elements[i]);
  156. real_t axis_scale = 1.0 / axis.length();
  157. axis *= axis_scale;
  158. real_t smin, smax;
  159. p_shape_A->project_rangev(axis, rel_transform, smin, smax);
  160. smin *= axis_scale;
  161. smax *= axis_scale;
  162. local_aabb.position[i] = smin;
  163. local_aabb.size[i] = smax - smin;
  164. }
  165. concave_B->cull(local_aabb, concave_callback, &cinfo);
  166. //print_line("Rect2 TESTS: "+itos(cinfo.aabb_tests));
  167. return cinfo.collided;
  168. }
  169. bool CollisionSolver2DSW::solve(const Shape2DSW *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const Shape2DSW *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, Vector2 *sep_axis, real_t p_margin_A, real_t p_margin_B) {
  170. Physics2DServer::ShapeType type_A = p_shape_A->get_type();
  171. Physics2DServer::ShapeType type_B = p_shape_B->get_type();
  172. bool concave_A = p_shape_A->is_concave();
  173. bool concave_B = p_shape_B->is_concave();
  174. real_t margin_A = p_margin_A, margin_B = p_margin_B;
  175. bool swap = false;
  176. if (type_A > type_B) {
  177. SWAP(type_A, type_B);
  178. SWAP(concave_A, concave_B);
  179. SWAP(margin_A, margin_B);
  180. swap = true;
  181. }
  182. if (type_A == Physics2DServer::SHAPE_LINE) {
  183. if (type_B == Physics2DServer::SHAPE_LINE || type_B == Physics2DServer::SHAPE_RAY) {
  184. return false;
  185. }
  186. /*
  187. if (type_B==Physics2DServer::SHAPE_RAY) {
  188. return false;
  189. */
  190. if (swap) {
  191. return solve_static_line(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  192. } else {
  193. return solve_static_line(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  194. }
  195. /*} else if (type_A==Physics2DServer::SHAPE_RAY) {
  196. if (type_B==Physics2DServer::SHAPE_RAY)
  197. return false;
  198. if (swap) {
  199. return solve_ray(p_shape_B,p_transform_B,p_shape_A,p_transform_A,p_inverse_A,p_result_callback,p_userdata,true);
  200. } else {
  201. return solve_ray(p_shape_A,p_transform_A,p_shape_B,p_transform_B,p_inverse_B,p_result_callback,p_userdata,false);
  202. }
  203. */
  204. } else if (type_A == Physics2DServer::SHAPE_RAY) {
  205. if (type_B == Physics2DServer::SHAPE_RAY) {
  206. return false; //no ray-ray
  207. }
  208. if (swap) {
  209. return solve_raycast(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, sep_axis);
  210. } else {
  211. return solve_raycast(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, sep_axis);
  212. }
  213. } else if (concave_B) {
  214. if (concave_A)
  215. return false;
  216. if (!swap)
  217. return solve_concave(p_shape_A, p_transform_A, p_motion_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, sep_axis, margin_A, margin_B);
  218. else
  219. return solve_concave(p_shape_B, p_transform_B, p_motion_B, p_shape_A, p_transform_A, p_motion_A, p_result_callback, p_userdata, true, sep_axis, margin_A, margin_B);
  220. } else {
  221. return collision_solver(p_shape_A, p_transform_A, p_motion_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, sep_axis, margin_A, margin_B);
  222. }
  223. return false;
  224. }