vector2.h 12 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. #pragma once
  31. #include "core/error/error_macros.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/templates/hashfuncs.h"
  34. class String;
  35. struct Vector2i;
  36. struct [[nodiscard]] Vector2 {
  37. static const Vector2 LEFT;
  38. static const Vector2 RIGHT;
  39. static const Vector2 UP;
  40. static const Vector2 DOWN;
  41. static constexpr int AXIS_COUNT = 2;
  42. enum Axis {
  43. AXIS_X,
  44. AXIS_Y,
  45. };
  46. union {
  47. // NOLINTBEGIN(modernize-use-default-member-init)
  48. struct {
  49. real_t x;
  50. real_t y;
  51. };
  52. struct {
  53. real_t width;
  54. real_t height;
  55. };
  56. real_t coord[2] = { 0 };
  57. // NOLINTEND(modernize-use-default-member-init)
  58. };
  59. _FORCE_INLINE_ real_t &operator[](int p_axis) {
  60. DEV_ASSERT((unsigned int)p_axis < 2);
  61. return coord[p_axis];
  62. }
  63. _FORCE_INLINE_ const real_t &operator[](int p_axis) const {
  64. DEV_ASSERT((unsigned int)p_axis < 2);
  65. return coord[p_axis];
  66. }
  67. _FORCE_INLINE_ Vector2::Axis min_axis_index() const {
  68. return x < y ? Vector2::AXIS_X : Vector2::AXIS_Y;
  69. }
  70. _FORCE_INLINE_ Vector2::Axis max_axis_index() const {
  71. return x < y ? Vector2::AXIS_Y : Vector2::AXIS_X;
  72. }
  73. void normalize();
  74. Vector2 normalized() const;
  75. bool is_normalized() const;
  76. real_t length() const;
  77. real_t length_squared() const;
  78. Vector2 limit_length(real_t p_len = 1.0) const;
  79. Vector2 min(const Vector2 &p_vector2) const {
  80. return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
  81. }
  82. Vector2 minf(real_t p_scalar) const {
  83. return Vector2(MIN(x, p_scalar), MIN(y, p_scalar));
  84. }
  85. Vector2 max(const Vector2 &p_vector2) const {
  86. return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
  87. }
  88. Vector2 maxf(real_t p_scalar) const {
  89. return Vector2(MAX(x, p_scalar), MAX(y, p_scalar));
  90. }
  91. real_t distance_to(const Vector2 &p_vector2) const;
  92. real_t distance_squared_to(const Vector2 &p_vector2) const;
  93. real_t angle_to(const Vector2 &p_vector2) const;
  94. real_t angle_to_point(const Vector2 &p_vector2) const;
  95. _FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_to) const;
  96. real_t dot(const Vector2 &p_other) const;
  97. real_t cross(const Vector2 &p_other) const;
  98. Vector2 posmod(real_t p_mod) const;
  99. Vector2 posmodv(const Vector2 &p_modv) const;
  100. Vector2 project(const Vector2 &p_to) const;
  101. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  102. _FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, real_t p_weight) const;
  103. _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight) const;
  104. _FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const;
  105. _FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
  106. _FORCE_INLINE_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const;
  107. _FORCE_INLINE_ Vector2 bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const;
  108. Vector2 move_toward(const Vector2 &p_to, real_t p_delta) const;
  109. Vector2 slide(const Vector2 &p_normal) const;
  110. Vector2 bounce(const Vector2 &p_normal) const;
  111. Vector2 reflect(const Vector2 &p_normal) const;
  112. bool is_equal_approx(const Vector2 &p_v) const;
  113. bool is_same(const Vector2 &p_v) const;
  114. bool is_zero_approx() const;
  115. bool is_finite() const;
  116. constexpr Vector2 operator+(const Vector2 &p_v) const;
  117. constexpr void operator+=(const Vector2 &p_v);
  118. constexpr Vector2 operator-(const Vector2 &p_v) const;
  119. constexpr void operator-=(const Vector2 &p_v);
  120. constexpr Vector2 operator*(const Vector2 &p_v1) const;
  121. constexpr Vector2 operator*(real_t p_rvalue) const;
  122. constexpr void operator*=(real_t p_rvalue);
  123. constexpr void operator*=(const Vector2 &p_rvalue) { *this = *this * p_rvalue; }
  124. constexpr Vector2 operator/(const Vector2 &p_v1) const;
  125. constexpr Vector2 operator/(real_t p_rvalue) const;
  126. constexpr void operator/=(real_t p_rvalue);
  127. constexpr void operator/=(const Vector2 &p_rvalue) { *this = *this / p_rvalue; }
  128. constexpr Vector2 operator-() const;
  129. constexpr bool operator==(const Vector2 &p_vec2) const;
  130. constexpr bool operator!=(const Vector2 &p_vec2) const;
  131. constexpr bool operator<(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y < p_vec2.y) : (x < p_vec2.x); }
  132. constexpr bool operator>(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y > p_vec2.y) : (x > p_vec2.x); }
  133. constexpr bool operator<=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  134. constexpr bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  135. real_t angle() const;
  136. static Vector2 from_angle(real_t p_angle);
  137. _FORCE_INLINE_ Vector2 abs() const {
  138. return Vector2(Math::abs(x), Math::abs(y));
  139. }
  140. Vector2 rotated(real_t p_by) const;
  141. Vector2 orthogonal() const {
  142. return Vector2(y, -x);
  143. }
  144. Vector2 sign() const;
  145. Vector2 floor() const;
  146. Vector2 ceil() const;
  147. Vector2 round() const;
  148. Vector2 snapped(const Vector2 &p_by) const;
  149. Vector2 snappedf(real_t p_by) const;
  150. Vector2 clamp(const Vector2 &p_min, const Vector2 &p_max) const;
  151. Vector2 clampf(real_t p_min, real_t p_max) const;
  152. real_t aspect() const { return width / height; }
  153. explicit operator String() const;
  154. operator Vector2i() const;
  155. uint32_t hash() const {
  156. uint32_t h = hash_murmur3_one_real(x);
  157. h = hash_murmur3_one_real(y, h);
  158. return hash_fmix32(h);
  159. }
  160. // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
  161. constexpr Vector2() :
  162. x(0), y(0) {}
  163. constexpr Vector2(real_t p_x, real_t p_y) :
  164. x(p_x), y(p_y) {}
  165. // NOLINTEND(cppcoreguidelines-pro-type-member-init)
  166. };
  167. inline constexpr Vector2 Vector2::LEFT = { -1, 0 };
  168. inline constexpr Vector2 Vector2::RIGHT = { 1, 0 };
  169. inline constexpr Vector2 Vector2::UP = { 0, -1 };
  170. inline constexpr Vector2 Vector2::DOWN = { 0, 1 };
  171. _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
  172. return p_vec - *this * (dot(p_vec) - p_d);
  173. }
  174. constexpr Vector2 Vector2::operator+(const Vector2 &p_v) const {
  175. return Vector2(x + p_v.x, y + p_v.y);
  176. }
  177. constexpr void Vector2::operator+=(const Vector2 &p_v) {
  178. x += p_v.x;
  179. y += p_v.y;
  180. }
  181. constexpr Vector2 Vector2::operator-(const Vector2 &p_v) const {
  182. return Vector2(x - p_v.x, y - p_v.y);
  183. }
  184. constexpr void Vector2::operator-=(const Vector2 &p_v) {
  185. x -= p_v.x;
  186. y -= p_v.y;
  187. }
  188. constexpr Vector2 Vector2::operator*(const Vector2 &p_v1) const {
  189. return Vector2(x * p_v1.x, y * p_v1.y);
  190. }
  191. constexpr Vector2 Vector2::operator*(real_t p_rvalue) const {
  192. return Vector2(x * p_rvalue, y * p_rvalue);
  193. }
  194. constexpr void Vector2::operator*=(real_t p_rvalue) {
  195. x *= p_rvalue;
  196. y *= p_rvalue;
  197. }
  198. constexpr Vector2 Vector2::operator/(const Vector2 &p_v1) const {
  199. return Vector2(x / p_v1.x, y / p_v1.y);
  200. }
  201. constexpr Vector2 Vector2::operator/(real_t p_rvalue) const {
  202. return Vector2(x / p_rvalue, y / p_rvalue);
  203. }
  204. constexpr void Vector2::operator/=(real_t p_rvalue) {
  205. x /= p_rvalue;
  206. y /= p_rvalue;
  207. }
  208. constexpr Vector2 Vector2::operator-() const {
  209. return Vector2(-x, -y);
  210. }
  211. constexpr bool Vector2::operator==(const Vector2 &p_vec2) const {
  212. return x == p_vec2.x && y == p_vec2.y;
  213. }
  214. constexpr bool Vector2::operator!=(const Vector2 &p_vec2) const {
  215. return x != p_vec2.x || y != p_vec2.y;
  216. }
  217. Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const {
  218. Vector2 res = *this;
  219. res.x = Math::lerp(res.x, p_to.x, p_weight);
  220. res.y = Math::lerp(res.y, p_to.y, p_weight);
  221. return res;
  222. }
  223. Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
  224. real_t start_length_sq = length_squared();
  225. real_t end_length_sq = p_to.length_squared();
  226. if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
  227. // Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
  228. return lerp(p_to, p_weight);
  229. }
  230. real_t start_length = Math::sqrt(start_length_sq);
  231. real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
  232. real_t angle = angle_to(p_to);
  233. return rotated(angle * p_weight) * (result_length / start_length);
  234. }
  235. Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const {
  236. Vector2 res = *this;
  237. res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
  238. res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
  239. return res;
  240. }
  241. Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const {
  242. Vector2 res = *this;
  243. res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  244. res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  245. return res;
  246. }
  247. Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const {
  248. Vector2 res = *this;
  249. res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
  250. res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
  251. return res;
  252. }
  253. Vector2 Vector2::bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const {
  254. Vector2 res = *this;
  255. res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
  256. res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
  257. return res;
  258. }
  259. Vector2 Vector2::direction_to(const Vector2 &p_to) const {
  260. Vector2 ret(p_to.x - x, p_to.y - y);
  261. ret.normalize();
  262. return ret;
  263. }
  264. // Multiplication operators required to workaround issues with LLVM using implicit conversion
  265. // to Vector2i instead for integers where it should not.
  266. constexpr Vector2 operator*(float p_scalar, const Vector2 &p_vec) {
  267. return p_vec * p_scalar;
  268. }
  269. constexpr Vector2 operator*(double p_scalar, const Vector2 &p_vec) {
  270. return p_vec * p_scalar;
  271. }
  272. constexpr Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) {
  273. return p_vec * p_scalar;
  274. }
  275. constexpr Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) {
  276. return p_vec * p_scalar;
  277. }
  278. typedef Vector2 Size2;
  279. typedef Vector2 Point2;
  280. template <>
  281. struct is_zero_constructible<Vector2> : std::true_type {};