rect2.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* rect2.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_cpp/variant/rect2.hpp>
  31. #include <godot_cpp/variant/transform2d.hpp>
  32. namespace godot {
  33. bool Rect2::is_equal_approx(const Rect2 &p_rect) const {
  34. return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size);
  35. }
  36. bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
  37. real_t min = 0, max = 1;
  38. int axis = 0;
  39. real_t sign = 0;
  40. for (int i = 0; i < 2; i++) {
  41. real_t seg_from = p_from[i];
  42. real_t seg_to = p_to[i];
  43. real_t box_begin = position[i];
  44. real_t box_end = box_begin + size[i];
  45. real_t cmin, cmax;
  46. real_t csign;
  47. if (seg_from < seg_to) {
  48. if (seg_from > box_end || seg_to < box_begin) {
  49. return false;
  50. }
  51. real_t length = seg_to - seg_from;
  52. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  53. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  54. csign = -1.0;
  55. } else {
  56. if (seg_to > box_end || seg_from < box_begin) {
  57. return false;
  58. }
  59. real_t length = seg_to - seg_from;
  60. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  61. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  62. csign = 1.0;
  63. }
  64. if (cmin > min) {
  65. min = cmin;
  66. axis = i;
  67. sign = csign;
  68. }
  69. if (cmax < max) {
  70. max = cmax;
  71. }
  72. if (max < min) {
  73. return false;
  74. }
  75. }
  76. Vector2 rel = p_to - p_from;
  77. if (r_normal) {
  78. Vector2 normal;
  79. normal[axis] = sign;
  80. *r_normal = normal;
  81. }
  82. if (r_pos) {
  83. *r_pos = p_from + rel * min;
  84. }
  85. return true;
  86. }
  87. bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
  88. //SAT intersection between local and transformed rect2
  89. Vector2 xf_points[4] = {
  90. p_xform.xform(p_rect.position),
  91. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
  92. p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  93. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  94. };
  95. real_t low_limit;
  96. //base rect2 first (faster)
  97. if (xf_points[0].y > position.y) {
  98. goto next1;
  99. }
  100. if (xf_points[1].y > position.y) {
  101. goto next1;
  102. }
  103. if (xf_points[2].y > position.y) {
  104. goto next1;
  105. }
  106. if (xf_points[3].y > position.y) {
  107. goto next1;
  108. }
  109. return false;
  110. next1:
  111. low_limit = position.y + size.y;
  112. if (xf_points[0].y < low_limit) {
  113. goto next2;
  114. }
  115. if (xf_points[1].y < low_limit) {
  116. goto next2;
  117. }
  118. if (xf_points[2].y < low_limit) {
  119. goto next2;
  120. }
  121. if (xf_points[3].y < low_limit) {
  122. goto next2;
  123. }
  124. return false;
  125. next2:
  126. if (xf_points[0].x > position.x) {
  127. goto next3;
  128. }
  129. if (xf_points[1].x > position.x) {
  130. goto next3;
  131. }
  132. if (xf_points[2].x > position.x) {
  133. goto next3;
  134. }
  135. if (xf_points[3].x > position.x) {
  136. goto next3;
  137. }
  138. return false;
  139. next3:
  140. low_limit = position.x + size.x;
  141. if (xf_points[0].x < low_limit) {
  142. goto next4;
  143. }
  144. if (xf_points[1].x < low_limit) {
  145. goto next4;
  146. }
  147. if (xf_points[2].x < low_limit) {
  148. goto next4;
  149. }
  150. if (xf_points[3].x < low_limit) {
  151. goto next4;
  152. }
  153. return false;
  154. next4:
  155. Vector2 xf_points2[4] = {
  156. position,
  157. Vector2(position.x + size.x, position.y),
  158. Vector2(position.x, position.y + size.y),
  159. Vector2(position.x + size.x, position.y + size.y),
  160. };
  161. real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
  162. real_t mina = maxa;
  163. real_t dp = p_xform.elements[0].dot(xf_points2[1]);
  164. maxa = Math::max(dp, maxa);
  165. mina = Math::min(dp, mina);
  166. dp = p_xform.elements[0].dot(xf_points2[2]);
  167. maxa = Math::max(dp, maxa);
  168. mina = Math::min(dp, mina);
  169. dp = p_xform.elements[0].dot(xf_points2[3]);
  170. maxa = Math::max(dp, maxa);
  171. mina = Math::min(dp, mina);
  172. real_t maxb = p_xform.elements[0].dot(xf_points[0]);
  173. real_t minb = maxb;
  174. dp = p_xform.elements[0].dot(xf_points[1]);
  175. maxb = Math::max(dp, maxb);
  176. minb = Math::min(dp, minb);
  177. dp = p_xform.elements[0].dot(xf_points[2]);
  178. maxb = Math::max(dp, maxb);
  179. minb = Math::min(dp, minb);
  180. dp = p_xform.elements[0].dot(xf_points[3]);
  181. maxb = Math::max(dp, maxb);
  182. minb = Math::min(dp, minb);
  183. if (mina > maxb) {
  184. return false;
  185. }
  186. if (minb > maxa) {
  187. return false;
  188. }
  189. maxa = p_xform.elements[1].dot(xf_points2[0]);
  190. mina = maxa;
  191. dp = p_xform.elements[1].dot(xf_points2[1]);
  192. maxa = Math::max(dp, maxa);
  193. mina = Math::min(dp, mina);
  194. dp = p_xform.elements[1].dot(xf_points2[2]);
  195. maxa = Math::max(dp, maxa);
  196. mina = Math::min(dp, mina);
  197. dp = p_xform.elements[1].dot(xf_points2[3]);
  198. maxa = Math::max(dp, maxa);
  199. mina = Math::min(dp, mina);
  200. maxb = p_xform.elements[1].dot(xf_points[0]);
  201. minb = maxb;
  202. dp = p_xform.elements[1].dot(xf_points[1]);
  203. maxb = Math::max(dp, maxb);
  204. minb = Math::min(dp, minb);
  205. dp = p_xform.elements[1].dot(xf_points[2]);
  206. maxb = Math::max(dp, maxb);
  207. minb = Math::min(dp, minb);
  208. dp = p_xform.elements[1].dot(xf_points[3]);
  209. maxb = Math::max(dp, maxb);
  210. minb = Math::min(dp, minb);
  211. if (mina > maxb) {
  212. return false;
  213. }
  214. if (minb > maxa) {
  215. return false;
  216. }
  217. return true;
  218. }
  219. } // namespace godot