Table of Contents

Struct Vector2

Namespace
Godot
Assembly
GodotSharp.dll

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

public struct Vector2 : IEquatable<Vector2>
Implements
Inherited Members

Constructors

Vector2(float, float)

Constructs a new Vector2 with the given components.

public Vector2(float x, float y)

Parameters

x float

The vector's X component.

y float

The vector's Y component.

Fields

X

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

public float X

Field Value

float

Y

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

public float Y

Field Value

float

Properties

Down

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

public static Vector2 Down { get; }

Property Value

Vector2

Equivalent to new Vector2(0, 1).

Inf

Infinity vector, a vector with all components set to Inf.

public static Vector2 Inf { get; }

Property Value

Vector2

Equivalent to new Vector2(Mathf.Inf, Mathf.Inf).

this[int]

Access vector components using their index.

public float this[int index] { readonly get; set; }

Parameters

index int

Property Value

float

[0] is equivalent to X, [1] is equivalent to Y.

Exceptions

ArgumentOutOfRangeException

index is not 0 or 1.

Left

Left unit vector. Represents the direction of left.

public static Vector2 Left { get; }

Property Value

Vector2

Equivalent to new Vector2(-1, 0).

One

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

public static Vector2 One { get; }

Property Value

Vector2

Equivalent to new Vector2(1, 1).

Right

Right unit vector. Represents the direction of right.

public static Vector2 Right { get; }

Property Value

Vector2

Equivalent to new Vector2(1, 0).

Up

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

public static Vector2 Up { get; }

Property Value

Vector2

Equivalent to new Vector2(0, -1).

Zero

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

public static Vector2 Zero { get; }

Property Value

Vector2

Equivalent to new Vector2(0, 0).

Methods

Abs()

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

public readonly Vector2 Abs()

Returns

Vector2

A vector with Abs(float) called on each component.

Angle()

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

Equivalent to the result of Atan2(float, float) when called with the vector's Y and X as parameters: Mathf.Atan2(v.Y, v.X).

public readonly float Angle()

Returns

float

The angle of this vector, in radians.

AngleTo(Vector2)

Returns the angle to the given vector, in radians.

public readonly float AngleTo(Vector2 to)

Parameters

to Vector2

The other vector to compare this vector to.

Returns

float

The angle between the two vectors, in radians.

AngleToPoint(Vector2)

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

public readonly float AngleToPoint(Vector2 to)

Parameters

to Vector2

The other vector to compare this vector to.

Returns

float

The angle between the two vectors, in radians.

Aspect()

Returns the aspect ratio of this vector, the ratio of X to Y.

public readonly float Aspect()

Returns

float

The X component divided by the Y component.

BezierDerivative(Vector2, Vector2, Vector2, float)

Returns the derivative at the given t on the Bezier curve defined by this vector and the given control1, control2, and end points.

public readonly Vector2 BezierDerivative(Vector2 control1, Vector2 control2, Vector2 end, float t)

Parameters

control1 Vector2

Control point that defines the bezier curve.

control2 Vector2

Control point that defines the bezier curve.

end Vector2

The destination value for the interpolation.

t float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector2

The resulting value of the interpolation.

BezierInterpolate(Vector2, Vector2, Vector2, float)

Returns the point at the given t on a one-dimensional Bezier curve defined by this vector and the given control1, control2, and end points.

public readonly Vector2 BezierInterpolate(Vector2 control1, Vector2 control2, Vector2 end, float t)

Parameters

control1 Vector2

Control point that defines the bezier curve.

control2 Vector2

Control point that defines the bezier curve.

end Vector2

The destination vector.

t float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector2

The interpolated vector.

Bounce(Vector2)

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

public readonly Vector2 Bounce(Vector2 normal)

Parameters

normal Vector2

The normal vector defining the plane to bounce off. Must be normalized.

Returns

Vector2

The bounced vector.

Ceil()

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

public readonly Vector2 Ceil()

Returns

Vector2

A vector with Ceil(float) called on each component.

Clamp(Vector2, Vector2)

Returns a new vector with all components clamped between the components of min and max using Clamp(float, float, float).

public readonly Vector2 Clamp(Vector2 min, Vector2 max)

Parameters

min Vector2

The vector with minimum allowed values.

max Vector2

The vector with maximum allowed values.

Returns

Vector2

The vector with all components clamped.

Cross(Vector2)

Returns the cross product of this vector and with.

public readonly float Cross(Vector2 with)

Parameters

with Vector2

The other vector.

