Skip to content

Latest commit

 

History

History
1182 lines (671 loc) · 70.3 KB

class_vector2.rst

File metadata and controls

1182 lines (671 loc) · 70.3 KB
github_url:hide

Vector2

A 2D vector using floating point coordinates.

.. rst-class:: classref-introduction-group

Description

A 2-element structure that can be used to represent 2D coordinates or any other pair of numeric values.

It uses floating-point coordinates. By default, these floating-point values use 32-bit precision, unlike :ref:`float<class_float>` which is always 64-bit. If double precision is needed, compile the engine with the option precision=double.

See :ref:`Vector2i<class_Vector2i>` for its integer counterpart.

Note: In a boolean context, a Vector2 will evaluate to false if it's equal to Vector2(0, 0). Otherwise, a Vector2 will always evaluate to true.

.. rst-class:: classref-introduction-group

Tutorials

.. rst-class:: classref-reftable-group

Properties

:ref:`float<class_float>` :ref:`x<class_Vector2_property_x>` 0.0
:ref:`float<class_float>` :ref:`y<class_Vector2_property_y>` 0.0
.. rst-class:: classref-reftable-group

Constructors

:ref:`Vector2<class_Vector2>` :ref:`Vector2<class_Vector2_constructor_Vector2>` ( )
:ref:`Vector2<class_Vector2>` :ref:`Vector2<class_Vector2_constructor_Vector2>` ( :ref:`Vector2<class_Vector2>` from )
:ref:`Vector2<class_Vector2>` :ref:`Vector2<class_Vector2_constructor_Vector2>` ( :ref:`Vector2i<class_Vector2i>` from )
:ref:`Vector2<class_Vector2>` :ref:`Vector2<class_Vector2_constructor_Vector2>` ( :ref:`float<class_float>` x, :ref:`float<class_float>` y )
.. rst-class:: classref-reftable-group

Methods

