Struct Transform2D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
2×3 matrix (2 rows, 3 columns) used for 2D linear transformations. It can represent transformations such as translation, rotation, or scaling. It consists of a three Vector2 values: x, y, and the origin.
For more information, read this documentation article: https://docs.godotengine.org/en/latest/tutorials/math/matrices_and_transforms.html
public struct Transform2D : IEquatable<Transform2D>
- Implements
- Inherited Members
Constructors
Transform2D(Vector2, Vector2, Vector2)
Constructs a transformation matrix from 3 vectors (matrix columns).
public Transform2D(Vector2 xAxis, Vector2 yAxis, Vector2 originPos)
Parameters
xAxis
Vector2The X vector, or column index 0.
yAxis
Vector2The Y vector, or column index 1.
originPos
Vector2The origin vector, or column index 2.
Transform2D(float, Vector2)
Constructs a transformation matrix from a rotation
value and
origin
vector.
public Transform2D(float rotation, Vector2 origin)
Parameters
rotation
floatThe rotation of the new transform, in radians.
origin
Vector2The origin vector, or column index 2.
Transform2D(float, Vector2, float, Vector2)
Constructs a transformation matrix from a rotation
value,
scale
vector, skew
value, and
origin
vector.
public Transform2D(float rotation, Vector2 scale, float skew, Vector2 origin)
Parameters
rotation
floatThe rotation of the new transform, in radians.
scale
Vector2The scale of the new transform.
skew
floatThe skew of the new transform, in radians.
origin
Vector2The origin vector, or column index 2.
Transform2D(float, float, float, float, float, float)
Constructs a transformation matrix from the given components.
Arguments are named such that xy is equal to calling X.Y
.
public Transform2D(float xx, float xy, float yx, float yy, float ox, float oy)
Parameters
xx
floatThe X component of the X column vector, accessed via
t.X.X
or[0][0]
.xy
floatThe Y component of the X column vector, accessed via
t.X.Y
or[0][1]
.yx
floatThe X component of the Y column vector, accessed via
t.Y.X
or[1][0]
.yy
floatThe Y component of the Y column vector, accessed via
t.Y.Y
or[1][1]
.ox
floatThe X component of the origin vector, accessed via
t.Origin.X
or[2][0]
.oy
floatThe Y component of the origin vector, accessed via
t.Origin.Y
or[2][1]
.
Fields
Origin
The origin vector (column 2, the third column). Equivalent to array index [2]
.
The origin vector represents translation.
public Vector2 Origin
Field Value
X
The basis matrix's X vector (column 0). Equivalent to array index [0]
.
public Vector2 X
Field Value
Y
The basis matrix's Y vector (column 1). Equivalent to array index [1]
.
public Vector2 Y
Field Value
Properties
FlipX
The transform that will flip something along the X axis.
public static Transform2D FlipX { get; }
Property Value
- Transform2D
Equivalent to
new Transform2D(Vector2.Left, Vector2.Down, Vector2.Zero)
.
FlipY
The transform that will flip something along the Y axis.
public static Transform2D FlipY { get; }
Property Value
- Transform2D
Equivalent to
new Transform2D(Vector2.Right, Vector2.Up, Vector2.Zero)
.
Identity
The identity transform, with no translation, rotation, or scaling applied.
This is used as a replacement for Transform2D()
in GDScript.
Do not use new Transform2D()
with no arguments in C#, because it sets all values to zero.
public static Transform2D Identity { get; }
Property Value
- Transform2D
Equivalent to
new Transform2D(Vector2.Right, Vector2.Down, Vector2.Zero)
.
this[int]
public Vector2 this[int column] { readonly get; set; }
Parameters
column
intWhich column vector.
Property Value
Exceptions
- ArgumentOutOfRangeException
column
is not 0, 1 or 2.
this[int, int]
Access matrix elements in column-major order. The third column is the Origin vector.
public float this[int column, int row] { readonly get; set; }
Parameters
column
intWhich column, the matrix horizontal position.
row
intWhich row, the matrix vertical position.
Property Value
Rotation
Returns the transform's rotation (in radians).
public readonly float Rotation { get; }
Property Value
Scale
Returns the scale.
public readonly Vector2 Scale { get; }
Property Value
Skew
Returns the transform's skew (in radians).
public readonly float Skew { get; }
Property Value
Methods
AffineInverse()
Returns the inverse of the transform, under the assumption that the basis is invertible (must have non-zero determinant).
public readonly Transform2D AffineInverse()
Returns
- Transform2D
The inverse transformation matrix.
- See Also
BasisXform(Vector2)
Returns a vector transformed (multiplied) by the basis matrix. This method does not account for translation (the Origin vector).
public readonly Vector2 BasisXform(Vector2 v)
Parameters
v
Vector2A vector to transform.
Returns
- Vector2
The transformed vector.
- See Also
BasisXformInv(Vector2)
Returns a vector transformed (multiplied) by the inverse basis matrix,
under the assumption that the basis is orthonormal (i.e. rotation/reflection
is fine, scaling/skew is not).
This method does not account for translation (the Origin vector).
transform.BasisXformInv(vector)
is equivalent to transform.Inverse().BasisXform(vector)
. See Inverse().
For non-orthonormal transforms (e.g. with scaling) transform.AffineInverse().BasisXform(vector)
can be used instead. See AffineInverse().
public readonly Vector2 BasisXformInv(Vector2 v)
Parameters
v
Vector2A vector to inversely transform.
Returns
- Vector2
The inversely transformed vector.
- See Also
Equals(Transform2D)
Returns true if the transforms are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Transform2D) instead, which is more reliable.
public readonly bool Equals(Transform2D other)
Parameters
other
Transform2DThe other transform to compare.
Returns
- bool
Whether or not the matrices are exactly equal.
Equals(object)
Returns true if the transform is exactly equal
to the given object (obj
).
Note: Due to floating-point precision errors, consider using
IsEqualApprox(Transform2D) instead, which is more reliable.
public override readonly bool Equals(object obj)
Parameters
obj
objectThe object to compare with.
Returns
- bool
Whether or not the transform and the object are exactly equal.
GetHashCode()
Serves as the hash function for Transform2D.
public override readonly int GetHashCode()
Returns
- int
A hash code for this transform.
InterpolateWith(Transform2D, float)
Interpolates this transform to the other transform
by weight
.
public readonly Transform2D InterpolateWith(Transform2D transform, float weight)
Parameters
transform
Transform2DThe other transform.
weight
floatA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Transform2D
The interpolated transform.
Inverse()
Returns the inverse of the transform, under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection is fine, scaling/skew is not). Use AffineInverse() for non-orthonormal transforms (e.g. with scaling).
public readonly Transform2D Inverse()
Returns
- Transform2D
The inverse matrix.
IsEqualApprox(Transform2D)
Returns true if this transform and other
are approximately equal,
by running IsEqualApprox(Vector2) on each component.
public readonly bool IsEqualApprox(Transform2D other)
Parameters
other
Transform2DThe other transform to compare.
Returns
- bool
Whether or not the matrices are approximately equal.
IsFinite()
Returns true if this transform is finite, by calling IsFinite(float) on each component.
public readonly bool IsFinite()
Returns
- bool
Whether this vector is finite or not.
Orthonormalized()
Returns the transform with the basis orthogonal (90 degrees), and normalized axis vectors (scale of 1 or -1).
public readonly Transform2D Orthonormalized()
Returns
- Transform2D
The orthonormalized transform.
Rotated(float)
Rotates the transform by angle
(in radians).
The operation is done in the parent/global frame, equivalent to
multiplying the matrix from the left.
public readonly Transform2D Rotated(float angle)
Parameters
angle
floatThe angle to rotate, in radians.
Returns
- Transform2D
The rotated transformation matrix.
RotatedLocal(float)
Rotates the transform by angle
(in radians).
The operation is done in the local frame, equivalent to
multiplying the matrix from the right.
public readonly Transform2D RotatedLocal(float angle)
Parameters
angle
floatThe angle to rotate, in radians.
Returns
- Transform2D
The rotated transformation matrix.
Scaled(Vector2)
Scales the transform by the given scaling factor. The operation is done in the parent/global frame, equivalent to multiplying the matrix from the left.
public readonly Transform2D Scaled(Vector2 scale)
Parameters
scale
Vector2The scale to introduce.
Returns
- Transform2D
The scaled transformation matrix.
ScaledLocal(Vector2)
Scales the transform by the given scaling factor. The operation is done in the local frame, equivalent to multiplying the matrix from the right.
public readonly Transform2D ScaledLocal(Vector2 scale)
Parameters
scale
Vector2The scale to introduce.
Returns
- Transform2D
The scaled transformation matrix.
ToString()
Converts this Transform2D to a string.
public override readonly string ToString()
Returns
- string
A string representation of this transform.
ToString(string)
Converts this Transform2D to a string with the given format
.
public readonly string ToString(string format)
Parameters
format
string
Returns
- string
A string representation of this transform.
Translated(Vector2)
Translates the transform by the given offset
.
The operation is done in the parent/global frame, equivalent to
multiplying the matrix from the left.
public readonly Transform2D Translated(Vector2 offset)
Parameters
offset
Vector2The offset to translate by.
Returns
- Transform2D
The translated matrix.
TranslatedLocal(Vector2)
Translates the transform by the given offset
.
The operation is done in the local frame, equivalent to
multiplying the matrix from the right.
public readonly Transform2D TranslatedLocal(Vector2 offset)
Parameters
offset
Vector2The offset to translate by.
Returns
- Transform2D
The translated matrix.
Operators
operator ==(Transform2D, Transform2D)
Returns true if the transforms are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Transform2D) instead, which is more reliable.
public static bool operator ==(Transform2D left, Transform2D right)
Parameters
left
Transform2DThe left transform.
right
Transform2DThe right transform.
Returns
- bool
Whether or not the transforms are exactly equal.
operator !=(Transform2D, Transform2D)
Returns true if the transforms are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Transform2D) instead, which is more reliable.
public static bool operator !=(Transform2D left, Transform2D right)
Parameters
left
Transform2DThe left transform.
right
Transform2DThe right transform.
Returns
- bool
Whether or not the transforms are not equal.
operator *(Rect2, Transform2D)
Returns a Rect2 transformed (multiplied) by the inverse transformation matrix,
under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection
is fine, scaling/skew is not).
rect * transform
is equivalent to transform.Inverse() * rect
. See Inverse().
For transforming by inverse of an affine transformation (e.g. with scaling) transform.AffineInverse() * rect
can be used instead. See AffineInverse().
public static Rect2 operator *(Rect2 rect, Transform2D transform)
Parameters
rect
Rect2A Rect2 to inversely transform.
transform
Transform2DThe transformation to apply.
Returns
- Rect2
The inversely transformed Rect2.
operator *(Transform2D, Rect2)
Returns a Rect2 transformed (multiplied) by the transformation matrix.
public static Rect2 operator *(Transform2D transform, Rect2 rect)
Parameters
transform
Transform2DThe transformation to apply.
rect
Rect2A Rect2 to transform.
Returns
- Rect2
The transformed Rect2.
operator *(Transform2D, Transform2D)
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).
public static Transform2D operator *(Transform2D left, Transform2D right)
Parameters
left
Transform2DThe parent transform.
right
Transform2DThe child transform.
Returns
- Transform2D
The composed transform.
operator *(Transform2D, Vector2)
Returns a Vector2 transformed (multiplied) by the transformation matrix.
public static Vector2 operator *(Transform2D transform, Vector2 vector)
Parameters
transform
Transform2DThe transformation to apply.
vector
Vector2A Vector2 to transform.
Returns
- Vector2
The transformed Vector2.
operator *(Transform2D, Vector2[])
Returns a copy of the given Vector2[] transformed (multiplied) by the transformation matrix.
public static Vector2[] operator *(Transform2D transform, Vector2[] array)
Parameters
transform
Transform2DThe transformation to apply.
array
Vector2[]A Vector2[] to transform.
Returns
- Vector2[]
The transformed copy of the Vector2[].
operator *(Vector2, Transform2D)
Returns a Vector2 transformed (multiplied) by the inverse transformation matrix,
under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection
is fine, scaling/skew is not).
vector * transform
is equivalent to transform.Inverse() * vector
. See Inverse().
For transforming by inverse of an affine transformation (e.g. with scaling) transform.AffineInverse() * vector
can be used instead. See AffineInverse().
public static Vector2 operator *(Vector2 vector, Transform2D transform)
Parameters
vector
Vector2A Vector2 to inversely transform.
transform
Transform2DThe transformation to apply.
Returns
- Vector2
The inversely transformed Vector2.
operator *(Vector2[], Transform2D)
Returns a copy of the given Vector2[] transformed (multiplied) by the inverse transformation matrix,
under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection
is fine, scaling/skew is not).
array * transform
is equivalent to transform.Inverse() * array
. See Inverse().
For transforming by inverse of an affine transformation (e.g. with scaling) transform.AffineInverse() * array
can be used instead. See AffineInverse().
public static Vector2[] operator *(Vector2[] array, Transform2D transform)
Parameters
array
Vector2[]A Vector2[] to inversely transform.
transform
Transform2DThe transformation to apply.
Returns
- Vector2[]
The inversely transformed copy of the Vector2[].