Table of Contents

Struct Vector2I

Namespace
Godot
Assembly
GodotSharp.dll

2-element structure that can be used to represent 2D grid coordinates or pairs of integers.

public struct Vector2I : IEquatable<Vector2I>
Implements
Inherited Members

Constructors

Vector2I(int, int)

Constructs a new Vector2I with the given components.

public Vector2I(int x, int y)

Parameters

x int

The vector's X component.

y int

The vector's Y component.

Fields

X

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

public int X

Field Value

int

Y

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

public int Y

Field Value

int

Properties

Down

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

public static Vector2I Down { get; }

Property Value

Vector2I

Equivalent to new Vector2I(0, 1).

this[int]

Access vector components using their index.

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

Parameters

index int

Property Value

int

[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 Vector2I Left { get; }

Property Value

Vector2I

Equivalent to new Vector2I(-1, 0).

MaxValue

Max vector, a vector with all components equal to MaxValue. Can be used as an integer equivalent of Inf.

public static Vector2I MaxValue { get; }

Property Value

Vector2I

Equivalent to new Vector2I(int.MaxValue, int.MaxValue).

MinValue

Min vector, a vector with all components equal to MinValue. Can be used as a negative integer equivalent of Inf.

public static Vector2I MinValue { get; }

Property Value

Vector2I

Equivalent to new Vector2I(int.MinValue, int.MinValue).

One

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

public static Vector2I One { get; }

Property Value

Vector2I

Equivalent to new Vector2I(1, 1).

Right

Right unit vector. Represents the direction of right.

public static Vector2I Right { get; }

Property Value

Vector2I

Equivalent to new Vector2I(1, 0).

Up

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

public static Vector2I Up { get; }

Property Value

Vector2I

Equivalent to new Vector2I(0, -1).

Zero

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

public static Vector2I Zero { get; }

Property Value

Vector2I

Equivalent to new Vector2I(0, 0).

Methods

Abs()

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

public readonly Vector2I Abs()

Returns

Vector2I

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

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.

Clamp(Vector2I, Vector2I)

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

public readonly Vector2I Clamp(Vector2I min, Vector2I max)

Parameters

min Vector2I

The vector with minimum allowed values.

max Vector2I

The vector with maximum allowed values.

Returns

Vector2I

The vector with all components clamped.

Deconstruct(out int, out int)

Helper method for deconstruction into a tuple.

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

Parameters

x int
y int

Equals(Vector2I)

Returns true if the vectors are equal.

public readonly bool Equals(Vector2I other)

Parameters

other Vector2I

The other vector.

Returns

bool

Whether or not the vectors are equal.

Equals(object)

Returns true if the vector is equal to the given object (obj).

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.

GetHashCode()

Serves as the hash function for Vector2I.

public override readonly int GetHashCode()

Returns

int

A hash code for this vector.

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 int LengthSquared()

Returns

int

The squared length of this vector.

MaxAxisIndex()

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

public readonly Vector2I.Axis MaxAxisIndex()

Returns

Vector2I.Axis

The index of the highest axis.

MinAxisIndex()

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

public readonly Vector2I.Axis MinAxisIndex()

Returns

Vector2I.Axis

The index of the lowest axis.

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(int) on each component.

public readonly Vector2I Sign()

Returns

Vector2I

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

ToString()

Converts this Vector2I to a string.

public override readonly string ToString()

Returns

string

A string representation of this vector.

ToString(string)

Converts this Vector2I 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 +(Vector2I, Vector2I)

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

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

Parameters

left Vector2I

The left vector.

right Vector2I

The right vector.

Returns

Vector2I

The added vector.

operator /(Vector2I, Vector2I)

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

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

Parameters

vec Vector2I

The dividend vector.

divisorv Vector2I

The divisor vector.

Returns

Vector2I

The divided vector.

operator /(Vector2I, int)

Divides each component of the Vector2I by the given int.

public static Vector2I operator /(Vector2I vec, int divisor)

Parameters

vec Vector2I

The dividend vector.

divisor int

The divisor value.

Returns

Vector2I

The divided vector.

operator ==(Vector2I, Vector2I)

Returns true if the vectors are equal.

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

Parameters

left Vector2I

The left vector.

right Vector2I

The right vector.

Returns

bool

Whether or not the vectors are equal.

explicit operator Vector2I(Vector2)

Converts a Vector2 to a Vector2I by truncating components' fractional parts (rounding towards zero). For a different behavior consider passing the result of Ceil(), Floor() or Round() to this conversion operator instead.

public static explicit operator Vector2I(Vector2 value)

Parameters

value Vector2

The vector to convert.

Returns

Vector2I

operator >(Vector2I, Vector2I)

Compares two Vector2I 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 >(Vector2I left, Vector2I right)

Parameters

left Vector2I

The left vector.

right Vector2I

The right vector.

Returns

bool

Whether or not the left is greater than the right.

operator >=(Vector2I, Vector2I)

Compares two Vector2I 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 >=(Vector2I left, Vector2I right)

Parameters

left Vector2I

The left vector.

right Vector2I

The right vector.

Returns

bool

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

implicit operator Vector2(Vector2I)

Converts this Vector2I to a Vector2.

public static implicit operator Vector2(Vector2I value)

Parameters

value Vector2I

The vector to convert.

Returns

Vector2

operator !=(Vector2I, Vector2I)

Returns true if the vectors are not equal.

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

Parameters

left Vector2I

The left vector.

right Vector2I

The right vector.

Returns

bool

Whether or not the vectors are not equal.

operator <(Vector2I, Vector2I)

Compares two Vector2I 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 <(Vector2I left, Vector2I right)

Parameters

left Vector2I

The left vector.

right Vector2I

The right vector.

Returns

bool

Whether or not the left is less than the right.

operator <=(Vector2I, Vector2I)

Compares two Vector2I 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 <=(Vector2I left, Vector2I right)

Parameters

left Vector2I

The left vector.

right Vector2I

The right vector.

Returns

bool

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

operator %(Vector2I, Vector2I)

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

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

Parameters

vec Vector2I

The dividend vector.

divisorv Vector2I

The divisor vector.

Returns

Vector2I

The remainder vector.

Examples

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

operator %(Vector2I, int)

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

public static Vector2I operator %(Vector2I vec, int divisor)

Parameters

vec Vector2I

The dividend vector.

divisor int

The divisor value.

Returns

Vector2I

The remainder vector.

Examples

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

operator *(Vector2I, Vector2I)

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

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

Parameters

left Vector2I

The left vector.

right Vector2I

The right vector.

Returns

Vector2I

The multiplied vector.

operator *(Vector2I, int)

Multiplies each component of the Vector2I by the given int.

public static Vector2I operator *(Vector2I vec, int scale)

Parameters

vec Vector2I

The vector to multiply.

scale int

The scale to multiply by.

Returns

Vector2I

The multiplied vector.

operator *(int, Vector2I)

Multiplies each component of the Vector2I by the given int.

public static Vector2I operator *(int scale, Vector2I vec)

Parameters

scale int

The scale to multiply by.

vec Vector2I

The vector to multiply.

Returns

Vector2I

The multiplied vector.

operator -(Vector2I, Vector2I)

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

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

Parameters

left Vector2I

The left vector.

right Vector2I

The right vector.

Returns

Vector2I

The subtracted vector.

operator -(Vector2I)

Returns the negative value of the Vector2I. This is the same as writing new Vector2I(-v.X, -v.Y). This operation flips the direction of the vector while keeping the same magnitude.

public static Vector2I operator -(Vector2I vec)

Parameters

vec Vector2I

The vector to negate/flip.

Returns

Vector2I

The negated/flipped vector.