Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Vector2

Represents a 2D vector in Among Us, i.e. a position or velocity.

Hierarchy

  • Vector2

Index

Methods

Static Deserialize

Static add

  • Add two vectors together

    example
    const a = new Vector2(5, 8);
    const b = new Vector2(3, 7);
    
    const c = Vector2.add(a, b);
    console.log(c); // => (8, 15)
    

    Parameters

    Returns Vector2

    A new vector with the two vectors added together.

Static clamp

  • clamp(val: number, min: number, max: number): number
  • Clamp a value between a minimum and a maximum.

    example
    console.log(Vector2.clamp(50, 0, 100)); // => 50
    console.log(Vector2.clamp(125, 0, 100)); // => 100
    console.log(Vector2.clamp(-50, 0, 100)); // => 0
    

    Parameters

    • val: number

      The value to clamp.

    • min: number

      The minimum value.

    • max: number

      The maximum value.

    Returns number

    The clamped value.

Static dist

  • Calculate the distance between one vector and another.

    example
    const a = new Vector(3, 3);
    const b = new Vector(6, 7);
    
    const dist = Vector2.dist(a, b);
    console.log(dist); // => 5
    

    Parameters

    Returns number

    The distance between the two vectors.

Static div

  • Divide one vector from another.

    example
    const a = new Vector2(4, 81);
    const b = new Vector2(2, 9);
    
    const c = Vector2.div(a, b);
    console.log(c); // => (2, 9)
    

    Parameters

    Returns Vector2

    A new vector with one vector divided by the other.

  • Divide a vector by a scalar value.

    example
    const a = new Vector(40, 32);
    const c = Vector2.div(a, 8);
    console.log(c); // => (5, 4)
    

    Parameters

    • a: Vector2

      The vector to scale.

    • b: number

      The scalar.

    Returns Vector2

    A new vector with the vector scaled by the scalar.

Static lerp

  • lerp(val: number, min?: number, max?: number): number
  • Map a value between 0 and 1 to between a minimum and maximum value.

    example
    console.log(Vector2.lerp(0.9)); // => 40
    console.log(Vector2.lerp(0.9, -30, 30)); // => 24
    

    Parameters

    • val: number

      The value to lerp.

    • min: number = -50

      The minimum value to lerp from.

    • max: number = 50

      The maximum value to lerp to.

    Returns number

    The lerped value.

Static mul

  • Multiply one vector with another.

    example
    const a = new Vector2(2, 9);
    const b = new Vector2(2, 9);
    
    const c = Vector2.mul(a, b);
    console.log(c); // => (4, 81)
    

    Parameters

    Returns Vector2

    A new vector with the two vectors multiplied together.

  • Scale a vector with a scalar.

    example
    const a = new Vector(5, 4);
    const c = Vector2.mul(a, 8);
    console.log(c); // => (40, 32)
    

    Parameters

    • a: Vector2

      The vector to scale.

    • b: number

      The scalar.

    Returns Vector2

    A new vector with the vector scaled by the scalar.

Static negate

Static normalize

Static rotate

Static rotateDeg

Static sub

  • Subtract one vector from another.

    example
    const a = new Vector2(9, 9);
    const b = new Vector2(3, 3);
    
    const c = Vector2.sub(a, b);
    console.log(c); // => (6, 6)
    

    Parameters

    Returns Vector2

    A new vector with the second vector subtracted from the first.

Static unlerp

  • unlerp(val: number, min?: number, max?: number): number
  • Map a value between a minimum and maximum value to between 0 and 1.

    example
    console.log(Vector2.unlerp(0)); // => 0.5
    console.log(Vector2.unlerp(0, -15, 25)); // => 0.375
    

    Parameters

    • val: number

      The value to unlerp.

    • min: number = -50

      The minimum value to unlerp from.

    • max: number = 50

      The maximum value to unlerp to.

    Returns number

    The unlerped value.

Serialize

add

clone

dist

div

mul

negate

normalize

rotate

rotateDeg

  • rotateDeg(degrees: number): Vector2

sub

toString

  • toString(): string
  • Convert the vector into a string.

    example
    const vector = new Vector2(5, 6);
    
    console.log(vector.toString()); // => (5, 6)
    

    Returns string

    The vector as a string.

Accessors

Static down

Static left

Static null

Static right

Static up

Constructors

constructor

  • new Vector2(x?: number, y?: number): Vector2
  • new Vector2(pos: [number, number]): Vector2
  • new Vector2(other: Vector2): Vector2
  • new Vector2(pos: { x: number; y: number }): Vector2
  • Create a 2D Vector from two numbers.

    example
    const vector = new Vector2(62, 50);
    

    Parameters

    • Optional x: number

      X coordinate of the vector.

    • Optional y: number

      Y coordinate of the vector. Leave out to use the same value as x.

    Returns Vector2

  • Create a 2D vector from a number tuple.

    example
    const vector = new Vector2([ 60, 70 ]);
    

    Parameters

    • pos: [number, number]

      The X and Y coordinates of the vector.

    Returns Vector2

  • Create a 2D vector from another 2D vector, cloning it.

    example
    const vector = new Vector2(62, 50);
    const other = new Vector2(other);
    

    Parameters

    • other: Vector2

      The vector to clone.

    Returns Vector2

  • Create a 2D vector from an object containing an x and y property.

    example
    const vector = new Vector2({
      x: 50,
      y: 20
    });
    

    Parameters

    • pos: { x: number; y: number }

      The object to create from.

      • x: number
      • y: number

    Returns Vector2

Properties

x

x: number

The X coordinate of the vector.

y

y: number

The Y coordinate of the vector.

Generated using TypeDoc