class_vector2.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the Vector2.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_Vector2:
  6. Vector2
  7. =======
  8. **Category:** Built-In Types
  9. Brief Description
  10. -----------------
  11. Vector used for 2D math.
  12. Properties
  13. ----------
  14. +---------------------------+------------------------------------+-----+
  15. | :ref:`float<class_float>` | :ref:`x<class_Vector2_property_x>` | 0.0 |
  16. +---------------------------+------------------------------------+-----+
  17. | :ref:`float<class_float>` | :ref:`y<class_Vector2_property_y>` | 0.0 |
  18. +---------------------------+------------------------------------+-----+
  19. Methods
  20. -------
  21. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`Vector2<class_Vector2>` | :ref:`Vector2<class_Vector2_method_Vector2>` **(** :ref:`float<class_float>` x, :ref:`float<class_float>` y **)** |
  23. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | :ref:`Vector2<class_Vector2>` | :ref:`abs<class_Vector2_method_abs>` **(** **)** |
  25. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :ref:`float<class_float>` | :ref:`angle<class_Vector2_method_angle>` **(** **)** |
  27. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | :ref:`float<class_float>` | :ref:`angle_to<class_Vector2_method_angle_to>` **(** :ref:`Vector2<class_Vector2>` to **)** |
  29. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. | :ref:`float<class_float>` | :ref:`angle_to_point<class_Vector2_method_angle_to_point>` **(** :ref:`Vector2<class_Vector2>` to **)** |
  31. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | :ref:`float<class_float>` | :ref:`aspect<class_Vector2_method_aspect>` **(** **)** |
  33. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  34. | :ref:`Vector2<class_Vector2>` | :ref:`bounce<class_Vector2_method_bounce>` **(** :ref:`Vector2<class_Vector2>` n **)** |
  35. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | :ref:`Vector2<class_Vector2>` | :ref:`ceil<class_Vector2_method_ceil>` **(** **)** |
  37. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | :ref:`Vector2<class_Vector2>` | :ref:`clamped<class_Vector2_method_clamped>` **(** :ref:`float<class_float>` length **)** |
  39. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`float<class_float>` | :ref:`cross<class_Vector2_method_cross>` **(** :ref:`Vector2<class_Vector2>` with **)** |
  41. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`Vector2<class_Vector2>` | :ref:`cubic_interpolate<class_Vector2_method_cubic_interpolate>` **(** :ref:`Vector2<class_Vector2>` b, :ref:`Vector2<class_Vector2>` pre_a, :ref:`Vector2<class_Vector2>` post_b, :ref:`float<class_float>` t **)** |
  43. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | :ref:`Vector2<class_Vector2>` | :ref:`direction_to<class_Vector2_method_direction_to>` **(** :ref:`Vector2<class_Vector2>` b **)** |
  45. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`float<class_float>` | :ref:`distance_squared_to<class_Vector2_method_distance_squared_to>` **(** :ref:`Vector2<class_Vector2>` to **)** |
  47. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`float<class_float>` | :ref:`distance_to<class_Vector2_method_distance_to>` **(** :ref:`Vector2<class_Vector2>` to **)** |
  49. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`float<class_float>` | :ref:`dot<class_Vector2_method_dot>` **(** :ref:`Vector2<class_Vector2>` with **)** |
  51. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`Vector2<class_Vector2>` | :ref:`floor<class_Vector2_method_floor>` **(** **)** |
  53. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`bool<class_bool>` | :ref:`is_equal_approx<class_Vector2_method_is_equal_approx>` **(** :ref:`Vector2<class_Vector2>` v **)** |
  55. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`bool<class_bool>` | :ref:`is_normalized<class_Vector2_method_is_normalized>` **(** **)** |
  57. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`float<class_float>` | :ref:`length<class_Vector2_method_length>` **(** **)** |
  59. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`float<class_float>` | :ref:`length_squared<class_Vector2_method_length_squared>` **(** **)** |
  61. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`Vector2<class_Vector2>` | :ref:`linear_interpolate<class_Vector2_method_linear_interpolate>` **(** :ref:`Vector2<class_Vector2>` b, :ref:`float<class_float>` t **)** |
  63. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`Vector2<class_Vector2>` | :ref:`move_toward<class_Vector2_method_move_toward>` **(** :ref:`Vector2<class_Vector2>` to, :ref:`float<class_float>` delta **)** |
  65. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`Vector2<class_Vector2>` | :ref:`normalized<class_Vector2_method_normalized>` **(** **)** |
  67. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`Vector2<class_Vector2>` | :ref:`posmod<class_Vector2_method_posmod>` **(** :ref:`float<class_float>` mod **)** |
  69. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | :ref:`Vector2<class_Vector2>` | :ref:`posmodv<class_Vector2_method_posmodv>` **(** :ref:`Vector2<class_Vector2>` modv **)** |
  71. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`Vector2<class_Vector2>` | :ref:`project<class_Vector2_method_project>` **(** :ref:`Vector2<class_Vector2>` b **)** |
  73. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`Vector2<class_Vector2>` | :ref:`reflect<class_Vector2_method_reflect>` **(** :ref:`Vector2<class_Vector2>` n **)** |
  75. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`Vector2<class_Vector2>` | :ref:`rotated<class_Vector2_method_rotated>` **(** :ref:`float<class_float>` phi **)** |
  77. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | :ref:`Vector2<class_Vector2>` | :ref:`round<class_Vector2_method_round>` **(** **)** |
  79. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | :ref:`Vector2<class_Vector2>` | :ref:`sign<class_Vector2_method_sign>` **(** **)** |
  81. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | :ref:`Vector2<class_Vector2>` | :ref:`slerp<class_Vector2_method_slerp>` **(** :ref:`Vector2<class_Vector2>` b, :ref:`float<class_float>` t **)** |
  83. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | :ref:`Vector2<class_Vector2>` | :ref:`slide<class_Vector2_method_slide>` **(** :ref:`Vector2<class_Vector2>` n **)** |
  85. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  86. | :ref:`Vector2<class_Vector2>` | :ref:`snapped<class_Vector2_method_snapped>` **(** :ref:`Vector2<class_Vector2>` by **)** |
  87. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  88. | :ref:`Vector2<class_Vector2>` | :ref:`tangent<class_Vector2_method_tangent>` **(** **)** |
  89. +-------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  90. Constants
  91. ---------
  92. .. _class_Vector2_constant_AXIS_X:
  93. .. _class_Vector2_constant_AXIS_Y:
  94. .. _class_Vector2_constant_ZERO:
  95. .. _class_Vector2_constant_ONE:
  96. .. _class_Vector2_constant_INF:
  97. .. _class_Vector2_constant_LEFT:
  98. .. _class_Vector2_constant_RIGHT:
  99. .. _class_Vector2_constant_UP:
  100. .. _class_Vector2_constant_DOWN:
  101. - **AXIS_X** = **0** --- Enumerated value for the X axis.
  102. - **AXIS_Y** = **1** --- Enumerated value for the Y axis.
  103. - **ZERO** = **Vector2( 0, 0 )** --- Zero vector.
  104. - **ONE** = **Vector2( 1, 1 )** --- One vector.
  105. - **INF** = **Vector2( inf, inf )** --- Infinity vector.
  106. - **LEFT** = **Vector2( -1, 0 )** --- Left unit vector.
  107. - **RIGHT** = **Vector2( 1, 0 )** --- Right unit vector.
  108. - **UP** = **Vector2( 0, -1 )** --- Up unit vector.
  109. - **DOWN** = **Vector2( 0, 1 )** --- Down unit vector.
  110. Description
  111. -----------
  112. 2-element structure that can be used to represent positions in 2D space or any other pair of numeric values.
  113. Tutorials
  114. ---------
  115. - :doc:`../tutorials/math/index`
  116. Property Descriptions
  117. ---------------------
  118. .. _class_Vector2_property_x:
  119. - :ref:`float<class_float>` **x**
  120. +-----------+-----+
  121. | *Default* | 0.0 |
  122. +-----------+-----+
  123. The vector's X component. Also accessible by using the index position ``[0]``.
  124. ----
  125. .. _class_Vector2_property_y:
  126. - :ref:`float<class_float>` **y**
  127. +-----------+-----+
  128. | *Default* | 0.0 |
  129. +-----------+-----+
  130. The vector's Y component. Also accessible by using the index position ``[1]``.
  131. Method Descriptions
  132. -------------------
  133. .. _class_Vector2_method_Vector2:
  134. - :ref:`Vector2<class_Vector2>` **Vector2** **(** :ref:`float<class_float>` x, :ref:`float<class_float>` y **)**
  135. Constructs a new Vector2 from the given ``x`` and ``y``.
  136. ----
  137. .. _class_Vector2_method_abs:
  138. - :ref:`Vector2<class_Vector2>` **abs** **(** **)**
  139. Returns a new vector with all components in absolute values (i.e. positive).
  140. ----
  141. .. _class_Vector2_method_angle:
  142. - :ref:`float<class_float>` **angle** **(** **)**
  143. Returns the vector's angle in radians with respect to the X axis, or ``(1, 0)`` vector.
  144. Equivalent to the result of :ref:`@GDScript.atan2<class_@GDScript_method_atan2>` when called with the vector's :ref:`x<class_Vector2_property_x>` and :ref:`y<class_Vector2_property_y>` as parameters: ``atan2(x, y)``.
  145. ----
  146. .. _class_Vector2_method_angle_to:
  147. - :ref:`float<class_float>` **angle_to** **(** :ref:`Vector2<class_Vector2>` to **)**
  148. Returns the angle in radians between the two vectors.
  149. ----
  150. .. _class_Vector2_method_angle_to_point:
  151. - :ref:`float<class_float>` **angle_to_point** **(** :ref:`Vector2<class_Vector2>` to **)**
  152. Returns the angle in radians between the line connecting the two points and the X coordinate.
  153. ----
  154. .. _class_Vector2_method_aspect:
  155. - :ref:`float<class_float>` **aspect** **(** **)**
  156. Returns the ratio of :ref:`x<class_Vector2_property_x>` to :ref:`y<class_Vector2_property_y>`.
  157. ----
  158. .. _class_Vector2_method_bounce:
  159. - :ref:`Vector2<class_Vector2>` **bounce** **(** :ref:`Vector2<class_Vector2>` n **)**
  160. Returns the vector "bounced off" from a plane defined by the given normal.
  161. ----
  162. .. _class_Vector2_method_ceil:
  163. - :ref:`Vector2<class_Vector2>` **ceil** **(** **)**
  164. Returns the vector with all components rounded up.
  165. ----
  166. .. _class_Vector2_method_clamped:
  167. - :ref:`Vector2<class_Vector2>` **clamped** **(** :ref:`float<class_float>` length **)**
  168. Returns the vector with a maximum length.
  169. ----
  170. .. _class_Vector2_method_cross:
  171. - :ref:`float<class_float>` **cross** **(** :ref:`Vector2<class_Vector2>` with **)**
  172. Returns the 2-dimensional analog of the cross product with the given vector.
  173. ----
  174. .. _class_Vector2_method_cubic_interpolate:
  175. - :ref:`Vector2<class_Vector2>` **cubic_interpolate** **(** :ref:`Vector2<class_Vector2>` b, :ref:`Vector2<class_Vector2>` pre_a, :ref:`Vector2<class_Vector2>` post_b, :ref:`float<class_float>` t **)**
  176. Cubically interpolates between this vector and ``b`` using ``pre_a`` and ``post_b`` as handles, and returns the result at position ``t``. ``t`` is in the range of ``0.0 - 1.0``, representing the amount of interpolation.
  177. ----
  178. .. _class_Vector2_method_direction_to:
  179. - :ref:`Vector2<class_Vector2>` **direction_to** **(** :ref:`Vector2<class_Vector2>` b **)**
  180. Returns the normalized vector pointing from this vector to ``b``.
  181. ----
  182. .. _class_Vector2_method_distance_squared_to:
  183. - :ref:`float<class_float>` **distance_squared_to** **(** :ref:`Vector2<class_Vector2>` to **)**
  184. Returns the squared distance to vector ``b``. Prefer this function over :ref:`distance_to<class_Vector2_method_distance_to>` if you need to sort vectors or need the squared distance for some formula.
  185. ----
  186. .. _class_Vector2_method_distance_to:
  187. - :ref:`float<class_float>` **distance_to** **(** :ref:`Vector2<class_Vector2>` to **)**
  188. Returns the distance to vector ``b``.
  189. ----
  190. .. _class_Vector2_method_dot:
  191. - :ref:`float<class_float>` **dot** **(** :ref:`Vector2<class_Vector2>` with **)**
  192. Returns the dot product with vector ``b``.
  193. ----
  194. .. _class_Vector2_method_floor:
  195. - :ref:`Vector2<class_Vector2>` **floor** **(** **)**
  196. Returns the vector with all components rounded down.
  197. ----
  198. .. _class_Vector2_method_is_equal_approx:
  199. - :ref:`bool<class_bool>` **is_equal_approx** **(** :ref:`Vector2<class_Vector2>` v **)**
  200. ----
  201. .. _class_Vector2_method_is_normalized:
  202. - :ref:`bool<class_bool>` **is_normalized** **(** **)**
  203. Returns ``true`` if the vector is normalized.
  204. ----
  205. .. _class_Vector2_method_length:
  206. - :ref:`float<class_float>` **length** **(** **)**
  207. Returns the vector's length.
  208. ----
  209. .. _class_Vector2_method_length_squared:
  210. - :ref:`float<class_float>` **length_squared** **(** **)**
  211. Returns the vector's length squared. Prefer this method over :ref:`length<class_Vector2_method_length>` if you need to sort vectors or need the squared length for some formula.
  212. ----
  213. .. _class_Vector2_method_linear_interpolate:
  214. - :ref:`Vector2<class_Vector2>` **linear_interpolate** **(** :ref:`Vector2<class_Vector2>` b, :ref:`float<class_float>` t **)**
  215. Returns the result of the linear interpolation between this vector and ``b`` by amount ``t``. ``t`` is in the range of ``0.0 - 1.0``, representing the amount of interpolation.
  216. ----
  217. .. _class_Vector2_method_move_toward:
  218. - :ref:`Vector2<class_Vector2>` **move_toward** **(** :ref:`Vector2<class_Vector2>` to, :ref:`float<class_float>` delta **)**
  219. Moves the vector toward ``to`` by the fixed ``delta`` amount.
  220. ----
  221. .. _class_Vector2_method_normalized:
  222. - :ref:`Vector2<class_Vector2>` **normalized** **(** **)**
  223. Returns the vector scaled to unit length. Equivalent to ``v / v.length()``.
  224. ----
  225. .. _class_Vector2_method_posmod:
  226. - :ref:`Vector2<class_Vector2>` **posmod** **(** :ref:`float<class_float>` mod **)**
  227. Returns a vector composed of the ``fposmod`` of this vector's components and ``mod``.
  228. ----
  229. .. _class_Vector2_method_posmodv:
  230. - :ref:`Vector2<class_Vector2>` **posmodv** **(** :ref:`Vector2<class_Vector2>` modv **)**
  231. Returns a vector composed of the ``fposmod`` of this vector's components and ``modv``'s components.
  232. ----
  233. .. _class_Vector2_method_project:
  234. - :ref:`Vector2<class_Vector2>` **project** **(** :ref:`Vector2<class_Vector2>` b **)**
  235. Returns the vector projected onto the vector ``b``.
  236. ----
  237. .. _class_Vector2_method_reflect:
  238. - :ref:`Vector2<class_Vector2>` **reflect** **(** :ref:`Vector2<class_Vector2>` n **)**
  239. Returns the vector reflected from a plane defined by the given normal.
  240. ----
  241. .. _class_Vector2_method_rotated:
  242. - :ref:`Vector2<class_Vector2>` **rotated** **(** :ref:`float<class_float>` phi **)**
  243. Returns the vector rotated by ``phi`` radians. See also :ref:`@GDScript.deg2rad<class_@GDScript_method_deg2rad>`.
  244. ----
  245. .. _class_Vector2_method_round:
  246. - :ref:`Vector2<class_Vector2>` **round** **(** **)**
  247. Returns the vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
  248. ----
  249. .. _class_Vector2_method_sign:
  250. - :ref:`Vector2<class_Vector2>` **sign** **(** **)**
  251. Returns the vector with each component set to one or negative one, depending on the signs of the components.
  252. ----
  253. .. _class_Vector2_method_slerp:
  254. - :ref:`Vector2<class_Vector2>` **slerp** **(** :ref:`Vector2<class_Vector2>` b, :ref:`float<class_float>` t **)**
  255. Returns the result of spherical linear interpolation between this vector and ``b``, by amount ``t``. ``t`` is in the range of ``0.0 - 1.0``, representing the amount of interpolation.
  256. **Note:** Both vectors must be normalized.
  257. ----
  258. .. _class_Vector2_method_slide:
  259. - :ref:`Vector2<class_Vector2>` **slide** **(** :ref:`Vector2<class_Vector2>` n **)**
  260. Returns the component of the vector along a plane defined by the given normal.
  261. ----
  262. .. _class_Vector2_method_snapped:
  263. - :ref:`Vector2<class_Vector2>` **snapped** **(** :ref:`Vector2<class_Vector2>` by **)**
  264. Returns the vector snapped to a grid with the given size.
  265. ----
  266. .. _class_Vector2_method_tangent:
  267. - :ref:`Vector2<class_Vector2>` **tangent** **(** **)**
  268. Returns a perpendicular vector.