:ref:`Vector2<class_Vector2>` :ref:`abs<class_Vector2_method_abs>` ( ) |const|
:ref:`float<class_float>` :ref:`angle<class_Vector2_method_angle>` ( ) |const|
:ref:`float<class_float>` :ref:`angle_to<class_Vector2_method_angle_to>` ( :ref:`Vector2<class_Vector2>` to ) |const|
:ref:`float<class_float>` :ref:`angle_to_point<class_Vector2_method_angle_to_point>` ( :ref:`Vector2<class_Vector2>` to ) |const|
:ref:`float<class_float>` :ref:`aspect<class_Vector2_method_aspect>` ( ) |const|
:ref:`Vector2<class_Vector2>` :ref:`bezier_derivative<class_Vector2_method_bezier_derivative>` ( :ref:`Vector2<class_Vector2>` control_1, :ref:`Vector2<class_Vector2>` control_2, :ref:`Vector2<class_Vector2>` end, :ref:`float<class_float>` t ) |const|
:ref:`Vector2<class_Vector2>` :ref:`bezier_interpolate<class_Vector2_method_bezier_interpolate>` ( :ref:`Vector2<class_Vector2>` control_1, :ref:`Vector2<class_Vector2>` control_2, :ref:`Vector2<class_Vector2>` end, :ref:`float<class_float>` t ) |const|
:ref:`Vector2<class_Vector2>` :ref:`bounce<class_Vector2_method_bounce>` ( :ref:`Vector2<class_Vector2>` n ) |const|
:ref:`Vector2<class_Vector2>` :ref:`ceil<class_Vector2_method_ceil>` ( ) |const|
:ref:`Vector2<class_Vector2>` :ref:`clamp<class_Vector2_method_clamp>` ( :ref:`Vector2<class_Vector2>` min, :ref:`Vector2<class_Vector2>` max ) |const|
:ref:`float<class_float>` :ref:`cross<class_Vector2_method_cross>` ( :ref:`Vector2<class_Vector2>` with ) |const|
: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>` weight ) |const|
:ref:`Vector2<class_Vector2>` :ref:`cubic_interpolate_in_time<class_Vector2_method_cubic_interpolate_in_time>` ( :ref:`Vector2<class_Vector2>` b, :ref:`Vector2<class_Vector2>` pre_a, :ref:`Vector2<class_Vector2>` post_b, :ref:`float<class_float>` weight, :ref:`float<class_float>` b_t, :ref:`float<class_float>` pre_a_t, :ref:`float<class_float>` post_b_t ) |const|
:ref:`Vector2<class_Vector2>` :ref:`direction_to<class_Vector2_method_direction_to>` ( :ref:`Vector2<class_Vector2>` to ) |const|
:ref:`float<class_float>` :ref:`distance_squared_to<class_Vector2_method_distance_squared_to>` ( :ref:`Vector2<class_Vector2>` to ) |const|
:ref:`float<class_float>` :ref:`distance_to<class_Vector2_method_distance_to>` ( :ref:`Vector2<class_Vector2>` to ) |const|
:ref:`float<class_float>` :ref:`dot<class_Vector2_method_dot>` ( :ref:`Vector2<class_Vector2>` with ) |const|
:ref:`Vector2<class_Vector2>` :ref:`floor<class_Vector2_method_floor>` ( ) |const|
:ref:`Vector2<class_Vector2>` :ref:`from_angle<class_Vector2_method_from_angle>` ( :ref:`float<class_float>` angle ) |static|
:ref:`bool<class_bool>` :ref:`is_equal_approx<class_Vector2_method_is_equal_approx>` ( :ref:`Vector2<class_Vector2>` to ) |const|
:ref:`bool<class_bool>` :ref:`is_finite<class_Vector2_method_is_finite>` ( ) |const|
:ref:`bool<class_bool>` :ref:`is_normalized<class_Vector2_method_is_normalized>` ( ) |const|
:ref:`bool<class_bool>` :ref:`is_zero_approx<class_Vector2_method_is_zero_approx>` ( ) |const|
:ref:`float<class_float>` :ref:`length<class_Vector2_method_length>` ( ) |const|
:ref:`float<class_float>` :ref:`length_squared<class_Vector2_method_length_squared>` ( ) |const|
:ref:`Vector2<class_Vector2>` :ref:`lerp<class_Vector2_method_lerp>` ( :ref:`Vector2<class_Vector2>` to, :ref:`float<class_float>` weight ) |const|
:ref:`Vector2<class_Vector2>` :ref:`limit_length<class_Vector2_method_limit_length>` ( :ref:`float<class_float>` length=1.0 ) |const|
:ref:`int<class_int>` :ref:`max_axis_index<class_Vector2_method_max_axis_index>` ( ) |const|
:ref:`int<class_int>` :ref:`min_axis_index<class_Vector2_method_min_axis_index>` ( ) |const|
:ref:`Vector2<class_Vector2>` :ref:`move_toward<class_Vector2_method_move_toward>` ( :ref:`Vector2<class_Vector2>` to, :ref:`float<class_float>` delta ) |const|
:ref:`Vector2<class_Vector2>` :ref:`normalized<class_Vector2_method_normalized>` ( ) |const|
:ref:`Vector2<class_Vector2>` :ref:`orthogonal<class_Vector2_method_orthogonal>` ( ) |const|
:ref:`Vector2<class_Vector2>` :ref:`posmod<class_Vector2_method_posmod>` ( :ref:`float<class_float>` mod ) |const|
:ref:`Vector2<class_Vector2>` :ref:`posmodv<class_Vector2_method_posmodv>` ( :ref:`Vector2<class_Vector2>` modv ) |const|
:ref:`Vector2<class_Vector2>` :ref:`project<class_Vector2_method_project>` ( :ref:`Vector2<class_Vector2>` b ) |const|
:ref:`Vector2<class_Vector2>` :ref:`reflect<class_Vector2_method_reflect>` ( :ref:`Vector2<class_Vector2>` n ) |const|
:ref:`Vector2<class_Vector2>` :ref:`rotated<class_Vector2_method_rotated>` ( :ref:`float<class_float>` angle ) |const|
:ref:`Vector2<class_Vector2>` :ref:`round<class_Vector2_method_round>` ( ) |const|
:ref:`Vector2<class_Vector2>` :ref:`sign<class_Vector2_method_sign>` ( ) |const|
:ref:`Vector2<class_Vector2>` :ref:`slerp<class_Vector2_method_slerp>` ( :ref:`Vector2<class_Vector2>` to, :ref:`float<class_float>` weight ) |const|
:ref:`Vector2<class_Vector2>` :ref:`slide<class_Vector2_method_slide>` ( :ref:`Vector2<class_Vector2>` n ) |const|
:ref:`Vector2<class_Vector2>` :ref:`snapped<class_Vector2_method_snapped>` ( :ref:`Vector2<class_Vector2>` step ) |const|
.. rst-class:: classref-reftable-group

Operators

:ref:`bool<class_bool>` :ref:`operator !=<class_Vector2_operator_neq_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`Vector2<class_Vector2>` :ref:`operator *<class_Vector2_operator_mul_Transform2D>` ( :ref:`Transform2D<class_Transform2D>` right )
:ref:`Vector2<class_Vector2>` :ref:`operator *<class_Vector2_operator_mul_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`Vector2<class_Vector2>` :ref:`operator *<class_Vector2_operator_mul_float>` ( :ref:`float<class_float>` right )
:ref:`Vector2<class_Vector2>` :ref:`operator *<class_Vector2_operator_mul_int>` ( :ref:`int<class_int>` right )
:ref:`Vector2<class_Vector2>` :ref:`operator +<class_Vector2_operator_sum_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`Vector2<class_Vector2>` :ref:`operator -<class_Vector2_operator_dif_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`Vector2<class_Vector2>` :ref:`operator /<class_Vector2_operator_div_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`Vector2<class_Vector2>` :ref:`operator /<class_Vector2_operator_div_float>` ( :ref:`float<class_float>` right )
:ref:`Vector2<class_Vector2>` :ref:`operator /<class_Vector2_operator_div_int>` ( :ref:`int<class_int>` right )
:ref:`bool<class_bool>` :ref:`operator \<<class_Vector2_operator_lt_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`bool<class_bool>` :ref:`operator \<=<class_Vector2_operator_lte_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`bool<class_bool>` :ref:`operator ==<class_Vector2_operator_eq_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`bool<class_bool>` :ref:`operator ><class_Vector2_operator_gt_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`bool<class_bool>` :ref:`operator >=<class_Vector2_operator_gte_Vector2>` ( :ref:`Vector2<class_Vector2>` right )
:ref:`float<class_float>` :ref:`operator []<class_Vector2_operator_idx_int>` ( :ref:`int<class_int>` index )
:ref:`Vector2<class_Vector2>` :ref:`operator unary+<class_Vector2_operator_unplus>` ( )
:ref:`Vector2<class_Vector2>` :ref:`operator unary-<class_Vector2_operator_unminus>` ( )
.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Constants

