Vector2.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 VECTOR2_H
  31. #define VECTOR2_H
  32. #include <gdnative/vector2.h>
  33. #include "Defs.hpp"
  34. #include <Math.hpp>
  35. namespace godot {
  36. class String;
  37. struct Vector2 {
  38. enum Axis {
  39. AXIS_X = 0,
  40. AXIS_Y,
  41. AXIS_COUNT
  42. };
  43. static const Vector2 ZERO;
  44. static const Vector2 ONE;
  45. static const Vector2 INF;
  46. // Coordinate system of the 2D engine
  47. static const Vector2 LEFT;
  48. static const Vector2 RIGHT;
  49. static const Vector2 UP;
  50. static const Vector2 DOWN;
  51. union {
  52. real_t x;
  53. real_t width;
  54. };
  55. union {
  56. real_t y;
  57. real_t height;
  58. };
  59. inline Vector2(real_t p_x, real_t p_y) {
  60. x = p_x;
  61. y = p_y;
  62. }
  63. inline Vector2() {
  64. x = 0;
  65. y = 0;
  66. }
  67. inline real_t &operator[](int p_idx) {
  68. return p_idx ? y : x;
  69. }
  70. inline const real_t &operator[](int p_idx) const {
  71. return p_idx ? y : x;
  72. }
  73. inline Vector2 operator+(const Vector2 &p_v) const {
  74. return Vector2(x + p_v.x, y + p_v.y);
  75. }
  76. inline void operator+=(const Vector2 &p_v) {
  77. x += p_v.x;
  78. y += p_v.y;
  79. }
  80. inline Vector2 operator-(const Vector2 &p_v) const {
  81. return Vector2(x - p_v.x, y - p_v.y);
  82. }
  83. inline void operator-=(const Vector2 &p_v) {
  84. x -= p_v.x;
  85. y -= p_v.y;
  86. }
  87. inline Vector2 operator*(const Vector2 &p_v1) const {
  88. return Vector2(x * p_v1.x, y * p_v1.y);
  89. }
  90. inline Vector2 operator*(const real_t &rvalue) const {
  91. return Vector2(x * rvalue, y * rvalue);
  92. }
  93. inline void operator*=(const real_t &rvalue) {
  94. x *= rvalue;
  95. y *= rvalue;
  96. }
  97. inline void operator*=(const Vector2 &rvalue) {
  98. *this = *this * rvalue;
  99. }
  100. inline Vector2 operator/(const Vector2 &p_v1) const {
  101. return Vector2(x / p_v1.x, y / p_v1.y);
  102. }
  103. inline Vector2 operator/(const real_t &rvalue) const {
  104. return Vector2(x / rvalue, y / rvalue);
  105. }
  106. inline void operator/=(const real_t &rvalue) {
  107. x /= rvalue;
  108. y /= rvalue;
  109. }
  110. inline Vector2 operator-() const {
  111. return Vector2(-x, -y);
  112. }
  113. bool operator==(const Vector2 &p_vec2) const;
  114. bool operator!=(const Vector2 &p_vec2) const;
  115. inline bool operator<(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  116. inline bool operator<=(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y <= p_vec2.y) : (x <= p_vec2.x); }
  117. inline void normalize() {
  118. real_t l = x * x + y * y;
  119. if (l != 0) {
  120. l = sqrt(l);
  121. x /= l;
  122. y /= l;
  123. }
  124. }
  125. inline Vector2 normalized() const {
  126. Vector2 v = *this;
  127. v.normalize();
  128. return v;
  129. }
  130. inline real_t length() const {
  131. return sqrt(x * x + y * y);
  132. }
  133. inline real_t length_squared() const {
  134. return x * x + y * y;
  135. }
  136. inline real_t distance_to(const Vector2 &p_vector2) const {
  137. return sqrt((x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y));
  138. }
  139. inline real_t distance_squared_to(const Vector2 &p_vector2) const {
  140. return (x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y);
  141. }
  142. inline real_t angle_to(const Vector2 &p_vector2) const {
  143. return atan2(cross(p_vector2), dot(p_vector2));
  144. }
  145. inline real_t angle_to_point(const Vector2 &p_vector2) const {
  146. return atan2(y - p_vector2.y, x - p_vector2.x);
  147. }
  148. inline Vector2 direction_to(const Vector2 &p_b) const {
  149. Vector2 ret(p_b.x - x, p_b.y - y);
  150. ret.normalize();
  151. return ret;
  152. }
  153. inline real_t dot(const Vector2 &p_other) const {
  154. return x * p_other.x + y * p_other.y;
  155. }
  156. inline real_t cross(const Vector2 &p_other) const {
  157. return x * p_other.y - y * p_other.x;
  158. }
  159. inline Vector2 cross(real_t p_other) const {
  160. return Vector2(p_other * y, -p_other * x);
  161. }
  162. Vector2 project(const Vector2 &p_vec) const;
  163. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  164. Vector2 clamped(real_t p_len) const;
  165. static inline Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) {
  166. Vector2 res = p_a;
  167. res.x += (p_t * (p_b.x - p_a.x));
  168. res.y += (p_t * (p_b.y - p_a.y));
  169. return res;
  170. }
  171. inline Vector2 linear_interpolate(const Vector2 &p_b, real_t p_t) const {
  172. Vector2 res = *this;
  173. res.x += (p_t * (p_b.x - x));
  174. res.y += (p_t * (p_b.y - y));
  175. return res;
  176. }
  177. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
  178. Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const {
  179. Vector2 v = *this;
  180. Vector2 vd = p_to - v;
  181. real_t len = vd.length();
  182. return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta;
  183. }
  184. inline Vector2 slide(const Vector2 &p_vec) const {
  185. return p_vec - *this * this->dot(p_vec);
  186. }
  187. inline Vector2 bounce(const Vector2 &p_normal) const {
  188. return -reflect(p_normal);
  189. }
  190. inline Vector2 reflect(const Vector2 &p_normal) const {
  191. return -(*this - p_normal * this->dot(p_normal) * 2.0);
  192. }
  193. inline real_t angle() const {
  194. return atan2(y, x);
  195. }
  196. inline void set_rotation(real_t p_radians) {
  197. x = cosf(p_radians);
  198. y = sinf(p_radians);
  199. }
  200. inline Vector2 abs() const {
  201. return Vector2(fabs(x), fabs(y));
  202. }
  203. inline Vector2 rotated(real_t p_by) const {
  204. Vector2 v;
  205. v.set_rotation(angle() + p_by);
  206. v *= length();
  207. return v;
  208. }
  209. inline Vector2 tangent() const {
  210. return Vector2(y, -x);
  211. }
  212. inline Vector2 floor() const {
  213. return Vector2(Math::floor(x), Math::floor(y));
  214. }
  215. inline Vector2 snapped(const Vector2 &p_by) const {
  216. return Vector2(
  217. Math::stepify(x, p_by.x),
  218. Math::stepify(y, p_by.y));
  219. }
  220. inline real_t aspect() const { return width / height; }
  221. operator String() const;
  222. };
  223. inline Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
  224. return p_vec * p_scalar;
  225. }
  226. namespace Math {
  227. // Convenience, since they exist in GDScript
  228. inline Vector2 cartesian2polar(Vector2 v) {
  229. return Vector2(Math::sqrt(v.x * v.x + v.y * v.y), Math::atan2(v.y, v.x));
  230. }
  231. inline Vector2 polar2cartesian(Vector2 v) {
  232. // x == radius
  233. // y == angle
  234. return Vector2(v.x * Math::cos(v.y), v.x * Math::sin(v.y));
  235. }
  236. } // namespace Math
  237. } // namespace godot
  238. #endif // VECTOR2_H