collision_solver_2d_sw.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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) 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_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. }
  39. Vector2 n = p_transform_A.basis_xform(line->get_normal()).normalized();
  40. Vector2 p = p_transform_A.xform(line->get_normal() * line->get_d());
  41. real_t d = n.dot(p);
  42. Vector2 supports[2];
  43. int support_count;
  44. p_shape_B->get_supports(p_transform_B.affine_inverse().basis_xform(-n).normalized(), 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. real_t pd = n.dot(supports[i]);
  49. if (pd >= d) {
  50. continue;
  51. }
  52. found = true;
  53. Vector2 support_A = supports[i] - n * (pd - d);
  54. if (p_result_callback) {
  55. if (p_swap_result) {
  56. p_result_callback(supports[i], support_A, p_userdata);
  57. } else {
  58. p_result_callback(support_A, supports[i], p_userdata);
  59. }
  60. }
  61. }
  62. return found;
  63. }
  64. 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, real_t p_margin) {
  65. const RayShape2DSW *ray = static_cast<const RayShape2DSW *>(p_shape_A);
  66. if (p_shape_B->get_type() == Physics2DServer::SHAPE_RAY) {
  67. return false;
  68. }
  69. Vector2 from = p_transform_A.get_origin();
  70. Vector2 to = from + p_transform_A[1] * (ray->get_length() + p_margin);
  71. if (p_motion_A != Vector2()) {
  72. //not the best but should be enough
  73. Vector2 normal = (to - from).normalized();
  74. to += normal * MAX(0.0, normal.dot(p_motion_A));
  75. }
  76. Vector2 support_A = to;
  77. Transform2D invb = p_transform_B.affine_inverse();
  78. from = invb.xform(from);
  79. to = invb.xform(to);
  80. Vector2 p, n;
  81. if (!p_shape_B->intersect_segment(from, to, p, n)) {
  82. if (sep_axis) {
  83. *sep_axis = p_transform_A[1].normalized();
  84. }
  85. return false;
  86. }
  87. Vector2 support_B = p_transform_B.xform(p);
  88. if (ray->get_slips_on_slope()) {
  89. Vector2 global_n = invb.basis_xform_inv(n).normalized();
  90. support_B = support_A + (support_B - support_A).length() * global_n;
  91. }
  92. if (p_result_callback) {
  93. if (p_swap_result) {
  94. p_result_callback(support_B, support_A, p_userdata);
  95. } else {
  96. p_result_callback(support_A, support_B, p_userdata);
  97. }
  98. }
  99. return true;
  100. }
  101. struct _ConcaveCollisionInfo2D {
  102. const Transform2D *transform_A;
  103. const Shape2DSW *shape_A;
  104. const Transform2D *transform_B;
  105. Vector2 motion_A;
  106. Vector2 motion_B;
  107. real_t margin_A;
  108. real_t margin_B;
  109. CollisionSolver2DSW::CallbackResult result_callback;
  110. void *userdata;
  111. bool swap_result;
  112. bool collided;
  113. int aabb_tests;
  114. int collisions;
  115. Vector2 *sep_axis;
  116. };
  117. bool CollisionSolver2DSW::concave_callback(void *p_userdata, Shape2DSW *p_convex) {
  118. _ConcaveCollisionInfo2D &cinfo = *(_ConcaveCollisionInfo2D *)(p_userdata);
  119. cinfo.aabb_tests++;
  120. 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);
  121. if (!collided) {
  122. return false;
  123. }
  124. cinfo.collided = true;
  125. cinfo.collisions++;
  126. // Stop at first collision if contacts are not needed.
  127. return !cinfo.result_callback;
  128. }
  129. 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) {
  130. const ConcaveShape2DSW *concave_B = static_cast<const ConcaveShape2DSW *>(p_shape_B);
  131. _ConcaveCollisionInfo2D cinfo;
  132. cinfo.transform_A = &p_transform_A;
  133. cinfo.shape_A = p_shape_A;
  134. cinfo.transform_B = &p_transform_B;
  135. cinfo.motion_A = p_motion_A;
  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.sep_axis = sep_axis;
  142. cinfo.margin_A = p_margin_A;
  143. cinfo.margin_B = p_margin_B;
  144. cinfo.aabb_tests = 0;
  145. Transform2D rel_transform = p_transform_A;
  146. rel_transform.elements[2] -= p_transform_B.get_origin();
  147. //quickly compute a local Rect2
  148. Rect2 local_aabb;
  149. for (int i = 0; i < 2; i++) {
  150. Vector2 axis(p_transform_B.elements[i]);
  151. real_t axis_scale = 1.0 / axis.length();
  152. axis *= axis_scale;
  153. real_t smin, smax;
  154. p_shape_A->project_rangev(axis, rel_transform, smin, smax);
  155. smin *= axis_scale;
  156. smax *= axis_scale;
  157. local_aabb.position[i] = smin;
  158. local_aabb.size[i] = smax - smin;
  159. }
  160. concave_B->cull(local_aabb, concave_callback, &cinfo);
  161. return cinfo.collided;
  162. }
  163. 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) {
  164. Physics2DServer::ShapeType type_A = p_shape_A->get_type();
  165. Physics2DServer::ShapeType type_B = p_shape_B->get_type();
  166. bool concave_A = p_shape_A->is_concave();
  167. bool concave_B = p_shape_B->is_concave();
  168. real_t margin_A = p_margin_A, margin_B = p_margin_B;
  169. bool swap = false;
  170. if (type_A > type_B) {
  171. SWAP(type_A, type_B);
  172. SWAP(concave_A, concave_B);
  173. SWAP(margin_A, margin_B);
  174. swap = true;
  175. }
  176. if (type_A == Physics2DServer::SHAPE_LINE) {
  177. if (type_B == Physics2DServer::SHAPE_LINE || type_B == Physics2DServer::SHAPE_RAY) {
  178. return false;
  179. }
  180. if (swap) {
  181. return solve_static_line(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  182. } else {
  183. return solve_static_line(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  184. }
  185. } else if (type_A == Physics2DServer::SHAPE_RAY) {
  186. if (type_B == Physics2DServer::SHAPE_RAY) {
  187. return false; //no ray-ray
  188. }
  189. if (swap) {
  190. 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, p_margin_B);
  191. } else {
  192. 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, p_margin_A);
  193. }
  194. } else if (concave_B) {
  195. if (concave_A) {
  196. return false;
  197. }
  198. if (!swap) {
  199. 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);
  200. } else {
  201. 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);
  202. }
  203. } else {
  204. 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);
  205. }
  206. }