vector2.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*************************************************************************/
  2. /* vector2.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/math.hpp>
  33. #include <godot_cpp/variant/string.hpp>
  34. namespace godot {
  35. class Vector2i;
  36. class Vector2 {
  37. public:
  38. _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
  39. enum Axis {
  40. AXIS_X,
  41. AXIS_Y,
  42. };
  43. union {
  44. real_t x = 0;
  45. real_t width;
  46. };
  47. union {
  48. real_t y = 0;
  49. real_t height;
  50. };
  51. inline real_t &operator[](int p_idx) {
  52. return p_idx ? y : x;
  53. }
  54. inline const real_t &operator[](int p_idx) const {
  55. return p_idx ? y : x;
  56. }
  57. void normalize();
  58. Vector2 normalized() const;
  59. bool is_normalized() const;
  60. real_t length() const;
  61. real_t length_squared() const;
  62. Vector2 min(const Vector2 &p_vector2) const {
  63. return Vector2(Math::min(x, p_vector2.x), Math::min(y, p_vector2.y));
  64. }
  65. Vector2 max(const Vector2 &p_vector2) const {
  66. return Vector2(Math::max(x, p_vector2.x), Math::max(y, p_vector2.y));
  67. }
  68. real_t distance_to(const Vector2 &p_vector2) const;
  69. real_t distance_squared_to(const Vector2 &p_vector2) const;
  70. real_t angle_to(const Vector2 &p_vector2) const;
  71. real_t angle_to_point(const Vector2 &p_vector2) const;
  72. inline Vector2 direction_to(const Vector2 &p_to) const;
  73. real_t dot(const Vector2 &p_other) const;
  74. real_t cross(const Vector2 &p_other) const;
  75. Vector2 posmod(const real_t p_mod) const;
  76. Vector2 posmodv(const Vector2 &p_modv) const;
  77. Vector2 project(const Vector2 &p_to) const;
  78. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  79. Vector2 clamped(real_t p_len) const;
  80. inline Vector2 lerp(const Vector2 &p_to, real_t p_weight) const;
  81. inline Vector2 slerp(const Vector2 &p_to, real_t p_weight) const;
  82. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const;
  83. Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
  84. Vector2 slide(const Vector2 &p_normal) const;
  85. Vector2 bounce(const Vector2 &p_normal) const;
  86. Vector2 reflect(const Vector2 &p_normal) const;
  87. bool is_equal_approx(const Vector2 &p_v) const;
  88. Vector2 operator+(const Vector2 &p_v) const;
  89. void operator+=(const Vector2 &p_v);
  90. Vector2 operator-(const Vector2 &p_v) const;
  91. void operator-=(const Vector2 &p_v);
  92. Vector2 operator*(const Vector2 &p_v1) const;
  93. Vector2 operator*(const real_t &rvalue) const;
  94. void operator*=(const real_t &rvalue);
  95. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  96. Vector2 operator/(const Vector2 &p_v1) const;
  97. Vector2 operator/(const real_t &rvalue) const;
  98. void operator/=(const real_t &rvalue);
  99. void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; }
  100. Vector2 operator-() const;
  101. bool operator==(const Vector2 &p_vec2) const;
  102. bool operator!=(const Vector2 &p_vec2) const;
  103. bool operator<(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y < p_vec2.y) : (x < p_vec2.x); }
  104. bool operator>(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y > p_vec2.y) : (x > p_vec2.x); }
  105. bool operator<=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
  106. bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
  107. real_t angle() const;
  108. inline Vector2 abs() const {
  109. return Vector2(Math::abs(x), Math::abs(y));
  110. }
  111. Vector2 rotated(real_t p_by) const;
  112. Vector2 orthogonal() const {
  113. return Vector2(y, -x);
  114. }
  115. Vector2 sign() const;
  116. Vector2 floor() const;
  117. Vector2 ceil() const;
  118. Vector2 round() const;
  119. Vector2 snapped(const Vector2 &p_by) const;
  120. real_t aspect() const { return width / height; }
  121. operator String() const;
  122. inline Vector2() {}
  123. inline Vector2(real_t p_x, real_t p_y) {
  124. x = p_x;
  125. y = p_y;
  126. }
  127. };
  128. inline Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
  129. return p_vec - *this * (dot(p_vec) - p_d);
  130. }
  131. inline Vector2 operator*(float p_scalar, const Vector2 &p_vec) {
  132. return p_vec * p_scalar;
  133. }
  134. inline Vector2 operator*(double p_scalar, const Vector2 &p_vec) {
  135. return p_vec * p_scalar;
  136. }
  137. inline Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) {
  138. return p_vec * p_scalar;
  139. }
  140. inline Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) {
  141. return p_vec * p_scalar;
  142. }
  143. inline Vector2 Vector2::operator+(const Vector2 &p_v) const {
  144. return Vector2(x + p_v.x, y + p_v.y);
  145. }
  146. inline void Vector2::operator+=(const Vector2 &p_v) {
  147. x += p_v.x;
  148. y += p_v.y;
  149. }
  150. inline Vector2 Vector2::operator-(const Vector2 &p_v) const {
  151. return Vector2(x - p_v.x, y - p_v.y);
  152. }
  153. inline void Vector2::operator-=(const Vector2 &p_v) {
  154. x -= p_v.x;
  155. y -= p_v.y;
  156. }
  157. inline Vector2 Vector2::operator*(const Vector2 &p_v1) const {
  158. return Vector2(x * p_v1.x, y * p_v1.y);
  159. }
  160. inline Vector2 Vector2::operator*(const real_t &rvalue) const {
  161. return Vector2(x * rvalue, y * rvalue);
  162. }
  163. inline void Vector2::operator*=(const real_t &rvalue) {
  164. x *= rvalue;
  165. y *= rvalue;
  166. }
  167. inline Vector2 Vector2::operator/(const Vector2 &p_v1) const {
  168. return Vector2(x / p_v1.x, y / p_v1.y);
  169. }
  170. inline Vector2 Vector2::operator/(const real_t &rvalue) const {
  171. return Vector2(x / rvalue, y / rvalue);
  172. }
  173. inline void Vector2::operator/=(const real_t &rvalue) {
  174. x /= rvalue;
  175. y /= rvalue;
  176. }
  177. inline Vector2 Vector2::operator-() const {
  178. return Vector2(-x, -y);
  179. }
  180. inline bool Vector2::operator==(const Vector2 &p_vec2) const {
  181. return x == p_vec2.x && y == p_vec2.y;
  182. }
  183. inline bool Vector2::operator!=(const Vector2 &p_vec2) const {
  184. return x != p_vec2.x || y != p_vec2.y;
  185. }
  186. Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const {
  187. Vector2 res = *this;
  188. res.x += (p_weight * (p_to.x - x));
  189. res.y += (p_weight * (p_to.y - y));
  190. return res;
  191. }
  192. Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
  193. #ifdef MATH_CHECKS
  194. ERR_FAIL_COND_V(!is_normalized(), Vector2());
  195. #endif
  196. real_t theta = angle_to(p_to);
  197. return rotated(theta * p_weight);
  198. }
  199. Vector2 Vector2::direction_to(const Vector2 &p_to) const {
  200. Vector2 ret(p_to.x - x, p_to.y - y);
  201. ret.normalize();
  202. return ret;
  203. }
  204. typedef Vector2 Size2;
  205. typedef Vector2 Point2;
  206. } // namespace godot
  207. #endif // GODOT_VECTOR2_HPP