.. rst-class:: classref-constant

AXIS_X = 0

Enumerated value for the X axis. Returned by :ref:`max_axis_index<class_Vector2_method_max_axis_index>` and :ref:`min_axis_index<class_Vector2_method_min_axis_index>`.

.. rst-class:: classref-constant

AXIS_Y = 1

Enumerated value for the Y axis. Returned by :ref:`max_axis_index<class_Vector2_method_max_axis_index>` and :ref:`min_axis_index<class_Vector2_method_min_axis_index>`.

.. rst-class:: classref-constant

ZERO = Vector2(0, 0)

Zero vector, a vector with all components set to 0.

.. rst-class:: classref-constant

ONE = Vector2(1, 1)

One vector, a vector with all components set to 1.

.. rst-class:: classref-constant

INF = Vector2(inf, inf)

Infinity vector, a vector with all components set to :ref:`@GDScript.INF<class_@GDScript_constant_INF>`.

.. rst-class:: classref-constant

LEFT = Vector2(-1, 0)

Left unit vector. Represents the direction of left.

.. rst-class:: classref-constant

RIGHT = Vector2(1, 0)

Right unit vector. Represents the direction of right.

.. rst-class:: classref-constant

UP = Vector2(0, -1)

Up unit vector. Y is down in 2D, so this vector points -Y.

