• Studio

  • Bots

  • Web API

  • Designer Resources

  • Host Resources

  • API

    Datatypes

    Edit

    Vector2

    Represents a 2D vector for positions, velocities, and directions.

    Properties

    The normalized version of the Vector2.

    local position = Vector2.new(1, 2)
    local normalized = position.normalized
    
    print(normalized.x) -- 0.44721359014511
    

    magnitude

    number

    The length of the Vector2.

    local position = Vector2.new(1, 2)
    print(position.magnitude) -- 2.2360679774998
    

    The square of the Vector2's magnitude.

    local position = Vector2.new(1, 2)
    print(position.sqrMagnitude) -- 5
    

    x

    number

    The x-component of the Vector2.

    local position = Vector2.new(1, 2)
    print(position.x) -- 1
    
    position.x = 3
    print(position.x) -- 3
    

    y

    number

    The y-component of the Vector2.

    local position = Vector2.new(1, 2)
    print(position.y) -- 2
    
    position.y = 3
    print(position.y) -- 3
    

    A Vector2 with both components set to zero.

    print(Vector2.zero.x) -- 0
    print(Vector2.zero.y) -- 0
    

    A Vector2 with both components set to one.

    print(Vector2.one.x) -- 1
    print(Vector2.one.y) -- 1
    

    A Vector2 pointing upwards.

    print(Vector2.up.x) -- 0
    print(Vector2.up.y) -- 1
    

    A Vector2 pointing downwards.

    print(Vector2.down.x) -- 0
    print(Vector2.down.y) -- -1
    

    A Vector2 pointing to the left.

    print(Vector2.left.x) -- -1
    print(Vector2.left.y) -- 0
    

    A Vector2 pointing to the right.

    print(Vector2.right.x) -- 1
    print(Vector2.right.y) -- 0
    

    A Vector2 with both components at positive infinity.

    print(Vector2.positiveInfinity.x) -- inf
    print(Vector2.positiveInfinity.y) -- inf
    

    A Vector2 with both components at negative infinity.

    print(Vector2.negativeInfinity.x) -- -inf
    print(Vector2.negativeInfinity.y) -- -inf
    

    kEpsilon

    number

    A very small positive number greater than zero.

    print(Vector2.kEpsilon) -- 1.4012984643248e-45
    

    A small positive number for Vector2 computations.

    print(Vector2.kEpsilonNormalSqrt) -- 1.1754943508223e-38
    

    Methods

    Sets the x and y components of the Vector2.

    local position = Vector2.new(1, 2)
    
    position:Set(3, 4)
    print(position.x) -- 3
    print(position.y) -- 4
    

    Parameters

    newX

    number

    New 'x' component.

    newY

    number

    New 'y' component.

    Returns

    void

    Scales the Vector2 by another's components.

    local position = Vector2.new(1, 2)
    local scale = Vector2.new(3, 4)
    
    position:Scale(scale)
    print(position.x) -- 3
    print(position.y) -- 8
    

    Parameters

    scale
    Vector2

    Vector2 to scale by.

    Returns

    void

    Converts Vector2 to a direction vector of length 1.

    local position = Vector2.new(1, 2)
    position:Normalize()
    
    print(position.x) -- 0.44721359014511
    print(position.y) -- 0.89442718029022
    

    Returns

    void

    The square of the Vector2's magnitude.

    local position = Vector2.new(1, 2)
    print(position:SqrMagnitude()) -- 5
    

    Returns

    number

    Linearly interpolates between two Vector2's.

    local a = Vector2.new(1, 2)
    local b = Vector2.new(3, 4)
    
    local result = Vector2.Lerp(a, b, 0.5)
    print(result.x) -- 2
    print(result.y) -- 3
    

    Parameters

    First Vector2.

    Second Vector2.

    t

    number

    Interpolation factor.

    Returns

    Linearly interpolates between two Vector2's without clamping 't'.

    local a = Vector2.new(1, 2)
    local b = Vector2.new(3, 4)
    
    local result = Vector2.LerpUnclamped(a, b, 2)
    print(result.x) -- 5
    print(result.y) -- 6
    

    Parameters

    Starting Vector2.

    Ending Vector2.

    t

    number

    Interpolation factor.

    Returns

    Moves a Vector2 towards another Vector2 within a maximum distance.

    local current = Vector2.new(1, 2)
    local target = Vector2.new(3, 4)
    
    local result = Vector2.MoveTowards(current, target, 2)
    print(result.x) -- 3
    print(result.y) -- 4
    

    Parameters

    current
    Vector2

    Current Vector2.

    target
    Vector2

    Target Vector2.

    maxDistanceDelta

    number

    Maximum movement distance.

    Returns

    Multiplies two Vector2's component-wise.

    local a = Vector2.new(1, 2)
    local b = Vector2.new(3, 4)
    
    local result = Vector2.Scale(a, b)
    print(result.x) -- 3
    print(result.y) -- 8
    

    Parameters

    First Vector2.

    Second Vector2.

    Returns

    Reflects a Vector2 off a surface Vector2.

    local inDirection = Vector2.new(1, 2)
    local surfaceNormal = Vector2.new(0, 1)
    
    local result = Vector2.Reflect(inDirection, surfaceNormal)
    print(result.x) -- 1
    print(result.y) -- -2
    

    Parameters

    inDirection
    Vector2

    Direction of incoming Vector2.

    Returns

    Checks if two Vector2's are not equal by comparing components.

    Negates the x and y components of a Vector2.

    Transforms a Vector2 into a unit vector, maintaining direction.

    Updated 3 months ago

    PocketWorlds Icon

    © 2024 Pocket Worlds. All rights reserved.