transform2d.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #ifndef GODOT_TRANSFORM2D_HPP
  2. #define GODOT_TRANSFORM2D_HPP
  3. #include <godot_cpp/core/error_macros.hpp>
  4. #include <godot_cpp/core/math.hpp>
  5. #include <godot_cpp/variant/packed_vector2_array.hpp>
  6. #include <godot_cpp/variant/rect2.hpp>
  7. #include <godot_cpp/variant/vector2.hpp>
  8. namespace godot {
  9. class Transform2D {
  10. public:
  11. _FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
  12. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  13. // M = (elements[0][0] elements[1][0])
  14. // (elements[0][1] elements[1][1])
  15. // 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].
  16. // 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.
  17. // This requires additional care when working with explicit indices.
  18. // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
  19. // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  20. // and angle is measure from +X to +Y in a clockwise-fashion.
  21. Vector2 elements[3];
  22. inline real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  23. inline real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  24. const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
  25. Vector2 &operator[](int p_idx) { return elements[p_idx]; }
  26. inline Vector2 get_axis(int p_axis) const {
  27. ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
  28. return elements[p_axis];
  29. }
  30. inline void set_axis(int p_axis, const Vector2 &p_vec) {
  31. ERR_FAIL_INDEX(p_axis, 3);
  32. elements[p_axis] = p_vec;
  33. }
  34. void invert();
  35. Transform2D inverse() const;
  36. void affine_invert();
  37. Transform2D affine_inverse() const;
  38. void set_rotation(real_t p_rot);
  39. real_t get_rotation() const;
  40. real_t get_skew() const;
  41. void set_skew(float p_angle);
  42. inline void set_rotation_and_scale(real_t p_rot, const Size2 &p_scale);
  43. inline void set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, float p_skew);
  44. void rotate(real_t p_phi);
  45. void scale(const Size2 &p_scale);
  46. void scale_basis(const Size2 &p_scale);
  47. void translate(real_t p_tx, real_t p_ty);
  48. void translate(const Vector2 &p_translation);
  49. real_t basis_determinant() const;
  50. Size2 get_scale() const;
  51. void set_scale(const Size2 &p_scale);
  52. inline const Vector2 &get_origin() const { return elements[2]; }
  53. inline void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
  54. Transform2D scaled(const Size2 &p_scale) const;
  55. Transform2D basis_scaled(const Size2 &p_scale) const;
  56. Transform2D translated(const Vector2 &p_offset) const;
  57. Transform2D rotated(real_t p_phi) const;
  58. Transform2D untranslated() const;
  59. void orthonormalize();
  60. Transform2D orthonormalized() const;
  61. bool is_equal_approx(const Transform2D &p_transform) const;
  62. bool operator==(const Transform2D &p_transform) const;
  63. bool operator!=(const Transform2D &p_transform) const;
  64. void operator*=(const Transform2D &p_transform);
  65. Transform2D operator*(const Transform2D &p_transform) const;
  66. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  67. inline Vector2 basis_xform(const Vector2 &p_vec) const;
  68. inline Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  69. inline Vector2 xform(const Vector2 &p_vec) const;
  70. inline Vector2 xform_inv(const Vector2 &p_vec) const;
  71. inline Rect2 xform(const Rect2 &p_rect) const;
  72. inline Rect2 xform_inv(const Rect2 &p_rect) const;
  73. inline PackedVector2Array xform(const PackedVector2Array &p_array) const;
  74. inline PackedVector2Array xform_inv(const PackedVector2Array &p_array) const;
  75. operator String() const;
  76. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
  77. elements[0][0] = xx;
  78. elements[0][1] = xy;
  79. elements[1][0] = yx;
  80. elements[1][1] = yy;
  81. elements[2][0] = ox;
  82. elements[2][1] = oy;
  83. }
  84. Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) {
  85. elements[0] = p_x;
  86. elements[1] = p_y;
  87. elements[2] = p_origin;
  88. }
  89. Transform2D(real_t p_rot, const Vector2 &p_pos);
  90. Transform2D() {
  91. elements[0][0] = 1.0;
  92. elements[1][1] = 1.0;
  93. }
  94. };
  95. Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
  96. return Vector2(
  97. tdotx(p_vec),
  98. tdoty(p_vec));
  99. }
  100. Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const {
  101. return Vector2(
  102. elements[0].dot(p_vec),
  103. elements[1].dot(p_vec));
  104. }
  105. Vector2 Transform2D::xform(const Vector2 &p_vec) const {
  106. return Vector2(
  107. tdotx(p_vec),
  108. tdoty(p_vec)) +
  109. elements[2];
  110. }
  111. Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const {
  112. Vector2 v = p_vec - elements[2];
  113. return Vector2(
  114. elements[0].dot(v),
  115. elements[1].dot(v));
  116. }
  117. Rect2 Transform2D::xform(const Rect2 &p_rect) const {
  118. Vector2 x = elements[0] * p_rect.size.x;
  119. Vector2 y = elements[1] * p_rect.size.y;
  120. Vector2 pos = xform(p_rect.position);
  121. Rect2 new_rect;
  122. new_rect.position = pos;
  123. new_rect.expand_to(pos + x);
  124. new_rect.expand_to(pos + y);
  125. new_rect.expand_to(pos + x + y);
  126. return new_rect;
  127. }
  128. void Transform2D::set_rotation_and_scale(real_t p_rot, const Size2 &p_scale) {
  129. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  130. elements[1][1] = Math::cos(p_rot) * p_scale.y;
  131. elements[1][0] = -Math::sin(p_rot) * p_scale.y;
  132. elements[0][1] = Math::sin(p_rot) * p_scale.x;
  133. }
  134. void Transform2D::set_rotation_scale_and_skew(real_t p_rot, const Size2 &p_scale, float p_skew) {
  135. elements[0][0] = Math::cos(p_rot) * p_scale.x;
  136. elements[1][1] = Math::cos(p_rot + p_skew) * p_scale.y;
  137. elements[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y;
  138. elements[0][1] = Math::sin(p_rot) * p_scale.x;
  139. }
  140. Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const {
  141. Vector2 ends[4] = {
  142. xform_inv(p_rect.position),
  143. xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  144. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  145. xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y))
  146. };
  147. Rect2 new_rect;
  148. new_rect.position = ends[0];
  149. new_rect.expand_to(ends[1]);
  150. new_rect.expand_to(ends[2]);
  151. new_rect.expand_to(ends[3]);
  152. return new_rect;
  153. }
  154. PackedVector2Array Transform2D::xform(const PackedVector2Array &p_array) const {
  155. PackedVector2Array array;
  156. array.resize(p_array.size());
  157. for (int i = 0; i < p_array.size(); ++i) {
  158. array[i] = xform(p_array[i]);
  159. }
  160. return array;
  161. }
  162. PackedVector2Array Transform2D::xform_inv(const PackedVector2Array &p_array) const {
  163. PackedVector2Array array;
  164. array.resize(p_array.size());
  165. for (int i = 0; i < p_array.size(); ++i) {
  166. array[i] = xform_inv(p_array[i]);
  167. }
  168. return array;
  169. }
  170. } // namespace godot
  171. #endif // GODOT_TRANSFORM2D_HPP