Debian Codegraph @

doc/classes/Transform2D.xml

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <class name="Transform2D" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 A 2×3 matrix representing a 2D transformation.
5 </brief_description>
6 <description>
7 The [Transform2D] built-in [Variant] type is a 2×3 [url=https://en.wikipedia.org/wiki/Matrix_(mathematics)]matrix[/url] representing a transformation in 2D space. It contains three [Vector2] values: [member x], [member y], and [member origin]. Together, they can represent translation, rotation, scale, and skew.
8 The [member x] and [member y] axes form a 2×2 matrix, known as the transform's [b]basis[/b]. The length of each axis ([method Vector2.length]) influences the transform's scale, while the direction of all axes influence the rotation. Usually, both axes are perpendicular to one another. However, when you rotate one axis individually, the transform becomes skewed. Applying a skewed transform to a 2D sprite will make the sprite appear distorted.
9 For a general introduction, see the [url=$DOCS_URL/tutorials/math/matrices_and_transforms.html]Matrices and transforms[/url] tutorial.
10 [b]Note:[/b] Unlike [Transform3D], there is no 2D equivalent to the [Basis] type. All mentions of "basis" refer to the [member x] and [member y] components of [Transform2D].
11 </description>
12 <tutorials>
13 <link title="Math documentation index">$DOCS_URL/tutorials/math/index.html</link>
14 <link title="Matrices and transforms">$DOCS_URL/tutorials/math/matrices_and_transforms.html</link>
15 <link title="Matrix Transform Demo">https://godotengine.org/asset-library/asset/2787</link>
16 <link title="2.5D Game Demo">https://godotengine.org/asset-library/asset/2783</link>
17 </tutorials>
18 <constructors>
19 <constructor name="Transform2D">
20 <return type="Transform2D" />
21 <description>
22 Constructs a [Transform2D] identical to [constant IDENTITY].
23 [b]Note:[/b] In C#, this constructs a [Transform2D] with all of its components set to [constant Vector2.ZERO].
24 </description>
25 </constructor>
26 <constructor name="Transform2D">
27 <return type="Transform2D" />
28 <param index="0" name="from" type="Transform2D" />
29 <description>
30 Constructs a [Transform2D] as a copy of the given [Transform2D].
31 </description>
32 </constructor>
33 <constructor name="Transform2D">
34 <return type="Transform2D" />
35 <param index="0" name="rotation" type="float" />
36 <param index="1" name="position" type="Vector2" />
37 <description>
38 Constructs a [Transform2D] from a given angle (in radians) and position.
39 </description>
40 </constructor>
41 <constructor name="Transform2D">
42 <return type="Transform2D" />
43 <param index="0" name="rotation" type="float" />
44 <param index="1" name="scale" type="Vector2" />
45 <param index="2" name="skew" type="float" />
46 <param index="3" name="position" type="Vector2" />
47 <description>
48 Constructs a [Transform2D] from a given angle (in radians), scale, skew (in radians), and position.
49 </description>
50 </constructor>
51 <constructor name="Transform2D">
52 <return type="Transform2D" />
53 <param index="0" name="x_axis" type="Vector2" />
54 <param index="1" name="y_axis" type="Vector2" />
55 <param index="2" name="origin" type="Vector2" />
56 <description>
57 Constructs a [Transform2D] from 3 [Vector2] values representing [member x], [member y], and the [member origin] (the three matrix columns).
58 </description>
59 </constructor>
60 </constructors>
61 <methods>
62 <method name="affine_inverse" qualifiers="const">
63 <return type="Transform2D" />
64 <description>
65 Returns the inverted version of this transform. Unlike [method inverse], this method works with almost any basis, including non-uniform ones, but is slower.
66 [b]Note:[/b] For this method to return correctly, the transform's basis needs to have a determinant that is not exactly [code]0.0[/code] (see [method determinant]).
67 </description>
68 </method>
69 <method name="basis_xform" qualifiers="const">
70 <return type="Vector2" />
71 <param index="0" name="v" type="Vector2" />
72 <description>
73 Returns a copy of the [param v] vector, transformed (multiplied) by the transform basis's matrix. Unlike the multiplication operator ([code]*[/code]), this method ignores the [member origin].
74 </description>
75 </method>
76 <method name="basis_xform_inv" qualifiers="const">
77 <return type="Vector2" />
78 <param index="0" name="v" type="Vector2" />
79 <description>
80 Returns a copy of the [param v] vector, transformed (multiplied) by the inverse transform basis's matrix (see [method inverse]). This method ignores the [member origin].
81 [b]Note:[/b] This method assumes that this transform's basis is [i]orthonormal[/i] (see [method orthonormalized]). If the basis is not orthonormal, [code]transform.affine_inverse().basis_xform(vector)[/code] should be used instead (see [method affine_inverse]).
82 </description>
83 </method>
84 <method name="determinant" qualifiers="const">
85 <return type="float" />
86 <description>
87 Returns the [url=https://en.wikipedia.org/wiki/Determinant]determinant[/url] of this transform basis's matrix. For advanced math, this number can be used to determine a few attributes:
88 - If the determinant is exactly [code]0.0[/code], the basis is not invertible (see [method inverse]).
89 - If the determinant is a negative number, the basis represents a negative scale.
90 [b]Note:[/b] If the basis's scale is the same for every axis, its determinant is always that scale by the power of 2.
91 </description>
92 </method>
93 <method name="get_origin" qualifiers="const">
94 <return type="Vector2" />
95 <description>
96 Returns this transform's translation. Equivalent to [member origin].
97 </description>
98 </method>
99 <method name="get_rotation" qualifiers="const">
100 <return type="float" />
101 <description>
102 Returns this transform's rotation (in radians). This is equivalent to [member x]'s angle (see [method Vector2.angle]).
103 </description>
104 </method>
105 <method name="get_scale" qualifiers="const">
106 <return type="Vector2" />
107 <description>
108 Returns the length of both [member x] and [member y], as a [Vector2]. If this transform's basis is not skewed, this value is the scaling factor. It is not affected by rotation.
109 [codeblocks]
110 [gdscript]
111 var my_transform = Transform2D(
112 Vector2(2, 0),
113 Vector2(0, 4),
114 Vector2(0, 0)
115 )
116 # Rotating the Transform2D in any way preserves its scale.
117 my_transform = my_transform.rotated(TAU / 2)
118
119 print(my_transform.get_scale()) # Prints (2.0, 4.0)
120 [/gdscript]
121 [csharp]
122 var myTransform = new Transform2D(
123 Vector3(2.0f, 0.0f),
124 Vector3(0.0f, 4.0f),
125 Vector3(0.0f, 0.0f)
126 );
127 // Rotating the Transform2D in any way preserves its scale.
128 myTransform = myTransform.Rotated(Mathf.Tau / 2.0f);
129
130 GD.Print(myTransform.GetScale()); // Prints (2, 4)
131 [/csharp]
132 [/codeblocks]
133 [b]Note:[/b] If the value returned by [method determinant] is negative, the scale is also negative.
134 </description>
135 </method>
136 <method name="get_skew" qualifiers="const">
137 <return type="float" />
138 <description>
139 Returns this transform's skew (in radians).
140 </description>
141 </method>
142 <method name="interpolate_with" qualifiers="const">
143 <return type="Transform2D" />
144 <param index="0" name="xform" type="Transform2D" />
145 <param index="1" name="weight" type="float" />
146 <description>
147 Returns the result of the linear interpolation between this transform and [param xform] by the given [param weight].
148 The [param weight] should be between [code]0.0[/code] and [code]1.0[/code] (inclusive). Values outside this range are allowed and can be used to perform [i]extrapolation[/i] instead.
149 </description>
150 </method>
151 <method name="inverse" qualifiers="const">
152 <return type="Transform2D" />
153 <description>
154 Returns the [url=https://en.wikipedia.org/wiki/Invertible_matrix]inverted version of this transform[/url].
155 [b]Note:[/b] For this method to return correctly, the transform's basis needs to be [i]orthonormal[/i] (see [method orthonormalized]). That means the basis should only represent a rotation. If it does not, use [method affine_inverse] instead.
156 </description>
157 </method>
158 <method name="is_conformal" qualifiers="const">
159 <return type="bool" />
160 <description>
161 Returns [code]true[/code] if this transform's basis is conformal. A conformal basis is both [i]orthogonal[/i] (the axes are perpendicular to each other) and [i]uniform[/i] (the axes share the same length). This method can be especially useful during physics calculations.
162 </description>
163 </method>
164 <method name="is_equal_approx" qualifiers="const">
165 <return type="bool" />
166 <param index="0" name="xform" type="Transform2D" />
167 <description>
168 Returns [code]true[/code] if this transform and [param xform] are approximately equal, by running [method @GlobalScope.is_equal_approx] on each component.
169 </description>
170 </method>
171 <method name="is_finite" qualifiers="const">
172 <return type="bool" />
173 <description>
174 Returns [code]true[/code] if this transform is finite, by calling [method @GlobalScope.is_finite] on each component.
175 </description>
176 </method>
177 <method name="looking_at" qualifiers="const">
178 <return type="Transform2D" />
179 <param index="0" name="target" type="Vector2" default="Vector2(0, 0)" />
180 <description>
181 Returns a copy of the transform rotated such that the rotated X-axis points towards the [param target] position, in global space.
182 </description>
183 </method>
184 <method name="orthonormalized" qualifiers="const">
185 <return type="Transform2D" />
186 <description>
187 Returns a copy of this transform with its basis orthonormalized. An orthonormal basis is both [i]orthogonal[/i] (the axes are perpendicular to each other) and [i]normalized[/i] (the axes have a length of [code]1.0[/code]), which also means it can only represent a rotation.
188 </description>
189 </method>
190 <method name="rotated" qualifiers="const">
191 <return type="Transform2D" />
192 <param index="0" name="angle" type="float" />
193 <description>
194 Returns a copy of this transform rotated by the given [param angle] (in radians).
195 If [param angle] is positive, the transform is rotated clockwise.
196 This method is an optimized version of multiplying the given transform [code]X[/code] with a corresponding rotation transform [code]R[/code] from the left, i.e., [code]R * X[/code].
197 This can be seen as transforming with respect to the global/parent frame.
198 </description>
199 </method>
200 <method name="rotated_local" qualifiers="const">
201 <return type="Transform2D" />
202 <param index="0" name="angle" type="float" />
203 <description>
204 Returns a copy of the transform rotated by the given [param angle] (in radians).
205 This method is an optimized version of multiplying the given transform [code]X[/code] with a corresponding rotation transform [code]R[/code] from the right, i.e., [code]X * R[/code].
206 This can be seen as transforming with respect to the local frame.
207 </description>
208 </method>
209 <method name="scaled" qualifiers="const">
210 <return type="Transform2D" />
211 <param index="0" name="scale" type="Vector2" />
212 <description>
213 Returns a copy of the transform scaled by the given [param scale] factor.
214 This method is an optimized version of multiplying the given transform [code]X[/code] with a corresponding scaling transform [code]S[/code] from the left, i.e., [code]S * X[/code].
215 This can be seen as transforming with respect to the global/parent frame.
216 </description>
217 </method>
218 <method name="scaled_local" qualifiers="const">
219 <return type="Transform2D" />
220 <param index="0" name="scale" type="Vector2" />
221 <description>
222 Returns a copy of the transform scaled by the given [param scale] factor.
223 This method is an optimized version of multiplying the given transform [code]X[/code] with a corresponding scaling transform [code]S[/code] from the right, i.e., [code]X * S[/code].
224 This can be seen as transforming with respect to the local frame.
225 </description>
226 </method>
227 <method name="translated" qualifiers="const">
228 <return type="Transform2D" />
229 <param index="0" name="offset" type="Vector2" />
230 <description>
231 Returns a copy of the transform translated by the given [param offset].
232 This method is an optimized version of multiplying the given transform [code]X[/code] with a corresponding translation transform [code]T[/code] from the left, i.e., [code]T * X[/code].
233 This can be seen as transforming with respect to the global/parent frame.
234 </description>
235 </method>
236 <method name="translated_local" qualifiers="const">
237 <return type="Transform2D" />
238 <param index="0" name="offset" type="Vector2" />
239 <description>
240 Returns a copy of the transform translated by the given [param offset].
241 This method is an optimized version of multiplying the given transform [code]X[/code] with a corresponding translation transform [code]T[/code] from the right, i.e., [code]X * T[/code].
242 This can be seen as transforming with respect to the local frame.
243 </description>
244 </method>
245 </methods>
246 <members>
247 <member name="origin" type="Vector2" setter="" getter="" default="Vector2(0, 0)">
248 The translation offset of this transform, and the column [code]2[/code] of the matrix. In 2D space, this can be seen as the position.
249 </member>
250 <member name="x" type="Vector2" setter="" getter="" default="Vector2(1, 0)">
251 The transform basis's X axis, and the column [code]0[/code] of the matrix. Combined with [member y], this represents the transform's rotation, scale, and skew.
252 On the identity transform, this vector points right ([constant Vector2.RIGHT]).
253 </member>
254 <member name="y" type="Vector2" setter="" getter="" default="Vector2(0, 1)">
255 The transform basis's Y axis, and the column [code]1[/code] of the matrix. Combined with [member x], this represents the transform's rotation, scale, and skew.
256 On the identity transform, this vector points down ([constant Vector2.DOWN]).
257 </member>
258 </members>
259 <constants>
260 <constant name="IDENTITY" value="Transform2D(1, 0, 0, 1, 0, 0)">
261 The identity [Transform2D]. This is a transform with no translation, no rotation, and a scale of [constant Vector2.ONE]. This also means that:
262 - The [member x] points right ([constant Vector2.RIGHT]);
263 - The [member y] points down ([constant Vector2.DOWN]).
264 [codeblock]
265 var transform = Transform2D.IDENTITY
266 print("| X | Y | Origin")
267 print("| %.f | %.f | %.f" % [transform.x.x, transform.y.x, transform.origin.x])
268 print("| %.f | %.f | %.f" % [transform.x.y, transform.y.y, transform.origin.y])
269 # Prints:
270 # | X | Y | Origin
271 # | 1 | 0 | 0
272 # | 0 | 1 | 0
273 [/codeblock]
274 If a [Vector2], a [Rect2], a [PackedVector2Array], or another [Transform2D] is transformed (multiplied) by this constant, no transformation occurs.
275 [b]Note:[/b] In GDScript, this constant is equivalent to creating a [constructor Transform2D] without any arguments. It can be used to make your code clearer, and for consistency with C#.
276 </constant>
277 <constant name="FLIP_X" value="Transform2D(-1, 0, 0, 1, 0, 0)">
278 When any transform is multiplied by [constant FLIP_X], it negates all components of the [member x] axis (the X column).
279 When [constant FLIP_X] is multiplied by any transform, it negates the [member Vector2.x] component of all axes (the X row).
280 </constant>
281 <constant name="FLIP_Y" value="Transform2D(1, 0, 0, -1, 0, 0)">
282 When any transform is multiplied by [constant FLIP_Y], it negates all components of the [member y] axis (the Y column).
283 When [constant FLIP_Y] is multiplied by any transform, it negates the [member Vector2.y] component of all axes (the Y row).
284 </constant>
285 </constants>
286 <operators>
287 <operator name="operator !=">
288 <return type="bool" />
289 <param index="0" name="right" type="Transform2D" />
290 <description>
291 Returns [code]true[/code] if the components of both transforms are not equal.
292 [b]Note:[/b] Due to floating-point precision errors, consider using [method is_equal_approx] instead, which is more reliable.
293 </description>
294 </operator>
295 <operator name="operator *">
296 <return type="PackedVector2Array" />
297 <param index="0" name="right" type="PackedVector2Array" />
298 <description>
299 Transforms (multiplies) every [Vector2] element of the given [PackedVector2Array] by this transformation matrix.
300 On larger arrays, this operation is much faster than transforming each [Vector2] individually.
301 </description>
302 </operator>
303 <operator name="operator *">
304 <return type="Rect2" />
305 <param index="0" name="right" type="Rect2" />
306 <description>
307 Transforms (multiplies) the [Rect2] by this transformation matrix.
308 </description>
309 </operator>
310 <operator name="operator *">
311 <return type="Transform2D" />
312 <param index="0" name="right" type="Transform2D" />
313 <description>
314 Transforms (multiplies) this transform by the [param right] transform.
315 This is the operation performed between parent and child [CanvasItem] nodes.
316 [b]Note:[/b] If you need to only modify one attribute of this transform, consider using one of the following methods, instead:
317 - For translation, see [method translated] or [method translated_local].
318 - For rotation, see [method rotated] or [method rotated_local].
319 - For scale, see [method scaled] or [method scaled_local].
320 </description>
321 </operator>
322 <operator name="operator *">
323 <return type="Vector2" />
324 <param index="0" name="right" type="Vector2" />
325 <description>
326 Transforms (multiplies) the [Vector2] by this transformation matrix.
327 </description>
328 </operator>
329 <operator name="operator *">
330 <return type="Transform2D" />
331 <param index="0" name="right" type="float" />
332 <description>
333 Multiplies all components of the [Transform2D] by the given [float], including the [member origin]. This affects the transform's scale uniformly.
334 </description>
335 </operator>
336 <operator name="operator *">
337 <return type="Transform2D" />
338 <param index="0" name="right" type="int" />
339 <description>
340 Multiplies all components of the [Transform2D] by the given [int], including the [member origin]. This affects the transform's scale uniformly.
341 </description>
342 </operator>
343 <operator name="operator /">
344 <return type="Transform2D" />
345 <param index="0" name="right" type="float" />
346 <description>
347 Divides all components of the [Transform2D] by the given [float], including the [member origin]. This affects the transform's scale uniformly.
348 </description>
349 </operator>
350 <operator name="operator /">
351 <return type="Transform2D" />
352 <param index="0" name="right" type="int" />
353 <description>
354 Divides all components of the [Transform2D] by the given [int], including the [member origin]. This affects the transform's scale uniformly.
355 </description>
356 </operator>
357 <operator name="operator ==">
358 <return type="bool" />
359 <param index="0" name="right" type="Transform2D" />
360 <description>
361 Returns [code]true[/code] if the components of both transforms are exactly equal.
362 [b]Note:[/b] Due to floating-point precision errors, consider using [method is_equal_approx] instead, which is more reliable.
363 </description>
364 </operator>
365 <operator name="operator []">
366 <return type="Vector2" />
367 <param index="0" name="index" type="int" />
368 <description>
369 Accesses each axis (column) of this transform by their index. Index [code]0[/code] is the same as [member x], index [code]1[/code] is the same as [member y], and index [code]2[/code] is the same as [member origin].
370 </description>
371 </operator>
372 </operators>
373 </class>