Vector2.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #ifndef VECTOR2_H
  2. #define VECTOR2_H
  3. #include <gdnative/vector2.h>
  4. #include "Defs.hpp"
  5. #include <cmath>
  6. namespace godot {
  7. class String;
  8. struct Vector2 {
  9. union {
  10. real_t x;
  11. real_t width;
  12. };
  13. union {
  14. real_t y;
  15. real_t height;
  16. };
  17. inline Vector2(real_t p_x, real_t p_y) {
  18. x = p_x;
  19. y = p_y;
  20. }
  21. inline Vector2() {
  22. x = 0;
  23. y = 0;
  24. }
  25. inline real_t &operator[](int p_idx) {
  26. return p_idx ? y : x;
  27. }
  28. inline const real_t &operator[](int p_idx) const {
  29. return p_idx ? y : x;
  30. }
  31. inline Vector2 operator+(const Vector2 &p_v) const {
  32. return Vector2(x + p_v.x, y + p_v.y);
  33. }
  34. inline void operator+=(const Vector2 &p_v) {
  35. x += p_v.x;
  36. y += p_v.y;
  37. }
  38. inline Vector2 operator-(const Vector2 &p_v) const {
  39. return Vector2(x - p_v.x, y - p_v.y);
  40. }
  41. inline void operator-=(const Vector2 &p_v) {
  42. x -= p_v.x;
  43. y -= p_v.y;
  44. }
  45. inline Vector2 operator*(const Vector2 &p_v1) const {
  46. return Vector2(x * p_v1.x, y * p_v1.y);
  47. }
  48. inline Vector2 operator*(const real_t &rvalue) const {
  49. return Vector2(x * rvalue, y * rvalue);
  50. }
  51. inline void operator*=(const real_t &rvalue) {
  52. x *= rvalue;
  53. y *= rvalue;
  54. }
  55. inline void operator*=(const Vector2 &rvalue) {
  56. *this = *this * rvalue;
  57. }
  58. inline Vector2 operator/(const Vector2 &p_v1) const {
  59. return Vector2(x / p_v1.x, y / p_v1.y);
  60. }
  61. inline Vector2 operator/(const real_t &rvalue) const {
  62. return Vector2(x / rvalue, y / rvalue);
  63. }
  64. inline void operator/=(const real_t &rvalue) {
  65. x /= rvalue;
  66. y /= rvalue;
  67. }
  68. inline Vector2 operator-() const {
  69. return Vector2(-x, -y);
  70. }
  71. bool operator==(const Vector2 &p_vec2) const;
  72. bool operator!=(const Vector2 &p_vec2) const;
  73. inline bool operator<(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
  74. inline bool operator<=(const Vector2 &p_vec2) const { return (x == p_vec2.x) ? (y <= p_vec2.y) : (x <= p_vec2.x); }
  75. inline void normalize() {
  76. real_t l = x * x + y * y;
  77. if (l != 0) {
  78. l = sqrt(l);
  79. x /= l;
  80. y /= l;
  81. }
  82. }
  83. inline Vector2 normalized() const {
  84. Vector2 v = *this;
  85. v.normalize();
  86. return v;
  87. }
  88. inline real_t length() const {
  89. return sqrt(x * x + y * y);
  90. }
  91. inline real_t length_squared() const {
  92. return x * x + y * y;
  93. }
  94. inline real_t distance_to(const Vector2 &p_vector2) const {
  95. return sqrt((x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y));
  96. }
  97. inline real_t distance_squared_to(const Vector2 &p_vector2) const {
  98. return (x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y);
  99. }
  100. inline real_t angle_to(const Vector2 &p_vector2) const {
  101. return atan2(cross(p_vector2), dot(p_vector2));
  102. }
  103. inline real_t angle_to_point(const Vector2 &p_vector2) const {
  104. return atan2(y - p_vector2.y, x - p_vector2.x);
  105. }
  106. inline Vector2 direction_to(const Vector2 &p_b) const {
  107. Vector2 ret(p_b.x - x, p_b.y - y);
  108. ret.normalize();
  109. return ret;
  110. }
  111. inline real_t dot(const Vector2 &p_other) const {
  112. return x * p_other.x + y * p_other.y;
  113. }
  114. inline real_t cross(const Vector2 &p_other) const {
  115. return x * p_other.y - y * p_other.x;
  116. }
  117. inline Vector2 cross(real_t p_other) const {
  118. return Vector2(p_other * y, -p_other * x);
  119. }
  120. Vector2 project(const Vector2 &p_vec) const;
  121. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  122. Vector2 clamped(real_t p_len) const;
  123. static inline Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) {
  124. Vector2 res = p_a;
  125. res.x += (p_t * (p_b.x - p_a.x));
  126. res.y += (p_t * (p_b.y - p_a.y));
  127. return res;
  128. }
  129. inline Vector2 linear_interpolate(const Vector2 &p_b, real_t p_t) const {
  130. Vector2 res = *this;
  131. res.x += (p_t * (p_b.x - x));
  132. res.y += (p_t * (p_b.y - y));
  133. return res;
  134. }
  135. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
  136. Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const {
  137. Vector2 v = *this;
  138. Vector2 vd = p_to - v;
  139. real_t len = vd.length();
  140. return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta;
  141. }
  142. inline Vector2 slide(const Vector2 &p_vec) const {
  143. return p_vec - *this * this->dot(p_vec);
  144. }
  145. inline Vector2 bounce(const Vector2 &p_normal) const {
  146. return -reflect(p_normal);
  147. }
  148. inline Vector2 reflect(const Vector2 &p_vec) const {
  149. return p_vec - *this * this->dot(p_vec) * 2.0;
  150. }
  151. inline real_t angle() const {
  152. return atan2(y, x);
  153. }
  154. inline void set_rotation(real_t p_radians) {
  155. x = cosf(p_radians);
  156. y = sinf(p_radians);
  157. }
  158. inline Vector2 abs() const {
  159. return Vector2(fabs(x), fabs(y));
  160. }
  161. inline Vector2 rotated(real_t p_by) const {
  162. Vector2 v;
  163. v.set_rotation(angle() + p_by);
  164. v *= length();
  165. return v;
  166. }
  167. inline Vector2 tangent() const {
  168. return Vector2(y, -x);
  169. }
  170. inline Vector2 floor() const {
  171. return Vector2(::floor(x), ::floor(y));
  172. }
  173. inline Vector2 snapped(const Vector2 &p_by) const {
  174. return Vector2(
  175. p_by.x != 0 ? ::floor(x / p_by.x + 0.5) * p_by.x : x,
  176. p_by.y != 0 ? ::floor(y / p_by.y + 0.5) * p_by.y : y);
  177. }
  178. inline real_t aspect() const { return width / height; }
  179. operator String() const;
  180. };
  181. inline Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
  182. return p_vec * p_scalar;
  183. }
  184. } // namespace godot
  185. #endif // VECTOR2_H