2
0

godot_collision_solver_2d.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "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, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result) {
  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_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 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) {
  65. const GodotSeparationRayShape2D *ray = static_cast<const GodotSeparationRayShape2D *>(p_shape_A);
  66. if (p_shape_B->get_type() == PhysicsServer2D::SHAPE_SEPARATION_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 (r_sep_axis) {
  83. *r_sep_axis = p_transform_A[1].normalized();
  84. }
  85. return false;
  86. }
  87. // Discard contacts when the ray is fully contained inside the shape.
  88. if (n == Vector2()) {
  89. if (r_sep_axis) {
  90. *r_sep_axis = p_transform_A[1].normalized();
  91. }
  92. return false;
  93. }
  94. // Discard contacts in the wrong direction.
  95. if (n.dot(from - to) < CMP_EPSILON) {
  96. if (r_sep_axis) {
  97. *r_sep_axis = p_transform_A[1].normalized();
  98. }
  99. return false;
  100. }
  101. Vector2 support_B = p_transform_B.xform(p);
  102. if (ray->get_slide_on_slope()) {
  103. Vector2 global_n = invb.basis_xform_inv(n).normalized();
  104. support_B = support_A + (support_B - support_A).length() * global_n;
  105. }
  106. if (p_result_callback) {
  107. if (p_swap_result) {
  108. p_result_callback(support_B, support_A, p_userdata);
  109. } else {
  110. p_result_callback(support_A, support_B, p_userdata);
  111. }
  112. }
  113. return true;
  114. }
  115. struct _ConcaveCollisionInfo2D {
  116. const Transform2D *transform_A = nullptr;
  117. const GodotShape2D *shape_A = nullptr;
  118. const Transform2D *transform_B = nullptr;
  119. Vector2 motion_A;
  120. Vector2 motion_B;
  121. real_t margin_A = 0.0;
  122. real_t margin_B = 0.0;
  123. GodotCollisionSolver2D::CallbackResult result_callback;
  124. void *userdata = nullptr;
  125. bool swap_result = false;
  126. bool collided = false;
  127. int aabb_tests = 0;
  128. int collisions = 0;
  129. Vector2 *sep_axis = nullptr;
  130. };
  131. bool GodotCollisionSolver2D::concave_callback(void *p_userdata, GodotShape2D *p_convex) {
  132. _ConcaveCollisionInfo2D &cinfo = *(_ConcaveCollisionInfo2D *)(p_userdata);
  133. cinfo.aabb_tests++;
  134. 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);
  135. if (!collided) {
  136. return false;
  137. }
  138. cinfo.collided = true;
  139. cinfo.collisions++;
  140. // Stop at first collision if contacts are not needed.
  141. return !cinfo.result_callback;
  142. }
  143. 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) {
  144. const GodotConcaveShape2D *concave_B = static_cast<const GodotConcaveShape2D *>(p_shape_B);
  145. _ConcaveCollisionInfo2D cinfo;
  146. cinfo.transform_A = &p_transform_A;
  147. cinfo.shape_A = p_shape_A;
  148. cinfo.transform_B = &p_transform_B;
  149. cinfo.motion_A = p_motion_A;
  150. cinfo.result_callback = p_result_callback;
  151. cinfo.userdata = p_userdata;
  152. cinfo.swap_result = p_swap_result;
  153. cinfo.collided = false;
  154. cinfo.collisions = 0;
  155. cinfo.sep_axis = r_sep_axis;
  156. cinfo.margin_A = p_margin_A;
  157. cinfo.margin_B = p_margin_B;
  158. cinfo.aabb_tests = 0;
  159. Transform2D rel_transform = p_transform_A;
  160. rel_transform.elements[2] -= p_transform_B.get_origin();
  161. //quickly compute a local Rect2
  162. Rect2 local_aabb;
  163. for (int i = 0; i < 2; i++) {
  164. Vector2 axis(p_transform_B.elements[i]);
  165. real_t axis_scale = 1.0 / axis.length();
  166. axis *= axis_scale;
  167. real_t smin, smax;
  168. p_shape_A->project_rangev(axis, rel_transform, smin, smax);
  169. smin *= axis_scale;
  170. smax *= axis_scale;
  171. local_aabb.position[i] = smin;
  172. local_aabb.size[i] = smax - smin;
  173. }
  174. concave_B->cull(local_aabb, concave_callback, &cinfo);
  175. return cinfo.collided;
  176. }
  177. 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) {
  178. PhysicsServer2D::ShapeType type_A = p_shape_A->get_type();
  179. PhysicsServer2D::ShapeType type_B = p_shape_B->get_type();
  180. bool concave_A = p_shape_A->is_concave();
  181. bool concave_B = p_shape_B->is_concave();
  182. real_t margin_A = p_margin_A, margin_B = p_margin_B;
  183. bool swap = false;
  184. if (type_A > type_B) {
  185. SWAP(type_A, type_B);
  186. SWAP(concave_A, concave_B);
  187. SWAP(margin_A, margin_B);
  188. swap = true;
  189. }
  190. if (type_A == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
  191. if (type_B == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {
  192. return false;
  193. }
  194. if (swap) {
  195. return solve_static_world_boundary(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
  196. } else {
  197. return solve_static_world_boundary(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
  198. }
  199. } else if (type_A == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
  200. if (type_B == PhysicsServer2D::SHAPE_SEPARATION_RAY) {
  201. return false; //no ray-ray
  202. }
  203. if (swap) {
  204. 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);
  205. } else {
  206. 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);
  207. }
  208. } else if (concave_B) {
  209. if (concave_A) {
  210. return false;
  211. }
  212. if (!swap) {
  213. 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);
  214. } else {
  215. 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);
  216. }
  217. } else {
  218. 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);
  219. }
  220. }