Returns

float

The cross product value.

CubicInterpolate(Vector2, Vector2, Vector2, float)

Performs a cubic interpolation between vectors preA, this vector, b, and postB, by the given amount weight.

public readonly Vector2 CubicInterpolate(Vector2 b, Vector2 preA, Vector2 postB, float weight)

Parameters

b Vector2

The destination vector.

preA Vector2

A vector before this vector.

postB Vector2

A vector after b.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector2

The interpolated vector.

CubicInterpolateInTime(Vector2, Vector2, Vector2, float, float, float, float)

Performs a cubic interpolation between vectors preA, this vector, b, and postB, by the given amount weight. It can perform smoother interpolation than CubicInterpolate(Vector2, Vector2, Vector2, float) by the time values.

public readonly Vector2 CubicInterpolateInTime(Vector2 b, Vector2 preA, Vector2 postB, float weight, float t, float preAT, float postBT)

Parameters

b Vector2

The destination vector.

preA Vector2

A vector before this vector.

postB Vector2

A vector after b.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

t float
preAT float
postBT float

Returns

Vector2

The interpolated vector.

Deconstruct(out float, out float)

Helper method for deconstruction into a tuple.

public readonly void Deconstruct(out float x, out float y)

Parameters

x float
y float

DirectionTo(Vector2)

Returns the normalized vector pointing from this vector to to.

public readonly Vector2 DirectionTo(Vector2 to)

Parameters

to Vector2

The other vector to point towards.

Returns

Vector2

The direction from this vector to to.

DistanceSquaredTo(Vector2)

Returns the squared distance between this vector and to. This method runs faster than DistanceTo(Vector2), so prefer it if you need to compare vectors or need the squared distance for some formula.

public readonly float DistanceSquaredTo(Vector2 to)

Parameters

to Vector2

The other vector to use.

Returns

float

The squared distance between the two vectors.

DistanceTo(Vector2)

Returns the distance between this vector and to.

public readonly float DistanceTo(Vector2 to)

Parameters

to Vector2

The other vector to use.

Returns

float

The distance between the two vectors.

Dot(Vector2)

Returns the dot product of this vector and with.

public readonly float Dot(Vector2 with)

Parameters

with Vector2

The other vector to use.

Returns

float

The dot product of the two vectors.

Equals(Vector2)

Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector2) instead, which is more reliable.

public readonly bool Equals(Vector2 other)

Parameters

other Vector2

The other vector.

Returns

bool

Whether or not the vectors are exactly equal.

Equals(object)

Returns true if the vector is exactly equal to the given object (obj). Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector2) instead, which is more reliable.

public override readonly bool Equals(object obj)

Parameters

obj object

The object to compare with.

Returns

bool

Whether or not the vector and the object are equal.

Floor()

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

public readonly Vector2 Floor()

Returns

Vector2

A vector with Floor(float) called on each component.

FromAngle(float)

Creates a unit Vector2 rotated to the given angle. This is equivalent to doing Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) or Vector2.Right.Rotated(angle).

public static Vector2 FromAngle(float angle)

Parameters

angle float

Angle of the vector, in radians.

Returns

Vector2

The resulting vector.

GetHashCode()

Serves as the hash function for Vector2.

public override readonly int GetHashCode()

Returns

int

A hash code for this vector.

Inverse()

Returns the inverse of this vector. This is the same as new Vector2(1 / v.X, 1 / v.Y).

public readonly Vector2 Inverse()

Returns

Vector2

The inverse of this vector.

IsEqualApprox(Vector2)

Returns true if this vector and other are approximately equal, by running IsEqualApprox(float, float) on each component.

public readonly bool IsEqualApprox(Vector2 other)

Parameters

other Vector2

The other vector to compare.

Returns

bool

Whether or not the vectors are approximately equal.

IsFinite()

Returns true if this vector is finite, by calling IsFinite(float) on each component.

public readonly bool IsFinite()

Returns

bool

Whether this vector is finite or not.

IsNormalized()

Returns true if the vector is normalized, and false otherwise.

public readonly bool IsNormalized()

Returns

bool

A bool indicating whether or not the vector is normalized.

IsZeroApprox()

Returns true if this vector's values are approximately zero, by running IsZeroApprox(float) on each component. This method is faster than using IsEqualApprox(Vector2) with one value as a zero vector.

public readonly bool IsZeroApprox()

Returns

bool

Whether or not the vector is approximately zero.

Length()

Returns the length (magnitude) of this vector.

