Transform2D.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef TRANSFORM2D_H
  2. #define TRANSFORM2D_H
  3. #include "Vector2.hpp"
  4. namespace godot {
  5. typedef Vector2 Size2;
  6. struct Rect2;
  7. struct Transform2D {
  8. static const Transform2D IDENTITY;
  9. static const Transform2D FLIP_X;
  10. static const Transform2D FLIP_Y;
  11. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  12. // M = (elements[0][0] elements[1][0])
  13. // (elements[0][1] elements[1][1])
  14. // This is such that the columns, which can be interpreted as basis vectors of the coordinate system "painted" on the object, can be accessed as elements[i].
  15. // Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to elements[1][0] here.
  16. // This requires additional care when working with explicit indices.
  17. // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
  18. // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  19. // and angle is measure from +X to +Y in a clockwise-fashion.
  20. Vector2 elements[3];
  21. inline real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  22. inline real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  23. inline const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
  24. inline Vector2 &operator[](int p_idx) { return elements[p_idx]; }
  25. inline Vector2 get_axis(int p_axis) const {
  26. ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
  27. return elements[p_axis];
  28. }
  29. inline void set_axis(int p_axis, const Vector2 &p_vec) {
  30. ERR_FAIL_INDEX(p_axis, 3);
  31. elements[p_axis] = p_vec;
  32. }
  33. void invert();
  34. Transform2D inverse() const;
  35. void affine_invert();
  36. Transform2D affine_inverse() const;
  37. void set_rotation(real_t p_phi);
  38. real_t get_rotation() const;
  39. void set_rotation_and_scale(real_t p_phi, const Size2 &p_scale);
  40. void rotate(real_t p_phi);
  41. void scale(const Size2 &p_scale);
  42. void scale_basis(const Size2 &p_scale);
  43. void translate(real_t p_tx, real_t p_ty);
  44. void translate(const Vector2 &p_translation);
  45. real_t basis_determinant() const;
  46. Size2 get_scale() const;
  47. inline const Vector2 &get_origin() const { return elements[2]; }
  48. inline void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
  49. Transform2D scaled(const Size2 &p_scale) const;
  50. Transform2D basis_scaled(const Size2 &p_scale) const;
  51. Transform2D translated(const Vector2 &p_offset) const;
  52. Transform2D rotated(real_t p_phi) const;
  53. Transform2D untranslated() const;
  54. void orthonormalize();
  55. Transform2D orthonormalized() const;
  56. bool operator==(const Transform2D &p_transform) const;
  57. bool operator!=(const Transform2D &p_transform) const;
  58. void operator*=(const Transform2D &p_transform);
  59. Transform2D operator*(const Transform2D &p_transform) const;
  60. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  61. Vector2 basis_xform(const Vector2 &p_vec) const;
  62. Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  63. Vector2 xform(const Vector2 &p_vec) const;
  64. Vector2 xform_inv(const Vector2 &p_vec) const;
  65. Rect2 xform(const Rect2 &p_vec) const;
  66. Rect2 xform_inv(const Rect2 &p_vec) const;
  67. operator String() const;
  68. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy);
  69. Transform2D(real_t p_rot, const Vector2 &p_pos);
  70. inline Transform2D() {
  71. elements[0][0] = 1.0;
  72. elements[1][1] = 1.0;
  73. }
  74. };
  75. } // namespace godot
  76. #endif // TRANSFORM2D_H