vector2.hpp 8.1 KB

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