Up to date

This page is up to date for Godot 4.1. If you still find outdated information, please open an issue.

Vector2

A 2D vector using floating point coordinates.

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 float which is always 64-bit. If double precision is needed, compile the engine with the option precision=double.

See 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.

Tutorials

Properties

float

x

0.0

float

y

0.0

Constructors

Vector2

Vector2 ( )

Vector2

Vector2 ( Vector2 from )

Vector2

Vector2 ( Vector2i from )

Vector2

Vector2 ( float x, float y )

Methods

Vector2

abs ( ) const

float

angle ( ) const

float

angle_to ( Vector2 to ) const

float

angle_to_point ( Vector2 to ) const

float

aspect ( ) const

Vector2

bezier_derivative ( Vector2 control_1, Vector2 control_2, Vector2 end, float t ) const

Vector2

bezier_interpolate ( Vector2 control_1, Vector2 control_2, Vector2 end, float t ) const

Vector2

bounce ( Vector2 n ) const

Vector2

ceil ( ) const

Vector2

clamp ( Vector2 min, Vector2 max ) const

float

cross ( Vector2 with ) const

Vector2

cubic_interpolate ( Vector2 b, Vector2 pre_a, Vector2 post_b, float weight ) const

Vector2

cubic_interpolate_in_time ( Vector2 b, Vector2 pre_a, Vector2 post_b, float weight, float b_t, float pre_a_t, float post_b_t ) const

Vector2

direction_to ( Vector2 to ) const

float

distance_squared_to ( Vector2 to ) const

float

distance_to ( Vector2 to ) const

float

dot ( Vector2 with ) const

Vector2

floor ( ) const

Vector2

from_angle ( float angle ) static

bool

is_equal_approx ( Vector2 to ) const

bool

is_finite ( ) const

bool

is_normalized ( ) const

bool

is_zero_approx ( ) const

float

length ( ) const

float

length_squared ( ) const

Vector2

lerp ( Vector2 to, float weight ) const

Vector2

limit_length ( float length=1.0 ) const

int

max_axis_index ( ) const

int

min_axis_index ( ) const

Vector2

move_toward ( Vector2 to, float delta ) const

Vector2

normalized ( ) const

Vector2

orthogonal ( ) const

Vector2

posmod ( float mod ) const

Vector2

posmodv ( Vector2 modv ) const

Vector2

project ( Vector2 b ) const

Vector2

reflect ( Vector2 n ) const

Vector2

rotated ( float angle ) const

Vector2

round ( ) const

Vector2

sign ( ) const

Vector2

slerp ( Vector2 to, float weight ) const

Vector2

slide ( Vector2 n ) const

Vector2

snapped ( Vector2 step ) const

Operators

bool

operator != ( Vector2 right )

Vector2

operator ( Transform2D right )

Vector2

operator ( Vector2 right )

Vector2

operator ( float right )

Vector2

operator ( int right )

Vector2

operator + ( Vector2 right )

Vector2

operator - ( Vector2 right )

Vector2

operator / ( Vector2 right )

Vector2

operator / ( float right )

Vector2

operator / ( int right )

bool

operator < ( Vector2 right )

bool

operator <= ( Vector2 right )

bool

operator == ( Vector2 right )

bool

operator > ( Vector2 right )

bool

operator >= ( Vector2 right )

float

operator [] ( int index )

Vector2

operator unary+ ( )

Vector2

operator unary- ( )


Constants

AXIS_X = 0

Enumerated value for the X axis. Returned by max_axis_index and min_axis_index.

AXIS_Y = 1

Enumerated value for the Y axis. Returned by max_axis_index and min_axis_index.

ZERO = Vector2(0, 0)

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

ONE = Vector2(1, 1)

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

INF = Vector2(inf, inf)

Infinity vector, a vector with all components set to @GDScript.INF.

LEFT = Vector2(-1, 0)

Left unit vector. Represents the direction of left.

RIGHT = Vector2(1, 0)

Right unit vector. Represents the direction of right.

UP = Vector2(0, -1)

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

DOWN = Vector2(0, 1)

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


Property Descriptions

float x = 0.0

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


float y = 0.0

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


Constructor Descriptions

Vector2 Vector2 ( )

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


Vector2 Vector2 ( Vector2 from )

Constructs a Vector2 as a copy of the given Vector2.


Vector2 Vector2 ( Vector2i from )

Constructs a new Vector2 from Vector2i.


Vector2 Vector2 ( float x, float y )

Constructs a new Vector2 from the given x and y.


Method Descriptions

Vector2 abs ( ) const

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


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 @GlobalScope.atan2 when called with the vector’s y and x as parameters: atan2(y, x).


float angle_to ( Vector2 to ) const

Returns the angle to the given vector, in radians.

Illustration of the returned angle.