.. rst-class:: classref-constant

DOWN = Vector2(0, 1)

Down unit vector. Y is down in 2D, so this vector points +Y.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Property Descriptions

.. rst-class:: classref-property

:ref:`float<class_float>` x = 0.0

The vector's X component. Also accessible by using the index position [0].

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` y = 0.0

The vector's Y component. Also accessible by using the index position [1].

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Constructor Descriptions

.. rst-class:: classref-constructor

:ref:`Vector2<class_Vector2>` Vector2 ( )

Constructs a default-initialized Vector2 with all components set to 0.

.. rst-class:: classref-item-separator


.. rst-class:: classref-constructor

:ref:`Vector2<class_Vector2>` Vector2 ( :ref:`Vector2<class_Vector2>` from )

Constructs a Vector2 as a copy of the given Vector2.

.. rst-class:: classref-item-separator


.. rst-class:: classref-constructor

:ref:`Vector2<class_Vector2>` Vector2 ( :ref:`Vector2i<class_Vector2i>` from )

Constructs a new Vector2 from :ref:`Vector2i<class_Vector2i>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-constructor

:ref:`Vector2<class_Vector2>` Vector2 ( :ref:`float<class_float>` x, :ref:`float<class_float>` y )

Constructs a new Vector2 from the given x and y.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Method Descriptions

.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` abs ( ) |const|

Returns a new vector with all components in absolute values (i.e. positive).

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` angle ( ) |const|

Returns this vector's angle with respect to the positive X axis, or (1, 0) vector, in radians.

For example, Vector2.RIGHT.angle() will return zero, Vector2.DOWN.angle() will return PI / 2 (a quarter turn, or 90 degrees), and Vector2(1, -1).angle() will return -PI / 4 (a negative eighth turn, or -45 degrees).

Illustration of the returned angle.

Equivalent to the result of :ref:`@GlobalScope.atan2<class_@GlobalScope_method_atan2>` when called with the vector's :ref:`y<class_Vector2_property_y>` and :ref:`x<class_Vector2_property_x>` as parameters: atan2(y, x).

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` angle_to ( :ref:`Vector2<class_Vector2>` to ) |const|

Returns the angle to the given vector, in radians.

Illustration of the returned angle.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` angle_to_point ( :ref:`Vector2<class_Vector2>` to ) |const|

Returns the angle between the line connecting the two points and the X axis, in radians.

a.angle_to_point(b) is equivalent of doing (b - a).angle().

Illustration of the returned angle.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` aspect ( ) |const|

Returns the aspect ratio of this vector, the ratio of :ref:`x<class_Vector2_property_x>` to :ref:`y<class_Vector2_property_y>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` bezier_derivative ( :ref:`Vector2<class_Vector2>` control_1, :ref:`Vector2<class_Vector2>` control_2, :ref:`Vector2<class_Vector2>` end, :ref:`float<class_float>` t ) |const|

Returns the derivative at the given t on the Bézier curve defined by this vector and the given control_1, control_2, and end points.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` bezier_interpolate ( :ref:`Vector2<class_Vector2>` control_1, :ref:`Vector2<class_Vector2>` control_2, :ref:`Vector2<class_Vector2>` end, :ref:`float<class_float>` t ) |const|

Returns the point at the given t on the Bézier curve defined by this vector and the given control_1, control_2, and end points.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` bounce ( :ref:`Vector2<class_Vector2>` n ) |const|

Returns a new vector "bounced off" from a plane defined by the given normal.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` ceil ( ) |const|

