vector2.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*************************************************************************/
  2. /* vector2.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 GODOT_VECTOR2_HPP
  31. #define GODOT_VECTOR2_HPP
  32. #include <godot_cpp/core/error_macros.hpp>
  33. #include <godot_cpp/core/math.hpp>
  34. namespace godot {
  35. class String;
  36. struct Vector2i;
  37. struct _NO_DISCARD_ Vector2 {
  38. static const int AXIS_COUNT = 2;
  39. enum Axis {
  40. AXIS_X,
  41. AXIS_Y,
  42. };
  43. union {
  44. struct {
  45. union {
  46. real_t x;
  47. real_t width;
  48. };
  49. union {
  50. real_t y;
  51. real_t height;
  52. };
  53. };
  54. real_t coord[2] = { 0 };
  55. };
  56. _FORCE_INLINE_ real_t &operator[](int p_idx) {
  57. DEV_ASSERT((unsigned int)p_idx < 2);
  58. return coord[p_idx];
  59. }
  60. _FORCE_INLINE_ const real_t &operator[](int p_idx) const {
  61. DEV_ASSERT((unsigned int)p_idx < 2);
  62. return coord[p_idx];
  63. }
  64. _FORCE_INLINE_ Vector2::Axis min_axis_index() const {
  65. return x < y ? Vector2::AXIS_X : Vector2::AXIS_Y;
  66. }
  67. _FORCE_INLINE_ Vector2::Axis max_axis_index() const {
  68. return x < y ? Vector2::AXIS_Y : Vector2::AXIS_X;
  69. }
  70. void normalize();
  71. Vector2 normalized() const;
  72. bool is_normalized() const;
  73. real_t length() const;
  74. real_t length_squared() const;
  75. Vector2 limit_length(const real_t p_len = 1.0) const;
  76. Vector2 min(const Vector2 &p_vector2) const {
  77. return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
  78. }
  79. Vector2 max(const Vector2 &p_vector2) const {
  80. return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
  81. }
  82. real_t distance_to(const Vector2 &p_vector2) const;
  83. real_t distance_squared_to(const Vector2 &p_vector2) const;
  84. real_t angle_to(const Vector2 &p_vector2) const;
  85. real_t angle_to_point(const Vector2 &p_vector2) const;
  86. _FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_to) const;
  87. real_t dot(const Vector2 &p_other) const;
  88. real_t cross(const Vector2 &p_other) const;
  89. Vector2 posmod(const real_t p_mod) const;
  90. Vector2 posmodv(const Vector2 &p_modv) const;
  91. Vector2 project(const Vector2 &p_to) const;
  92. Vector2 plane_project(const real_t p_d, const Vector2 &p_vec) const;
  93. _FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, const real_t p_weight) const;
  94. _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, const real_t p_weight) const;
  95. _FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const;
  96. _FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
  97. _FORCE_INLINE_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const;
  98. Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
  99. Vector2 slide(const Vector2 &p_normal) const;
  100. Vector2 bounce(const Vector2 &p_normal) const;
  101. Vector2 reflect(const Vector2 &p_normal) const;
  102. bool is_equal_approx(const Vector2 &p_v) const;
  103. bool is_zero_approx() const;
  104. Vector2 operator+(const Vector2 &p_v) const;
  105. void operator+=(const Vector2 &p_v);
  106. Vector2 operator-(const Vector2 &p_v) const;
  107. void operator-=(const Vector2 &p_v);
  108. Vector2 operator*(const Vector2 &p_v1) const;
  109. Vector2 operator*(const real_t &rvalue) const;
  110. void operator*=(const real_t &rvalue);
  111. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  112. Vector2 operator/(const Vector2 &p_v1) const;
  113. Vector2 operator/(const real_t &rvalue) const;
  114. void operator/=(const real_t &rvalue);
  115. void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; }
  116. Vector2 operator-() const;
  117. bool operator==(const Vector2 &p_vec2) const;
  118. bool operator!=(const Vector2 &p_vec2) const;
  119. bool operator<(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y < p_vec2.y) : (x < p_vec2.x); }
  120. bool operator>(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y > p_vec2.y) : (x > p_vec2.x); }
  121. bool operator<=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  122. bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  123. real_t angle() const;
  124. static Vector2 from_angle(const real_t p_angle);
  125. _FORCE_INLINE_ Vector2 abs() const {
  126. return Vector2(Math::abs(x), Math::abs(y));
  127. }
  128. Vector2 rotated(const real_t p_by) const;
  129. Vector2 orthogonal() const {
  130. return Vector2(y, -x);
  131. }
  132. Vector2 sign() const;
  133. Vector2 floor() const;
  134. Vector2 ceil() const;
  135. Vector2 round() const;
  136. Vector2 snapped(const Vector2 &p_by) const;
  137. Vector2 clamp(const Vector2 &p_min, const Vector2 &p_max) const;
  138. real_t aspect() const { return width / height; }
  139. operator String() const;
  140. operator Vector2i() const;
  141. _FORCE_INLINE_ Vector2() {}
  142. _FORCE_INLINE_ Vector2(const real_t p_x, const real_t p_y) {
  143. x = p_x;
  144. y = p_y;
  145. }
  146. };
  147. _FORCE_INLINE_ Vector2 Vector2::plane_project(const real_t p_d, const Vector2 &p_vec) const {
  148. return p_vec - *this * (dot(p_vec) - p_d);
  149. }
  150. _FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const {
  151. return Vector2(x + p_v.x, y + p_v.y);
  152. }
  153. _FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) {
  154. x += p_v.x;
  155. y += p_v.y;
  156. }
  157. _FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const {
  158. return Vector2(x - p_v.x, y - p_v.y);
  159. }
  160. _FORCE_INLINE_ void Vector2::operator-=(const Vector2 &p_v) {
  161. x -= p_v.x;
  162. y -= p_v.y;
  163. }
  164. _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const {
  165. return Vector2(x * p_v1.x, y * p_v1.y);
  166. }
  167. _FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
  168. return Vector2(x * rvalue, y * rvalue);
  169. }
  170. _FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
  171. x *= rvalue;
  172. y *= rvalue;
  173. }
  174. _FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const {
  175. return Vector2(x / p_v1.x, y / p_v1.y);
  176. }
  177. _FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const {
  178. return Vector2(x / rvalue, y / rvalue);
  179. }
  180. _FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) {
  181. x /= rvalue;
  182. y /= rvalue;
  183. }
  184. _FORCE_INLINE_ Vector2 Vector2::operator-() const {
  185. return Vector2(-x, -y);
  186. }
  187. _FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const {
  188. return x == p_vec2.x && y == p_vec2.y;
  189. }
  190. _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
  191. return x != p_vec2.x || y != p_vec2.y;
  192. }
  193. Vector2 Vector2::lerp(const Vector2 &p_to, const real_t p_weight) const {
  194. Vector2 res = *this;
  195. res.x += (p_weight * (p_to.x - x));
  196. res.y += (p_weight * (p_to.y - y));
  197. return res;
  198. }
  199. Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const {
  200. real_t start_length_sq = length_squared();
  201. real_t end_length_sq = p_to.length_squared();
  202. if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
  203. // Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
  204. return lerp(p_to, p_weight);
  205. }
  206. real_t start_length = Math::sqrt(start_length_sq);
  207. real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
  208. real_t angle = angle_to(p_to);
  209. return rotated(angle * p_weight) * (result_length / start_length);
  210. }
  211. Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const {
  212. Vector2 res = *this;
  213. res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
  214. res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
  215. return res;
  216. }
  217. Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
  218. Vector2 res = *this;
  219. 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);
  220. 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);
  221. return res;
  222. }
  223. Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const {
  224. Vector2 res = *this;
  225. /* Formula from Wikipedia article on Bezier curves. */
  226. real_t omt = (1.0 - p_t);
  227. real_t omt2 = omt * omt;
  228. real_t omt3 = omt2 * omt;
  229. real_t t2 = p_t * p_t;
  230. real_t t3 = t2 * p_t;
  231. return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
  232. }
  233. Vector2 Vector2::direction_to(const Vector2 &p_to) const {
  234. Vector2 ret(p_to.x - x, p_to.y - y);
  235. ret.normalize();
  236. return ret;
  237. }
  238. // Multiplication operators required to workaround issues with LLVM using implicit conversion
  239. // to Vector2i instead for integers where it should not.
  240. _FORCE_INLINE_ Vector2 operator*(const float p_scalar, const Vector2 &p_vec) {
  241. return p_vec * p_scalar;
  242. }
  243. _FORCE_INLINE_ Vector2 operator*(const double p_scalar, const Vector2 &p_vec) {
  244. return p_vec * p_scalar;
  245. }
  246. _FORCE_INLINE_ Vector2 operator*(const int32_t p_scalar, const Vector2 &p_vec) {
  247. return p_vec * p_scalar;
  248. }
  249. _FORCE_INLINE_ Vector2 operator*(const int64_t p_scalar, const Vector2 &p_vec) {
  250. return p_vec * p_scalar;
  251. }
  252. typedef Vector2 Size2;
  253. typedef Vector2 Point2;
  254. } // namespace godot
  255. #endif // GODOT_VECTOR2_HPP