math_2d.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*************************************************************************/
  2. /* math_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "math_2d.h"
  31. real_t Vector2::angle() const {
  32. return Math::atan2(y, x);
  33. }
  34. real_t Vector2::length() const {
  35. return Math::sqrt(x * x + y * y);
  36. }
  37. real_t Vector2::length_squared() const {
  38. return x * x + y * y;
  39. }
  40. void Vector2::normalize() {
  41. real_t l = x * x + y * y;
  42. if (l != 0) {
  43. l = Math::sqrt(l);
  44. x /= l;
  45. y /= l;
  46. }
  47. }
  48. Vector2 Vector2::normalized() const {
  49. Vector2 v = *this;
  50. v.normalize();
  51. return v;
  52. }
  53. bool Vector2::is_normalized() const {
  54. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  55. return Math::is_equal_approx(length_squared(), 1.0);
  56. }
  57. real_t Vector2::distance_to(const Vector2 &p_vector2) const {
  58. return Math::sqrt((x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y));
  59. }
  60. real_t Vector2::distance_squared_to(const Vector2 &p_vector2) const {
  61. return (x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y);
  62. }
  63. real_t Vector2::angle_to(const Vector2 &p_vector2) const {
  64. return Math::atan2(cross(p_vector2), dot(p_vector2));
  65. }
  66. real_t Vector2::angle_to_point(const Vector2 &p_vector2) const {
  67. return Math::atan2(y - p_vector2.y, x - p_vector2.x);
  68. }
  69. real_t Vector2::dot(const Vector2 &p_other) const {
  70. return x * p_other.x + y * p_other.y;
  71. }
  72. real_t Vector2::cross(const Vector2 &p_other) const {
  73. return x * p_other.y - y * p_other.x;
  74. }
  75. Vector2 Vector2::cross(real_t p_other) const {
  76. return Vector2(p_other * y, -p_other * x);
  77. }
  78. Vector2 Vector2::floor() const {
  79. return Vector2(Math::floor(x), Math::floor(y));
  80. }
  81. Vector2 Vector2::ceil() const {
  82. return Vector2(Math::ceil(x), Math::ceil(y));
  83. }
  84. Vector2 Vector2::round() const {
  85. return Vector2(Math::round(x), Math::round(y));
  86. }
  87. Vector2 Vector2::rotated(real_t p_by) const {
  88. Vector2 v;
  89. v.set_rotation(angle() + p_by);
  90. v *= length();
  91. return v;
  92. }
  93. Vector2 Vector2::project(const Vector2 &p_vec) const {
  94. Vector2 v1 = p_vec;
  95. Vector2 v2 = *this;
  96. return v2 * (v1.dot(v2) / v2.dot(v2));
  97. }
  98. Vector2 Vector2::snapped(const Vector2 &p_by) const {
  99. return Vector2(
  100. Math::stepify(x, p_by.x),
  101. Math::stepify(y, p_by.y));
  102. }
  103. Vector2 Vector2::clamped(real_t p_len) const {
  104. real_t l = length();
  105. Vector2 v = *this;
  106. if (l > 0 && p_len < l) {
  107. v /= l;
  108. v *= p_len;
  109. }
  110. return v;
  111. }
  112. Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const {
  113. Vector2 p0 = p_pre_a;
  114. Vector2 p1 = *this;
  115. Vector2 p2 = p_b;
  116. Vector2 p3 = p_post_b;
  117. real_t t = p_t;
  118. real_t t2 = t * t;
  119. real_t t3 = t2 * t;
  120. Vector2 out;
  121. out = 0.5 * ((p1 * 2.0) +
  122. (-p0 + p2) * t +
  123. (2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
  124. (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3);
  125. return out;
  126. }
  127. // slide returns the component of the vector along the given plane, specified by its normal vector.
  128. Vector2 Vector2::slide(const Vector2 &p_normal) const {
  129. #ifdef MATH_CHECKS
  130. ERR_FAIL_COND_V(p_normal.is_normalized() == false, Vector2());
  131. #endif
  132. return *this - p_normal * this->dot(p_normal);
  133. }
  134. Vector2 Vector2::bounce(const Vector2 &p_normal) const {
  135. return -reflect(p_normal);
  136. }
  137. Vector2 Vector2::reflect(const Vector2 &p_normal) const {
  138. #ifdef MATH_CHECKS
  139. ERR_FAIL_COND_V(p_normal.is_normalized() == false, Vector2());
  140. #endif
  141. return 2.0 * p_normal * this->dot(p_normal) - *this;
  142. }
  143. bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
  144. real_t min = 0, max = 1;
  145. int axis = 0;
  146. real_t sign = 0;
  147. for (int i = 0; i < 2; i++) {
  148. real_t seg_from = p_from[i];
  149. real_t seg_to = p_to[i];
  150. real_t box_begin = position[i];
  151. real_t box_end = box_begin + size[i];
  152. real_t cmin, cmax;
  153. real_t csign;
  154. if (seg_from < seg_to) {
  155. if (seg_from > box_end || seg_to < box_begin)
  156. return false;
  157. real_t length = seg_to - seg_from;
  158. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  159. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  160. csign = -1.0;
  161. } else {
  162. if (seg_to > box_end || seg_from < box_begin)
  163. return false;
  164. real_t length = seg_to - seg_from;
  165. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  166. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  167. csign = 1.0;
  168. }
  169. if (cmin > min) {
  170. min = cmin;
  171. axis = i;
  172. sign = csign;
  173. }
  174. if (cmax < max)
  175. max = cmax;
  176. if (max < min)
  177. return false;
  178. }
  179. Vector2 rel = p_to - p_from;
  180. if (r_normal) {
  181. Vector2 normal;
  182. normal[axis] = sign;
  183. *r_normal = normal;
  184. }
  185. if (r_pos)
  186. *r_pos = p_from + rel * min;
  187. return true;
  188. }
  189. /* Point2i */
  190. Point2i Point2i::operator+(const Point2i &p_v) const {
  191. return Point2i(x + p_v.x, y + p_v.y);
  192. }
  193. void Point2i::operator+=(const Point2i &p_v) {
  194. x += p_v.x;
  195. y += p_v.y;
  196. }
  197. Point2i Point2i::operator-(const Point2i &p_v) const {
  198. return Point2i(x - p_v.x, y - p_v.y);
  199. }
  200. void Point2i::operator-=(const Point2i &p_v) {
  201. x -= p_v.x;
  202. y -= p_v.y;
  203. }
  204. Point2i Point2i::operator*(const Point2i &p_v1) const {
  205. return Point2i(x * p_v1.x, y * p_v1.y);
  206. };
  207. Point2i Point2i::operator*(const int &rvalue) const {
  208. return Point2i(x * rvalue, y * rvalue);
  209. };
  210. void Point2i::operator*=(const int &rvalue) {
  211. x *= rvalue;
  212. y *= rvalue;
  213. };
  214. Point2i Point2i::operator/(const Point2i &p_v1) const {
  215. return Point2i(x / p_v1.x, y / p_v1.y);
  216. };
  217. Point2i Point2i::operator/(const int &rvalue) const {
  218. return Point2i(x / rvalue, y / rvalue);
  219. };
  220. void Point2i::operator/=(const int &rvalue) {
  221. x /= rvalue;
  222. y /= rvalue;
  223. };
  224. Point2i Point2i::operator-() const {
  225. return Point2i(-x, -y);
  226. }
  227. bool Point2i::operator==(const Point2i &p_vec2) const {
  228. return x == p_vec2.x && y == p_vec2.y;
  229. }
  230. bool Point2i::operator!=(const Point2i &p_vec2) const {
  231. return x != p_vec2.x || y != p_vec2.y;
  232. }
  233. void Transform2D::invert() {
  234. // FIXME: this function assumes the basis is a rotation matrix, with no scaling.
  235. // Transform2D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
  236. SWAP(elements[0][1], elements[1][0]);
  237. elements[2] = basis_xform(-elements[2]);
  238. }
  239. Transform2D Transform2D::inverse() const {
  240. Transform2D inv = *this;
  241. inv.invert();
  242. return inv;
  243. }
  244. void Transform2D::affine_invert() {
  245. real_t det = basis_determinant();
  246. #ifdef MATH_CHECKS
  247. ERR_FAIL_COND(det == 0);
  248. #endif
  249. real_t idet = 1.0 / det;
  250. SWAP(elements[0][0], elements[1][1]);
  251. elements[0] *= Vector2(idet, -idet);
  252. elements[1] *= Vector2(-idet, idet);
  253. elements[2] = basis_xform(-elements[2]);
  254. }
  255. Transform2D Transform2D::affine_inverse() const {
  256. Transform2D inv = *this;
  257. inv.affine_invert();
  258. return inv;
  259. }
  260. void Transform2D::rotate(real_t p_phi) {
  261. *this = Transform2D(p_phi, Vector2()) * (*this);
  262. }
  263. real_t Transform2D::get_rotation() const {
  264. real_t det = basis_determinant();
  265. Transform2D m = orthonormalized();
  266. if (det < 0) {
  267. m.scale_basis(Size2(1, -1)); // convention to separate rotation and reflection for 2D is to absorb a flip along y into scaling.
  268. }
  269. return Math::atan2(m[0].y, m[0].x);
  270. }
  271. void Transform2D::set_rotation(real_t p_rot) {
  272. real_t cr = Math::cos(p_rot);
  273. real_t sr = Math::sin(p_rot);
  274. elements[0][0] = cr;
  275. elements[0][1] = sr;
  276. elements[1][0] = -sr;
  277. elements[1][1] = cr;
  278. }
  279. Transform2D::Transform2D(real_t p_rot, const Vector2 &p_pos) {
  280. real_t cr = Math::cos(p_rot);
  281. real_t sr = Math::sin(p_rot);
  282. elements[0][0] = cr;
  283. elements[0][1] = sr;
  284. elements[1][0] = -sr;
  285. elements[1][1] = cr;
  286. elements[2] = p_pos;
  287. }
  288. Size2 Transform2D::get_scale() const {
  289. real_t det_sign = basis_determinant() > 0 ? 1 : -1;
  290. return Size2(elements[0].length(), det_sign * elements[1].length());
  291. }
  292. void Transform2D::scale(const Size2 &p_scale) {
  293. scale_basis(p_scale);
  294. elements[2] *= p_scale;
  295. }
  296. void Transform2D::scale_basis(const Size2 &p_scale) {
  297. elements[0][0] *= p_scale.x;
  298. elements[0][1] *= p_scale.y;
  299. elements[1][0] *= p_scale.x;
  300. elements[1][1] *= p_scale.y;
  301. }
  302. void Transform2D::translate(real_t p_tx, real_t p_ty) {
  303. translate(Vector2(p_tx, p_ty));
  304. }
  305. void Transform2D::translate(const Vector2 &p_translation) {
  306. elements[2] += basis_xform(p_translation);
  307. }
  308. void Transform2D::orthonormalize() {
  309. // Gram-Schmidt Process
  310. Vector2 x = elements[0];
  311. Vector2 y = elements[1];
  312. x.normalize();
  313. y = (y - x * (x.dot(y)));
  314. y.normalize();
  315. elements[0] = x;
  316. elements[1] = y;
  317. }
  318. Transform2D Transform2D::orthonormalized() const {
  319. Transform2D on = *this;
  320. on.orthonormalize();
  321. return on;
  322. }
  323. bool Transform2D::operator==(const Transform2D &p_transform) const {
  324. for (int i = 0; i < 3; i++) {
  325. if (elements[i] != p_transform.elements[i])
  326. return false;
  327. }
  328. return true;
  329. }
  330. bool Transform2D::operator!=(const Transform2D &p_transform) const {
  331. for (int i = 0; i < 3; i++) {
  332. if (elements[i] != p_transform.elements[i])
  333. return true;
  334. }
  335. return false;
  336. }
  337. void Transform2D::operator*=(const Transform2D &p_transform) {
  338. elements[2] = xform(p_transform.elements[2]);
  339. real_t x0, x1, y0, y1;
  340. x0 = tdotx(p_transform.elements[0]);
  341. x1 = tdoty(p_transform.elements[0]);
  342. y0 = tdotx(p_transform.elements[1]);
  343. y1 = tdoty(p_transform.elements[1]);
  344. elements[0][0] = x0;
  345. elements[0][1] = x1;
  346. elements[1][0] = y0;
  347. elements[1][1] = y1;
  348. }
  349. Transform2D Transform2D::operator*(const Transform2D &p_transform) const {
  350. Transform2D t = *this;
  351. t *= p_transform;
  352. return t;
  353. }
  354. Transform2D Transform2D::scaled(const Size2 &p_scale) const {
  355. Transform2D copy = *this;
  356. copy.scale(p_scale);
  357. return copy;
  358. }
  359. Transform2D Transform2D::basis_scaled(const Size2 &p_scale) const {
  360. Transform2D copy = *this;
  361. copy.scale_basis(p_scale);
  362. return copy;
  363. }
  364. Transform2D Transform2D::untranslated() const {
  365. Transform2D copy = *this;
  366. copy.elements[2] = Vector2();
  367. return copy;
  368. }
  369. Transform2D Transform2D::translated(const Vector2 &p_offset) const {
  370. Transform2D copy = *this;
  371. copy.translate(p_offset);
  372. return copy;
  373. }
  374. Transform2D Transform2D::rotated(real_t p_phi) const {
  375. Transform2D copy = *this;
  376. copy.rotate(p_phi);
  377. return copy;
  378. }
  379. real_t Transform2D::basis_determinant() const {
  380. return elements[0].x * elements[1].y - elements[0].y * elements[1].x;
  381. }
  382. Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t p_c) const {
  383. //extract parameters
  384. Vector2 p1 = get_origin();
  385. Vector2 p2 = p_transform.get_origin();
  386. real_t r1 = get_rotation();
  387. real_t r2 = p_transform.get_rotation();
  388. Size2 s1 = get_scale();
  389. Size2 s2 = p_transform.get_scale();
  390. //slerp rotation
  391. Vector2 v1(Math::cos(r1), Math::sin(r1));
  392. Vector2 v2(Math::cos(r2), Math::sin(r2));
  393. real_t dot = v1.dot(v2);
  394. dot = (dot < -1.0) ? -1.0 : ((dot > 1.0) ? 1.0 : dot); //clamp dot to [-1,1]
  395. Vector2 v;
  396. if (dot > 0.9995) {
  397. v = Vector2::linear_interpolate(v1, v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
  398. } else {
  399. real_t angle = p_c * Math::acos(dot);
  400. Vector2 v3 = (v2 - v1 * dot).normalized();
  401. v = v1 * Math::cos(angle) + v3 * Math::sin(angle);
  402. }
  403. //construct matrix
  404. Transform2D res(Math::atan2(v.y, v.x), Vector2::linear_interpolate(p1, p2, p_c));
  405. res.scale_basis(Vector2::linear_interpolate(s1, s2, p_c));
  406. return res;
  407. }
  408. Transform2D::operator String() const {
  409. return String(String() + elements[0] + ", " + elements[1] + ", " + elements[2]);
  410. }