Returns a new vector with all components rounded up (towards positive infinity).

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` clamp ( :ref:`Vector2<class_Vector2>` min, :ref:`Vector2<class_Vector2>` max ) |const|

Returns a new vector with all components clamped between the components of min and max, by running :ref:`@GlobalScope.clamp<class_@GlobalScope_method_clamp>` on each component.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` cross ( :ref:`Vector2<class_Vector2>` with ) |const|

Returns the 2D analog of the cross product for this vector and with.

This is the signed area of the parallelogram formed by the two vectors. If the second vector is clockwise from the first vector, then the cross product is the positive area. If counter-clockwise, the cross product is the negative area.

Note: Cross product is not defined in 2D mathematically. This method embeds the 2D vectors in the XY plane of 3D space and uses their cross product's Z component as the analog.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

: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>` weight ) |const|

Performs a cubic interpolation between this vector and b using pre_a and post_b as handles, and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` cubic_interpolate_in_time ( :ref:`Vector2<class_Vector2>` b, :ref:`Vector2<class_Vector2>` pre_a, :ref:`Vector2<class_Vector2>` post_b, :ref:`float<class_float>` weight, :ref:`float<class_float>` b_t, :ref:`float<class_float>` pre_a_t, :ref:`float<class_float>` post_b_t ) |const|

Performs a cubic interpolation between this vector and b using pre_a and post_b as handles, and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

It can perform smoother interpolation than cubic_interpolate() by the time values.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` direction_to ( :ref:`Vector2<class_Vector2>` to ) |const|

Returns the normalized vector pointing from this vector to to. This is equivalent to using (b - a).normalized().

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` distance_squared_to ( :ref:`Vector2<class_Vector2>` to ) |const|

Returns the squared distance between this vector and to.

This method runs faster than :ref:`distance_to<class_Vector2_method_distance_to>`, so prefer it if you need to compare vectors or need the squared distance for some formula.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` distance_to ( :ref:`Vector2<class_Vector2>` to ) |const|

Returns the distance between this vector and to.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` dot ( :ref:`Vector2<class_Vector2>` with ) |const|

Returns the dot product of this vector and with. This can be used to compare the angle between two vectors. For example, this can be used to determine whether an enemy is facing the player.

The dot product will be 0 for a straight angle (90 degrees), greater than 0 for angles narrower than 90 degrees and lower than 0 for angles wider than 90 degrees.

When using unit (normalized) vectors, the result will always be between -1.0 (180 degree angle) when the vectors are facing opposite directions, and 1.0 (0 degree angle) when the vectors are aligned.

Note: a.dot(b) is equivalent to b.dot(a).

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` floor ( ) |const|

Returns a new vector with all components rounded down (towards negative infinity).

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` from_angle ( :ref:`float<class_float>` angle ) |static|

Creates a unit Vector2 rotated to the given angle in radians. This is equivalent to doing Vector2(cos(angle), sin(angle)) or Vector2.RIGHT.rotated(angle).

print(Vector2.from_angle(0)) # Prints (1, 0).
print(Vector2(1, 0).angle()) # Prints 0, which is the angle used above.
print(Vector2.from_angle(PI / 2)) # Prints (0, 1).
.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` is_equal_approx ( :ref:`Vector2<class_Vector2>` to ) |const|

Returns true if this vector and to are approximately equal, by running :ref:`@GlobalScope.is_equal_approx<class_@GlobalScope_method_is_equal_approx>` on each component.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` is_finite ( ) |const|

Returns true if this vector is finite, by calling :ref:`@GlobalScope.is_finite<class_@GlobalScope_method_is_finite>` on each component.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` is_normalized ( ) |const|

Returns true if the vector is normalized, i.e. its length is approximately equal to 1.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` is_zero_approx ( ) |const|

Returns true if this vector's values are approximately zero, by running :ref:`@GlobalScope.is_zero_approx<class_@GlobalScope_method_is_zero_approx>` on each component.

