Skip to main content

Vector2

Vector2 is a utility class representing a 2D vector with x and y components.
It is widely used in Weo for positions, sizes, and vector math operations.

Vector2
Utility

Constructor

Vector2.new()
Function

Vector2.new(x, y)

  • Creates a new Vector2 instance.

Parameters:

  • x
    number
    • X component.
  • y
    number
    • Y component.

Returns:

  • Vector2
    Vector2
    • The created Vector2 instance.

Throws an error if any parameter is not a number.

Methods

Vector2:Lerp()
Method

Vector2:Lerp(other, alpha)

  • Linearly interpolates between this vector and another.

Parameters:

  • other
    Vector2
    • The target Vector2 to interpolate towards.
  • alpha
    number
    • Interpolation factor (0-1).

Returns:

  • Vector2
    Vector2
    • A new Vector2 instance.

Vector2:Magnitude()
Method

Vector2:Magnitude()

  • Returns the length of the vector.

Returns:

  • number
    number
    • The magnitude of the vector.

Vector2:MagnitudeSquared()
Method

Vector2:MagnitudeSquared()

  • Returns the squared length of the vector.

Faster for comparisons than Magnitude().

Returns:

  • number
    number
    • The squared magnitude of the vector.

Vector2:Normalize()
Method

Vector2:Normalize()

  • Returns a normalized vector with magnitude 1.

Returns (0,0) if the original vector is zero.

Returns:

  • Vector2
    Vector2
    • A normalized Vector2 instance.

Vector2:Dot()
Method

Vector2:Dot(other)

  • Returns the dot product with another vector.

Parameters:

  • other
    Vector2
    • The other Vector2 to calculate dot product with.

Returns:

  • number
    number
    • The dot product result.

Vector2:Distance()
Method

Vector2:Distance(other)

  • Returns the distance between this vector and another.

Parameters:

  • other
    Vector2
    • The other Vector2 to calculate distance to.

Returns:

  • number
    number
    • The distance between the vectors.

Vector2:DistanceSquared()
Method

Vector2:DistanceSquared(other)

  • Returns the squared distance between this vector and another.

Faster for comparisons than Distance().

Parameters:

  • other
    Vector2
    • The other Vector2 to calculate squared distance to.

Returns:

  • number
    number
    • The squared distance between the vectors.

Operators

__add
Operator

  • Adds two vectors.
  • Usage: result = vector2A + vector2B
  • Returns: Vector2

__sub
Operator

  • Subtracts two vectors.
  • Usage: result = vector2A - vector2B
  • Returns: Vector2

__mul
Operator

  • Multiplies vector by a number or number by vector.
  • Usage: result = vector2 * number or result = number * vector2
  • Returns: Vector2

__div
Operator

  • Divides vector by a number.
  • Usage: result = vector2 / number
  • Returns: Vector2

__eq
Operator

  • Checks equality of two vectors.
  • Usage: isEqual = vector2A == vector2B
  • Returns: boolean

__tostring
Operator

  • Returns a string representation.
  • Usage: string = tostring(vector2)
  • Returns: string

Example

local position = Vector2.new(10, 20)
local velocity = Vector2.new(5, -3)

-- Basic operations
local newPosition = position + velocity
local scaled = velocity * 2
local normalized = velocity:Normalize()

-- Vector math
local distance = position:Distance(Vector2.new(0, 0))
local magnitude = velocity:Magnitude()
local dotProduct = position:Dot(velocity)

-- Interpolation
local startPos = Vector2.new(0, 0)
local endPos = Vector2.new(100, 50)
local interpolated = startPos:Lerp(endPos, 0.5)

-- Comparisons (using squared methods for better performance)
local distSq = position:DistanceSquared(velocity)
local magSq = velocity:MagnitudeSquared()

print(tostring(position)) -- Output: Vector2 representation