/Godot 3.0

# Vector2

Category: Built-In Types

## Brief Description

Vector used for 2D Math.

## Member Functions

 Vector2 Vector2 ( float x, float y ) Vector2 abs ( ) float angle ( ) float angle_to ( Vector2 to ) float angle_to_point ( Vector2 to ) float aspect ( ) Vector2 bounce ( Vector2 n ) Vector2 clamped ( float length ) Vector2 cubic_interpolate ( Vector2 b, Vector2 pre_a, Vector2 post_b, float t ) float distance_squared_to ( Vector2 to ) float distance_to ( Vector2 to ) float dot ( Vector2 with ) Vector2 floor ( ) bool is_normalized ( ) float length ( ) float length_squared ( ) Vector2 linear_interpolate ( Vector2 b, float t ) Vector2 normalized ( ) Vector2 reflect ( Vector2 n ) Vector2 rotated ( float phi ) Vector2 slide ( Vector2 n ) Vector2 snapped ( Vector2 by ) Vector2 tangent ( )

## Member Variables

• float x - X component of the vector.
• float y - Y component of the vector.

## Description

2-element structure that can be used to represent positions in 2d-space, or any other pair of numeric values.

## Member Function Description

### Vector2 Vector2 ( float x, float y )

Constructs a new Vector2 from the given x and y.

### Vector2 abs ( )

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

### float angle ( )

Returns the result of atan2 when called with the Vector’s x and y as parameters (Math::atan2(x,y)).

Be aware that it therefore returns an angle oriented clockwise with regard to the (0, 1) unit vector, and not an angle oriented counter-clockwise with regard to the (1, 0) unit vector (which would be the typical trigonometric representation of the angle when calling Math::atan2(y,x)).

### float angle_to ( Vector2 to )

Returns the angle in radians between the two vectors.

### float angle_to_point ( Vector2 to )

Returns the angle in radians between the line connecting the two points and the x coordinate.

### float aspect ( )

Returns the ratio of X to Y.

### Vector2 bounce ( Vector2 n )

Bounce returns the vector “bounced off” from the given plane, specified by its normal vector.

### Vector2 clamped ( float length )

Returns the vector with a maximum length.

### Vector2 cubic_interpolate ( Vector2 b, Vector2 pre_a, Vector2 post_b, float t )

Cubicly interpolates between this Vector and “b”, using “pre_a” and “post_b” as handles, and returning the result at position “t”. “t” should be a float of 0.0-1.0, a percentage of how far along the interpolation is.

### float distance_squared_to ( Vector2 to )

Returns the squared distance to vector “b”. Prefer this function over “distance_to” if you need to sort vectors or need the squared distance for some formula.

### float distance_to ( Vector2 to )

Returns the distance to vector “b”.

### float dot ( Vector2 with )

Returns the dot product with vector “b”.

### Vector2 floor ( )

Remove the fractional part of x and y.

### bool is_normalized ( )

Returns whether the vector is normalized or not.

### float length ( )

Returns the length of the vector.

### float length_squared ( )

Returns the squared length of the vector. Prefer this function over “length” if you need to sort vectors or need the squared length for some formula.

### Vector2 linear_interpolate ( Vector2 b, float t )

Returns the result of the linear interpolation between this vector and “b”, by amount “t”. “t” should be a float of 0.0-1.0, a percentage of how far along the interpolation is.

### Vector2 normalized ( )

Returns a normalized vector to unit length.

### Vector2 reflect ( Vector2 n )

Reflects the vector along the given plane, specified by its normal vector.

### Vector2 rotated ( float phi )

Rotates the vector by “phi” radians.

### Vector2 slide ( Vector2 n )

Slide returns the component of the vector along the given plane, specified by its normal vector.

### Vector2 snapped ( Vector2 by )

Snaps the vector to a grid with the given size.

### Vector2 tangent ( )

Returns a perpendicular vector.

© 2014–2018 Juan Linietsky, Ariel Manzur, Godot Engine contributors