public readonly float Length()

Returns

float

The length of this vector.

See Also

LengthSquared()

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 length for some formula.

public readonly float LengthSquared()

Returns

float

The squared length of this vector.

Lerp(Vector2, float)

Returns the result of the linear interpolation between this vector and to by amount weight.

public readonly Vector2 Lerp(Vector2 to, float weight)

Parameters

to Vector2

The destination vector for interpolation.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector2

The resulting vector of the interpolation.

LimitLength(float)

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

public readonly Vector2 LimitLength(float length = 1)

Parameters

length float

The length to limit to.

Returns

Vector2

The vector with its length limited.

MaxAxisIndex()

Returns the axis of the vector's highest value. See Vector2.Axis. If both components are equal, this method returns X.

public readonly Vector2.Axis MaxAxisIndex()

Returns

Vector2.Axis

The index of the highest axis.

MinAxisIndex()

Returns the axis of the vector's lowest value. See Vector2.Axis. If both components are equal, this method returns Y.

public readonly Vector2.Axis MinAxisIndex()

Returns

Vector2.Axis

The index of the lowest axis.

MoveToward(Vector2, float)

Moves this vector toward to by the fixed delta amount.

public readonly Vector2 MoveToward(Vector2 to, float delta)

Parameters

to Vector2

The vector to move towards.

delta float

The amount to move towards by.

Returns

Vector2

The resulting vector.

Normalized()

Returns the vector scaled to unit length. Equivalent to v / v.Length().

public readonly Vector2 Normalized()

Returns

Vector2

A normalized version of the vector.

Orthogonal()

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

public readonly Vector2 Orthogonal()

Returns

Vector2

The perpendicular vector.

PosMod(Vector2)

Returns a vector composed of the PosMod(float, float) of this vector's components and modv's components.

public readonly Vector2 PosMod(Vector2 modv)

Parameters

modv Vector2

A vector representing the divisors of the operation.

Returns

Vector2

A vector with each component PosMod(float, float) by modv's components.

PosMod(float)

Returns a vector composed of the PosMod(float, float) of this vector's components and mod.

public readonly Vector2 PosMod(float mod)

Parameters

mod float

A value representing the divisor of the operation.

Returns

Vector2

A vector with each component PosMod(float, float) by mod.

Project(Vector2)

Returns this vector projected onto another vector onNormal.

public readonly Vector2 Project(Vector2 onNormal)

Parameters

onNormal Vector2

The vector to project onto.

Returns

Vector2

The projected vector.

Reflect(Vector2)

Returns this vector reflected from a plane defined by the given normal.

public readonly Vector2 Reflect(Vector2 normal)

Parameters

normal Vector2

The normal vector defining the plane to reflect from. Must be normalized.

Returns

Vector2

The reflected vector.

Rotated(float)

Rotates this vector by angle radians.

public readonly Vector2 Rotated(float angle)

Parameters

angle float

The angle to rotate by, in radians.

Returns

Vector2

The rotated vector.

Round()

Returns this vector with all components rounded to the nearest integer, with halfway cases rounded towards the nearest multiple of two.

public readonly Vector2 Round()

Returns

Vector2

The rounded vector.

Sign()

Returns a vector with each component set to one or negative one, depending on the signs of this vector's components, or zero if the component is zero, by calling Sign(float) on each component.

public readonly Vector2 Sign()

Returns

Vector2

A vector with all components as either 1, -1, or 0.

Slerp(Vector2, float)

Returns the result of the spherical linear interpolation between this vector and to by amount weight.

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, float).

public readonly Vector2 Slerp(Vector2 to, float weight)

Parameters

to Vector2

The destination vector for interpolation.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector2

The resulting vector of the interpolation.

Slide(Vector2)

Returns this vector slid along a plane defined by the given normal.

public readonly Vector2 Slide(Vector2 normal)

Parameters

normal Vector2

The normal vector defining the plane to slide on.

Returns

Vector2

The slid vector.

Snapped(Vector2)

Returns this vector with each component snapped to the nearest multiple of step. This can also be used to round to an arbitrary number of decimals.

public readonly Vector2 Snapped(Vector2 step)

Parameters

step Vector2

A vector value representing the step size to snap to.

Returns

Vector2

The snapped vector.

ToString()

Converts this Vector2 to a string.

public override readonly string ToString()

Returns

string

A string representation of this vector.

ToString(string)

Converts this Vector2 to a string with the given format.

public readonly string ToString(string format)

Parameters

format string

Returns

