matrices_and_transforms.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. .. _doc_matrices_and_transforms:
  2. Matrices and transforms
  3. =======================
  4. Introduction
  5. ------------
  6. Before reading this tutorial, we recommend that you thoroughly read
  7. and understand the :ref:`doc_vector_math` tutorial, as this tutorial
  8. requires a knowledge of vectors.
  9. This tutorial is about *transformations* and how we represent them
  10. in Godot using matrices. It is not a full in-depth guide to matrices.
  11. Transformations are most of the time applied as translation, rotation,
  12. and scale, so we will focus on how to represent those with matrices.
  13. Most of this guide focuses on 2D, using :ref:`class_Transform2D` and
  14. :ref:`class_Vector2`, but the way things work in 3D is very similar.
  15. .. note:: As mentioned in the previous tutorial, it is important to
  16. remember that in Godot, the Y axis points *down* in 2D.
  17. This is the opposite of how most schools teach linear
  18. algebra, with the Y axis pointing up.
  19. .. note:: The convention is that the X axis is red, the Y axis is
  20. green, and the Z axis is blue. This tutorial is color-coded
  21. to match these conventions, but we will also represent
  22. the origin vector with a blue color.
  23. Matrix components and the Identity matrix
  24. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. The identity matrix represents a transform with no translation,
  26. no rotation, and no scale. Let's start by looking at the identity
  27. matrix and how its components relate to how it visually appears.
  28. .. image:: img/matrices_and_transforms/identity.png
  29. Matrices have rows and columns, and a transformation matrix has
  30. specific conventions on what each does.
  31. In the image above, we can see that the red X vector is represented
  32. by the first column of the matrix, and the green Y vector is
  33. likewise represented by the second column. A change to the columns
  34. will change these vectors. We will see how they can be manipulated
  35. in the next few examples.
  36. You should not worry about manipulating rows directly, as we usually
  37. work with columns. However, you can think of the rows of the matrix
  38. as showing which vectors contribute to moving in a given direction.
  39. When we refer to a value such as ``t.x.y``, that's the Y component of
  40. the X column vector. In other words, the bottom-left of the matrix.
  41. Similarly, ``t.x.x`` is top-left, ``t.y.x`` is top-right, and ``t.y.y``
  42. is bottom-right, where ``t`` is the Transform2D.
  43. Scaling the transformation matrix
  44. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. Applying a scale is one of the easiest operations to understand.
  46. Let's start by placing the Godot logo underneath our vectors
  47. so that we can visually see the effects on an object:
  48. .. image:: img/matrices_and_transforms/identity-godot.png
  49. Now, to scale the matrix, all we need to do is multiply each
  50. component by the scale we want. Let's scale it up by 2. 1 times 2
  51. becomes 2, and 0 times 2 becomes 0, so we end up with this:
  52. .. image:: img/matrices_and_transforms/scale.png
  53. To do this in code, we multiply each of the vectors:
  54. .. tabs::
  55. .. code-tab:: gdscript GDScript
  56. var t = Transform2D()
  57. # Scale
  58. t.x *= 2
  59. t.y *= 2
  60. transform = t # Change the node's transform to what we calculated.
  61. .. code-tab:: csharp
  62. Transform2D t = Transform2D.Identity;
  63. // Scale
  64. t.x *= 2;
  65. t.y *= 2;
  66. Transform = t; // Change the node's transform to what we calculated.
  67. If we wanted to return it to its original scale, we can multiply
  68. each component by 0.5. That's pretty much all there is to scaling
  69. a transformation matrix.
  70. To calculate the object's scale from an existing transformation
  71. matrix, you can use ``length()`` on each of the column vectors.
  72. .. note:: In actual projects, you can use the ``scaled()``
  73. method to perform scaling.
  74. Rotating the transformation matrix
  75. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. We'll start the same way as earlier, with the Godot logo underneath
  77. the identity matrix:
  78. .. image:: img/matrices_and_transforms/identity-godot.png
  79. As an example, let's say we want to rotate our Godot logo clockwise
  80. by 90 degrees. Right now the X axis points right and the Y axis
  81. points down. If we rotate these in our head, we would logically
  82. see that the new X axis should point down and the new Y axis
  83. should point left.
  84. You can imagine that you grab both the Godot logo and its vectors,
  85. and then spin it around the center. Wherever you finish spinning,
  86. the orientation of the vectors determines what the matrix is.
  87. We need to represent "down" and "left" in normal coordinates,
  88. so means we'll set X to (0, 1) and Y to (-1, 0). These are
  89. also the values of ``Vector2.DOWN`` and ``Vector2.LEFT``.
  90. When we do this, we get the desired result of rotating the object:
  91. .. image:: img/matrices_and_transforms/rotate1.png
  92. If you have trouble understanding the above, try this exercise:
  93. Cut a square of paper, draw X and Y vectors on top of it, place
  94. it on graph paper, then rotate it and note the endpoints.
  95. To perform rotation in code, we need to be able to calculate
  96. the values programmatically. This image shows the formulas needed
  97. to calculate the transformation matrix from a rotation angle.
  98. Don't worry if this part seems complicated, I promise it's the
  99. hardest thing you need to know.
  100. .. image:: img/matrices_and_transforms/rotate2.png
  101. .. note:: Godot represents all rotations with radians, not degrees.
  102. A full turn is `TAU` or `PI*2` radians, and a quarter
  103. turn of 90 degrees is `TAU/4` or `PI/2` radians. Working
  104. with `TAU` usually results in more readable code.
  105. .. note:: Fun fact: In addition to Y being *down* in Godot, rotation
  106. is represented clockwise. This means that all the math and
  107. trig functions behave the same as a Y-is-up CCW system,
  108. since these differences "cancel out". You can think of
  109. rotations in both systems being "from X to Y".
  110. In order to perform a rotation of 0.5 radians (about 28.65 degrees),
  111. we plug in a value of 0.5 to the formula above and evaluate
  112. to find what the actual values should be:
  113. .. image:: img/matrices_and_transforms/rotate3.png
  114. Here's how that would be done in code (place the script on a Node2D):
  115. .. tabs::
  116. .. code-tab:: gdscript GDScript
  117. var rot = 0.5 # The rotation to apply.
  118. var t = Transform2D()
  119. t.x.x = cos(rot)
  120. t.y.y = cos(rot)
  121. t.x.y = sin(rot)
  122. t.y.x = -sin(rot)
  123. transform = t # Change the node's transform to what we calculated.
  124. .. code-tab:: csharp
  125. float rot = 0.5f; // The rotation to apply.
  126. Transform2D t = Transform2D.Identity;
  127. t.X.X = t.Y.Y = Mathf.Cos(rot);
  128. t.X.Y = t.Y.X = Mathf.Sin(rot);
  129. t.Y.X *= -1;
  130. Transform = t; // Change the node's transform to what we calculated.
  131. To calculate the object's rotation from an existing transformation
  132. matrix, you can use ``atan2(t.x.y, t.x.x)``, where t is the Transform2D.
  133. .. note:: In actual projects, you can use the ``rotated()``
  134. method to perform rotations.
  135. Basis of the transformation matrix
  136. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  137. So far we have only been working with the ``x`` and ``y``, vectors, which
  138. are in charge of representing rotation, scale, and/or shearing
  139. (advanced, covered at the end). The X and Y vectors are together
  140. called the *basis* of the transformation matrix. The terms "basis"
  141. and "basis vectors" are important to know.
  142. You might have noticed that :ref:`class_Transform2D` actually
  143. has three :ref:`class_Vector2` values: ``x``, ``y``, and ``origin``.
  144. The ``origin`` value is not part of the basis, but it is part of the
  145. transform, and we need it to represent position. From now on we'll
  146. keep track of the origin vector in all examples. You can think of
  147. origin as another column, but it's often better to think of it as
  148. completely separate.
  149. Note that in 3D, Godot has a separate :ref:`class_Basis` structure
  150. for holding the three :ref:`class_Vector3` values of the basis,
  151. since the code can get complex and it makes sense to separate
  152. it from :ref:`class_Transform3D` (which is composed of one
  153. :ref:`class_Basis` and one extra :ref:`class_Vector3` for the origin).
  154. Translating the transformation matrix
  155. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  156. Changing the ``origin`` vector is called *translating* the transformation
  157. matrix. Translating is basically a technical term for "moving" the
  158. object, but it explicitly does not involve any rotation.
  159. Let's work through an example to help understand this. We will start
  160. with the identity transform like last time, except we will keep track
  161. of the origin vector this time.
  162. .. image:: img/matrices_and_transforms/identity-origin.png
  163. If we want to move the object to a position of (1, 2), we need
  164. to set its ``origin`` vector to (1, 2):
  165. .. image:: img/matrices_and_transforms/translate.png
  166. There is also a ``translated()`` method, which performs a different
  167. operation to adding or changing ``origin`` directly. The ``translated()``
  168. method will translate the object *relative to its own rotation*.
  169. For example, an object rotated 90 degrees clockwise will move to
  170. the right when ``translated()`` with ``Vector2.UP``.
  171. .. note:: Godot's 2D uses coordinates based on pixels, so in actual
  172. projects you will want to translate by hundreds of units.
  173. Putting it all together
  174. ~~~~~~~~~~~~~~~~~~~~~~~
  175. We're going to apply everything we mentioned so far onto one transform.
  176. To follow along, create a project with a Sprite2D node and use the
  177. Godot logo for the texture resource.
  178. Let's set the translation to (350, 150), rotate by -0.5 rad, and scale by 3.
  179. I've posted a screenshot, and the code to reproduce it, but I encourage
  180. you to try and reproduce the screenshot without looking at the code!
  181. .. image:: img/matrices_and_transforms/putting-all-together.png
  182. .. tabs::
  183. .. code-tab:: gdscript GDScript
  184. var t = Transform2D()
  185. # Translation
  186. t.origin = Vector2(350, 150)
  187. # Rotation
  188. var rot = -0.5 # The rotation to apply.
  189. t.x.x = cos(rot)
  190. t.y.y = cos(rot)
  191. t.x.y = sin(rot)
  192. t.y.x = -sin(rot)
  193. # Scale
  194. t.x *= 3
  195. t.y *= 3
  196. transform = t # Change the node's transform to what we calculated.
  197. .. code-tab:: csharp
  198. Transform2D t = Transform2D.Identity;
  199. // Translation
  200. t.Origin = new Vector2(350, 150);
  201. // Rotation
  202. float rot = -0.5f; // The rotation to apply.
  203. t.X.X = t.Y.Y = Mathf.Cos(rot);
  204. t.X.Y = t.Y.X = Mathf.Sin(rot);
  205. t.Y.X *= -1;
  206. // Scale
  207. t.X *= 3;
  208. t.Y *= 3;
  209. Transform = t; // Change the node's transform to what we calculated.
  210. Shearing the transformation matrix (advanced)
  211. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  212. .. note:: If you are only looking for how to *use* transformation matrices,
  213. feel free to skip this section of the tutorial. This section
  214. explores an uncommonly used aspect of transformation matrices
  215. for the purpose of building an understanding of them.
  216. Node2D provides a shearing property out of the box.
  217. You may have noticed that a transform has more degrees of freedom than
  218. the combination of the above actions. The basis of a 2D transformation
  219. matrix has four total numbers in two :ref:`class_Vector2` values, while
  220. a rotation value and a Vector2 for scale only has 3 numbers. The high-level
  221. concept for the missing degree of freedom is called *shearing*.
  222. Normally, you will always have the basis vectors perpendicular to each
  223. other. However, shearing can be useful in some situations, and
  224. understanding shearing helps you understand how transforms work.
  225. To show you visually how it will look, let's overlay a grid onto the Godot
  226. logo:
  227. .. image:: img/matrices_and_transforms/identity-grid.png
  228. Each point on this grid is obtained by adding the basis vectors together.
  229. The bottom-right corner is X + Y, while the top-right corner is X - Y.
  230. If we change the basis vectors, the entire grid moves with it, as the
  231. grid is composed of the basis vectors. All lines on the grid that are
  232. currently parallel will remain parallel no matter what changes we make to
  233. the basis vectors.
  234. As an example, let's set Y to (1, 1):
  235. .. image:: img/matrices_and_transforms/shear.png
  236. .. tabs::
  237. .. code-tab:: gdscript GDScript
  238. var t = Transform2D()
  239. # Shear by setting Y to (1, 1)
  240. t.y = Vector2.ONE
  241. transform = t # Change the node's transform to what we calculated.
  242. .. code-tab:: csharp
  243. Transform2D t = Transform2D.Identity;
  244. // Shear by setting Y to (1, 1)
  245. t.y = Vector2.One;
  246. Transform = t; // Change the node's transform to what we calculated.
  247. .. note:: You can't set the raw values of a Transform2D in the editor,
  248. so you *must* use code if you want to shear the object.
  249. Due to the vectors no longer being perpendicular, the object has been
  250. sheared. The bottom-center of the grid, which is (0, 1) relative
  251. to itself, is now located at a world position of (1, 1).
  252. The intra-object coordinates are called UV coordinates in textures,
  253. so let's borrow that terminology for here. To find the world position
  254. from a relative position, the formula is U * X + V * Y, where U and V
  255. are numbers and X and Y are the basis vectors.
  256. The bottom-right corner of the grid, which is always at the UV position
  257. of (1, 1), is at the world position of (2, 1), which is calculated from
  258. X*1 + Y*1, which is (1, 0) + (1, 1), or (1 + 1, 0 + 1), or (2, 1).
  259. This matches up with our observation of where the bottom-right corner
  260. of the image is.
  261. Similarly, the top-right corner of the grid, which is always at the UV
  262. position of (1, -1), is at the world position of (0, -1), which is calculated
  263. from X*1 + Y*-1, which is (1, 0) - (1, 1), or (1 - 1, 0 - 1), or (0, -1).
  264. This matches up with our observation of where the top-right corner
  265. of the image is.
  266. Hopefully you now fully understand the how a transformation matrix affects
  267. the object, and the relationship between the basis vectors and how the
  268. object's "UV" or "intra-coordinates" have their world position changed.
  269. .. note:: In Godot, all transform math is done relative to the parent node.
  270. When we refer to "world position", that would be relative to the
  271. node's parent instead, if the node had a parent.
  272. If you would like additional explanation, you should check out
  273. 3Blue1Brown's excellent video about linear transformations:
  274. https://www.youtube.com/watch?v=kYB8IZa5AuE
  275. Practical applications of transforms
  276. ------------------------------------
  277. In actual projects, you will usually be working with transforms inside
  278. transforms by having multiple :ref:`class_Node2D` or :ref:`class_Node3D`
  279. nodes parented to each other.
  280. However, it's useful to understand how to manually calculate the values we
  281. need. We will go over how you could use :ref:`class_Transform2D` or
  282. :ref:`class_Transform3D` to manually calculate transforms of nodes.
  283. Converting positions between transforms
  284. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  285. There are many cases where you'd want to convert a position in and out of
  286. a transform. For example, if you have a position relative to the player
  287. and would like to find the world (parent-relative) position, or if you
  288. have a world position and want to know where it is relative to the player.
  289. We can find what a vector relative to the player would be defined in
  290. world space as using the ``*`` operator:
  291. .. tabs::
  292. .. code-tab:: gdscript GDScript
  293. # World space vector 100 units below the player.
  294. print(transform * Vector2(0, 100))
  295. .. code-tab:: csharp
  296. // World space vector 100 units below the player.
  297. GD.Print(Transform * new Vector2(0, 100));
  298. And we can use the ``*`` operator in the opposite order to find a what world
  299. space position would be if it was defined relative to the player:
  300. .. tabs::
  301. .. code-tab:: gdscript GDScript
  302. # Where is (0, 100) relative to the player?
  303. print(Vector2(0, 100) * transform)
  304. .. code-tab:: csharp
  305. // Where is (0, 100) relative to the player?
  306. GD.Print(new Vector2(0, 100) * Transform);
  307. .. note:: If you know in advance that the transform is positioned at
  308. (0, 0), you can use the "basis_xform" or "basis_xform_inv"
  309. methods instead, which skip dealing with translation.
  310. Moving an object relative to itself
  311. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  312. A common operation, especially in 3D games, is to move an object relative
  313. to itself. For example, in first-person shooter games, you would want the
  314. character to move forward (-Z axis) when you press :kbd:`W`.
  315. Since the basis vectors are the orientation relative to the parent,
  316. and the origin vector is the position relative to the parent, we can
  317. add multiples of the basis vectors to move an object relative to itself.
  318. This code moves an object 100 units to its own right:
  319. .. tabs::
  320. .. code-tab:: gdscript GDScript
  321. transform.origin += transform.x * 100
  322. .. code-tab:: csharp
  323. Transform2D t = Transform;
  324. t.Origin += t.X * 100;
  325. Transform = t;
  326. For moving in 3D, you would need to replace "x" with "basis.x".
  327. .. note:: In actual projects, you can use ``translate_object_local`` in 3D
  328. or ``move_local_x`` and ``move_local_y`` in 2D to do this.
  329. Applying transforms onto transforms
  330. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  331. One of the most important things to know about transforms is how you
  332. can use several of them together. A parent node's transform affects
  333. all of its children. Let's dissect an example.
  334. In this image, the child node has a "2" after the component names
  335. to distinguish them from the parent node. It might look a bit
  336. overwhelming with so many numbers, but remember that each number
  337. is displayed twice (next to the arrows and also in the matrices),
  338. and that almost half of the numbers are zero.
  339. .. image:: img/matrices_and_transforms/apply.png
  340. The only transformations going on here are that the parent node has
  341. been given a scale of (2, 1), the child has been given a scale of
  342. (0.5, 0.5), and both nodes have been given positions.
  343. All child transformations are affected by the parent transformations.
  344. The child has a scale of (0.5, 0.5), so you would expect it to be
  345. a 1:1 ratio square, and it is, but only relative to the parent.
  346. The child's X vector ends up being (1, 0) in world space, because
  347. it is scaled by the parent's basis vectors.
  348. Similarly, the child node's ``origin`` vector is set to (1, 1), but this
  349. actually moves it (2, 1) in world space, due to the parent node's
  350. basis vectors.
  351. To calculate a child transform's world space transform manually, this is
  352. the code we would use:
  353. .. tabs::
  354. .. code-tab:: gdscript GDScript
  355. # Set up transforms like in the image, except make positions be 100 times bigger.
  356. var parent = Transform2D(Vector2(2, 0), Vector2(0, 1), Vector2(100, 200))
  357. var child = Transform2D(Vector2(0.5, 0), Vector2(0, 0.5), Vector2(100, 100))
  358. # Calculate the child's world space transform
  359. # origin = (2, 0) * 100 + (0, 1) * 100 + (100, 200)
  360. var origin = parent.x * child.origin.x + parent.y * child.origin.y + parent.origin
  361. # basis_x = (2, 0) * 0.5 + (0, 1) * 0
  362. var basis_x = parent.x * child.x.x + parent.y * child.x.y
  363. # basis_y = (2, 0) * 0 + (0, 1) * 0.5
  364. var basis_y = parent.x * child.y.x + parent.y * child.y.y
  365. # Change the node's transform to what we calculated.
  366. transform = Transform2D(basis_x, basis_y, origin)
  367. .. code-tab:: csharp
  368. // Set up transforms like in the image, except make positions be 100 times bigger.
  369. Transform2D parent = new Transform2D(2, 0, 0, 1, 100, 200);
  370. Transform2D child = new Transform2D(0.5f, 0, 0, 0.5f, 100, 100);
  371. // Calculate the child's world space transform
  372. // origin = (2, 0) * 100 + (0, 1) * 100 + (100, 200)
  373. Vector2 origin = parent.X * child.Origin.X + parent.Y * child.Origin.Y + parent.Origin;
  374. // basisX = (2, 0) * 0.5 + (0, 1) * 0 = (0.5, 0)
  375. Vector2 basisX = parent.X * child.X.X + parent.Y * child.X.Y;
  376. // basisY = (2, 0) * 0 + (0, 1) * 0.5 = (0.5, 0)
  377. Vector2 basisY = parent.X * child.Y.X + parent.Y * child.Y.Y;
  378. // Change the node's transform to what we calculated.
  379. Transform = new Transform2D(basisX, basisY, origin);
  380. In actual projects, we can find the world transform of the child by
  381. applying one transform onto another using the ``*`` operator:
  382. .. tabs::
  383. .. code-tab:: gdscript GDScript
  384. # Set up transforms like in the image, except make positions be 100 times bigger.
  385. var parent = Transform2D(Vector2(2, 0), Vector2(0, 1), Vector2(100, 200))
  386. var child = Transform2D(Vector2(0.5, 0), Vector2(0, 0.5), Vector2(100, 100))
  387. # Change the node's transform to what would be the child's world transform.
  388. transform = parent * child
  389. .. code-tab:: csharp
  390. // Set up transforms like in the image, except make positions be 100 times bigger.
  391. Transform2D parent = new Transform2D(2, 0, 0, 1, 100, 200);
  392. Transform2D child = new Transform2D(0.5f, 0, 0, 0.5f, 100, 100);
  393. // Change the node's transform to what would be the child's world transform.
  394. Transform = parent * child;
  395. .. note:: When multiplying matrices, order matters! Don't mix them up.
  396. Lastly, applying the identity transform will always do nothing.
  397. If you would like additional explanation, you should check out
  398. 3Blue1Brown's excellent video about matrix composition:
  399. https://www.youtube.com/watch?v=XkY2DOUCWMU
  400. Inverting a transformation matrix
  401. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  402. The "affine_inverse" function returns a transform that "undoes" the
  403. previous transform. This can be useful in some situations.
  404. Let's take a look at a few examples.
  405. Multiplying an inverse transform by the normal transform undoes all
  406. transformations:
  407. .. tabs::
  408. .. code-tab:: gdscript GDScript
  409. var ti = transform.affine_inverse()
  410. var t = ti * transform
  411. # The transform is the identity transform.
  412. .. code-tab:: csharp
  413. Transform2D ti = Transform.AffineInverse();
  414. Transform2D t = ti * Transform;
  415. // The transform is the identity transform.
  416. Transforming a position by a transform and its inverse results in the
  417. same position:
  418. .. tabs::
  419. .. code-tab:: gdscript GDScript
  420. var ti = transform.affine_inverse()
  421. position = transform * position
  422. position = ti * position
  423. # The position is the same as before.
  424. .. code-tab:: csharp
  425. Transform2D ti = Transform.AffineInverse();
  426. Position = Transform * Position;
  427. Position = ti * Position;
  428. // The position is the same as before.
  429. How does it all work in 3D?
  430. ---------------------------
  431. One of the great things about transformation matrices is that they
  432. work very similarly between 2D and 3D transformations.
  433. All the code and formulas used above for 2D work the same in 3D,
  434. with 3 exceptions: the addition of a third axis, that each
  435. axis is of type :ref:`class_Vector3`, and also that Godot stores
  436. the :ref:`class_Basis` separately from the :ref:`class_Transform3D`,
  437. since the math can get complex and it makes sense to separate it.
  438. All of the concepts for how translation, rotation, scale, and shearing
  439. work in 3D are all the same compared to 2D. To scale, we take each
  440. component and multiply it; to rotate, we change where each basis vector
  441. is pointing; to translate, we manipulate the origin; and to shear, we
  442. change the basis vectors to be non-perpendicular.
  443. .. image:: img/matrices_and_transforms/3d-identity.png
  444. If you would like, it's a good idea to play around with transforms
  445. to get an understanding of how they work. Godot allows you to edit
  446. 3D transform matrices directly from the inspector. You can download
  447. this project which has colored lines and cubes to help visualize the
  448. :ref:`class_Basis` vectors and the origin in both 2D and 3D:
  449. https://github.com/godotengine/godot-demo-projects/tree/master/misc/matrix_transform
  450. .. note:: You cannot edit Node2D's transform matrix directly in Godot 4.0's
  451. inspector. This may be changed in a future release of Godot.
  452. If you would like additional explanation, you should check out
  453. 3Blue1Brown's excellent video about 3D linear transformations:
  454. https://www.youtube.com/watch?v=rHLEWRxRGiM
  455. Representing rotation in 3D (advanced)
  456. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  457. The biggest difference between 2D and 3D transformation matrices is
  458. how you represent rotation by itself without the basis vectors.
  459. With 2D, we have an easy way (atan2) to switch between a transformation
  460. matrix and an angle. In 3D, rotation is too complex to represent as one
  461. number. There is something called Euler angles, which can represent
  462. rotations as a set of 3 numbers, however, they are limited and not very
  463. useful, except for trivial cases.
  464. In 3D we do not typically use angles, we either use a transformation basis
  465. (used pretty much everywhere in Godot), or we use quaternions. Godot can
  466. represent quaternions using the :ref:`class_Quaternion` struct. My suggestion
  467. to you is to completely ignore how they work under-the-hood, because
  468. they are very complicated and unintuitive.
  469. However, if you really must know how it works, here are some great
  470. resources, which you can follow in order:
  471. https://www.youtube.com/watch?v=mvmuCPvRoWQ
  472. https://www.youtube.com/watch?v=d4EgbgTm0Bg
  473. https://eater.net/quaternions