Vector2.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef VECTOR2_H
  2. #define VECTOR2_H
  3. #include <godot/godot_vector2.h>
  4. #include "Defs.hpp"
  5. namespace godot {
  6. class String;
  7. struct Vector2 {
  8. union {
  9. real_t x;
  10. real_t width;
  11. };
  12. union {
  13. real_t y;
  14. real_t height;
  15. };
  16. inline real_t& operator[](int p_idx) {
  17. return p_idx?y:x;
  18. }
  19. inline const real_t& operator[](int p_idx) const {
  20. return p_idx?y:x;
  21. }
  22. Vector2 operator+(const Vector2& p_v) const;
  23. void operator+=(const Vector2& p_v);
  24. Vector2 operator-(const Vector2& p_v) const;
  25. void operator-=(const Vector2& p_v);
  26. Vector2 operator*(const Vector2 &p_v1) const;
  27. Vector2 operator*(const real_t &rvalue) const;
  28. void operator*=(const real_t &rvalue);
  29. inline void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
  30. Vector2 operator/(const Vector2 &p_v1) const;
  31. Vector2 operator/(const real_t &rvalue) const;
  32. void operator/=(const real_t &rvalue);
  33. Vector2 operator-() const;
  34. bool operator==(const Vector2& p_vec2) const;
  35. bool operator!=(const Vector2& p_vec2) const;
  36. inline bool operator<(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<p_vec2.y):(x<p_vec2.x); }
  37. inline bool operator<=(const Vector2& p_vec2) const { return (x==p_vec2.x)?(y<=p_vec2.y):(x<=p_vec2.x); }
  38. void normalize();
  39. Vector2 normalized() const;
  40. real_t length() const;
  41. real_t length_squared() const;
  42. real_t distance_to(const Vector2& p_vector2) const;
  43. real_t distance_squared_to(const Vector2& p_vector2) const;
  44. real_t angle_to(const Vector2& p_vector2) const;
  45. real_t angle_to_point(const Vector2& p_vector2) const;
  46. real_t dot(const Vector2& p_other) const;
  47. real_t cross(const Vector2& p_other) const;
  48. Vector2 cross(real_t p_other) const;
  49. Vector2 project(const Vector2& p_vec) const;
  50. Vector2 plane_project(real_t p_d, const Vector2& p_vec) const;
  51. Vector2 clamped(real_t p_len) const;
  52. static Vector2 linear_interpolate(const Vector2& p_a, const Vector2& p_b,real_t p_t);
  53. Vector2 linear_interpolate(const Vector2& p_b,real_t p_t) const;
  54. Vector2 cubic_interpolate(const Vector2& p_b,const Vector2& p_pre_a, const Vector2& p_post_b,real_t p_t) const;
  55. Vector2 slide(const Vector2& p_vec) const;
  56. Vector2 reflect(const Vector2& p_vec) const;
  57. real_t angle() const;
  58. void set_rotation(real_t p_radians);
  59. Vector2 abs() const;
  60. Vector2 rotated(real_t p_by) const;
  61. Vector2 tangent() const;
  62. Vector2 floor() const;
  63. Vector2 snapped(const Vector2& p_by) const;
  64. inline real_t aspect() const { return width/height; }
  65. operator String() const;
  66. inline Vector2(real_t p_x,real_t p_y) { x=p_x; y=p_y; }
  67. inline Vector2() { x=0; y=0; }
  68. };
  69. inline Vector2 operator*(real_t p_scalar, const Vector2& p_vec)
  70. {
  71. return p_vec*p_scalar;
  72. }
  73. }
  74. #endif // VECTOR2_H