vector2.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**************************************************************************/
  2. /* vector2.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 VECTOR2_H
  31. #define VECTOR2_H
  32. #include "core/math/math_funcs.h"
  33. #include "core/ustring.h"
  34. struct Vector2i;
  35. struct _NO_DISCARD_CLASS_ Vector2 {
  36. static const int AXIS_COUNT = 2;
  37. enum Axis {
  38. AXIS_X,
  39. AXIS_Y,
  40. };
  41. union {
  42. struct {
  43. union {
  44. real_t x;
  45. real_t width;
  46. };
  47. union {
  48. real_t y;
  49. real_t height;
  50. };
  51. };
  52. real_t coord[2];
  53. };
  54. _FORCE_INLINE_ real_t &operator[](int p_idx) {
  55. DEV_ASSERT((unsigned int)p_idx < 2);
  56. return coord[p_idx];
  57. }
  58. _FORCE_INLINE_ const real_t &operator[](int p_idx) const {
  59. DEV_ASSERT((unsigned int)p_idx < 2);
  60. return coord[p_idx];
  61. }
  62. _FORCE_INLINE_ void set_all(real_t p_value) {
  63. x = y = p_value;
  64. }
  65. _FORCE_INLINE_ int min_axis() const {
  66. return x < y ? 0 : 1;
  67. }
  68. _FORCE_INLINE_ int max_axis() const {
  69. return x < y ? 1 : 0;
  70. }
  71. void normalize();
  72. Vector2 normalized() const;
  73. bool is_normalized() const;
  74. real_t length() const;
  75. real_t length_squared() const;
  76. real_t distance_to(const Vector2 &p_vector2) const;
  77. real_t distance_squared_to(const Vector2 &p_vector2) const;
  78. real_t angle_to(const Vector2 &p_vector2) const;
  79. real_t angle_to_point(const Vector2 &p_vector2) const;
  80. _FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_to) const;
  81. real_t dot(const Vector2 &p_other) const;
  82. real_t cross(const Vector2 &p_other) const;
  83. Vector2 posmod(real_t p_mod) const;
  84. Vector2 posmodv(const Vector2 &p_modv) const;
  85. Vector2 project(const Vector2 &p_to) const;
  86. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  87. Vector2 clamped(real_t p_len) const;
  88. Vector2 limit_length(real_t p_len = 1.0) const;
  89. _FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_weight);
  90. _FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_to, real_t p_weight) const;
  91. _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight) const;
  92. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const;
  93. Vector2 move_toward(const Vector2 &p_to, real_t p_delta) const;
  94. Vector2 slide(const Vector2 &p_normal) const;
  95. Vector2 bounce(const Vector2 &p_normal) const;
  96. Vector2 reflect(const Vector2 &p_normal) const;
  97. bool is_equal_approx(const Vector2 &p_v) const;
  98. bool is_zero_approx() const;
  99. Vector2 operator+(const Vector2 &p_v) const;
  100. void operator+=(const Vector2 &p_v);
  101. Vector2 operator-(const Vector2 &p_v) const;
  102. void operator-=(const Vector2 &p_v);
  103. Vector2 operator*(const Vector2 &p_v) const;
  104. Vector2 operator*(real_t p_scalar) const;
  105. void operator*=(real_t p_scalar);
  106. void operator*=(const Vector2 &p_v) { *this = *this * p_v; }
  107. Vector2 operator/(const Vector2 &p_v) const;
  108. Vector2 operator/(real_t p_scalar) const;
  109. void operator/=(real_t p_scalar);
  110. void operator/=(const Vector2 &p_v) { *this = *this / p_v; }
  111. Vector2 operator-() const;
  112. bool operator==(const Vector2 &p_vec2) const;
  113. bool operator!=(const Vector2 &p_vec2) const;
  114. bool operator<(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y < p_vec2.y) : (x < p_vec2.x); }
  115. bool operator>(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y > p_vec2.y) : (x > p_vec2.x); }
  116. bool operator<=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  117. bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  118. real_t angle() const;
  119. void set_rotation(real_t p_radians) {
  120. x = Math::cos(p_radians);
  121. y = Math::sin(p_radians);
  122. }
  123. _FORCE_INLINE_ Vector2 abs() const {
  124. return Vector2(Math::abs(x), Math::abs(y));
  125. }
  126. Vector2 rotated(real_t p_by) const;
  127. Vector2 tangent() const {
  128. return Vector2(y, -x);
  129. }
  130. Vector2 sign() const;
  131. Vector2 floor() const;
  132. Vector2 ceil() const;
  133. Vector2 round() const;
  134. Vector2 snapped(const Vector2 &p_by) const;
  135. real_t aspect() const { return width / height; }
  136. operator String() const { return String::num(x) + ", " + String::num(y); }
  137. _FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
  138. x = p_x;
  139. y = p_y;
  140. }
  141. _FORCE_INLINE_ Vector2() { x = y = 0; }
  142. };
  143. _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
  144. return p_vec - *this * (dot(p_vec) - p_d);
  145. }
  146. _FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
  147. return p_vec * p_scalar;
  148. }
  149. _FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const {
  150. return Vector2(x + p_v.x, y + p_v.y);
  151. }
  152. _FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) {
  153. x += p_v.x;
  154. y += p_v.y;
  155. }
  156. _FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const {
  157. return Vector2(x - p_v.x, y - p_v.y);
  158. }
  159. _FORCE_INLINE_ void Vector2::operator-=(const Vector2 &p_v) {
  160. x -= p_v.x;
  161. y -= p_v.y;
  162. }
  163. _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v) const {
  164. return Vector2(x * p_v.x, y * p_v.y);
  165. }
  166. _FORCE_INLINE_ Vector2 Vector2::operator*(real_t p_scalar) const {
  167. return Vector2(x * p_scalar, y * p_scalar);
  168. }
  169. _FORCE_INLINE_ void Vector2::operator*=(real_t p_scalar) {
  170. x *= p_scalar;
  171. y *= p_scalar;
  172. }
  173. _FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v) const {
  174. return Vector2(x / p_v.x, y / p_v.y);
  175. }
  176. _FORCE_INLINE_ Vector2 Vector2::operator/(real_t p_scalar) const {
  177. return Vector2(x / p_scalar, y / p_scalar);
  178. }
  179. _FORCE_INLINE_ void Vector2::operator/=(real_t p_scalar) {
  180. x /= p_scalar;
  181. y /= p_scalar;
  182. }
  183. _FORCE_INLINE_ Vector2 Vector2::operator-() const {
  184. return Vector2(-x, -y);
  185. }
  186. _FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const {
  187. return x == p_vec2.x && y == p_vec2.y;
  188. }
  189. _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
  190. return x != p_vec2.x || y != p_vec2.y;
  191. }
  192. Vector2 Vector2::linear_interpolate(const Vector2 &p_to, real_t p_weight) const {
  193. Vector2 res = *this;
  194. res.x += (p_weight * (p_to.x - x));
  195. res.y += (p_weight * (p_to.y - y));
  196. return res;
  197. }
  198. Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
  199. #ifdef MATH_CHECKS
  200. ERR_FAIL_COND_V_MSG(!is_normalized(), Vector2(), "The start Vector2 must be normalized.");
  201. #endif
  202. real_t theta = angle_to(p_to);
  203. return rotated(theta * p_weight);
  204. }
  205. Vector2 Vector2::direction_to(const Vector2 &p_to) const {
  206. Vector2 ret(p_to.x - x, p_to.y - y);
  207. ret.normalize();
  208. return ret;
  209. }
  210. Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_weight) {
  211. Vector2 res = p_a;
  212. res.x += (p_weight * (p_b.x - p_a.x));
  213. res.y += (p_weight * (p_b.y - p_a.y));
  214. return res;
  215. }
  216. typedef Vector2 Size2;
  217. typedef Vector2 Point2;
  218. /* INTEGER STUFF */
  219. struct _NO_DISCARD_CLASS_ Vector2i {
  220. enum Axis {
  221. AXIS_X,
  222. AXIS_Y,
  223. };
  224. union {
  225. struct {
  226. union {
  227. int x;
  228. int width;
  229. };
  230. union {
  231. int y;
  232. int height;
  233. };
  234. };
  235. int coord[2];
  236. };
  237. _FORCE_INLINE_ int &operator[](int p_idx) {
  238. DEV_ASSERT((unsigned int)p_idx < 2);
  239. return coord[p_idx];
  240. }
  241. _FORCE_INLINE_ const int &operator[](int p_idx) const {
  242. DEV_ASSERT((unsigned int)p_idx < 2);
  243. return coord[p_idx];
  244. }
  245. Vector2i operator+(const Vector2i &p_v) const;
  246. void operator+=(const Vector2i &p_v);
  247. Vector2i operator-(const Vector2i &p_v) const;
  248. void operator-=(const Vector2i &p_v);
  249. Vector2i operator*(const Vector2i &p_v) const;
  250. Vector2i operator*(int p_scalar) const;
  251. void operator*=(int p_scalar);
  252. Vector2i operator/(const Vector2i &p_v) const;
  253. Vector2i operator/(int p_scalar) const;
  254. void operator/=(int p_scalar);
  255. Vector2i operator-() const;
  256. bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  257. bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
  258. bool operator==(const Vector2i &p_vec2) const;
  259. bool operator!=(const Vector2i &p_vec2) const;
  260. real_t get_aspect() const { return width / (real_t)height; }
  261. operator String() const { return String::num(x) + ", " + String::num(y); }
  262. operator Vector2() const { return Vector2(x, y); }
  263. inline Vector2i(const Vector2 &p_vec2) {
  264. x = (int)p_vec2.x;
  265. y = (int)p_vec2.y;
  266. }
  267. inline Vector2i(int p_x, int p_y) {
  268. x = p_x;
  269. y = p_y;
  270. }
  271. inline Vector2i() {
  272. x = 0;
  273. y = 0;
  274. }
  275. };
  276. typedef Vector2i Size2i;
  277. typedef Vector2i Point2i;
  278. #endif // VECTOR2_H