rect2.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*************************************************************************/
  2. /* rect2.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "core/math/transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D
  31. bool Rect2::is_equal_approx(const Rect2 &p_rect) const {
  32. return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size);
  33. }
  34. bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
  35. real_t min = 0, max = 1;
  36. int axis = 0;
  37. real_t sign = 0;
  38. for (int i = 0; i < 2; i++) {
  39. real_t seg_from = p_from[i];
  40. real_t seg_to = p_to[i];
  41. real_t box_begin = position[i];
  42. real_t box_end = box_begin + size[i];
  43. real_t cmin, cmax;
  44. real_t csign;
  45. if (seg_from < seg_to) {
  46. if (seg_from > box_end || seg_to < box_begin)
  47. return false;
  48. real_t length = seg_to - seg_from;
  49. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  50. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  51. csign = -1.0;
  52. } else {
  53. if (seg_to > box_end || seg_from < box_begin)
  54. return false;
  55. real_t length = seg_to - seg_from;
  56. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  57. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  58. csign = 1.0;
  59. }
  60. if (cmin > min) {
  61. min = cmin;
  62. axis = i;
  63. sign = csign;
  64. }
  65. if (cmax < max)
  66. max = cmax;
  67. if (max < min)
  68. return false;
  69. }
  70. Vector2 rel = p_to - p_from;
  71. if (r_normal) {
  72. Vector2 normal;
  73. normal[axis] = sign;
  74. *r_normal = normal;
  75. }
  76. if (r_pos)
  77. *r_pos = p_from + rel * min;
  78. return true;
  79. }
  80. bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
  81. //SAT intersection between local and transformed rect2
  82. Vector2 xf_points[4] = {
  83. p_xform.xform(p_rect.position),
  84. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
  85. p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  86. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  87. };
  88. real_t low_limit;
  89. //base rect2 first (faster)
  90. if (xf_points[0].y > position.y)
  91. goto next1;
  92. if (xf_points[1].y > position.y)
  93. goto next1;
  94. if (xf_points[2].y > position.y)
  95. goto next1;
  96. if (xf_points[3].y > position.y)
  97. goto next1;
  98. return false;
  99. next1:
  100. low_limit = position.y + size.y;
  101. if (xf_points[0].y < low_limit)
  102. goto next2;
  103. if (xf_points[1].y < low_limit)
  104. goto next2;
  105. if (xf_points[2].y < low_limit)
  106. goto next2;
  107. if (xf_points[3].y < low_limit)
  108. goto next2;
  109. return false;
  110. next2:
  111. if (xf_points[0].x > position.x)
  112. goto next3;
  113. if (xf_points[1].x > position.x)
  114. goto next3;
  115. if (xf_points[2].x > position.x)
  116. goto next3;
  117. if (xf_points[3].x > position.x)
  118. goto next3;
  119. return false;
  120. next3:
  121. low_limit = position.x + size.x;
  122. if (xf_points[0].x < low_limit)
  123. goto next4;
  124. if (xf_points[1].x < low_limit)
  125. goto next4;
  126. if (xf_points[2].x < low_limit)
  127. goto next4;
  128. if (xf_points[3].x < low_limit)
  129. goto next4;
  130. return false;
  131. next4:
  132. Vector2 xf_points2[4] = {
  133. position,
  134. Vector2(position.x + size.x, position.y),
  135. Vector2(position.x, position.y + size.y),
  136. Vector2(position.x + size.x, position.y + size.y),
  137. };
  138. real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
  139. real_t mina = maxa;
  140. real_t dp = p_xform.elements[0].dot(xf_points2[1]);
  141. maxa = MAX(dp, maxa);
  142. mina = MIN(dp, mina);
  143. dp = p_xform.elements[0].dot(xf_points2[2]);
  144. maxa = MAX(dp, maxa);
  145. mina = MIN(dp, mina);
  146. dp = p_xform.elements[0].dot(xf_points2[3]);
  147. maxa = MAX(dp, maxa);
  148. mina = MIN(dp, mina);
  149. real_t maxb = p_xform.elements[0].dot(xf_points[0]);
  150. real_t minb = maxb;
  151. dp = p_xform.elements[0].dot(xf_points[1]);
  152. maxb = MAX(dp, maxb);
  153. minb = MIN(dp, minb);
  154. dp = p_xform.elements[0].dot(xf_points[2]);
  155. maxb = MAX(dp, maxb);
  156. minb = MIN(dp, minb);
  157. dp = p_xform.elements[0].dot(xf_points[3]);
  158. maxb = MAX(dp, maxb);
  159. minb = MIN(dp, minb);
  160. if (mina > maxb)
  161. return false;
  162. if (minb > maxa)
  163. return false;
  164. maxa = p_xform.elements[1].dot(xf_points2[0]);
  165. mina = maxa;
  166. dp = p_xform.elements[1].dot(xf_points2[1]);
  167. maxa = MAX(dp, maxa);
  168. mina = MIN(dp, mina);
  169. dp = p_xform.elements[1].dot(xf_points2[2]);
  170. maxa = MAX(dp, maxa);
  171. mina = MIN(dp, mina);
  172. dp = p_xform.elements[1].dot(xf_points2[3]);
  173. maxa = MAX(dp, maxa);
  174. mina = MIN(dp, mina);
  175. maxb = p_xform.elements[1].dot(xf_points[0]);
  176. minb = maxb;
  177. dp = p_xform.elements[1].dot(xf_points[1]);
  178. maxb = MAX(dp, maxb);
  179. minb = MIN(dp, minb);
  180. dp = p_xform.elements[1].dot(xf_points[2]);
  181. maxb = MAX(dp, maxb);
  182. minb = MIN(dp, minb);
  183. dp = p_xform.elements[1].dot(xf_points[3]);
  184. maxb = MAX(dp, maxb);
  185. minb = MIN(dp, minb);
  186. if (mina > maxb)
  187. return false;
  188. if (minb > maxa)
  189. return false;
  190. return true;
  191. }