vector2.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. operator Vector2i() const;
  123. inline Vector2() {}
  124. inline Vector2(real_t p_x, real_t p_y) {
  125. x = p_x;
  126. y = p_y;
  127. }
  128. };
  129. inline Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
  130. return p_vec - *this * (dot(p_vec) - p_d);
  131. }
  132. inline Vector2 operator*(float p_scalar, const Vector2 &p_vec) {
  133. return p_vec * (real_t)p_scalar;
  134. }
  135. inline Vector2 operator*(double p_scalar, const Vector2 &p_vec) {
  136. return p_vec * (real_t)p_scalar;
  137. }
  138. inline Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) {
  139. return p_vec * (real_t)p_scalar;
  140. }
  141. inline Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) {
  142. return p_vec * (real_t)p_scalar;
  143. }
  144. inline Vector2 Vector2::operator+(const Vector2 &p_v) const {
  145. return Vector2(x + p_v.x, y + p_v.y);
  146. }
  147. inline void Vector2::operator+=(const Vector2 &p_v) {
  148. x += p_v.x;
  149. y += p_v.y;
  150. }
  151. inline Vector2 Vector2::operator-(const Vector2 &p_v) const {
  152. return Vector2(x - p_v.x, y - p_v.y);
  153. }
  154. inline void Vector2::operator-=(const Vector2 &p_v) {
  155. x -= p_v.x;
  156. y -= p_v.y;
  157. }
  158. inline Vector2 Vector2::operator*(const Vector2 &p_v1) const {
  159. return Vector2(x * p_v1.x, y * p_v1.y);
  160. }
  161. inline Vector2 Vector2::operator*(const real_t &rvalue) const {
  162. return Vector2(x * rvalue, y * rvalue);
  163. }
  164. inline void Vector2::operator*=(const real_t &rvalue) {
  165. x *= rvalue;
  166. y *= rvalue;
  167. }
  168. inline Vector2 Vector2::operator/(const Vector2 &p_v1) const {
  169. return Vector2(x / p_v1.x, y / p_v1.y);
  170. }
  171. inline Vector2 Vector2::operator/(const real_t &rvalue) const {
  172. return Vector2(x / rvalue, y / rvalue);
  173. }
  174. inline void Vector2::operator/=(const real_t &rvalue) {
  175. x /= rvalue;
  176. y /= rvalue;
  177. }
  178. inline Vector2 Vector2::operator-() const {
  179. return Vector2(-x, -y);
  180. }
  181. inline bool Vector2::operator==(const Vector2 &p_vec2) const {
  182. return x == p_vec2.x && y == p_vec2.y;
  183. }
  184. inline bool Vector2::operator!=(const Vector2 &p_vec2) const {
  185. return x != p_vec2.x || y != p_vec2.y;
  186. }
  187. Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const {
  188. Vector2 res = *this;
  189. res.x += (p_weight * (p_to.x - x));
  190. res.y += (p_weight * (p_to.y - y));
  191. return res;
  192. }
  193. Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
  194. #ifdef MATH_CHECKS
  195. ERR_FAIL_COND_V(!is_normalized(), Vector2());
  196. #endif
  197. real_t theta = angle_to(p_to);
  198. return rotated(theta * p_weight);
  199. }
  200. Vector2 Vector2::direction_to(const Vector2 &p_to) const {
  201. Vector2 ret(p_to.x - x, p_to.y - y);
  202. ret.normalize();
  203. return ret;
  204. }
  205. typedef Vector2 Size2;
  206. typedef Vector2 Point2;
  207. } // namespace godot
  208. #endif // GODOT_VECTOR2_HPP