math_2d.cpp 13 KB

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