godot_collision_solver_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**************************************************************************/
  2. /* godot_collision_solver_2d.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 "godot_collision_solver_2d.h"
  31. #include "godot_collision_solver_2d_sat.h"
  32. #define collision_solver sat_2d_calculate_penetration
  33. //#define collision_solver gjk_epa_calculate_penetration
  34. bool GodotCollisionSolver2D::solve_static_world_boundary(const GodotShape2D *p_shape_A, const Transform2D &p_transform_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, real_t p_margin) {
  35. const GodotWorldBoundaryShape2D *world_boundary = static_cast<const GodotWorldBoundaryShape2D *>(p_shape_A);
  36. if (p_shape_B->get_type() == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
  37. return false;
  38. }
  39. Vector2 n = p_transform_A.basis_xform(world_boundary->get_normal()).normalized();
  40. Vector2 p = p_transform_A.xform(world_boundary->get_normal() * world_boundary->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_margin * supports[i].normalized();
  48. supports[i] = p_transform_B.xform(supports[i]);
  49. supports[i] += p_motion_B;
  50. real_t pd = n.dot(supports[i]);
  51. if (pd >= d) {
  52. continue;
  53. }
  54. found = true;
  55. Vector2 support_A = supports[i] - n * (pd - d);
  56. if (p_result_callback) {
  57. if (p_swap_result) {
  58. p_result_callback(supports[i], support_A, p_userdata);
  59. } else {
  60. p_result_callback(support_A, supports[i], p_userdata);
  61. }
  62. }
  63. }
  64. return found;
  65. }
  66. bool GodotCollisionSolver2D::solve_separation_ray(const GodotShape2D *p_shape_A, const Vector2 &p_motion_A, const Transform2D &p_transform_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, Vector2 *r_sep_axis, real_t p_margin) {
  67. const GodotSeparationRayShape2D *ray = static_cast<const GodotSeparationRayShape2D *>(p_shape_A);
  68. if (p_shape_B->get_type() == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
  69. return false;
  70. }
  71. Vector2 from = p_transform_A.get_origin();
  72. Vector2 to = from + p_transform_A[1] * (ray->get_length() + p_margin);
  73. if (p_motion_A != Vector2()) {
  74. //not the best but should be enough
  75. Vector2 normal = (to - from).normalized();
  76. to += normal * MAX(0.0, normal.dot(p_motion_A));
  77. }
  78. Vector2 support_A = to;
  79. Transform2D invb = p_transform_B.affine_inverse();
  80. from = invb.xform(from);
  81. to = invb.xform(to);
  82. Vector2 p, n;
  83. if (!p_shape_B->intersect_segment(from, to, p, n)) {
  84. if (r_sep_axis) {
  85. *r_sep_axis = p_transform_A[1].normalized();
  86. }
  87. return false;
  88. }
  89. // Discard contacts when the ray is fully contained inside the shape.
  90. if (n == Vector2()) {
  91. if (r_sep_axis) {
  92. *r_sep_axis = p_transform_A[1].normalized();
  93. }
  94. return false;
  95. }
  96. // Discard contacts in the wrong direction.
  97. if (n.dot(from - to) < CMP_EPSILON) {
  98. if (r_sep_axis) {
  99. *r_sep_axis = p_transform_A[1].normalized();
  100. }
  101. return false;
  102. }
  103. Vector2 support_B = p_transform_B.xform(p);
  104. if (ray->get_slide_on_slope()) {
  105. Vector2 global_n = invb.basis_xform_inv(n).normalized();
  106. support_B = support_A + (support_B - support_A).length() * global_n;
  107. }
  108. if (p_result_callback) {
  109. if (p_swap_result) {
  110. p_result_callback(support_B, support_A, p_userdata);
  111. } else {
  112. p_result_callback(support_A, support_B, p_userdata);
  113. }
  114. }
  115. return true;
  116. }
  117. struct _ConcaveCollisionInfo2D {
  118. const Transform2D *transform_A = nullptr;
  119. const GodotShape2D *shape_A = nullptr;
  120. const Transform2D *transform_B = nullptr;
  121. Vector2 motion_A;
  122. Vector2 motion_B;
  123. real_t margin_A = 0.0;
  124. real_t margin_B = 0.0;
  125. GodotCollisionSolver2D::CallbackResult result_callback = nullptr;
  126. void *userdata = nullptr;
  127. bool swap_result = false;
  128. bool collided = false;
  129. int aabb_tests = 0;
  130. int collisions = 0;
  131. Vector2 *sep_axis = nullptr;
  132. };
  133. bool GodotCollisionSolver2D::concave_callback(void *p_userdata, GodotShape2D *p_convex) {
  134. _ConcaveCollisionInfo2D &cinfo = *(static_cast<_ConcaveCollisionInfo2D *>(p_userdata));
  135. cinfo.aabb_tests++;
  136. 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);
  137. if (!collided) {
  138. return false;
  139. }
  140. cinfo.collided = true;
  141. cinfo.collisions++;
  142. // Stop at first collision if contacts are not needed.
  143. return !cinfo.result_callback;
  144. }
  145. bool GodotCollisionSolver2D::solve_concave(const GodotShape2D *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const GodotShape2D *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 *r_sep_axis, real_t p_margin_A, real_t p_margin_B) {
  146. const GodotConcaveShape2D *concave_B = static_cast<const GodotConcaveShape2D *>(p_shape_B);
  147. _ConcaveCollisionInfo2D cinfo;
  148. cinfo.transform_A = &p_transform_A;
  149. cinfo.shape_A = p_shape_A;
  150. cinfo.transform_B = &p_transform_B;
  151. cinfo.motion_A = p_motion_A;
  152. cinfo.result_callback = p_result_callback;
  153. cinfo.userdata = p_userdata;
  154. cinfo.swap_result = p_swap_result;
  155. cinfo.collided = false;
  156. cinfo.collisions = 0;
  157. cinfo.sep_axis = r_sep_axis;
  158. cinfo.margin_A = p_margin_A;
  159. cinfo.margin_B = p_margin_B;
  160. cinfo.aabb_tests = 0;
  161. Transform2D rel_transform = p_transform_A;
  162. rel_transform.columns[2] -= p_transform_B.get_origin();
  163. // Quickly compute a local Rect2.
  164. Rect2 local_aabb;
  165. for (int i = 0; i < 2; i++) {
  166. Vector2 axis(p_transform_B.columns[i]);
  167. real_t axis_scale = 1.0 / axis.length();
  168. axis *= axis_scale;
  169. real_t smin = 0.0, smax = 0.0;
  170. p_shape_A->project_rangev(axis, rel_transform, smin, smax);
  171. smin *= axis_scale;
  172. smax *= axis_scale;
  173. local_aabb.position[i] = smin;
  174. local_aabb.size[i] = smax - smin;
  175. }
  176. // In case of motion, expand the Rect2 in the motion direction.
  177. if (p_motion_A != Vector2()) {
  178. Rect2 moved_aabb = local_aabb;
  179. moved_aabb.position += p_motion_A;
  180. local_aabb = local_aabb.merge(moved_aabb);
  181. }
  182. concave_B->cull(local_aabb, concave_callback, &cinfo);
  183. return cinfo.collided;
  184. }
  185. bool GodotCollisionSolver2D::solve(const GodotShape2D *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, Vector2 *r_sep_axis, real_t p_margin_A, real_t p_margin_B) {
  186. PhysicsServer2D::ShapeType type_A = p_shape_A->get_type();
  187. PhysicsServer2D::ShapeType type_B = p_shape_B->get_type();
  188. bool concave_A = p_shape_A->is_concave();
  189. bool concave_B = p_shape_B->is_concave();
  190. real_t margin_A = p_margin_A, margin_B = p_margin_B;
  191. bool swap = false;
  192. if (type_A > type_B) {
  193. SWAP(type_A, type_B);
  194. SWAP(concave_A, concave_B);
  195. SWAP(margin_A, margin_B);
  196. swap = true;
  197. }
  198. if (type_A == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
  199. if (type_B == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
  200. WARN_PRINT_ONCE("Collisions between world boundaries are not supported.");
  201. return false;
  202. }
  203. if (swap) {
  204. return solve_static_world_boundary(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_motion_A, p_result_callback, p_userdata, true, p_margin_A);
  205. } else {
  206. return solve_static_world_boundary(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, p_margin_B);
  207. }
  208. } else if (type_A == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
  209. if (type_B == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
  210. WARN_PRINT_ONCE("Collisions between two rays are not supported.");
  211. return false; //no ray-ray
  212. }
  213. if (swap) {
  214. return solve_separation_ray(p_shape_B, p_motion_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, r_sep_axis, p_margin_B);
  215. } else {
  216. return solve_separation_ray(p_shape_A, p_motion_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, r_sep_axis, p_margin_A);
  217. }
  218. } else if (concave_B) {
  219. if (concave_A) {
  220. WARN_PRINT_ONCE("Collisions between two concave shapes are not supported.");
  221. return false;
  222. }
  223. if (!swap) {
  224. 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, r_sep_axis, margin_A, margin_B);
  225. } else {
  226. 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, r_sep_axis, margin_A, margin_B);
  227. }
  228. } else {
  229. 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, r_sep_axis, margin_A, margin_B);
  230. }
  231. }