string

A string representation of this vector.

Operators

operator +(Vector2, Vector2)

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

public static Vector2 operator +(Vector2 left, Vector2 right)

Parameters

left Vector2

The left vector.

right Vector2

The right vector.

Returns

Vector2

The added vector.

operator /(Vector2, Vector2)

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

public static Vector2 operator /(Vector2 vec, Vector2 divisorv)

Parameters

vec Vector2

The dividend vector.

divisorv Vector2

The divisor vector.

Returns

Vector2

The divided vector.

operator /(Vector2, float)

Multiplies each component of the Vector2 by the given float.

public static Vector2 operator /(Vector2 vec, float divisor)

Parameters

vec Vector2

The dividend vector.

divisor float

The divisor value.

Returns

Vector2

The divided vector.

operator ==(Vector2, Vector2)

Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector2) instead, which is more reliable.

public static bool operator ==(Vector2 left, Vector2 right)

Parameters

left Vector2

The left vector.

right Vector2

The right vector.

Returns

bool

Whether or not the vectors are exactly equal.

operator >(Vector2, Vector2)

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.

public static bool operator >(Vector2 left, Vector2 right)

Parameters

left Vector2

The left vector.

right Vector2

The right vector.

Returns

bool

Whether or not the left is greater than the right.

operator >=(Vector2, Vector2)

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.

public static bool operator >=(Vector2 left, Vector2 right)

Parameters

left Vector2

The left vector.

right Vector2

The right vector.

Returns

bool

Whether or not the left is greater than or equal to the right.

operator !=(Vector2, Vector2)

Returns true if the vectors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector2) instead, which is more reliable.

public static bool operator !=(Vector2 left, Vector2 right)

Parameters

left Vector2

The left vector.

right Vector2

The right vector.

Returns

bool

Whether or not the vectors are not equal.

operator <(Vector2, Vector2)

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.

public static bool operator <(Vector2 left, Vector2 right)

Parameters

left Vector2

The left vector.

right Vector2

The right vector.

Returns

bool

Whether or not the left is less than the right.

operator <=(Vector2, Vector2)

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.

public static bool operator <=(Vector2 left, Vector2 right)

Parameters

left Vector2

The left vector.

right Vector2

The right vector.

Returns

bool

Whether or not the left is less than or equal to the right.

operator %(Vector2, Vector2)

Gets the remainder of each component of the Vector2 with the components of the given Vector2. This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using PosMod(Vector2) instead if you want to handle negative numbers.

public static Vector2 operator %(Vector2 vec, Vector2 divisorv)

Parameters

vec Vector2

The dividend vector.

divisorv Vector2

The divisor vector.

Returns

Vector2

The remainder vector.

Examples

GD.Print(new Vector2(10, -20) % new Vector2(7, 8)); // Prints "(3, -4)"

operator %(Vector2, float)

Gets the remainder of each component of the Vector2 with the components of the given float. This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using PosMod(float) instead if you want to handle negative numbers.

public static Vector2 operator %(Vector2 vec, float divisor)

Parameters

vec Vector2

The dividend vector.

divisor float

The divisor value.

Returns

Vector2

The remainder vector.

Examples

GD.Print(new Vector2(10, -20) % 7); // Prints "(3, -6)"

operator *(Vector2, Vector2)

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

public static Vector2 operator *(Vector2 left, Vector2 right)

Parameters

left Vector2

The left vector.

right Vector2

The right vector.

Returns

Vector2

The multiplied vector.

operator *(Vector2, float)

Multiplies each component of the Vector2 by the given float.

public static Vector2 operator *(Vector2 vec, float scale)

Parameters

vec Vector2

The vector to multiply.

scale float

The scale to multiply by.

Returns

Vector2

The multiplied vector.

operator *(float, Vector2)

Multiplies each component of the Vector2 by the given float.

public static Vector2 operator *(float scale, Vector2 vec)

Parameters

scale float

The scale to multiply by.

vec Vector2

The vector to multiply.

Returns

Vector2

The multiplied vector.

operator -(Vector2, Vector2)

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

public static Vector2 operator -(Vector2 left, Vector2 right)

Parameters

left Vector2

The left vector.

right Vector2

The right vector.

Returns

Vector2

The subtracted vector.

operator -(Vector2)

Returns the negative value of the Vector2. This is the same as writing new 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.

public static Vector2 operator -(Vector2 vec)

Parameters

vec Vector2

The vector to negate/flip.

Returns

Vector2

The negated/flipped vector.