Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Vector2

struct in Math

Description

(code of this item is picked from Godot Engine in compliance with MIT license).

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

Constructors

SignatureDescription
( float x, float y )Constructs a new Vector2 with the given components.

Methods

Return TypeSignatureDescription
voidDeconstruct ( float x, float y )Helper method for deconstruction into a tuple.
Vector2Abs ( )Returns a new vector with all components in absolute values (i.e. positive).
floatAngle ( )Returns this vector's angle with respect to the X axis, or (1, 0) vector, in radians. Equivalent to the result of Mathf.Atan2(real_t, real_t) when called with the vector's Y and X as parameters: Mathf.Atan2(v.Y, v.X) .
floatAngleTo ( Vector2 to )Returns the angle to the given vector, in radians.
floatAngleToPoint ( Vector2 to )Returns the angle between the line connecting the two points and the X axis, in radians.
floatAspect ( )Returns the aspect ratio of this vector, the ratio of X to Y .
Vector2Bounce ( Vector2 normal )Returns the vector "bounced off" from a plane defined by the given normal.
Vector2Ceil ( )Returns a new vector with all components rounded up (towards positive infinity).
Vector2Clamp ( Vector2 min, Vector2 max )Returns a new vector with all components clamped between the components of name and name using Mathf.Clamp(real_t, real_t, real_t) .
Vector2Clamp ( float min, float max )Returns a new vector with all components clamped between the name and name using Mathf.Clamp(real_t, real_t, real_t) .
floatCross ( Vector2 with )Returns the cross product of this vector and name .
Vector2CubicInterpolate ( Vector2 b, Vector2 preA, Vector2 postB, float weight )Performs a cubic interpolation between vectors name , this vector, name , and name , by the given amount name .
Vector2CubicInterpolateInTime ( Vector2 b, Vector2 preA, Vector2 postB, float weight, float t, float preAT, float postBT )Performs a cubic interpolation between vectors name , this vector, name , and name , by the given amount name . It can perform smoother interpolation than CubicInterpolate by the time values.
Vector2BezierInterpolate ( Vector2 control1, Vector2 control2, Vector2 end, float t )Returns the point at the given name on a one-dimensional Bezier curve defined by this vector and the given name , name , and name points.
Vector2BezierDerivative ( Vector2 control1, Vector2 control2, Vector2 end, float t )Returns the derivative at the given name on the Bezier curve defined by this vector and the given name , name , and name points.
Vector2DirectionTo ( Vector2 to )Returns the normalized vector pointing from this vector to name .
floatDistanceSquaredTo ( Vector2 to )Returns the squared distance between this vector and name . This method runs faster than DistanceTo , so prefer it if you need to compare vectors or need the squared distance for some formula.
floatDistanceTo ( Vector2 to )Returns the distance between this vector and name .
floatDot ( Vector2 with )Returns the dot product of this vector and name .
Vector2Floor ( )Returns a new vector with all components rounded down (towards negative infinity).
Vector2Inverse ( )Returns the inverse of this vector. This is the same as new Vector2(1 / v.X, 1 / v.Y) .
boolIsFinite ( )Returns true if this vector is finite, by calling Mathf.IsFinite(real_t) on each component.
boolIsNormalized ( )Returns true if the vector is normalized, and false otherwise.
floatLength ( )Returns the length (magnitude) of this vector.
floatLengthSquared ( )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.
Vector2Lerp ( Vector2 to, float weight )Returns the result of the linear interpolation between this vector and name by amount name .
Vector2LimitLength ( float length )Returns the vector with a maximum length by limiting its length to name .
Vector2Max ( Vector2 with )Returns the result of the component-wise maximum between this vector and name . Equivalent to new Vector2(Mathf.Max(X, with.X), Mathf.Max(Y, with.Y)) .
Vector2Max ( float with )Returns the result of the component-wise maximum between this vector and name . Equivalent to new Vector2(Mathf.Max(X, with), Mathf.Max(Y, with)) .
Vector2Min ( Vector2 with )Returns the result of the component-wise minimum between this vector and name . Equivalent to new Vector2(Mathf.Min(X, with.X), Mathf.Min(Y, with.Y)) .
Vector2Min ( float with )Returns the result of the component-wise minimum between this vector and name . Equivalent to new Vector2(Mathf.Min(X, with), Mathf.Min(Y, with)) .
AxisMaxAxisIndex ( )Returns the axis of the vector's highest value. See Axis . If both components are equal, this method returns Axis.X .
AxisMinAxisIndex ( )Returns the axis of the vector's lowest value. See Axis . If both components are equal, this method returns Axis.Y .
Vector2MoveToward ( Vector2 to, float delta )Moves this vector toward name by the fixed name amount.
Vector2Normalized ( )Returns the vector scaled to unit length. Equivalent to v / v.Length() .
Vector2PosMod ( float mod )Returns a vector composed of the Mathf.PosMod(real_t, real_t) of this vector's components and name .
Vector2PosMod ( Vector2 modv )Returns a vector composed of the Mathf.PosMod(real_t, real_t) of this vector's components and name 's components.
Vector2Project ( Vector2 onNormal )Returns a new vector resulting from projecting this vector onto the given vector name . The resulting new vector is parallel to name . See also Slide . Note: If the vector name is a zero vector, the components of the resulting new vector will be real_t.NaN .
Vector2Reflect ( Vector2 normal )Returns this vector reflected from a plane defined by the given name .
Vector2Rotated ( float angle )Rotates this vector by name radians.
Vector2Round ( )Returns this vector with all components rounded to the nearest integer, with halfway cases rounded towards the nearest multiple of two.
Vector2Sign ( )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 Mathf.Sign(real_t) on each component.
Vector2Slerp ( Vector2 to, float weight )Returns the result of the spherical linear interpolation between this vector and name by amount name . 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 .
Vector2Slide ( Vector2 normal )Returns a new vector resulting from sliding this vector along a line with normal name . The resulting new vector is perpendicular to name , and is equivalent to this vector minus its projection on name . See also Project . Note: The vector name must be normalized. See also Normalized .
Vector2Snapped ( Vector2 step )Returns a new vector with each component snapped to the nearest multiple of the corresponding component in name . This can also be used to round to an arbitrary number of decimals.
Vector2Snapped ( float step )Returns a new vector with each component snapped to the nearest multiple of name . This can also be used to round to an arbitrary number of decimals.
Vector2Orthogonal ( )Returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.
boolEquals ( object? obj )Returns true if the vector is exactly equal to the given object ( name ). Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
boolEquals ( Vector2 other )Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
boolIsEqualApprox ( Vector2 other )Returns true if this vector and name are approximately equal, by running Mathf.IsEqualApprox(real_t, real_t) on each component.
boolIsZeroApprox ( )Returns true if this vector's values are approximately zero, by running Mathf.IsZeroApprox(real_t) on each component. This method is faster than using IsEqualApprox with one value as a zero vector.
intGetHashCode ( )Serves as the hash function for Vector2 .
stringToString ( )Converts this Vector2 to a string.
stringToString ( string? format )Converts this Vector2 to a string with the given name .

