github_url: | hide |
---|
A 2×3 matrix representing a 2D transformation.
.. rst-class:: classref-introduction-group
A 2×3 matrix (2 rows, 3 columns) used for 2D linear transformations. It can represent transformations such as translation, rotation, and scaling. It consists of three :ref:`Vector2<class_Vector2>` values: :ref:`x<class_Transform2D_property_x>`, :ref:`y<class_Transform2D_property_y>`, and the :ref:`origin<class_Transform2D_property_origin>`.
For more information, read the "Matrices and transforms" documentation article.
Note
There are notable differences when using this API with C#. See :ref:`doc_c_sharp_differences` for more information.
.. rst-class:: classref-introduction-group
- :doc:`Math documentation index <../tutorials/math/index>`
- :doc:`Matrices and transforms <../tutorials/math/matrices_and_transforms>`
- Matrix Transform Demo
- 2.5D Demo
.. rst-class:: classref-reftable-group
.. rst-class:: classref-reftable-group
.. rst-class:: classref-reftable-group
.. rst-class:: classref-reftable-group
.. rst-class:: classref-section-separator
.. rst-class:: classref-descriptions-group
.. rst-class:: classref-constant
IDENTITY = Transform2D(1, 0, 0, 1, 0, 0)
The identity Transform2D with no translation, rotation or scaling applied. When applied to other data structures, :ref:`IDENTITY<class_Transform2D_constant_IDENTITY>` performs no transformation.
.. rst-class:: classref-constant
FLIP_X = Transform2D(-1, 0, 0, 1, 0, 0)
The Transform2D that will flip something along the X axis.
.. rst-class:: classref-constant
FLIP_Y = Transform2D(1, 0, 0, -1, 0, 0)
The Transform2D that will flip something along the Y axis.
.. rst-class:: classref-section-separator
.. rst-class:: classref-descriptions-group
.. rst-class:: classref-property
:ref:`Vector2<class_Vector2>` origin = Vector2(0, 0)
The origin vector (column 2, the third column). Equivalent to array index 2
. The origin vector represents translation.
.. rst-class:: classref-item-separator
.. rst-class:: classref-property
:ref:`Vector2<class_Vector2>` x = Vector2(1, 0)
The basis matrix's X vector (column 0). Equivalent to array index 0
.
.. rst-class:: classref-item-separator
.. rst-class:: classref-property
:ref:`Vector2<class_Vector2>` y = Vector2(0, 1)
The basis matrix's Y vector (column 1). Equivalent to array index 1
.
.. rst-class:: classref-section-separator
.. rst-class:: classref-descriptions-group
.. rst-class:: classref-constructor
:ref:`Transform2D<class_Transform2D>` Transform2D ( )
Constructs a default-initialized Transform2D set to :ref:`IDENTITY<class_Transform2D_constant_IDENTITY>`.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Transform2D<class_Transform2D>` Transform2D ( :ref:`Transform2D<class_Transform2D>` from )
Constructs a Transform2D as a copy of the given Transform2D.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Transform2D<class_Transform2D>` Transform2D ( :ref:`float<class_float>` rotation, :ref:`Vector2<class_Vector2>` position )
Constructs the transform from a given angle (in radians) and position.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Transform2D<class_Transform2D>` Transform2D ( :ref:`float<class_float>` rotation, :ref:`Vector2<class_Vector2>` scale, :ref:`float<class_float>` skew, :ref:`Vector2<class_Vector2>` position )
Constructs the transform from a given angle (in radians), scale, skew (in radians) and position.
.. rst-class:: classref-item-separator
.. rst-class:: classref-constructor
:ref:`Transform2D<class_Transform2D>` Transform2D ( :ref:`Vector2<class_Vector2>` x_axis, :ref:`Vector2<class_Vector2>` y_axis, :ref:`Vector2<class_Vector2>` origin )
Constructs the transform from 3 :ref:`Vector2<class_Vector2>` values representing :ref:`x<class_Transform2D_property_x>`, :ref:`y<class_Transform2D_property_y>`, and the :ref:`origin<class_Transform2D_property_origin>` (the three column vectors).
.. rst-class:: classref-section-separator
.. rst-class:: classref-descriptions-group
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` affine_inverse ( ) |const|
Returns the inverse of the transform, under the assumption that the transformation is composed of rotation, scaling and translation.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Vector2<class_Vector2>` basis_xform ( :ref:`Vector2<class_Vector2>` v ) |const|
Returns a vector transformed (multiplied) by the basis matrix.
This method does not account for translation (the origin vector).
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Vector2<class_Vector2>` basis_xform_inv ( :ref:`Vector2<class_Vector2>` v ) |const|
Returns a vector transformed (multiplied) by the inverse basis matrix.
This method does not account for translation (the origin vector).
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`float<class_float>` determinant ( ) |const|
Returns the determinant of the basis matrix. If the basis is uniformly scaled, then its determinant equals the square of the scale factor.
A negative determinant means the basis was flipped, so one part of the scale is negative. A zero determinant means the basis isn't invertible, and is usually considered invalid.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Vector2<class_Vector2>` get_origin ( ) |const|
Returns the transform's origin (translation).
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`float<class_float>` get_rotation ( ) |const|
Returns the transform's rotation (in radians).
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Vector2<class_Vector2>` get_scale ( ) |const|
Returns the scale.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`float<class_float>` get_skew ( ) |const|
Returns the transform's skew (in radians).
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` interpolate_with ( :ref:`Transform2D<class_Transform2D>` xform, :ref:`float<class_float>` weight ) |const|
Returns a transform interpolated between this transform and another by a given weight
(on the range of 0.0 to 1.0).
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` inverse ( ) |const|
Returns the inverse of the transform, under the assumption that the transformation is composed of rotation and translation (no scaling, use :ref:`affine_inverse<class_Transform2D_method_affine_inverse>` for transforms with scaling).
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`bool<class_bool>` is_equal_approx ( :ref:`Transform2D<class_Transform2D>` xform ) |const|
Returns true
if this transform and xform
are approximately equal, by calling is_equal_approx
on each component.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`bool<class_bool>` is_finite ( ) |const|
Returns true
if this transform is finite, by calling :ref:`@GlobalScope.is_finite<class_@GlobalScope_method_is_finite>` on each component.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` looking_at ( :ref:`Vector2<class_Vector2>` target=Vector2(0, 0) ) |const|
Returns a copy of the transform rotated such that the rotated X-axis points towards the target
position.
Operations take place in global space.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` orthonormalized ( ) |const|
Returns the transform with the basis orthogonal (90 degrees), and normalized axis vectors (scale of 1 or -1).
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` rotated ( :ref:`float<class_float>` angle ) |const|
Returns a copy of the transform rotated by the given angle
(in radians).
This method is an optimized version of multiplying the given transform X
with a corresponding rotation transform R
from the left, i.e., R * X
.
This can be seen as transforming with respect to the global/parent frame.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` rotated_local ( :ref:`float<class_float>` angle ) |const|
Returns a copy of the transform rotated by the given angle
(in radians).
This method is an optimized version of multiplying the given transform X
with a corresponding rotation transform R
from the right, i.e., X * R
.
This can be seen as transforming with respect to the local frame.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` scaled ( :ref:`Vector2<class_Vector2>` scale ) |const|
Returns a copy of the transform scaled by the given scale
factor.
This method is an optimized version of multiplying the given transform X
with a corresponding scaling transform S
from the left, i.e., S * X
.
This can be seen as transforming with respect to the global/parent frame.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` scaled_local ( :ref:`Vector2<class_Vector2>` scale ) |const|
Returns a copy of the transform scaled by the given scale
factor.
This method is an optimized version of multiplying the given transform X
with a corresponding scaling transform S
from the right, i.e., X * S
.
This can be seen as transforming with respect to the local frame.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` translated ( :ref:`Vector2<class_Vector2>` offset ) |const|
Returns a copy of the transform translated by the given offset
.
This method is an optimized version of multiplying the given transform X
with a corresponding translation transform T
from the left, i.e., T * X
.
This can be seen as transforming with respect to the global/parent frame.
.. rst-class:: classref-item-separator
.. rst-class:: classref-method
:ref:`Transform2D<class_Transform2D>` translated_local ( :ref:`Vector2<class_Vector2>` offset ) |const|
Returns a copy of the transform translated by the given offset
.
This method is an optimized version of multiplying the given transform X
with a corresponding translation transform T
from the right, i.e., X * T
.
This can be seen as transforming with respect to the local frame.
.. rst-class:: classref-section-separator
.. rst-class:: classref-descriptions-group
.. rst-class:: classref-operator
:ref:`bool<class_bool>` operator != ( :ref:`Transform2D<class_Transform2D>` right )
Returns true
if the transforms are not equal.
Note: Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Transform2D_method_is_equal_approx>` instead, which is more reliable.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`PackedVector2Array<class_PackedVector2Array>` operator * ( :ref:`PackedVector2Array<class_PackedVector2Array>` right )
Transforms (multiplies) each element of the :ref:`Vector2<class_Vector2>` array by the given Transform2D matrix.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`Rect2<class_Rect2>` operator * ( :ref:`Rect2<class_Rect2>` right )
Transforms (multiplies) the :ref:`Rect2<class_Rect2>` by the given Transform2D matrix.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`Transform2D<class_Transform2D>` operator * ( :ref:`Transform2D<class_Transform2D>` right )
Composes these two transformation matrices by multiplying them together. This has the effect of transforming the second transform (the child) by the first transform (the parent).
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`Vector2<class_Vector2>` operator * ( :ref:`Vector2<class_Vector2>` right )
Transforms (multiplies) the :ref:`Vector2<class_Vector2>` by the given Transform2D matrix.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`Transform2D<class_Transform2D>` operator * ( :ref:`float<class_float>` right )
This operator multiplies all components of the Transform2D, including the origin vector, which scales it uniformly.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`Transform2D<class_Transform2D>` operator * ( :ref:`int<class_int>` right )
This operator multiplies all components of the Transform2D, including the origin vector, which scales it uniformly.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`bool<class_bool>` operator == ( :ref:`Transform2D<class_Transform2D>` right )
Returns true
if the transforms are exactly equal.
Note: Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Transform2D_method_is_equal_approx>` instead, which is more reliable.
.. rst-class:: classref-item-separator
.. rst-class:: classref-operator
:ref:`Vector2<class_Vector2>` operator [] ( :ref:`int<class_int>` index )
Access transform components using their index. t[0]
is equivalent to t.x
, t[1]
is equivalent to t.y
, and t[2]
is equivalent to t.origin
.