float angle_to_point ( 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.


float aspect ( ) const

Returns the aspect ratio of this vector, the ratio of x to y.


Vector2 bezier_derivative ( Vector2 control_1, Vector2 control_2, Vector2 end, 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.


Vector2 bezier_interpolate ( Vector2 control_1, Vector2 control_2, Vector2 end, 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.


Vector2 bounce ( Vector2 n ) const

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


Vector2 ceil ( ) const

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


Vector2 clamp ( Vector2 min, Vector2 max ) const

Returns a new vector with all components clamped between the components of min and max, by running @GlobalScope.clamp on each component.


float cross ( 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.


Vector2 cubic_interpolate ( Vector2 b, Vector2 pre_a, Vector2 post_b, 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.


Vector2 cubic_interpolate_in_time ( Vector2 b, Vector2 pre_a, Vector2 post_b, float weight, float b_t, float pre_a_t, 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.


Vector2 direction_to ( Vector2 to ) const

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


float distance_squared_to ( Vector2 to ) const

Returns the squared distance between this vector and to.

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


float distance_to ( Vector2 to ) const

Returns the distance between this vector and to.


float dot ( 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).


Vector2 floor ( ) const

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


Vector2 from_angle ( 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).

  1. print(Vector2.from_angle(0)) # Prints (1, 0).
  2. print(Vector2(1, 0).angle()) # Prints 0, which is the angle used above.
  3. print(Vector2.from_angle(PI / 2)) # Prints (0, 1).

bool is_equal_approx ( Vector2 to ) const

Returns true if this vector and to are approximately equal, by running @GlobalScope.is_equal_approx on each component.


bool is_finite ( ) const

Returns true if this vector is finite, by calling @GlobalScope.is_finite on each component.


bool is_normalized ( ) const

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


bool is_zero_approx ( ) const

Returns true if this vector’s values are approximately zero, by running @GlobalScope.is_zero_approx on each component.

This method is faster than using is_equal_approx with one value as a zero vector.


float length ( ) const

Returns the length (magnitude) of this vector.


float length_squared ( ) const

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

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


Vector2 lerp ( Vector2 to, 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.


Vector2 limit_length ( float length=1.0 ) const

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


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 AXIS_X.


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 AXIS_Y.


Vector2 move_toward ( Vector2 to, float delta ) const

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


Vector2 normalized ( ) const

Returns the result of scaling the vector to unit length. Equivalent to v / v.length(). See also is_normalized.

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


Vector2 orthogonal ( ) const

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


Vector2 posmod ( float mod ) const

Returns a vector composed of the @GlobalScope.fposmod of this vector’s components and mod.


Vector2 posmodv ( Vector2 modv ) const

Returns a vector composed of the @GlobalScope.fposmod of this vector’s components and modv‘s components.


Vector2 project ( Vector2 b ) const

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


Vector2 reflect ( Vector2 n ) const

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


Vector2 rotated ( float angle ) const

Returns the result of rotating this vector by angle (in radians). See also @GlobalScope.deg_to_rad.


Vector2 round ( ) const

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


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 @GlobalScope.sign on each component.


Vector2 slerp ( Vector2 to, 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 lerp.


Vector2 slide ( Vector2 n ) const

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


Vector2 snapped ( 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.


Operator Descriptions

bool operator != ( Vector2 right )

Returns true if the vectors are not equal.

Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.

Note: Vectors with @GDScript.NAN elements don’t behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.


Vector2 operator * ( Transform2D right )

Inversely transforms (multiplies) the Vector2 by the given Transform2D transformation matrix.


Vector2 operator * ( Vector2 right )

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

  1. print(Vector2(10, 20) * Vector2(3, 4)) # Prints "(30, 80)"

Vector2 operator * ( float right )

Multiplies each component of the Vector2 by the given float.


Vector2 operator * ( int right )

Multiplies each component of the Vector2 by the given int.


Vector2 operator + ( Vector2 right )

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

  1. print(Vector2(10, 20) + Vector2(3, 4)) # Prints "(13, 24)"

Vector2 operator - ( Vector2 right )

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

  1. print(Vector2(10, 20) - Vector2(3, 4)) # Prints "(7, 16)"

Vector2 operator / ( Vector2 right )

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

  1. print(Vector2(10, 20) / Vector2(2, 5)) # Prints "(5, 4)"

Vector2 operator / ( float right )

Divides each component of the Vector2 by the given float.


Vector2 operator / ( int right )

Divides each component of the Vector2 by the given int.


bool operator < ( 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 @GDScript.NAN elements don’t behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.


bool operator <= ( 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 @GDScript.NAN elements don’t behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.


bool operator == ( Vector2 right )

Returns true if the vectors are exactly equal.

Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.

Note: Vectors with @GDScript.NAN elements don’t behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.


bool operator > ( 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 @GDScript.NAN elements don’t behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.


bool operator >= ( 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 @GDScript.NAN elements don’t behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.


float operator [] ( int index )

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


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.


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.