class_basis.rst 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/4.1/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.1/doc/classes/Basis.xml.
  6. .. _class_Basis:
  7. Basis
  8. =====
  9. A 3×3 matrix for representing 3D rotation and scale.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. The **Basis** built-in :ref:`Variant<class_Variant>` type is a 3x3 `matrix <https://en.wikipedia.org/wiki/Matrix_(mathematics)>`__ used to represent 3D rotation, scale, and shear. It is frequently used within a :ref:`Transform3D<class_Transform3D>`.
  14. A **Basis** is composed by 3 axis vectors, each representing a column of the matrix: :ref:`x<class_Basis_property_x>`, :ref:`y<class_Basis_property_y>`, and :ref:`z<class_Basis_property_z>`. The length of each axis (:ref:`Vector3.length<class_Vector3_method_length>`) influences the basis's scale, while the direction of all axes influence the rotation. Usually, these axes are perpendicular to one another. However, when you rotate any axis individually, the basis becomes sheared. Applying a sheared basis to a 3D model will make the model appear distorted.
  15. A **Basis** is **orthogonal** if its axes are perpendicular to each other. A basis is **normalized** if the length of every axis is ``1``. A basis is **uniform** if all axes share the same length (see :ref:`get_scale<class_Basis_method_get_scale>`). A basis is **orthonormal** if it is both orthogonal and normalized, which allows it to only represent rotations. A basis is **conformal** if it is both orthogonal and uniform, which ensures it is not distorted.
  16. For a general introduction, see the :doc:`Matrices and transforms <../tutorials/math/matrices_and_transforms>` tutorial.
  17. \ **Note:** Godot uses a `right-handed coordinate system <https://en.wikipedia.org/wiki/Right-hand_rule>`__, which is a common standard. For directions, the convention for built-in types like :ref:`Camera3D<class_Camera3D>` is for -Z to point forward (+X is right, +Y is up, and +Z is back). Other objects may use different direction conventions. For more information, see the `Importing 3D Scenes <../tutorials/assets_pipeline/importing_scenes.html#d-asset-direction-conventions>`__ tutorial.
  18. \ **Note:** The basis matrices are exposed as `column-major <https://www.mindcontrol.org/~hplus/graphics/matrix-layout.html>`__ order, which is the same as OpenGL. However, they are stored internally in row-major order, which is the same as DirectX.
  19. .. rst-class:: classref-introduction-group
  20. Tutorials
  21. ---------
  22. - :doc:`Math documentation index <../tutorials/math/index>`
  23. - :doc:`Matrices and transforms <../tutorials/math/matrices_and_transforms>`
  24. - :doc:`Using 3D transforms <../tutorials/3d/using_transforms>`
  25. - `Matrix Transform Demo <https://godotengine.org/asset-library/asset/584>`__
  26. - `3D Platformer Demo <https://godotengine.org/asset-library/asset/125>`__
  27. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/676>`__
  28. - `2.5D Demo <https://godotengine.org/asset-library/asset/583>`__
  29. .. rst-class:: classref-reftable-group
  30. Properties
  31. ----------
  32. .. table::
  33. :widths: auto
  34. +-------------------------------+----------------------------------+----------------------+
  35. | :ref:`Vector3<class_Vector3>` | :ref:`x<class_Basis_property_x>` | ``Vector3(1, 0, 0)`` |
  36. +-------------------------------+----------------------------------+----------------------+
  37. | :ref:`Vector3<class_Vector3>` | :ref:`y<class_Basis_property_y>` | ``Vector3(0, 1, 0)`` |
  38. +-------------------------------+----------------------------------+----------------------+
  39. | :ref:`Vector3<class_Vector3>` | :ref:`z<class_Basis_property_z>` | ``Vector3(0, 0, 1)`` |
  40. +-------------------------------+----------------------------------+----------------------+
  41. .. rst-class:: classref-reftable-group
  42. Constructors
  43. ------------
  44. .. table::
  45. :widths: auto
  46. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`Basis<class_Basis>` | :ref:`Basis<class_Basis_constructor_Basis>` **(** **)** |
  48. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`Basis<class_Basis>` | :ref:`Basis<class_Basis_constructor_Basis>` **(** :ref:`Basis<class_Basis>` from **)** |
  50. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`Basis<class_Basis>` | :ref:`Basis<class_Basis_constructor_Basis>` **(** :ref:`Vector3<class_Vector3>` axis, :ref:`float<class_float>` angle **)** |
  52. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`Basis<class_Basis>` | :ref:`Basis<class_Basis_constructor_Basis>` **(** :ref:`Quaternion<class_Quaternion>` from **)** |
  54. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`Basis<class_Basis>` | :ref:`Basis<class_Basis_constructor_Basis>` **(** :ref:`Vector3<class_Vector3>` x_axis, :ref:`Vector3<class_Vector3>` y_axis, :ref:`Vector3<class_Vector3>` z_axis **)** |
  56. +---------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. .. rst-class:: classref-reftable-group
  58. Methods
  59. -------
  60. .. table::
  61. :widths: auto
  62. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`float<class_float>` | :ref:`determinant<class_Basis_method_determinant>` **(** **)** |const| |
  64. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`Basis<class_Basis>` | :ref:`from_euler<class_Basis_method_from_euler>` **(** :ref:`Vector3<class_Vector3>` euler, :ref:`int<class_int>` order=2 **)** |static| |
  66. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`Basis<class_Basis>` | :ref:`from_scale<class_Basis_method_from_scale>` **(** :ref:`Vector3<class_Vector3>` scale **)** |static| |
  68. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`Vector3<class_Vector3>` | :ref:`get_euler<class_Basis_method_get_euler>` **(** :ref:`int<class_int>` order=2 **)** |const| |
  70. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`Quaternion<class_Quaternion>` | :ref:`get_rotation_quaternion<class_Basis_method_get_rotation_quaternion>` **(** **)** |const| |
  72. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | :ref:`Vector3<class_Vector3>` | :ref:`get_scale<class_Basis_method_get_scale>` **(** **)** |const| |
  74. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`Basis<class_Basis>` | :ref:`inverse<class_Basis_method_inverse>` **(** **)** |const| |
  76. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`bool<class_bool>` | :ref:`is_equal_approx<class_Basis_method_is_equal_approx>` **(** :ref:`Basis<class_Basis>` b **)** |const| |
  78. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`bool<class_bool>` | :ref:`is_finite<class_Basis_method_is_finite>` **(** **)** |const| |
  80. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`Basis<class_Basis>` | :ref:`looking_at<class_Basis_method_looking_at>` **(** :ref:`Vector3<class_Vector3>` target, :ref:`Vector3<class_Vector3>` up=Vector3(0, 1, 0), :ref:`bool<class_bool>` use_model_front=false **)** |static| |
  82. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | :ref:`Basis<class_Basis>` | :ref:`orthonormalized<class_Basis_method_orthonormalized>` **(** **)** |const| |
  84. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`Basis<class_Basis>` | :ref:`rotated<class_Basis_method_rotated>` **(** :ref:`Vector3<class_Vector3>` axis, :ref:`float<class_float>` angle **)** |const| |
  86. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | :ref:`Basis<class_Basis>` | :ref:`scaled<class_Basis_method_scaled>` **(** :ref:`Vector3<class_Vector3>` scale **)** |const| |
  88. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`Basis<class_Basis>` | :ref:`slerp<class_Basis_method_slerp>` **(** :ref:`Basis<class_Basis>` to, :ref:`float<class_float>` weight **)** |const| |
  90. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | :ref:`float<class_float>` | :ref:`tdotx<class_Basis_method_tdotx>` **(** :ref:`Vector3<class_Vector3>` with **)** |const| |
  92. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  93. | :ref:`float<class_float>` | :ref:`tdoty<class_Basis_method_tdoty>` **(** :ref:`Vector3<class_Vector3>` with **)** |const| |
  94. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  95. | :ref:`float<class_float>` | :ref:`tdotz<class_Basis_method_tdotz>` **(** :ref:`Vector3<class_Vector3>` with **)** |const| |
  96. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  97. | :ref:`Basis<class_Basis>` | :ref:`transposed<class_Basis_method_transposed>` **(** **)** |const| |
  98. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  99. .. rst-class:: classref-reftable-group
  100. Operators
  101. ---------
  102. .. table::
  103. :widths: auto
  104. +-------------------------------+-----------------------------------------------------------------------------------------------------+
  105. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Basis_operator_neq_Basis>` **(** :ref:`Basis<class_Basis>` right **)** |
  106. +-------------------------------+-----------------------------------------------------------------------------------------------------+
  107. | :ref:`Basis<class_Basis>` | :ref:`operator *<class_Basis_operator_mul_Basis>` **(** :ref:`Basis<class_Basis>` right **)** |
  108. +-------------------------------+-----------------------------------------------------------------------------------------------------+
  109. | :ref:`Vector3<class_Vector3>` | :ref:`operator *<class_Basis_operator_mul_Vector3>` **(** :ref:`Vector3<class_Vector3>` right **)** |
  110. +-------------------------------+-----------------------------------------------------------------------------------------------------+
  111. | :ref:`Basis<class_Basis>` | :ref:`operator *<class_Basis_operator_mul_float>` **(** :ref:`float<class_float>` right **)** |
  112. +-------------------------------+-----------------------------------------------------------------------------------------------------+
  113. | :ref:`Basis<class_Basis>` | :ref:`operator *<class_Basis_operator_mul_int>` **(** :ref:`int<class_int>` right **)** |
  114. +-------------------------------+-----------------------------------------------------------------------------------------------------+
  115. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Basis_operator_eq_Basis>` **(** :ref:`Basis<class_Basis>` right **)** |
  116. +-------------------------------+-----------------------------------------------------------------------------------------------------+
  117. | :ref:`Vector3<class_Vector3>` | :ref:`operator []<class_Basis_operator_idx_int>` **(** :ref:`int<class_int>` index **)** |
  118. +-------------------------------+-----------------------------------------------------------------------------------------------------+
  119. .. rst-class:: classref-section-separator
  120. ----
  121. .. rst-class:: classref-descriptions-group
  122. Constants
  123. ---------
  124. .. _class_Basis_constant_IDENTITY:
  125. .. rst-class:: classref-constant
  126. **IDENTITY** = ``Basis(1, 0, 0, 0, 1, 0, 0, 0, 1)``
  127. The identity basis. This is a basis with no rotation, no shear, and its scale being ``1``. This means that:
  128. - The :ref:`x<class_Basis_property_x>` points right (:ref:`Vector3.RIGHT<class_Vector3_constant_RIGHT>`);
  129. - The :ref:`y<class_Basis_property_y>` points up (:ref:`Vector3.UP<class_Vector3_constant_UP>`);
  130. - The :ref:`z<class_Basis_property_z>` points back (:ref:`Vector3.BACK<class_Vector3_constant_BACK>`).
  131. ::
  132. var basis := Basis.IDENTITY
  133. print("| X | Y | Z")
  134. print("| %s | %s | %s" % [basis.x.x, basis.y.x, basis.z.x])
  135. print("| %s | %s | %s" % [basis.x.y, basis.y.y, basis.z.y])
  136. print("| %s | %s | %s" % [basis.x.z, basis.y.z, basis.z.z])
  137. # Prints:
  138. # | X | Y | Z
  139. # | 1 | 0 | 0
  140. # | 0 | 1 | 0
  141. # | 0 | 0 | 1
  142. This is identical to creating ``Basis()`` without any parameters. This constant can be used to make your code clearer, and for consistency with C#.
  143. .. _class_Basis_constant_FLIP_X:
  144. .. rst-class:: classref-constant
  145. **FLIP_X** = ``Basis(-1, 0, 0, 0, 1, 0, 0, 0, 1)``
  146. When any basis is multiplied by :ref:`FLIP_X<class_Basis_constant_FLIP_X>`, it negates all components of the :ref:`x<class_Basis_property_x>` axis (the X column).
  147. When :ref:`FLIP_X<class_Basis_constant_FLIP_X>` is multiplied by any basis, it negates the :ref:`Vector3.x<class_Vector3_property_x>` component of all axes (the X row).
  148. .. _class_Basis_constant_FLIP_Y:
  149. .. rst-class:: classref-constant
  150. **FLIP_Y** = ``Basis(1, 0, 0, 0, -1, 0, 0, 0, 1)``
  151. When any basis is multiplied by :ref:`FLIP_Y<class_Basis_constant_FLIP_Y>`, it negates all components of the :ref:`y<class_Basis_property_y>` axis (the Y column).
  152. When :ref:`FLIP_Y<class_Basis_constant_FLIP_Y>` is multiplied by any basis, it negates the :ref:`Vector3.y<class_Vector3_property_y>` component of all axes (the Y row).
  153. .. _class_Basis_constant_FLIP_Z:
  154. .. rst-class:: classref-constant
  155. **FLIP_Z** = ``Basis(1, 0, 0, 0, 1, 0, 0, 0, -1)``
  156. When any basis is multiplied by :ref:`FLIP_Z<class_Basis_constant_FLIP_Z>`, it negates all components of the :ref:`z<class_Basis_property_z>` axis (the Z column).
  157. When :ref:`FLIP_Z<class_Basis_constant_FLIP_Z>` is multiplied by any basis, it negates the :ref:`Vector3.z<class_Vector3_property_z>` component of all axes (the Z row).
  158. .. rst-class:: classref-section-separator
  159. ----
  160. .. rst-class:: classref-descriptions-group
  161. Property Descriptions
  162. ---------------------
  163. .. _class_Basis_property_x:
  164. .. rst-class:: classref-property
  165. :ref:`Vector3<class_Vector3>` **x** = ``Vector3(1, 0, 0)``
  166. The basis's X axis, and the column ``0`` of the matrix.
  167. On the identity basis, this vector points right (:ref:`Vector3.RIGHT<class_Vector3_constant_RIGHT>`).
  168. .. rst-class:: classref-item-separator
  169. ----
  170. .. _class_Basis_property_y:
  171. .. rst-class:: classref-property
  172. :ref:`Vector3<class_Vector3>` **y** = ``Vector3(0, 1, 0)``
  173. The basis's Y axis, and the column ``1`` of the matrix.
  174. On the identity basis, this vector points up (:ref:`Vector3.UP<class_Vector3_constant_UP>`).
  175. .. rst-class:: classref-item-separator
  176. ----
  177. .. _class_Basis_property_z:
  178. .. rst-class:: classref-property
  179. :ref:`Vector3<class_Vector3>` **z** = ``Vector3(0, 0, 1)``
  180. The basis's Z axis, and the column ``2`` of the matrix.
  181. On the identity basis, this vector points back (:ref:`Vector3.BACK<class_Vector3_constant_BACK>`).
  182. .. rst-class:: classref-section-separator
  183. ----
  184. .. rst-class:: classref-descriptions-group
  185. Constructor Descriptions
  186. ------------------------
  187. .. _class_Basis_constructor_Basis:
  188. .. rst-class:: classref-constructor
  189. :ref:`Basis<class_Basis>` **Basis** **(** **)**
  190. Constructs a **Basis** identical to the :ref:`IDENTITY<class_Basis_constant_IDENTITY>`.
  191. .. rst-class:: classref-item-separator
  192. ----
  193. .. rst-class:: classref-constructor
  194. :ref:`Basis<class_Basis>` **Basis** **(** :ref:`Basis<class_Basis>` from **)**
  195. Constructs a **Basis** as a copy of the given **Basis**.
  196. .. rst-class:: classref-item-separator
  197. ----
  198. .. rst-class:: classref-constructor
  199. :ref:`Basis<class_Basis>` **Basis** **(** :ref:`Vector3<class_Vector3>` axis, :ref:`float<class_float>` angle **)**
  200. Constructs a **Basis** that only represents rotation, rotated around the ``axis`` by the given ``angle``, in radians. The axis must be a normalized vector.
  201. \ **Note:** This is the same as using :ref:`rotated<class_Basis_method_rotated>` on the :ref:`IDENTITY<class_Basis_constant_IDENTITY>` basis. With more than one angle consider using :ref:`from_euler<class_Basis_method_from_euler>`, instead.
  202. .. rst-class:: classref-item-separator
  203. ----
  204. .. rst-class:: classref-constructor
  205. :ref:`Basis<class_Basis>` **Basis** **(** :ref:`Quaternion<class_Quaternion>` from **)**
  206. Constructs a **Basis** that only represents rotation from the given :ref:`Quaternion<class_Quaternion>`.
  207. \ **Note:** Quaternions *only* store rotation, not scale. Because of this, conversions from **Basis** to :ref:`Quaternion<class_Quaternion>` cannot always be reversed.
  208. .. rst-class:: classref-item-separator
  209. ----
  210. .. rst-class:: classref-constructor
  211. :ref:`Basis<class_Basis>` **Basis** **(** :ref:`Vector3<class_Vector3>` x_axis, :ref:`Vector3<class_Vector3>` y_axis, :ref:`Vector3<class_Vector3>` z_axis **)**
  212. Constructs a **Basis** from 3 axis vectors. These are the columns of the basis matrix.
  213. .. rst-class:: classref-section-separator
  214. ----
  215. .. rst-class:: classref-descriptions-group
  216. Method Descriptions
  217. -------------------
  218. .. _class_Basis_method_determinant:
  219. .. rst-class:: classref-method
  220. :ref:`float<class_float>` **determinant** **(** **)** |const|
  221. Returns the `determinant <https://en.wikipedia.org/wiki/Determinant>`__ of this basis's matrix. For advanced math, this number can be used to determine a few attributes:
  222. - If the determinant is exactly ``0``, the basis is not invertible (see :ref:`inverse<class_Basis_method_inverse>`).
  223. - If the determinant is a negative number, the basis represents a negative scale.
  224. \ **Note:** If the basis's scale is the same for every axis, its determinant is always that scale by the power of 2.
  225. .. rst-class:: classref-item-separator
  226. ----
  227. .. _class_Basis_method_from_euler:
  228. .. rst-class:: classref-method
  229. :ref:`Basis<class_Basis>` **from_euler** **(** :ref:`Vector3<class_Vector3>` euler, :ref:`int<class_int>` order=2 **)** |static|
  230. Constructs a new **Basis** that only represents rotation from the given :ref:`Vector3<class_Vector3>` of `Euler angles <https://en.wikipedia.org/wiki/Euler_angles>`__, in radians.
  231. - The :ref:`Vector3.x<class_Vector3_property_x>` should contain the angle around the :ref:`x<class_Basis_property_x>` axis (pitch).
  232. - The :ref:`Vector3.y<class_Vector3_property_y>` should contain the angle around the :ref:`y<class_Basis_property_y>` axis (yaw).
  233. - The :ref:`Vector3.z<class_Vector3_property_z>` should contain the angle around the :ref:`z<class_Basis_property_z>` axis (roll).
  234. .. tabs::
  235. .. code-tab:: gdscript
  236. # Creates a Basis whose z axis points down.
  237. var my_basis = Basis.from_euler(Vector3(TAU / 4, 0, 0))
  238. print(my_basis.z) # Prints (0, -1, 0).
  239. .. code-tab:: csharp
  240. // Creates a Basis whose z axis points down.
  241. var myBasis = Basis.FromEuler(new Vector3(Mathf.Tau / 4.0f, 0.0f, 0.0f));
  242. GD.Print(myBasis.Z); // Prints (0, -1, 0).
  243. The order of each consecutive rotation can be changed with ``order`` (see :ref:`EulerOrder<enum_@GlobalScope_EulerOrder>` constants). By default, the YXZ convention is used (:ref:`@GlobalScope.EULER_ORDER_YXZ<class_@GlobalScope_constant_EULER_ORDER_YXZ>`): the basis rotates first around the Y axis (yaw), then X (pitch), and lastly Z (roll). When using the opposite method :ref:`get_euler<class_Basis_method_get_euler>`, this order is reversed.
  244. .. rst-class:: classref-item-separator
  245. ----
  246. .. _class_Basis_method_from_scale:
  247. .. rst-class:: classref-method
  248. :ref:`Basis<class_Basis>` **from_scale** **(** :ref:`Vector3<class_Vector3>` scale **)** |static|
  249. Constructs a new **Basis** that only represents scale, with no rotation or shear, from the given ``scale`` vector.
  250. .. tabs::
  251. .. code-tab:: gdscript
  252. var my_basis = Basis.from_scale(Vector3(2, 4, 8))
  253. print(my_basis.x) # Prints (2, 0, 0).
  254. print(my_basis.y) # Prints (0, 4, 0).
  255. print(my_basis.z) # Prints (0, 0, 8).
  256. .. code-tab:: csharp
  257. var myBasis = Basis.FromScale(new Vector3(2.0f, 4.0f, 8.0f));
  258. GD.Print(myBasis.X); // Prints (2, 0, 0).
  259. GD.Print(myBasis.Y); // Prints (0, 4, 0).
  260. GD.Print(myBasis.Z); // Prints (0, 0, 8).
  261. \ **Note:** In linear algebra, the matrix of this basis is also known as a `diagonal matrix <https://en.wikipedia.org/wiki/Diagonal_matrix>`__.
  262. .. rst-class:: classref-item-separator
  263. ----
  264. .. _class_Basis_method_get_euler:
  265. .. rst-class:: classref-method
  266. :ref:`Vector3<class_Vector3>` **get_euler** **(** :ref:`int<class_int>` order=2 **)** |const|
  267. Returns this basis's rotation as a :ref:`Vector3<class_Vector3>` of `Euler angles <https://en.wikipedia.org/wiki/Euler_angles>`__, in radians.
  268. - The :ref:`Vector3.x<class_Vector3_property_x>` contains the angle around the :ref:`x<class_Basis_property_x>` axis (pitch);
  269. - The :ref:`Vector3.y<class_Vector3_property_y>` contains the angle around the :ref:`y<class_Basis_property_y>` axis (yaw);
  270. - The :ref:`Vector3.z<class_Vector3_property_z>` contains the angle around the :ref:`z<class_Basis_property_z>` axis (roll).
  271. The order of each consecutive rotation can be changed with ``order`` (see :ref:`EulerOrder<enum_@GlobalScope_EulerOrder>` constants). By default, the YXZ convention is used (:ref:`@GlobalScope.EULER_ORDER_YXZ<class_@GlobalScope_constant_EULER_ORDER_YXZ>`): Z (roll) is calculated first, then X (pitch), and lastly Y (yaw). When using the opposite method :ref:`from_euler<class_Basis_method_from_euler>`, this order is reversed.
  272. \ **Note:** Euler angles are much more intuitive but are not suitable for 3D math. Because of this, consider using the :ref:`get_rotation_quaternion<class_Basis_method_get_rotation_quaternion>` method instead, which returns a :ref:`Quaternion<class_Quaternion>`.
  273. \ **Note:** In the Inspector dock, a basis's rotation is often displayed in Euler angles (in degrees), as is the case with the :ref:`Node3D.rotation<class_Node3D_property_rotation>` property.
  274. .. rst-class:: classref-item-separator
  275. ----
  276. .. _class_Basis_method_get_rotation_quaternion:
  277. .. rst-class:: classref-method
  278. :ref:`Quaternion<class_Quaternion>` **get_rotation_quaternion** **(** **)** |const|
  279. Returns this basis's rotation as a :ref:`Quaternion<class_Quaternion>`.
  280. \ **Note:** Quatenions are much more suitable for 3D math but are less intuitive. For user interfaces, consider using the :ref:`get_euler<class_Basis_method_get_euler>` method, which returns Euler angles.
  281. .. rst-class:: classref-item-separator
  282. ----
  283. .. _class_Basis_method_get_scale:
  284. .. rst-class:: classref-method
  285. :ref:`Vector3<class_Vector3>` **get_scale** **(** **)** |const|
  286. Returns the length of each axis of this basis, as a :ref:`Vector3<class_Vector3>`. If the basis is not sheared, this is the scaling factor. It is not affected by rotation.
  287. .. tabs::
  288. .. code-tab:: gdscript
  289. var my_basis = Basis(
  290. Vector3(2, 0, 0),
  291. Vector3(0, 4, 0),
  292. Vector3(0, 0, 8)
  293. )
  294. # Rotating the Basis in any way preserves its scale.
  295. my_basis = my_basis.rotated(Vector3.UP, TAU / 2)
  296. my_basis = my_basis.rotated(Vector3.RIGHT, TAU / 4)
  297. print(my_basis.get_scale()) # Prints (2, 4, 8).
  298. .. code-tab:: csharp
  299. var myBasis = new Basis(
  300. Vector3(2.0f, 0.0f, 0.0f),
  301. Vector3(0.0f, 4.0f, 0.0f),
  302. Vector3(0.0f, 0.0f, 8.0f)
  303. );
  304. // Rotating the Basis in any way preserves its scale.
  305. myBasis = myBasis.Rotated(Vector3.Up, Mathf.Tau / 2.0f);
  306. myBasis = myBasis.Rotated(Vector3.Right, Mathf.Tau / 4.0f);
  307. GD.Print(myBasis.Scale); // Prints (2, 4, 8).
  308. \ **Note:** If the value returned by :ref:`determinant<class_Basis_method_determinant>` is negative, the scale is also negative.
  309. .. rst-class:: classref-item-separator
  310. ----
  311. .. _class_Basis_method_inverse:
  312. .. rst-class:: classref-method
  313. :ref:`Basis<class_Basis>` **inverse** **(** **)** |const|
  314. Returns the `inverse of this basis's matrix <https://en.wikipedia.org/wiki/Invertible_matrix>`__.
  315. .. rst-class:: classref-item-separator
  316. ----
  317. .. _class_Basis_method_is_equal_approx:
  318. .. rst-class:: classref-method
  319. :ref:`bool<class_bool>` **is_equal_approx** **(** :ref:`Basis<class_Basis>` b **)** |const|
  320. Returns ``true`` if this basis and ``b`` are approximately equal, by calling :ref:`@GlobalScope.is_equal_approx<class_@GlobalScope_method_is_equal_approx>` on all vector components.
  321. .. rst-class:: classref-item-separator
  322. ----
  323. .. _class_Basis_method_is_finite:
  324. .. rst-class:: classref-method
  325. :ref:`bool<class_bool>` **is_finite** **(** **)** |const|
  326. Returns ``true`` if this basis is finite, by calling :ref:`@GlobalScope.is_finite<class_@GlobalScope_method_is_finite>` on all vector components.
  327. .. rst-class:: classref-item-separator
  328. ----
  329. .. _class_Basis_method_looking_at:
  330. .. rst-class:: classref-method
  331. :ref:`Basis<class_Basis>` **looking_at** **(** :ref:`Vector3<class_Vector3>` target, :ref:`Vector3<class_Vector3>` up=Vector3(0, 1, 0), :ref:`bool<class_bool>` use_model_front=false **)** |static|
  332. Creates a new **Basis** with a rotation such that the forward axis (-Z) points towards the ``target`` position.
  333. By default, the -Z axis (camera forward) is treated as forward (implies +X is right). If ``use_model_front`` is ``true``, the +Z axis (asset front) is treated as forward (implies +X is left) and points toward the ``target`` position.
  334. The up axis (+Y) points as close to the ``up`` vector as possible while staying perpendicular to the forward axis. The returned basis is orthonormalized (see :ref:`orthonormalized<class_Basis_method_orthonormalized>`). The ``target`` and ``up`` vectors cannot be :ref:`Vector3.ZERO<class_Vector3_constant_ZERO>`, and cannot be parallel to each other.
  335. .. rst-class:: classref-item-separator
  336. ----
  337. .. _class_Basis_method_orthonormalized:
  338. .. rst-class:: classref-method
  339. :ref:`Basis<class_Basis>` **orthonormalized** **(** **)** |const|
  340. Returns the orthonormalized version of this basis. An orthonormal basis is both *orthogonal* (the axes are perpendicular to each other) and *normalized* (the axes have a length of ``1``), which also means it can only represent rotation.
  341. It is often useful to call this method to avoid rounding errors on a rotating basis:
  342. .. tabs::
  343. .. code-tab:: gdscript
  344. # Rotate this Node3D every frame.
  345. func _process(delta):
  346. basis = basis.rotated(Vector3.UP, TAU * delta)
  347. basis = basis.rotated(Vector3.RIGHT, TAU * delta)
  348. basis = basis.orthonormalized()
  349. .. code-tab:: csharp
  350. // Rotate this Node3D every frame.
  351. public override void _Process(double delta)
  352. {
  353. Basis = Basis.Rotated(Vector3.Up, Mathf.Tau * (float)delta)
  354. .Rotated(Vector3.Right, Mathf.Tau * (float)delta)
  355. .Orthonormalized();
  356. }
  357. .. rst-class:: classref-item-separator
  358. ----
  359. .. _class_Basis_method_rotated:
  360. .. rst-class:: classref-method
  361. :ref:`Basis<class_Basis>` **rotated** **(** :ref:`Vector3<class_Vector3>` axis, :ref:`float<class_float>` angle **)** |const|
  362. Returns this basis rotated around the given ``axis`` by ``angle`` (in radians). The ``axis`` must be a normalized vector (see :ref:`Vector3.normalized<class_Vector3_method_normalized>`).
  363. Positive values rotate this basis clockwise around the axis, while negative values rotate it counterclockwise.
  364. .. tabs::
  365. .. code-tab:: gdscript
  366. var my_basis = Basis.IDENTITY
  367. var angle = TAU / 2
  368. my_basis = my_basis.rotated(Vector3.UP, angle) # Rotate around the up axis (yaw).
  369. my_basis = my_basis.rotated(Vector3.RIGHT, angle) # Rotate around the right axis (pitch).
  370. my_basis = my_basis.rotated(Vector3.BACK, angle) # Rotate around the back axis (roll).
  371. .. code-tab:: csharp
  372. var myBasis = Basis.Identity;
  373. var angle = Mathf.Tau / 2.0f;
  374. myBasis = myBasis.Rotated(Vector3.Up, angle); // Rotate around the up axis (yaw).
  375. myBasis = myBasis.Rotated(Vector3.Right, angle); // Rotate around the right axis (pitch).
  376. myBasis = myBasis.Rotated(Vector3.Back, angle); // Rotate around the back axis (roll).
  377. .. rst-class:: classref-item-separator
  378. ----
  379. .. _class_Basis_method_scaled:
  380. .. rst-class:: classref-method
  381. :ref:`Basis<class_Basis>` **scaled** **(** :ref:`Vector3<class_Vector3>` scale **)** |const|
  382. Returns this basis with each axis's components scaled by the given ``scale``'s components.
  383. The basis matrix's rows are multiplied by ``scale``'s components. This operation is a global scale (relative to the parent).
  384. .. tabs::
  385. .. code-tab:: gdscript
  386. var my_basis = Basis(
  387. Vector3(1, 1, 1),
  388. Vector3(2, 2, 2),
  389. Vector3(3, 3, 3)
  390. )
  391. my_basis = my_basis.scaled(Vector3(0, 2, -2))
  392. print(my_basis.x) # Prints (0, 2, -2).
  393. print(my_basis.y) # Prints (0, 4, -4).
  394. print(my_basis.z) # Prints (0, 6, -6).
  395. .. code-tab:: csharp
  396. var myBasis = new Basis(
  397. new Vector3(1.0f, 1.0f, 1.0f),
  398. new Vector3(2.0f, 2.0f, 2.0f),
  399. new Vector3(3.0f, 3.0f, 3.0f)
  400. );
  401. myBasis = myBasis.Scaled(new Vector3(0.0f, 2.0f, -2.0f));
  402. GD.Print(myBasis.X); // Prints (0, 2, -2).
  403. GD.Print(myBasis.Y); // Prints (0, 4, -4).
  404. GD.Print(myBasis.Z); // Prints (0, 6, -6).
  405. .. rst-class:: classref-item-separator
  406. ----
  407. .. _class_Basis_method_slerp:
  408. .. rst-class:: classref-method
  409. :ref:`Basis<class_Basis>` **slerp** **(** :ref:`Basis<class_Basis>` to, :ref:`float<class_float>` weight **)** |const|
  410. Performs a spherical-linear interpolation with the ``to`` basis, given a ``weight``. Both this basis and ``to`` should represent a rotation.
  411. \ **Example:** Smoothly rotate a :ref:`Node3D<class_Node3D>` to the target basis over time, with a :ref:`Tween<class_Tween>`.
  412. ::
  413. var start_basis = Basis.IDENTITY
  414. var target_basis = Basis.IDENTITY.rotated(Vector3.UP, TAU / 2)
  415. func _ready():
  416. create_tween().tween_method(interpolate, 0.0, 1.0, 5.0).set_trans(Tween.TRANS_EXPO)
  417. func interpolate(weight):
  418. basis = start_basis.slerp(target_basis, weight)
  419. .. rst-class:: classref-item-separator
  420. ----
  421. .. _class_Basis_method_tdotx:
  422. .. rst-class:: classref-method
  423. :ref:`float<class_float>` **tdotx** **(** :ref:`Vector3<class_Vector3>` with **)** |const|
  424. Returns the transposed dot product between ``with`` and the :ref:`x<class_Basis_property_x>` axis (see :ref:`transposed<class_Basis_method_transposed>`).
  425. This is equivalent to ``basis.x.dot(vector)``.
  426. .. rst-class:: classref-item-separator
  427. ----
  428. .. _class_Basis_method_tdoty:
  429. .. rst-class:: classref-method
  430. :ref:`float<class_float>` **tdoty** **(** :ref:`Vector3<class_Vector3>` with **)** |const|
  431. Returns the transposed dot product between ``with`` and the :ref:`y<class_Basis_property_y>` axis (see :ref:`transposed<class_Basis_method_transposed>`).
  432. This is equivalent to ``basis.y.dot(vector)``.
  433. .. rst-class:: classref-item-separator
  434. ----
  435. .. _class_Basis_method_tdotz:
  436. .. rst-class:: classref-method
  437. :ref:`float<class_float>` **tdotz** **(** :ref:`Vector3<class_Vector3>` with **)** |const|
  438. Returns the transposed dot product between ``with`` and the :ref:`z<class_Basis_property_z>` axis (see :ref:`transposed<class_Basis_method_transposed>`).
  439. This is equivalent to ``basis.z.dot(vector)``.
  440. .. rst-class:: classref-item-separator
  441. ----
  442. .. _class_Basis_method_transposed:
  443. .. rst-class:: classref-method
  444. :ref:`Basis<class_Basis>` **transposed** **(** **)** |const|
  445. Returns the transposed version of this basis. This turns the basis matrix's columns into rows, and its rows into columns.
  446. .. tabs::
  447. .. code-tab:: gdscript
  448. var my_basis = Basis(
  449. Vector3(1, 2, 3),
  450. Vector3(4, 5, 6),
  451. Vector3(7, 8, 9)
  452. )
  453. my_basis = my_basis.transposed()
  454. print(my_basis.x) # Prints (1, 4, 7).
  455. print(my_basis.y) # Prints (2, 5, 8).
  456. print(my_basis.z) # Prints (3, 6, 9).
  457. .. code-tab:: csharp
  458. var myBasis = new Basis(
  459. new Vector3(1.0f, 2.0f, 3.0f),
  460. new Vector3(4.0f, 5.0f, 6.0f),
  461. new Vector3(7.0f, 8.0f, 9.0f)
  462. );
  463. myBasis = myBasis.Transposed();
  464. GD.Print(myBasis.X); // Prints (1, 4, 7).
  465. GD.Print(myBasis.Y); // Prints (2, 5, 8).
  466. GD.Print(myBasis.Z); // Prints (3, 6, 9).
  467. .. rst-class:: classref-section-separator
  468. ----
  469. .. rst-class:: classref-descriptions-group
  470. Operator Descriptions
  471. ---------------------
  472. .. _class_Basis_operator_neq_Basis:
  473. .. rst-class:: classref-operator
  474. :ref:`bool<class_bool>` **operator !=** **(** :ref:`Basis<class_Basis>` right **)**
  475. Returns ``true`` if the components of both **Basis** matrices are not equal.
  476. \ **Note:** Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Basis_method_is_equal_approx>` instead, which is more reliable.
  477. .. rst-class:: classref-item-separator
  478. ----
  479. .. _class_Basis_operator_mul_Basis:
  480. .. rst-class:: classref-operator
  481. :ref:`Basis<class_Basis>` **operator *** **(** :ref:`Basis<class_Basis>` right **)**
  482. Transforms (multiplies) the ``right`` basis by this basis.
  483. This is the operation performed between parent and child :ref:`Node3D<class_Node3D>`\ s.
  484. .. rst-class:: classref-item-separator
  485. ----
  486. .. _class_Basis_operator_mul_Vector3:
  487. .. rst-class:: classref-operator
  488. :ref:`Vector3<class_Vector3>` **operator *** **(** :ref:`Vector3<class_Vector3>` right **)**
  489. Transforms (multiplies) the ``right`` vector by this basis, returning a :ref:`Vector3<class_Vector3>`.
  490. .. tabs::
  491. .. code-tab:: gdscript
  492. var my_basis = Basis(Vector3(1, 1, 1), Vector3(1, 1, 1), Vector3(0, 2, 5))
  493. print(my_basis * Vector3(1, 2, 3)) # Prints (7, 3, 16)
  494. .. code-tab:: csharp
  495. var myBasis = new Basis(new Vector3(1, 1, 1), new Vector3(1, 1, 1), new Vector3(0, 2, 5));
  496. GD.Print(my_basis * new Vector3(1, 2, 3)); // Prints (7, 3, 16)
  497. .. rst-class:: classref-item-separator
  498. ----
  499. .. _class_Basis_operator_mul_float:
  500. .. rst-class:: classref-operator
  501. :ref:`Basis<class_Basis>` **operator *** **(** :ref:`float<class_float>` right **)**
  502. Multiplies all components of the **Basis** by the given :ref:`float<class_float>`. This affects the basis's scale uniformly, resizing all 3 axes by the ``right`` value.
  503. .. rst-class:: classref-item-separator
  504. ----
  505. .. _class_Basis_operator_mul_int:
  506. .. rst-class:: classref-operator
  507. :ref:`Basis<class_Basis>` **operator *** **(** :ref:`int<class_int>` right **)**
  508. Multiplies all components of the **Basis** by the given :ref:`int<class_int>`. This affects the basis's scale uniformly, resizing all 3 axes by the ``right`` value.
  509. .. rst-class:: classref-item-separator
  510. ----
  511. .. _class_Basis_operator_eq_Basis:
  512. .. rst-class:: classref-operator
  513. :ref:`bool<class_bool>` **operator ==** **(** :ref:`Basis<class_Basis>` right **)**
  514. Returns ``true`` if the components of both **Basis** matrices are exactly equal.
  515. \ **Note:** Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Basis_method_is_equal_approx>` instead, which is more reliable.
  516. .. rst-class:: classref-item-separator
  517. ----
  518. .. _class_Basis_operator_idx_int:
  519. .. rst-class:: classref-operator
  520. :ref:`Vector3<class_Vector3>` **operator []** **(** :ref:`int<class_int>` index **)**
  521. Accesses each axis (column) of this basis by their index. Index ``0`` is the same as :ref:`x<class_Basis_property_x>`, index ``1`` is the same as :ref:`y<class_Basis_property_y>`, and index ``2`` is the same as :ref:`z<class_Basis_property_z>`.
  522. \ **Note:** In C++, this operator accesses the rows of the basis matrix, *not* the columns. For the same behavior as scripting languages, use the ``set_column`` and ``get_column`` methods.
  523. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  524. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  525. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  526. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  527. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  528. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  529. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`