skeldjs
    Preparing search index...

    Class Vector2

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

    Index

    Constructors

    • Create a 2D Vector from two numbers.

      Parameters

      • Optionalx: number

        X coordinate of the vector.

      • Optionaly: number

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

      Returns Vector2

      const vector = new Vector2(62, 50);
      
    • Create a 2D vector from a number tuple.

      Parameters

      • pos: [number, number]

        The X and Y coordinates of the vector.

      Returns Vector2

      const vector = new Vector2([ 60, 70 ]);
      
    • Create a 2D vector from another 2D vector, cloning it.

      Parameters

      • other: Vector2

        The vector to clone.

      Returns Vector2

      const vector = new Vector2(62, 50);
      const other = new Vector2(other);
    • Create a 2D vector from an object containing an x and y property.

      Parameters

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

        The object to create from.

      Returns Vector2

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

    Properties

    x: number

    The X coordinate of the vector.

    y: number

    The Y coordinate of the vector.

    Accessors

    Methods

    • Convert the vector into a string.

      Returns string

      The vector as a string.

      const vector = new Vector2(5, 6);

      console.log(vector.toString()); // => (5, 6)
    • Add two vectors together

      Parameters

      Returns Vector2

      A new vector with the two vectors added together.

      const a = new Vector2(5, 8);
      const b = new Vector2(3, 7);

      const c = Vector2.add(a, b);
      console.log(c); // => (8, 15)
    • Clamp a value between a minimum and a maximum.

      Parameters

      • val: number

        The value to clamp.

      • min: number

        The minimum value.

      • max: number

        The maximum value.

      Returns number

      The clamped value.

      console.log(Vector2.clamp(50, 0, 100)); // => 50
      console.log(Vector2.clamp(125, 0, 100)); // => 100
      console.log(Vector2.clamp(-50, 0, 100)); // => 0
    • Calculate the distance between one vector and another.

      Parameters

      Returns number

      The distance between the two vectors.

      const a = new Vector(3, 3);
      const b = new Vector(6, 7);

      const dist = Vector2.dist(a, b);
      console.log(dist); // => 5
    • Divide one vector from another.

      Parameters

      Returns Vector2

      A new vector with one vector divided by the other.

      const a = new Vector2(4, 81);
      const b = new Vector2(2, 9);

      const c = Vector2.div(a, b);
      console.log(c); // => (2, 9)
    • Divide a vector by a scalar value.

      Parameters

      • a: Vector2

        The vector to scale.

      • b: number

        The scalar.

      Returns Vector2

      A new vector with the vector scaled by the scalar.

      const a = new Vector(40, 32);
      const c = Vector2.div(a, 8);
      console.log(c); // => (5, 4)
    • Map a value between 0 and 1 to between a minimum and maximum value.

      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.

      console.log(Vector2.lerp(0.9)); // => 40
      console.log(Vector2.lerp(0.9, -30, 30)); // => 24
    • Multiply one vector with another.

      Parameters

      Returns Vector2

      A new vector with the two vectors multiplied together.

      const a = new Vector2(2, 9);
      const b = new Vector2(2, 9);

      const c = Vector2.mul(a, b);
      console.log(c); // => (4, 81)
    • Scale a vector with a scalar.

      Parameters

      • a: Vector2

        The vector to scale.

      • b: number

        The scalar.

      Returns Vector2

      A new vector with the vector scaled by the scalar.

      const a = new Vector(5, 4);
      const c = Vector2.mul(a, 8);
      console.log(c); // => (40, 32)
    • Subtract one vector from another.

      Parameters

      Returns Vector2

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

      const a = new Vector2(9, 9);
      const b = new Vector2(3, 3);

      const c = Vector2.sub(a, b);
      console.log(c); // => (6, 6)
    • Map a value between a minimum and maximum value to between 0 and 1.

      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.

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