Vector2

< Global namespace | Roblox namespace

A Vector2 is a data-type that represents an X/Y coordinate system.

Quick Reference[edit]

Constructors
SyntaxDescription
Vector2.new()Constructs a Vector2 whose xy coordinates are set to [0,0]
Vector2.new(number x, number y)Constructs a Vector2 whose xy coordinates are set to [x,y]
Properties
SyntaxDescription
number X The x-coordinate
number Y The y-coordinate
Vector2 Unit A normalized copy of the vector.
number Magnitude The length of the vector.
Functions
SyntaxDescription
number Vector2:Dot(Vector2 other)Returns the scalar dot product of the two vectors.
Vector2 Vector2:Lerp(Vector2 goal, number alpha)Returns a Vector2 interpolated between this Vector2 and goal. alpha should be between 0 and 1.
Operators
SyntaxDescription
Vector2 + Vector2Returns Vector2 translated by Vector2.
Vector2 - Vector2Returns Vector2 translated by -Vector2.
Vector2 * Vector2Returns Vector2, with each component multiplied by the components of the other Vector2.
Vector2 / Vector2Returns Vector2, with each component divided by the components of the other Vector2.
Vector2 * numberReturns Vector2, with each component multiplied by number.
Vector2 / numberReturns Vector2, with each component divided by number.
number * Vector2Returns Vector2, whose components have been transformed as (number * component).
number / Vector2Returns Vector2, whose components have been transformed as (number / component).

Constructors[edit]

Vector2.new[edit]

Vector2 Vector2.new()

Description:
Constructs a Vector2 whose xy coordinates are set to [0,0]

Vector2 Vector2.new(number x, number y)

Description:
Constructs a Vector2 whose xy coordinates are set to [x,y]

Properties[edit]

X[edit]

number Vector2.X [readonly]

Description:
The x-coordinate

Y[edit]

number Vector2.Y [readonly]

Description:
The y-coordinate

Unit[edit]

Vector2 Vector2.Unit [readonly]

Description:
A normalized copy of the vector.

Magnitude[edit]

number Vector2.Magnitude [readonly]

Description:
The length of the vector.

Methods[edit]

Dot[edit]

number Vector2:Dot(Vector2 other)

Description:
Returns the scalar dot product of the two vectors.

Notes:

Lerp[edit]

Vector2 Vector2:Lerp(Vector2 goal, number alpha)

Description:
Returns a Vector2 interpolated between this Vector2 and goal. alpha should be between 0 and 1.

Operators[edit]

Vector2 + Vector2[edit]

Vector2 Vector2 + Vector2

Description:
Returns Vector2 translated by Vector2.

Vector2 - Vector2[edit]

Vector2 Vector2 - Vector2

Description:
Returns Vector2 translated by -Vector2.

Vector2 * Vector2[edit]

Vector2 Vector2 * Vector2

Description:
Returns Vector2, with each component multiplied by the components of the other Vector2.

Vector2 / Vector2[edit]

Vector2 Vector2 / Vector2

Description:
Returns Vector2, with each component divided by the components of the other Vector2.

Vector2 * number[edit]

Vector2 Vector2 * number

Description:
Returns Vector2, with each component multiplied by number.

Vector2 / number[edit]

Vector2 Vector2 / number

Description:
Returns Vector2, with each component divided by number.

number * Vector2[edit]

Vector2 number * Vector2

Description:
Returns Vector2, whose components have been transformed as (number * component).

number / Vector2[edit]

Vector2 number / Vector2

Description:
Returns Vector2, whose components have been transformed as (number / component).


Magnitude property[edit]

The Magnitude property is simply the length of the Vector2, which can be found by using the Pythagorean theorem. If you plug-in the difference of the Vector2's x coordinates as a and the difference of the Vector2's y coordinates b, and then solve for c, you will find the length of the Vector2. If there is only one coordinate, the second coordinate will be
0
0
.

Examples[edit]

In the following diagram, there are two line segments. These two line segments can be represented in Roblox with four Vector2 values.

Diagram of two line segments using Cartesian Coordinates

Let's say we have line A:

local lineA = {
    Vector2.new(2, 7),
    Vector2.new(2, 1)
}
lineA.Magnitude = (lineA[1] - lineA[2]).Magnitude
lineA.Unit      = (lineA[1] - lineA[2]).Unit

and line B:

local lineB = {
    Vector2.new(1, -5),
    Vector2.new(3, -3)
}
lineB.Magnitude = (lineB[1] - lineB[2]).Magnitude
lineB.Unit      = (lineB[1] - lineB[2]).Unit

Now, if we were to print the length (or magnitude) of each line segment, it would be the number displayed to the right of it:

print("The magnitude of line A is " .. lineA.magnitude .. " units.")
print("The magnitude of line B is " .. lineB.magnitude .. " units.")
The magnitude of line A is 6 units.
The magnitude of line B is 3.4142135623731 units.

See also[edit]

Referenced by