collision_solver_2d_sw.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 Vector2 &p_motion_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. if (p_motion_A != Vector2()) {
  68. //not the best but should be enough
  69. Vector2 normal = (to - from).normalized();
  70. to += normal * MAX(0.0, normal.dot(p_motion_A));
  71. }
  72. Vector2 support_A = to;
  73. Transform2D invb = p_transform_B.affine_inverse();
  74. from = invb.xform(from);
  75. to = invb.xform(to);
  76. Vector2 p, n;
  77. if (!p_shape_B->intersect_segment(from, to, p, n)) {
  78. if (sep_axis)
  79. *sep_axis = p_transform_A[1].normalized();
  80. return false;
  81. }
  82. Vector2 support_B = p_transform_B.xform(p);
  83. if (ray->get_slips_on_slope()) {
  84. Vector2 global_n = invb.basis_xform_inv(n).normalized();
  85. support_B = support_A + (support_B - support_A).length() * global_n;
  86. }
  87. if (p_result_callback) {
  88. if (p_swap_result)
  89. p_result_callback(support_B, support_A, p_userdata);
  90. else
  91. p_result_callback(support_A, support_B, p_userdata);
  92. }
  93. return true;
  94. }
  95. struct _ConcaveCollisionInfo2D {
  96. const Transform2D *transform_A;
  97. const Shape2DSW *shape_A;
  98. const Transform2D *transform_B;
  99. Vector2 motion_A;
  100. Vector2 motion_B;
  101. real_t margin_A;
  102. real_t margin_B;
  103. CollisionSolver2DSW::CallbackResult result_callback;
  104. void *userdata;
  105. bool swap_result;
  106. bool collided;
  107. int aabb_tests;
  108. int collisions;
  109. Vector2 *sep_axis;
  110. };
  111. void CollisionSolver2DSW::concave_callback(void *p_userdata, Shape2DSW *p_convex) {
  112. _ConcaveCollisionInfo2D &cinfo = *(_ConcaveCollisionInfo2D *)(p_userdata);
  113. cinfo.aabb_tests++;
  114. if (!cinfo.result_callback && cinfo.collided)
  115. return; //already collided and no contacts requested, don't test anymore
  116. 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);
  117. if (!collided)
  118. return;
  119. cinfo.collided = true;
  120. cinfo.collisions++;
  121. }
  122. 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) {
  123. const ConcaveShape2DSW *concave_B = static_cast<const ConcaveShape2DSW *>(p_shape_B);
  124. _ConcaveCollisionInfo2D cinfo;
  125. cinfo.transform_A = &p_transform_A;
  126. cinfo.shape_A = p_shape_A;
  127. cinfo.transform_B = &p_transform_B;
  128. cinfo.motion_A = p_motion_A;
  129. cinfo.result_callback = p_result_callback;
  130. cinfo.userdata = p_userdata;
  131. cinfo.swap_result = p_swap_result;
  132. cinfo.collided = false;
  133. cinfo.collisions = 0;
  134. cinfo.sep_axis = sep_axis;
  135. cinfo.margin_A = p_margin_A;
  136. cinfo.margin_B = p_margin_B;
  137. cinfo.aabb_tests = 0;
  138. Transform2D rel_transform = p_transform_A;
  139. rel_transform.elements[2] -= p_transform_B.get_origin();
  140. //quickly compute a local Rect2
  141. Rect2 local_aabb;
  142. for (int i = 0; i < 2; i++) {
  143. Vector2 axis(p_transform_B.elements[i]);
  144. real_t axis_scale = 1.0 / axis.length();
  145. axis *= axis_scale;
  146. real_t smin, smax;
  147. p_shape_A->project_rangev(axis, rel_transform, smin, smax);
  148. smin *= axis_scale;
  149. smax *= axis_scale;
  150. local_aabb.position[i] = smin;
  151. local_aabb.size[i] = smax - smin;
  152. }
  153. concave_B->cull(local_aabb, concave_callback, &cinfo);
  154. return cinfo.collided;
  155. }
  156. 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) {
  157. Physics2DServer::ShapeType type_A = p_shape_A->get_type();
  158. Physics2DServer::ShapeType type_B = p_shape_B->get_type();
  159. bool concave_A = p_shape_A->is_concave();
  160. bool concave_B = p_shape_B->is_concave();
  161. real_t margin_A = p_margin_A, margin_B = p_margin_B;
  162. bool swap = false;
  163. if (type_A > type_B) {
  164. SWAP(type_A, type_B);
  165. SWAP(concave_A, concave_B);
  166. SWAP(margin_A, margin_B);
  167. swap = true;
  168. }
  169. if (type_A == Physics2DServer::SHAPE_LINE) {
  170. if (type_B == Physics2DServer::SHAPE_LINE || type_B == Physics2DServer::SHAPE_RAY) {
  171. return false;
  172. }
  173. if (swap) {
  174. return solve_static_line(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  175. } else {
  176. return solve_static_line(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  177. }
  178. } else if (type_A == Physics2DServer::SHAPE_RAY) {
  179. if (type_B == Physics2DServer::SHAPE_RAY) {
  180. return false; //no ray-ray
  181. }
  182. if (swap) {
  183. return solve_raycast(p_shape_B, p_motion_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, sep_axis);
  184. } else {
  185. return solve_raycast(p_shape_A, p_motion_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, sep_axis);
  186. }
  187. } else if (concave_B) {
  188. if (concave_A)
  189. return false;
  190. if (!swap)
  191. 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);
  192. else
  193. 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);
  194. } else {
  195. 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);
  196. }
  197. return false;
  198. }