Class Vector2

Vector2 metatype, wrapper for godot_vector2.

Construct using the idiom Vector2(...), which calls __new.

The X and Y components may be accessed through elements or the pairs x/y, r/g, s/t, u/v, width/height:

typedef union godot_vector2 {
    uint8_t data[8];
    float elements[2];
    // xy
    struct { float x, y; };
    // rg
    struct { float r, g; };
    // st
    struct { float s, t; };
    // uv
    struct { float u, v; };
    // Size: width/height
    struct { float width, height; };
} godot_vector2;

Methods

Vector2:abs () Returns a new vector with all components in absolute values (i.e.
Vector2:angle () Returns this vector's angle with respect to the positive X axis, or (1, 0) vector, in radians.
Vector2:angle_to (to) Returns the angle to the given vector, in radians.
Vector2:angle_to_point (to) Returns the angle between the line connecting the two points and the X axis, in radians.
Vector2:aspect () Returns the aspect ratio of this vector, the ratio of x to y.
Vector2:bounce (normal) Returns the vector "bounced off" from a plane defined by the given normal.
Vector2:clamped () Returns the vector with a maximum length by limiting its length to length.
Vector2:cubic_interpolate (b, t) Cubically interpolates between this vector and b using prea and postb as handles, and returns the result at position t.
Vector2:direction_to (b) Returns the normalized vector pointing from this vector to b.
Vector2:distance_squared_to (to) Returns the squared distance between this vector and to.
Vector2:distance_to (to) Returns the distance between this vector and to.
Vector2:dot (b) Returns the dot product of this vector and b.
Vector2:floor () Returns the vector with all components rounded down (towards negative infinity).
Vector2:is_normalized () Returns true if the vector is normalized.
Vector2:length () Returns the length (magnitude) of this vector.
Vector2:length_squared () Returns the squared length (squared magnitude) of this vector.
Vector2:linear_interpolate (b, t) Returns the result of the linear interpolation between this vector and b by amount t.
Vector2:move_toward (to, delta) Moves the vector toward to by the fixed delta amount.
Vector2:normalized () Returns the vector scaled to unit length.
Vector2:reflect (normal) Returns the vector reflected from a plane defined by the given normal.
Vector2:rotated (phi) Returns the vector rotated by phi radians.
Vector2:slide (normal) Returns this vector slid along a plane defined by the given normal.
Vector2:snapped (step) Returns this vector with each component snapped to the nearest multiple of step.
Vector2:tangent () Returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.
Vector2:unpack () Returns all elements.

Metamethods

Vector2:__add (a, b) Addition operation
Vector2:__concat (a, b) Concatenates values.
Vector2:__div (a, b) Division operation
Vector2:__eq (a, b) Equality operation
Vector2:__lt (a, b) Less than operation
Vector2:__mod (a, b) Module operation
Vector2:__mul (a, b) Multiplication operation
Vector2:__new (...) Vector2 constructor, called by the idiom Vector2(...).
Vector2:__pow (a, b) Power operation
Vector2:__sub (a, b) Subtraction operation
Vector2:__tostring () Returns a Lua string representation of this vector.
Vector2:__unm () Unary minus operation

Constants

Vector2.AXIS_X Enumerated value for the X axis.
Vector2.AXIS_Y Enumerated value for the Y axis.
Vector2.DOWN Down unit vector.
Vector2.INF Infinity vector, a vector with all components set to inf.
Vector2.LEFT Left unit vector.
Vector2.ONE One vector, a vector with all components set to 1.
Vector2.RIGHT Right unit vector.
Vector2.UP Up unit vector.
Vector2.ZERO Zero vector, a vector with all components set to 0.


Methods

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

Returns:

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

Returns:

    number
Vector2:angle_to (to)
Returns the angle to the given vector, in radians.

Parameters:

Returns:

    number
Vector2:angle_to_point (to)
Returns the angle between the line connecting the two points and the X axis, in radians.

Parameters:

Returns:

    number
Vector2:aspect ()
Returns the aspect ratio of this vector, the ratio of x to y.

Returns:

    number
Vector2:bounce (normal)
Returns the vector "bounced off" from a plane defined by the given normal.

Parameters:

Returns:

    Vector2
Vector2:clamped ()
Returns the vector with a maximum length by limiting its length to length.

Returns:

    Vector2
Vector2:cubic_interpolate (b, t)
Cubically interpolates between this vector and b using prea and postb as handles, and returns the result at position t. t is on the range of 0.0 to 1.0, representing the amount of interpolation.

Parameters:

Returns:

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

Parameters:

Returns:

    Vector2
Vector2:distance_squared_to (to)
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.

Parameters:

Returns:

    number
Vector2:distance_to (to)
Returns the distance between this vector and to.

Parameters:

Returns:

    number
Vector2:dot (b)
Returns the dot product of this vector and b.

Parameters:

Returns:

    number
Vector2:floor ()
Returns the vector with all components rounded down (towards negative infinity).

Returns:

    Vector2
Vector2:is_normalized ()
Returns true if the vector is normalized.

Returns:

    bool
Vector2:length ()
Returns the length (magnitude) of this vector.

Returns:

    number
Vector2:length_squared ()
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.

Returns:

    number
Vector2:linear_interpolate (b, t)
Returns the result of the linear interpolation between this vector and b by amount t. t is on the range of 0.0 to 1.0, representing the amount of interpolation.

Parameters:

Returns:

    Vector2
Vector2:move_toward (to, delta)
Moves the vector toward to by the fixed delta amount.

Parameters:

Returns:

    Vector2
Vector2:normalized ()
Returns the vector scaled to unit length. Equivalent to v / v:length().

Returns:

    Vector2
Vector2:reflect (normal)
Returns the vector reflected from a plane defined by the given normal.

Parameters:

Returns:

    Vector2
Vector2:rotated (phi)
Returns the vector rotated by phi radians.

Parameters:

  • phi number

Returns:

    Vector2
Vector2:slide (normal)
Returns this vector slid along a plane defined by the given normal.

Parameters:

Returns:

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

Parameters:

Returns:

    Vector2
Vector2:tangent ()
Returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.

Returns:

    Vector2
Vector2:unpack ()
Returns all elements.

Returns:

  1. number X
  2. number Y

Metamethods

Vector2:__add (a, b)
Addition operation

Parameters:

Returns:

    Vector2
Vector2:__concat (a, b)
Concatenates values.

Parameters:

  • a First value, stringified with GD.str
  • b First value, stringified with GD.str

Returns:

    String
Vector2:__div (a, b)
Division operation

Parameters:

Returns:

    Vector2
Vector2:__eq (a, b)
Equality operation

Parameters:

Returns:

    bool
Vector2:__lt (a, b)
Less than operation

Parameters:

Returns:

    bool
Vector2:__mod (a, b)
Module operation

Parameters:

Returns:

    Vector2
Vector2:__mul (a, b)
Multiplication operation

Parameters:

Returns:

    Vector2
Vector2:__new (...)

Vector2 constructor, called by the idiom Vector2(...).

  • Vector2(): all zeros (Vector2() == Vector2(0, 0))
  • Vector2(number x): all components are set to x (Vector2(1) == Vector2(1, 1))
  • Vector2(number x, number y): set XY
  • Vector2(Vector2 other): copy values from other

Parameters:

  • ...

Returns:

    Vector2
Vector2:__pow (a, b)
Power operation

Parameters:

Returns:

    Vector2
Vector2:__sub (a, b)
Subtraction operation

Parameters:

Returns:

    Vector2
Vector2:__tostring ()
Returns a Lua string representation of this vector.

Returns:

    string
Vector2:__unm ()
Unary minus operation

Returns:

    Vector2

Constants

Vector2.AXIS_X
Enumerated value for the X axis.
  • AXIS_X 0
Vector2.AXIS_Y
Enumerated value for the Y axis.
  • AXIS_Y 1
Vector2.DOWN
Down unit vector. Y is down in 2D, so this vector points +Y.
  • DOWN Vector2(0, 1)
Vector2.INF
Infinity vector, a vector with all components set to inf.
  • INF Vector2(1 / 0)
Vector2.LEFT
Left unit vector. Represents the direction of left.
  • LEFT Vector2(-1, 0)
Vector2.ONE
One vector, a vector with all components set to 1.
  • ONE Vector2(1)
Vector2.RIGHT
Right unit vector. Represents the direction of right.
  • RIGHT Vector2(1, 0)
Vector2.UP
Up unit vector. Y is down in 2D, so this vector points -Y.
  • UP Vector2(0, -1)
Vector2.ZERO
Zero vector, a vector with all components set to 0.
  • ZERO Vector2(0)
generated by LDoc 1.4.6 Last updated 2023-01-04 08:52:34