Vector2.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 real_t dot(const Vector2 &p_other) const {
  107. return x * p_other.x + y * p_other.y;
  108. }
  109. inline real_t cross(const Vector2 &p_other) const {
  110. return x * p_other.y - y * p_other.x;
  111. }
  112. inline Vector2 cross(real_t p_other) const {
  113. return Vector2(p_other * y, -p_other * x);
  114. }
  115. Vector2 project(const Vector2 &p_vec) const;
  116. Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
  117. Vector2 clamped(real_t p_len) const;
  118. static inline Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) {
  119. Vector2 res = p_a;
  120. res.x += (p_t * (p_b.x - p_a.x));
  121. res.y += (p_t * (p_b.y - p_a.y));
  122. return res;
  123. }
  124. inline Vector2 linear_interpolate(const Vector2 &p_b, real_t p_t) const {
  125. Vector2 res = *this;
  126. res.x += (p_t * (p_b.x - x));
  127. res.y += (p_t * (p_b.y - y));
  128. return res;
  129. }
  130. Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
  131. inline Vector2 slide(const Vector2 &p_vec) const {
  132. return p_vec - *this * this->dot(p_vec);
  133. }
  134. inline Vector2 reflect(const Vector2 &p_vec) const {
  135. return p_vec - *this * this->dot(p_vec) * 2.0;
  136. }
  137. inline real_t angle() const {
  138. return atan2(y, x);
  139. }
  140. inline void set_rotation(real_t p_radians) {
  141. x = cosf(p_radians);
  142. y = sinf(p_radians);
  143. }
  144. inline Vector2 abs() const {
  145. return Vector2(fabs(x), fabs(y));
  146. }
  147. inline Vector2 rotated(real_t p_by) const {
  148. Vector2 v;
  149. v.set_rotation(angle() + p_by);
  150. v *= length();
  151. return v;
  152. }
  153. inline Vector2 tangent() const {
  154. return Vector2(y, -x);
  155. }
  156. inline Vector2 floor() const {
  157. return Vector2(::floor(x), ::floor(y));
  158. }
  159. inline Vector2 snapped(const Vector2 &p_by) const;
  160. inline real_t aspect() const { return width / height; }
  161. operator String() const;
  162. };
  163. inline Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
  164. return p_vec * p_scalar;
  165. }
  166. } // namespace godot
  167. #endif // VECTOR2_H