Vector2.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #ifndef VECTOR2_H
  2. #define VECTOR2_H
  3. #include <cmath>
  4. #include <godot/godot_vector2.h>
  5. #include "String.h"
  6. namespace godot {
  7. typedef float real_t; // @Todo move to a global Godot.h
  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. real_t& operator[](int p_idx) {
  18. return p_idx?y:x;
  19. }
  20. const real_t& operator[](int p_idx) const {
  21. return p_idx?y:x;
  22. }
  23. Vector2 operator+(const Vector2& p_v) const
  24. {
  25. return Vector2(x + p_v.x, y + p_v.y);
  26. }
  27. void operator+=(const Vector2& p_v)
  28. {
  29. x += p_v.x;
  30. y += p_v.y;
  31. }
  32. Vector2 operator-(const Vector2& p_v) const
  33. {
  34. return Vector2(x - p_v.x, y - p_v.y);
  35. }
  36. void operator-=(const Vector2& p_v)
  37. {
  38. x -= p_v.x;
  39. y -= p_v.y;
  40. }
  41. Vector2 operator*(const Vector2 &p_v1) const
  42. {
  43. return Vector2(x * p_v1.x, y * p_v1.y);
  44. }
  45. Vector2 operator*(const real_t &rvalue) const
  46. {
  47. return Vector2(x * rvalue, y * rvalue);
  48. }
  49. void operator*=(const real_t &rvalue)
  50. {
  51. x *= rvalue;
  52. y *= rvalue;
  53. }
  54. void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  55. Vector2 operator/(const Vector2 &p_v1) const
  56. {
  57. return Vector2(x / p_v1.x, y / p_v1.y);
  58. }
  59. Vector2 operator/(const real_t &rvalue) const
  60. {
  61. return Vector2(x / rvalue, y / rvalue);
  62. }
  63. void operator/=(const real_t &rvalue)
  64. {
  65. x /= rvalue;
  66. y /= rvalue;
  67. }
  68. Vector2 operator-() const
  69. {
  70. return Vector2(-x, -y);
  71. }
  72. bool operator==(const Vector2& p_vec2) const
  73. {
  74. return x == p_vec2.x && y == p_vec2.y;
  75. }
  76. bool operator!=(const Vector2& p_vec2) const
  77. {
  78. return x != p_vec2.x || y != p_vec2.y;
  79. }
  80. bool operator<(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<p_vec2.y):(x<p_vec2.x); }
  81. bool operator<=(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<=p_vec2.y):(x<=p_vec2.x); }
  82. void normalize()
  83. {
  84. real_t l = x*x + y*y;
  85. if (l != 0) {
  86. l = (l);
  87. x /= l;
  88. y /= l;
  89. }
  90. }
  91. Vector2 normalized() const
  92. {
  93. Vector2 v = *this;
  94. v.normalize();
  95. return v;
  96. }
  97. real_t length() const
  98. {
  99. return sqrt(x*x + y*y);
  100. }
  101. real_t length_squared() const
  102. {
  103. return x*x + y*y;
  104. }
  105. real_t distance_to(const Vector2& p_vector2) const
  106. {
  107. return sqrt((x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y));
  108. }
  109. real_t distance_squared_to(const Vector2& p_vector2) const
  110. {
  111. return (x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y);
  112. }
  113. real_t angle_to(const Vector2& p_vector2) const
  114. {
  115. return atan2(cross(p_vector2), dot(p_vector2));
  116. }
  117. real_t angle_to_point(const Vector2& p_vector2) const
  118. {
  119. return atan2(y - p_vector2.y, x-p_vector2.x);
  120. }
  121. real_t dot(const Vector2& p_other) const
  122. {
  123. return x * p_other.x + y * p_other.y;
  124. }
  125. real_t cross(const Vector2& p_other) const
  126. {
  127. return x * p_other.y - y * p_other.x;
  128. }
  129. Vector2 cross(real_t p_other) const
  130. {
  131. return Vector2(p_other * y, -p_other * x);
  132. }
  133. Vector2 project(const Vector2& p_vec) const
  134. {
  135. Vector2 v1 = p_vec;
  136. Vector2 v2 = *this;
  137. return v2 * (v1.dot(v2) / v2.dot(v2));
  138. }
  139. Vector2 plane_project(real_t p_d, const Vector2& p_vec) const
  140. {
  141. return p_vec - *this * ( dot(p_vec) -p_d);
  142. }
  143. Vector2 clamped(real_t p_len) const
  144. {
  145. real_t l = length();
  146. Vector2 v = *this;
  147. if (l > 0 && p_len < l) {
  148. v /= l;
  149. v *= p_len;
  150. }
  151. return v;
  152. }
  153. static Vector2 linear_interpolate(const Vector2& p_a, const Vector2& p_b,real_t p_t)
  154. {
  155. Vector2 res=p_a;
  156. res.x+= (p_t * (p_b.x-p_a.x));
  157. res.y+= (p_t * (p_b.y-p_a.y));
  158. return res;
  159. }
  160. Vector2 linear_interpolate(const Vector2& p_b,real_t p_t) const
  161. {
  162. Vector2 res=*this;
  163. res.x+= (p_t * (p_b.x-x));
  164. res.y+= (p_t * (p_b.y-y));
  165. return res;
  166. }
  167. Vector2 cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,real_t p_t) const
  168. {
  169. Vector2 p0=p_pre_a;
  170. Vector2 p1=*this;
  171. Vector2 p2=p_b;
  172. Vector2 p3=p_post_b;
  173. real_t t = p_t;
  174. real_t t2 = t * t;
  175. real_t t3 = t2 * t;
  176. Vector2 out;
  177. out = ( ( p1 * 2.0) +
  178. ( -p0 + p2 ) * t +
  179. ( p0 * 2.0 - p1 * 5.0 + p2 * 4 - p3 ) * t2 +
  180. ( -p0 + p1 * 3.0 - p2 * 3.0 + p3 ) * t3 ) * 0.5;
  181. return out;
  182. }
  183. // Vector2 cubic_interpolate_soft(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,real_t p_t) const;
  184. Vector2 slide(const Vector2& p_vec) const
  185. {
  186. return p_vec - *this * this->dot(p_vec);
  187. }
  188. Vector2 reflect(const Vector2& p_vec) const
  189. {
  190. return p_vec - *this * this->dot(p_vec) * 2.0;
  191. }
  192. real_t angle() const
  193. {
  194. return atan2(y, x);
  195. }
  196. void set_rotation(real_t p_radians) {
  197. x = cosf(p_radians);
  198. y = sinf(p_radians);
  199. }
  200. Vector2 abs() const {
  201. return Vector2( fabs(x), fabs(y) );
  202. }
  203. Vector2 rotated(real_t p_by) const
  204. {
  205. Vector2 v;
  206. v.set_rotation(angle() + p_by);
  207. v *= length();
  208. return v;
  209. }
  210. Vector2 tangent() const {
  211. return Vector2(y,-x);
  212. }
  213. Vector2 floor() const
  214. {
  215. return Vector2(::floor(x), ::floor(y));
  216. }
  217. Vector2 snapped(const Vector2& p_by) const
  218. {
  219. return Vector2(
  220. p_by.x != 0 ? ::floor(x / p_by.x + 0.5) * p_by.x : x,
  221. p_by.y != 0 ? ::floor(y / p_by.y + 0.5) * p_by.y : y
  222. );
  223. }
  224. real_t aspect() const { return width/height; }
  225. operator String() const { return String(); /* @Todo String::num() */ }
  226. Vector2(real_t p_x,real_t p_y) { x=p_x; y=p_y; }
  227. Vector2() { x=0; y=0; }
  228. };
  229. Vector2 operator*(real_t p_scalar, const Vector2& p_vec)
  230. {
  231. return p_vec*p_scalar;
  232. }
  233. }
  234. #endif // VECTOR2_H