This method is faster than using :ref:`is_equal_approx<class_Vector2_method_is_equal_approx>` with one value as a zero vector.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` length ( ) |const|

Returns the length (magnitude) of this vector.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`float<class_float>` length_squared ( ) |const|

Returns the squared length (squared magnitude) of this vector.

This method runs faster than :ref:`length<class_Vector2_method_length>`, so prefer it if you need to compare vectors or need the squared distance for some formula.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` lerp ( :ref:`Vector2<class_Vector2>` to, :ref:`float<class_float>` weight ) |const|

Returns the result of the linear interpolation between this vector and to by amount weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` limit_length ( :ref:`float<class_float>` length=1.0 ) |const|

Returns the vector with a maximum length by limiting its length to length.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`int<class_int>` max_axis_index ( ) |const|

Returns the axis of the vector's highest value. See AXIS_* constants. If all components are equal, this method returns :ref:`AXIS_X<class_Vector2_constant_AXIS_X>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`int<class_int>` min_axis_index ( ) |const|

Returns the axis of the vector's lowest value. See AXIS_* constants. If all components are equal, this method returns :ref:`AXIS_Y<class_Vector2_constant_AXIS_Y>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` move_toward ( :ref:`Vector2<class_Vector2>` to, :ref:`float<class_float>` delta ) |const|

Returns a new vector moved toward to by the fixed delta amount. Will not go past the final value.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` normalized ( ) |const|

Returns the result of scaling the vector to unit length. Equivalent to v / v.length(). See also :ref:`is_normalized<class_Vector2_method_is_normalized>`.

Note: This function may return incorrect values if the input vector length is near zero.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` orthogonal ( ) |const|

Returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` posmod ( :ref:`float<class_float>` mod ) |const|

Returns a vector composed of the :ref:`@GlobalScope.fposmod<class_@GlobalScope_method_fposmod>` of this vector's components and mod.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` posmodv ( :ref:`Vector2<class_Vector2>` modv ) |const|

Returns a vector composed of the :ref:`@GlobalScope.fposmod<class_@GlobalScope_method_fposmod>` of this vector's components and modv's components.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` project ( :ref:`Vector2<class_Vector2>` b ) |const|

Returns the result of projecting the vector onto the given vector b.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` reflect ( :ref:`Vector2<class_Vector2>` n ) |const|

Returns the result of reflecting the vector from a line defined by the given direction vector n.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` rotated ( :ref:`float<class_float>` angle ) |const|

Returns the result of rotating this vector by angle (in radians). See also :ref:`@GlobalScope.deg_to_rad<class_@GlobalScope_method_deg_to_rad>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` round ( ) |const|

Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` sign ( ) |const|

Returns a new vector with each component set to 1.0 if it's positive, -1.0 if it's negative, and 0.0 if it's zero. The result is identical to calling :ref:`@GlobalScope.sign<class_@GlobalScope_method_sign>` on each component.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` slerp ( :ref:`Vector2<class_Vector2>` to, :ref:`float<class_float>` weight ) |const|

Returns the result of spherical linear interpolation between this vector and to, by amount weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

This method also handles interpolating the lengths if the input vectors have different lengths. For the special case of one or both input vectors having zero length, this method behaves like :ref:`lerp<class_Vector2_method_lerp>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` slide ( :ref:`Vector2<class_Vector2>` n ) |const|

Returns the result of sliding the vector along a plane defined by the given normal.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` snapped ( :ref:`Vector2<class_Vector2>` step ) |const|

Returns a new vector with each component snapped to the nearest multiple of the corresponding component in step. This can also be used to round the components to an arbitrary number of decimals.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Operator Descriptions

.. rst-class:: classref-operator

:ref:`bool<class_bool>` operator != ( :ref:`Vector2<class_Vector2>` right )

Returns true if the vectors are not equal.

Note: Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Vector2_method_is_equal_approx>` instead, which is more reliable.