Constants

NameTypeDescriptionInitializer
ZeroVector2Zero vector, a vector with all components set to 0 . Equivalent to new Vector2(0, 0) .new(0, 0)
OneVector2One vector, a vector with all components set to 1 . Equivalent to new Vector2(1, 1) .new(1, 1)
InfVector2Infinity vector, a vector with all components set to Mathf.Inf . Equivalent to new Vector2(Mathf.Inf, Mathf.Inf) .new(Mathf.Inf, Mathf.Inf)
UpVector2Up unit vector. Y is down in 2D, so this vector points -Y. Equivalent to new Vector2(0, -1) .new(0, -1)
DownVector2Down unit vector. Y is down in 2D, so this vector points +Y. Equivalent to new Vector2(0, 1) .new(0, 1)
RightVector2Right unit vector. Represents the direction of right. Equivalent to new Vector2(1, 0) .new(1, 0)
LeftVector2Left unit vector. Represents the direction of left. Equivalent to new Vector2(-1, 0) .new(-1, 0)

Static Methods

Return TypeSignatureDescription
Vector2FromAngle ( float angle )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) .

Operators

Return TypeSignatureDescription
Vector2+ ( Vector2 left, Vector2 right )Adds each component of the Vector2 with the components of the given Vector2 .
Vector2- ( Vector2 left, Vector2 right )Subtracts each component of the Vector2 by the components of the given Vector2 .
Vector2- ( Vector2 vec )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.
Vector2* ( Vector2 vec, float scale )Multiplies each component of the Vector2 by the given real_t .
Vector2* ( float scale, Vector2 vec )Multiplies each component of the Vector2 by the given real_t .
Vector2* ( Vector2 left, Vector2 right )Multiplies each component of the Vector2 by the components of the given Vector2 .
Vector2/ ( Vector2 vec, float divisor )Divides each component of the Vector2 by the given real_t .
Vector2/ ( Vector2 vec, Vector2 divisorv )Divides each component of the Vector2 by the components of the given Vector2 .
Vector2% ( Vector2 vec, float divisor )Gets the remainder of each component of the Vector2 with the components of the given real_t . This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using PosMod instead if you want to handle negative numbers.
Vector2% ( Vector2 vec, Vector2 divisorv )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 instead if you want to handle negative numbers.
bool== ( Vector2 left, Vector2 right )Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
bool!= ( Vector2 left, Vector2 right )Returns true if the vectors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
bool< ( Vector2 left, Vector2 right )Compares two Vector2 vectors by first checking if the X value of the name vector is less than the X value of the name 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.
bool> ( Vector2 left, Vector2 right )Compares two Vector2 vectors by first checking if the X value of the name vector is greater than the X value of the name 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.
bool<= ( Vector2 left, Vector2 right )Compares two Vector2 vectors by first checking if the X value of the name vector is less than or equal to the X value of the name 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.
bool>= ( Vector2 left, Vector2 right )Compares two Vector2 vectors by first checking if the X value of the name vector is greater than or equal to the X value of the name 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.