Transform2D.hpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { ERR_FAIL_INDEX_V(p_axis,3,Vector2()); return elements[p_axis]; }
  23. inline void set_axis(int p_axis,const Vector2& p_vec) { ERR_FAIL_INDEX(p_axis,3); elements[p_axis]=p_vec; }
  24. void invert();
  25. Transform2D inverse() const;
  26. void affine_invert();
  27. Transform2D affine_inverse() const;
  28. void set_rotation(real_t p_phi);
  29. real_t get_rotation() const;
  30. void set_rotation_and_scale(real_t p_phi,const Size2& p_scale);
  31. void rotate(real_t p_phi);
  32. void scale(const Size2& p_scale);
  33. void scale_basis(const Size2& p_scale);
  34. void translate( real_t p_tx, real_t p_ty);
  35. void translate( const Vector2& p_translation );
  36. real_t basis_determinant() const;
  37. Size2 get_scale() const;
  38. inline const Vector2& get_origin() const { return elements[2]; }
  39. inline void set_origin(const Vector2& p_origin) { elements[2]=p_origin; }
  40. Transform2D scaled(const Size2& p_scale) const;
  41. Transform2D basis_scaled(const Size2& p_scale) const;
  42. Transform2D translated(const Vector2& p_offset) const;
  43. Transform2D rotated(real_t p_phi) const;
  44. Transform2D untranslated() const;
  45. void orthonormalize();
  46. Transform2D orthonormalized() const;
  47. bool operator==(const Transform2D& p_transform) const;
  48. bool operator!=(const Transform2D& p_transform) const;
  49. void operator*=(const Transform2D& p_transform);
  50. Transform2D operator*(const Transform2D& p_transform) const;
  51. Transform2D interpolate_with(const Transform2D& p_transform, real_t p_c) const;
  52. Vector2 basis_xform(const Vector2& p_vec) const;
  53. Vector2 basis_xform_inv(const Vector2& p_vec) const;
  54. Vector2 xform(const Vector2& p_vec) const;
  55. Vector2 xform_inv(const Vector2& p_vec) const;
  56. Rect2 xform(const Rect2& p_vec) const;
  57. Rect2 xform_inv(const Rect2& p_vec) const;
  58. operator String() const;
  59. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy);
  60. Transform2D(real_t p_rot, const Vector2& p_pos);
  61. inline Transform2D() { elements[0][0]=1.0; elements[1][1]=1.0; }
  62. };
  63. }
  64. #endif // TRANSFORM2D_H