Transform2D.hpp 3.3 KB

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