Note: Vectors with :ref:`@GDScript.NAN<class_@GDScript_constant_NAN>` elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator * ( :ref:`Transform2D<class_Transform2D>` right )

Inversely transforms (multiplies) the Vector2 by the given :ref:`Transform2D<class_Transform2D>` transformation matrix.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator * ( :ref:`Vector2<class_Vector2>` right )

Multiplies each component of the Vector2 by the components of the given Vector2.

print(Vector2(10, 20) * Vector2(3, 4)) # Prints "(30, 80)"
.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator * ( :ref:`float<class_float>` right )

Multiplies each component of the Vector2 by the given :ref:`float<class_float>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator * ( :ref:`int<class_int>` right )

Multiplies each component of the Vector2 by the given :ref:`int<class_int>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator + ( :ref:`Vector2<class_Vector2>` right )

Adds each component of the Vector2 by the components of the given Vector2.

print(Vector2(10, 20) + Vector2(3, 4)) # Prints "(13, 24)"
.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator - ( :ref:`Vector2<class_Vector2>` right )

Subtracts each component of the Vector2 by the components of the given Vector2.

print(Vector2(10, 20) - Vector2(3, 4)) # Prints "(7, 16)"
.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator / ( :ref:`Vector2<class_Vector2>` right )

Divides each component of the Vector2 by the components of the given Vector2.

print(Vector2(10, 20) / Vector2(2, 5)) # Prints "(5, 4)"
.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator / ( :ref:`float<class_float>` right )

Divides each component of the Vector2 by the given :ref:`float<class_float>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator / ( :ref:`int<class_int>` right )

Divides each component of the Vector2 by the given :ref:`int<class_int>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`bool<class_bool>` operator < ( :ref:`Vector2<class_Vector2>` right )

Compares two Vector2 vectors by first checking if the X value of the left vector is less than the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors. This operator is useful for sorting vectors.

Note: Vectors with :ref:`@GDScript.NAN<class_@GDScript_constant_NAN>` elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`bool<class_bool>` operator <= ( :ref:`Vector2<class_Vector2>` right )

Compares two Vector2 vectors by first checking if the X value of the left vector is less than or equal to the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors. This operator is useful for sorting vectors.

Note: Vectors with :ref:`@GDScript.NAN<class_@GDScript_constant_NAN>` elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`bool<class_bool>` operator == ( :ref:`Vector2<class_Vector2>` right )

Returns true if the vectors are exactly equal.

Note: Due to floating-point precision errors, consider using :ref:`is_equal_approx<class_Vector2_method_is_equal_approx>` instead, which is more reliable.

Note: Vectors with :ref:`@GDScript.NAN<class_@GDScript_constant_NAN>` elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`bool<class_bool>` operator > ( :ref:`Vector2<class_Vector2>` right )

Compares two Vector2 vectors by first checking if the X value of the left vector is greater than the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors. This operator is useful for sorting vectors.

Note: Vectors with :ref:`@GDScript.NAN<class_@GDScript_constant_NAN>` elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`bool<class_bool>` operator >= ( :ref:`Vector2<class_Vector2>` right )

Compares two Vector2 vectors by first checking if the X value of the left vector is greater than or equal to the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors. This operator is useful for sorting vectors.

Note: Vectors with :ref:`@GDScript.NAN<class_@GDScript_constant_NAN>` elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`float<class_float>` operator [] ( :ref:`int<class_int>` index )

Access vector components using their index. v[0] is equivalent to v.x, and v[1] is equivalent to v.y.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator unary+ ( )

Returns the same value as if the + was not there. Unary + does nothing, but sometimes it can make your code more readable.

.. rst-class:: classref-item-separator


.. rst-class:: classref-operator

:ref:`Vector2<class_Vector2>` operator unary- ( )

Returns the negative value of the Vector2. This is the same as writing Vector2(-v.x, -v.y). This operation flips the direction of the vector while keeping the same magnitude. With floats, the number zero can be either positive or negative.