math_2d.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*************************************************************************/
  2. /* math_2d.h */
  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. #ifndef MATH_2D_H
  31. #define MATH_2D_H
  32. #include "math_funcs.h"
  33. #include "ustring.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. enum Margin {
  38. MARGIN_LEFT,
  39. MARGIN_TOP,
  40. MARGIN_RIGHT,
  41. MARGIN_BOTTOM
  42. };
  43. enum Orientation {
  44. HORIZONTAL,
  45. VERTICAL
  46. };
  47. enum HAlign {
  48. HALIGN_LEFT,
  49. HALIGN_CENTER,
  50. HALIGN_RIGHT
  51. };
  52. enum VAlign {
  53. VALIGN_TOP,
  54. VALIGN_CENTER,
  55. VALIGN_BOTTOM
  56. };
  57. struct Vector2 {
  58. union {
  59. float x;
  60. float width;
  61. };
  62. union {
  63. float y;
  64. float height;
  65. };
  66. _FORCE_INLINE_ float &operator[](int p_idx) {
  67. return p_idx ? y : x;
  68. }
  69. _FORCE_INLINE_ const float &operator[](int p_idx) const {
  70. return p_idx ? y : x;
  71. }
  72. void normalize();
  73. Vector2 normalized() const;
  74. float length() const;
  75. float length_squared() const;
  76. float distance_to(const Vector2 &p_vector2) const;
  77. float distance_squared_to(const Vector2 &p_vector2) const;
  78. float angle_to(const Vector2 &p_vector2) const;
  79. float angle_to_point(const Vector2 &p_vector2) const;
  80. float dot(const Vector2 &p_other) const;
  81. float cross(const Vector2 &p_other) const;
  82. Vector2 cross(real_t p_other) const;
  83. Vector2 project(const Vector2 &p_vec) const;
  84. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  85. Vector2 clamped(real_t p_len) const;
  86. _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, float p_t);
  87. _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_b, float p_t) const;
  88. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, float p_t) const;
  89. Vector2 cubic_interpolate_soft(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, float p_t) const;
  90. Vector2 slide(const Vector2 &p_vec) const;
  91. Vector2 reflect(const Vector2 &p_vec) const;
  92. Vector2 operator+(const Vector2 &p_v) const;
  93. void operator+=(const Vector2 &p_v);
  94. Vector2 operator-(const Vector2 &p_v) const;
  95. void operator-=(const Vector2 &p_v);
  96. Vector2 operator*(const Vector2 &p_v1) const;
  97. Vector2 operator*(const float &rvalue) const;
  98. void operator*=(const float &rvalue);
  99. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  100. Vector2 operator/(const Vector2 &p_v1) const;
  101. Vector2 operator/(const float &rvalue) const;
  102. void operator/=(const float &rvalue);
  103. Vector2 operator-() const;
  104. bool operator==(const Vector2 &p_vec2) const;
  105. bool operator!=(const Vector2 &p_vec2) const;
  106. bool operator<(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  107. bool operator<=(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y <= p_vec2.y) : (x <= p_vec2.x); }
  108. real_t angle() const;
  109. void set_rotation(float p_radians) {
  110. x = Math::sin(p_radians);
  111. y = Math::cos(p_radians);
  112. }
  113. _FORCE_INLINE_ Vector2 abs() const {
  114. return Vector2(Math::abs(x), Math::abs(y));
  115. }
  116. Vector2 rotated(float p_by) const;
  117. Vector2 tangent() const {
  118. return Vector2(y, -x);
  119. }
  120. Vector2 floor() const;
  121. Vector2 snapped(const Vector2 &p_by) const;
  122. float get_aspect() const { return width / height; }
  123. operator String() const { return String::num(x) + ", " + String::num(y); }
  124. _FORCE_INLINE_ Vector2(float p_x, float p_y) {
  125. x = p_x;
  126. y = p_y;
  127. }
  128. _FORCE_INLINE_ Vector2() {
  129. x = 0;
  130. y = 0;
  131. }
  132. };
  133. _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
  134. return p_vec - *this * (dot(p_vec) - p_d);
  135. }
  136. _FORCE_INLINE_ Vector2 operator*(float p_scalar, const Vector2 &p_vec) {
  137. return p_vec * p_scalar;
  138. }
  139. Vector2 Vector2::linear_interpolate(const Vector2 &p_b, float p_t) const {
  140. Vector2 res = *this;
  141. res.x += (p_t * (p_b.x - x));
  142. res.y += (p_t * (p_b.y - y));
  143. return res;
  144. }
  145. Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, float p_t) {
  146. Vector2 res = p_a;
  147. res.x += (p_t * (p_b.x - p_a.x));
  148. res.y += (p_t * (p_b.y - p_a.y));
  149. return res;
  150. }
  151. typedef Vector2 Size2;
  152. typedef Vector2 Point2;
  153. struct Matrix32;
  154. struct Rect2 {
  155. Point2 pos;
  156. Size2 size;
  157. const Vector2 &get_pos() const { return pos; }
  158. void set_pos(const Vector2 &p_pos) { pos = p_pos; }
  159. const Vector2 &get_size() const { return size; }
  160. void set_size(const Vector2 &p_size) { size = p_size; }
  161. float get_area() const { return size.width * size.height; }
  162. inline bool intersects(const Rect2 &p_rect) const {
  163. if (pos.x >= (p_rect.pos.x + p_rect.size.width))
  164. return false;
  165. if ((pos.x + size.width) <= p_rect.pos.x)
  166. return false;
  167. if (pos.y >= (p_rect.pos.y + p_rect.size.height))
  168. return false;
  169. if ((pos.y + size.height) <= p_rect.pos.y)
  170. return false;
  171. return true;
  172. }
  173. inline float distance_to(const Vector2 &p_point) const {
  174. float dist = 1e20;
  175. if (p_point.x < pos.x) {
  176. dist = MIN(dist, pos.x - p_point.x);
  177. }
  178. if (p_point.y < pos.y) {
  179. dist = MIN(dist, pos.y - p_point.y);
  180. }
  181. if (p_point.x >= (pos.x + size.x)) {
  182. dist = MIN(p_point.x - (pos.x + size.x), dist);
  183. }
  184. if (p_point.y >= (pos.y + size.y)) {
  185. dist = MIN(p_point.y - (pos.y + size.y), dist);
  186. }
  187. if (dist == 1e20)
  188. return 0;
  189. else
  190. return dist;
  191. }
  192. _FORCE_INLINE_ bool intersects_transformed(const Matrix32 &p_xform, const Rect2 &p_rect) const;
  193. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = NULL, Point2 *r_normal = NULL) const;
  194. inline bool encloses(const Rect2 &p_rect) const {
  195. return (p_rect.pos.x >= pos.x) && (p_rect.pos.y >= pos.y) &&
  196. ((p_rect.pos.x + p_rect.size.x) < (pos.x + size.x)) &&
  197. ((p_rect.pos.y + p_rect.size.y) < (pos.y + size.y));
  198. }
  199. inline bool has_no_area() const {
  200. return (size.x <= 0 || size.y <= 0);
  201. }
  202. inline Rect2 clip(const Rect2 &p_rect) const { /// return a clipped rect
  203. Rect2 new_rect = p_rect;
  204. if (!intersects(new_rect))
  205. return Rect2();
  206. new_rect.pos.x = MAX(p_rect.pos.x, pos.x);
  207. new_rect.pos.y = MAX(p_rect.pos.y, pos.y);
  208. Point2 p_rect_end = p_rect.pos + p_rect.size;
  209. Point2 end = pos + size;
  210. new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.pos.x;
  211. new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.pos.y;
  212. return new_rect;
  213. }
  214. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  215. Rect2 new_rect;
  216. new_rect.pos.x = MIN(p_rect.pos.x, pos.x);
  217. new_rect.pos.y = MIN(p_rect.pos.y, pos.y);
  218. new_rect.size.x = MAX(p_rect.pos.x + p_rect.size.x, pos.x + size.x);
  219. new_rect.size.y = MAX(p_rect.pos.y + p_rect.size.y, pos.y + size.y);
  220. new_rect.size = new_rect.size - new_rect.pos; //make relative again
  221. return new_rect;
  222. };
  223. inline bool has_point(const Point2 &p_point) const {
  224. if (p_point.x < pos.x)
  225. return false;
  226. if (p_point.y < pos.y)
  227. return false;
  228. if (p_point.x >= (pos.x + size.x))
  229. return false;
  230. if (p_point.y >= (pos.y + size.y))
  231. return false;
  232. return true;
  233. }
  234. inline bool no_area() const { return (size.width <= 0 || size.height <= 0); }
  235. bool operator==(const Rect2 &p_rect) const { return pos == p_rect.pos && size == p_rect.size; }
  236. bool operator!=(const Rect2 &p_rect) const { return pos != p_rect.pos || size != p_rect.size; }
  237. inline Rect2 grow(real_t p_by) const {
  238. Rect2 g = *this;
  239. g.pos.x -= p_by;
  240. g.pos.y -= p_by;
  241. g.size.width += p_by * 2;
  242. g.size.height += p_by * 2;
  243. return g;
  244. }
  245. inline Rect2 grow_margin(Margin p_margin, real_t p_amount) const {
  246. Rect2 g = *this;
  247. g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0,
  248. (MARGIN_TOP == p_margin) ? p_amount : 0,
  249. (MARGIN_RIGHT == p_margin) ? p_amount : 0,
  250. (MARGIN_BOTTOM == p_margin) ? p_amount : 0);
  251. return g;
  252. }
  253. inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
  254. Rect2 g = *this;
  255. g.pos.x -= p_left;
  256. g.pos.y -= p_top;
  257. g.size.width += p_left + p_right;
  258. g.size.height += p_top + p_bottom;
  259. return g;
  260. }
  261. inline Rect2 expand(const Vector2 &p_vector) const {
  262. Rect2 r = *this;
  263. r.expand_to(p_vector);
  264. return r;
  265. }
  266. inline void expand_to(const Vector2 &p_vector) { //in place function for speed
  267. Vector2 begin = pos;
  268. Vector2 end = pos + size;
  269. if (p_vector.x < begin.x)
  270. begin.x = p_vector.x;
  271. if (p_vector.y < begin.y)
  272. begin.y = p_vector.y;
  273. if (p_vector.x > end.x)
  274. end.x = p_vector.x;
  275. if (p_vector.y > end.y)
  276. end.y = p_vector.y;
  277. pos = begin;
  278. size = end - begin;
  279. }
  280. operator String() const { return String(pos) + ", " + String(size); }
  281. Rect2() {}
  282. Rect2(float p_x, float p_y, float p_width, float p_height) {
  283. pos = Point2(p_x, p_y);
  284. size = Size2(p_width, p_height);
  285. }
  286. Rect2(const Point2 &p_pos, const Size2 &p_size) {
  287. pos = p_pos;
  288. size = p_size;
  289. }
  290. };
  291. /* INTEGER STUFF */
  292. struct Point2i {
  293. union {
  294. int x;
  295. int width;
  296. };
  297. union {
  298. int y;
  299. int height;
  300. };
  301. _FORCE_INLINE_ int &operator[](int p_idx) {
  302. return p_idx ? y : x;
  303. }
  304. _FORCE_INLINE_ const int &operator[](int p_idx) const {
  305. return p_idx ? y : x;
  306. }
  307. Point2i operator+(const Point2i &p_v) const;
  308. void operator+=(const Point2i &p_v);
  309. Point2i operator-(const Point2i &p_v) const;
  310. void operator-=(const Point2i &p_v);
  311. Point2i operator*(const Point2i &p_v1) const;
  312. Point2i operator*(const int &rvalue) const;
  313. void operator*=(const int &rvalue);
  314. Point2i operator/(const Point2i &p_v1) const;
  315. Point2i operator/(const int &rvalue) const;
  316. void operator/=(const int &rvalue);
  317. Point2i operator-() const;
  318. bool operator<(const Point2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  319. bool operator>(const Point2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  320. bool operator==(const Point2i &p_vec2) const;
  321. bool operator!=(const Point2i &p_vec2) const;
  322. float get_aspect() const { return width / (float)height; }
  323. operator String() const { return String::num(x) + ", " + String::num(y); }
  324. operator Vector2() const { return Vector2(x, y); }
  325. inline Point2i(const Vector2 &p_vec2) {
  326. x = (int)p_vec2.x;
  327. y = (int)p_vec2.y;
  328. }
  329. inline Point2i(int p_x, int p_y) {
  330. x = p_x;
  331. y = p_y;
  332. }
  333. inline Point2i() {
  334. x = 0;
  335. y = 0;
  336. }
  337. };
  338. typedef Point2i Size2i;
  339. struct Rect2i {
  340. Point2i pos;
  341. Size2i size;
  342. const Point2i &get_pos() const { return pos; }
  343. void set_pos(const Point2i &p_pos) { pos = p_pos; }
  344. const Point2i &get_size() const { return size; }
  345. void set_size(const Point2i &p_size) { size = p_size; }
  346. int get_area() const { return size.width * size.height; }
  347. inline bool intersects(const Rect2i &p_rect) const {
  348. if (pos.x > (p_rect.pos.x + p_rect.size.width))
  349. return false;
  350. if ((pos.x + size.width) < p_rect.pos.x)
  351. return false;
  352. if (pos.y > (p_rect.pos.y + p_rect.size.height))
  353. return false;
  354. if ((pos.y + size.height) < p_rect.pos.y)
  355. return false;
  356. return true;
  357. }
  358. inline bool encloses(const Rect2i &p_rect) const {
  359. return (p_rect.pos.x >= pos.x) && (p_rect.pos.y >= pos.y) &&
  360. ((p_rect.pos.x + p_rect.size.x) < (pos.x + size.x)) &&
  361. ((p_rect.pos.y + p_rect.size.y) < (pos.y + size.y));
  362. }
  363. inline bool has_no_area() const {
  364. return (size.x <= 0 || size.y <= 0);
  365. }
  366. inline Rect2i clip(const Rect2i &p_rect) const { /// return a clipped rect
  367. Rect2i new_rect = p_rect;
  368. if (!intersects(new_rect))
  369. return Rect2i();
  370. new_rect.pos.x = MAX(p_rect.pos.x, pos.x);
  371. new_rect.pos.y = MAX(p_rect.pos.y, pos.y);
  372. Point2 p_rect_end = p_rect.pos + p_rect.size;
  373. Point2 end = pos + size;
  374. new_rect.size.x = (int)(MIN(p_rect_end.x, end.x) - new_rect.pos.x);
  375. new_rect.size.y = (int)(MIN(p_rect_end.y, end.y) - new_rect.pos.y);
  376. return new_rect;
  377. }
  378. inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
  379. Rect2i new_rect;
  380. new_rect.pos.x = MIN(p_rect.pos.x, pos.x);
  381. new_rect.pos.y = MIN(p_rect.pos.y, pos.y);
  382. new_rect.size.x = MAX(p_rect.pos.x + p_rect.size.x, pos.x + size.x);
  383. new_rect.size.y = MAX(p_rect.pos.y + p_rect.size.y, pos.y + size.y);
  384. new_rect.size = new_rect.size - new_rect.pos; //make relative again
  385. return new_rect;
  386. };
  387. bool has_point(const Point2 &p_point) const {
  388. if (p_point.x < pos.x)
  389. return false;
  390. if (p_point.y < pos.y)
  391. return false;
  392. if (p_point.x >= (pos.x + size.x))
  393. return false;
  394. if (p_point.y >= (pos.y + size.y))
  395. return false;
  396. return true;
  397. }
  398. bool no_area() { return (size.width <= 0 || size.height <= 0); }
  399. bool operator==(const Rect2i &p_rect) const { return pos == p_rect.pos && size == p_rect.size; }
  400. bool operator!=(const Rect2i &p_rect) const { return pos != p_rect.pos || size != p_rect.size; }
  401. Rect2i grow(int p_by) const {
  402. Rect2i g = *this;
  403. g.pos.x -= p_by;
  404. g.pos.y -= p_by;
  405. g.size.width += p_by * 2;
  406. g.size.height += p_by * 2;
  407. return g;
  408. }
  409. inline void expand_to(const Point2i &p_vector) {
  410. Point2i begin = pos;
  411. Point2i end = pos + size;
  412. if (p_vector.x < begin.x)
  413. begin.x = p_vector.x;
  414. if (p_vector.y < begin.y)
  415. begin.y = p_vector.y;
  416. if (p_vector.x > end.x)
  417. end.x = p_vector.x;
  418. if (p_vector.y > end.y)
  419. end.y = p_vector.y;
  420. pos = begin;
  421. size = end - begin;
  422. }
  423. operator String() const { return String(pos) + ", " + String(size); }
  424. operator Rect2() const { return Rect2(pos, size); }
  425. Rect2i(const Rect2 &p_r2) {
  426. pos = p_r2.pos;
  427. size = p_r2.size;
  428. }
  429. Rect2i() {}
  430. Rect2i(int p_x, int p_y, int p_width, int p_height) {
  431. pos = Point2(p_x, p_y);
  432. size = Size2(p_width, p_height);
  433. }
  434. Rect2i(const Point2 &p_pos, const Size2 &p_size) {
  435. pos = p_pos;
  436. size = p_size;
  437. }
  438. };
  439. struct Matrix32 {
  440. Vector2 elements[3];
  441. _FORCE_INLINE_ float tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  442. _FORCE_INLINE_ float tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  443. const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
  444. Vector2 &operator[](int p_idx) { return elements[p_idx]; }
  445. _FORCE_INLINE_ Vector2 get_axis(int p_axis) const {
  446. ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
  447. return elements[p_axis];
  448. }
  449. _FORCE_INLINE_ void set_axis(int p_axis, const Vector2 &p_vec) {
  450. ERR_FAIL_INDEX(p_axis, 3);
  451. elements[p_axis] = p_vec;
  452. }
  453. void invert();
  454. Matrix32 inverse() const;
  455. void affine_invert();
  456. Matrix32 affine_inverse() const;
  457. void set_rotation(real_t p_phi);
  458. real_t get_rotation() const;
  459. _FORCE_INLINE_ void set_rotation_and_scale(real_t p_phi, const Size2 &p_scale);
  460. void rotate(real_t p_phi);
  461. void scale(const Size2 &p_scale);
  462. void scale_basis(const Size2 &p_scale);
  463. void translate(real_t p_tx, real_t p_ty);
  464. void translate(const Vector2 &p_translation);
  465. float basis_determinant() const;
  466. Size2 get_scale() const;
  467. _FORCE_INLINE_ const Vector2 &get_origin() const { return elements[2]; }
  468. _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
  469. Matrix32 scaled(const Size2 &p_scale) const;
  470. Matrix32 basis_scaled(const Size2 &p_scale) const;
  471. Matrix32 translated(const Vector2 &p_offset) const;
  472. Matrix32 rotated(float p_phi) const;
  473. Matrix32 untranslated() const;
  474. void orthonormalize();
  475. Matrix32 orthonormalized() const;
  476. bool operator==(const Matrix32 &p_transform) const;
  477. bool operator!=(const Matrix32 &p_transform) const;
  478. void operator*=(const Matrix32 &p_transform);
  479. Matrix32 operator*(const Matrix32 &p_transform) const;
  480. Matrix32 interpolate_with(const Matrix32 &p_transform, float p_c) const;
  481. _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const;
  482. _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  483. _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const;
  484. _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const;
  485. _FORCE_INLINE_ Rect2 xform(const Rect2 &p_vec) const;
  486. _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_vec) const;
  487. operator String() const;
  488. Matrix32(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
  489. elements[0][0] = xx;
  490. elements[0][1] = xy;
  491. elements[1][0] = yx;
  492. elements[1][1] = yy;
  493. elements[2][0] = ox;
  494. elements[2][1] = oy;
  495. }
  496. Matrix32(real_t p_rot, const Vector2 &p_pos);
  497. Matrix32() {
  498. elements[0][0] = 1.0;
  499. elements[1][1] = 1.0;
  500. }
  501. };
  502. bool Rect2::intersects_transformed(const Matrix32 &p_xform, const Rect2 &p_rect) const {
  503. //SAT intersection between local and transformed rect2
  504. Vector2 xf_points[4] = {
  505. p_xform.xform(p_rect.pos),
  506. p_xform.xform(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y)),
  507. p_xform.xform(Vector2(p_rect.pos.x, p_rect.pos.y + p_rect.size.y)),
  508. p_xform.xform(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y + p_rect.size.y)),
  509. };
  510. real_t low_limit;
  511. //base rect2 first (faster)
  512. if (xf_points[0].y > pos.y)
  513. goto next1;
  514. if (xf_points[1].y > pos.y)
  515. goto next1;
  516. if (xf_points[2].y > pos.y)
  517. goto next1;
  518. if (xf_points[3].y > pos.y)
  519. goto next1;
  520. return false;
  521. next1:
  522. low_limit = pos.y + size.y;
  523. if (xf_points[0].y < low_limit)
  524. goto next2;
  525. if (xf_points[1].y < low_limit)
  526. goto next2;
  527. if (xf_points[2].y < low_limit)
  528. goto next2;
  529. if (xf_points[3].y < low_limit)
  530. goto next2;
  531. return false;
  532. next2:
  533. if (xf_points[0].x > pos.x)
  534. goto next3;
  535. if (xf_points[1].x > pos.x)
  536. goto next3;
  537. if (xf_points[2].x > pos.x)
  538. goto next3;
  539. if (xf_points[3].x > pos.x)
  540. goto next3;
  541. return false;
  542. next3:
  543. low_limit = pos.x + size.x;
  544. if (xf_points[0].x < low_limit)
  545. goto next4;
  546. if (xf_points[1].x < low_limit)
  547. goto next4;
  548. if (xf_points[2].x < low_limit)
  549. goto next4;
  550. if (xf_points[3].x < low_limit)
  551. goto next4;
  552. return false;
  553. next4:
  554. Vector2 xf_points2[4] = {
  555. pos,
  556. Vector2(pos.x + size.x, pos.y),
  557. Vector2(pos.x, pos.y + size.y),
  558. Vector2(pos.x + size.x, pos.y + size.y),
  559. };
  560. real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
  561. real_t mina = maxa;
  562. real_t dp = p_xform.elements[0].dot(xf_points2[1]);
  563. maxa = MAX(dp, maxa);
  564. mina = MIN(dp, mina);
  565. dp = p_xform.elements[0].dot(xf_points2[2]);
  566. maxa = MAX(dp, maxa);
  567. mina = MIN(dp, mina);
  568. dp = p_xform.elements[0].dot(xf_points2[3]);
  569. maxa = MAX(dp, maxa);
  570. mina = MIN(dp, mina);
  571. real_t maxb = p_xform.elements[0].dot(xf_points[0]);
  572. real_t minb = maxb;
  573. dp = p_xform.elements[0].dot(xf_points[1]);
  574. maxb = MAX(dp, maxb);
  575. minb = MIN(dp, minb);
  576. dp = p_xform.elements[0].dot(xf_points[2]);
  577. maxb = MAX(dp, maxb);
  578. minb = MIN(dp, minb);
  579. dp = p_xform.elements[0].dot(xf_points[3]);
  580. maxb = MAX(dp, maxb);
  581. minb = MIN(dp, minb);
  582. if (mina > maxb)
  583. return false;
  584. if (minb > maxa)
  585. return false;
  586. maxa = p_xform.elements[1].dot(xf_points2[0]);
  587. mina = maxa;
  588. dp = p_xform.elements[1].dot(xf_points2[1]);
  589. maxa = MAX(dp, maxa);
  590. mina = MIN(dp, mina);
  591. dp = p_xform.elements[1].dot(xf_points2[2]);
  592. maxa = MAX(dp, maxa);
  593. mina = MIN(dp, mina);
  594. dp = p_xform.elements[1].dot(xf_points2[3]);
  595. maxa = MAX(dp, maxa);
  596. mina = MIN(dp, mina);
  597. maxb = p_xform.elements[1].dot(xf_points[0]);
  598. minb = maxb;
  599. dp = p_xform.elements[1].dot(xf_points[1]);
  600. maxb = MAX(dp, maxb);
  601. minb = MIN(dp, minb);
  602. dp = p_xform.elements[1].dot(xf_points[2]);
  603. maxb = MAX(dp, maxb);
  604. minb = MIN(dp, minb);
  605. dp = p_xform.elements[1].dot(xf_points[3]);
  606. maxb = MAX(dp, maxb);
  607. minb = MIN(dp, minb);
  608. if (mina > maxb)
  609. return false;
  610. if (minb > maxa)
  611. return false;
  612. return true;
  613. }
  614. Vector2 Matrix32::basis_xform(const Vector2 &v) const {
  615. return Vector2(
  616. tdotx(v),
  617. tdoty(v));
  618. }
  619. Vector2 Matrix32::basis_xform_inv(const Vector2 &v) const {
  620. return Vector2(
  621. elements[0].dot(v),
  622. elements[1].dot(v));
  623. }
  624. Vector2 Matrix32::xform(const Vector2 &v) const {
  625. return Vector2(
  626. tdotx(v),
  627. tdoty(v)) +
  628. elements[2];
  629. }
  630. Vector2 Matrix32::xform_inv(const Vector2 &p_vec) const {
  631. Vector2 v = p_vec - elements[2];
  632. return Vector2(
  633. elements[0].dot(v),
  634. elements[1].dot(v));
  635. }
  636. Rect2 Matrix32::xform(const Rect2 &p_rect) const {
  637. Vector2 x = elements[0] * p_rect.size.x;
  638. Vector2 y = elements[1] * p_rect.size.y;
  639. Vector2 pos = xform(p_rect.pos);
  640. Rect2 new_rect;
  641. new_rect.pos = pos;
  642. new_rect.expand_to(pos + x);
  643. new_rect.expand_to(pos + y);
  644. new_rect.expand_to(pos + x + y);
  645. return new_rect;
  646. }
  647. void Matrix32::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
  648. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  649. elements[1][1] = Math::cos(p_rot) * p_scale.y;
  650. elements[0][1] = -Math::sin(p_rot) * p_scale.x;
  651. elements[1][0] = Math::sin(p_rot) * p_scale.y;
  652. }
  653. Rect2 Matrix32::xform_inv(const Rect2 &p_rect) const {
  654. Vector2 ends[4] = {
  655. xform_inv(p_rect.pos),
  656. xform_inv(Vector2(p_rect.pos.x, p_rect.pos.y + p_rect.size.y)),
  657. xform_inv(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y + p_rect.size.y)),
  658. xform_inv(Vector2(p_rect.pos.x + p_rect.size.x, p_rect.pos.y))
  659. };
  660. Rect2 new_rect;
  661. new_rect.pos = ends[0];
  662. new_rect.expand_to(ends[1]);
  663. new_rect.expand_to(ends[2]);
  664. new_rect.expand_to(ends[3]);
  665. return new_rect;
  666. }
  667. #endif