Constructors

  • Parameters

    • _x: number = 0
    • _y: number = 0

    Returns default

Accessors

  • get x(): number
  • Gets the x-coordinate of the vector.

    Returns number

  • get y(): number
  • Gets the y-coordinate of the vector.

    Returns number

  • get down(): default
  • Returns a Vec2 representing the "down" direction (0, 1).

    This is a unit vector pointing downwards along the y-axis, commonly used in 2D space to represent movement or direction towards the bottom.

    Returns default

    A Vec2 instance representing the down direction (0, 1).

  • get left(): default
  • Returns a Vec2 representing the "left" direction (-1, 0).

    This is a unit vector pointing leftwards along the x-axis, commonly used in 2D space to represent movement or direction towards the left.

    Returns default

    A Vec2 instance representing the left direction (-1, 0).

  • get right(): default
  • Returns a Vec2 representing the "right" direction (1, 0).

    This is a unit vector pointing rightwards along the x-axis, commonly used in 2D space to represent movement or direction towards the right.

    Returns default

    A Vec2 instance representing the right direction (1, 0).

  • get top(): default
  • Returns a Vec2 representing the "top" direction (0, -1).

    This is a unit vector pointing upwards along the y-axis, commonly used in 2D space to represent movement or direction towards the top.

    Returns default

    A Vec2 instance representing the top direction (0, -1).

Methods

  • Adds another vector to this one.

    Parameters

    Returns default

    A new Vec2 instance representing the sum.

  • Calculates the angle of the vector relative to the positive x-axis.

    The angle is computed using the atan2 function, which returns the angle in radians between the positive x-axis and the vector. The result is in the range [-π, π].

    Returns number

    The angle in radians between the vector and the positive x-axis.

  • Calculates the angle in radians between this vector and another vector.

    The angle is calculated using the dot product formula: cos(theta) = (this · v) / (|this| * |v|), where this and v are the two vectors. The result is the smallest angle between the two vectors, in the range [0, π].

    Parameters

    • v: default

      The vector to calculate the angle to.

    Returns number

    The angle in radians between this vector and the other vector.

  • Creates a new vector with the same x and y values as this vector.

    Returns default

    A new Vec2 instance that is an exact copy of this vector.

  • Calculates the distance between this vector and another vector.

    The distance is computed as the length of the vector difference: distance = sqrt((x2 - x1)^2 + (y2 - y1)^2), where x1, y1 are the components of this vector and x2, y2 are the components of the other vector.

    Parameters

    • v: default

      The vector to calculate the distance to.

    Returns number

    The distance between the two vectors.

  • Divides this vector by a scalar value.

    Parameters

    • scalar: number

      The scalar value to divide the vector by.

    Returns default

    A new Vec2 instance representing the resulting vector after division.

    Throws an error if the scalar is zero, as division by zero is not allowed.

  • Computes the dot product of this vector and another vector.

    The dot product is calculated as x1 * x2 + y1 * y2, where x1, y1 are the components of this vector and x2, y2 are the components of the other vector.

    Parameters

    • v: default

      The vector to compute the dot product with.

    Returns number

    The dot product as a scalar value.

  • Checks if this vector is equal to another vector. Two vectors are considered equal if their x and y coordinates are identical.

    Parameters

    • v: default

      The vector to compare with.

    Returns boolean

    true if the vectors are equal, otherwise false.

  • Calculates the length (magnitude) of the vector.

    The length is computed using the formula sqrt(x^2 + y^2), where x and y are the components of the vector.

    Returns number

    The length of the vector as a positive number.

  • Performs linear interpolation (lerp) between this vector and another vector.

    The result is a vector that is a blend between the two vectors, based on the parameter t. If t is 0, the result is this vector. If t is 1, the result is the vector v. Values of t between 0 and 1 interpolate linearly between the two vectors.

    Parameters

    • v: default

      The target vector to interpolate towards.

    • t: number

      The interpolation factor, where 0 <= t <= 1. A value of 0 returns the current vector, and a value of 1 returns the target vector.

    Returns default

    A new Vec2 instance representing the interpolated vector.

  • Returns a new vector with the maximum values of each component from this vector and another vector.

    This method compares the corresponding x and y components of both vectors and creates a new vector where each component is the greater of the two. This can be useful for clamping or finding bounds.

    Parameters

    • v: default

      The vector to compare against.

    Returns default

    A new Vec2 instance with the maximum x and y components.

  • Returns a new vector with the minimum values of each component from this vector and another vector.

    This method compares the corresponding x and y components of both vectors and creates a new vector where each component is the lesser of the two. This can be useful for clamping or finding bounds.

    Parameters

    • v: default

      The vector to compare against.

    Returns default

    A new Vec2 instance with the minimum x and y components.

  • Moves this vector towards a target vector by a maximum distance delta.

    The method computes the vector difference between the target and this vector. It then scales this difference so that the resulting vector moves towards the target by no more than maxDistanceDelta. If the distance to the target is less than or equal to the maxDistanceDelta, the method will return the target vector.

    Parameters

    • target: default

      The target vector to move towards.

    • maxDistanceDelta: number

      The maximum distance to move towards the target.

    Returns default

    A new Vec2 instance representing the new position after moving towards the target.

  • Multiplies this vector by a scalar value.

    Parameters

    • scalar: number

      The scalar value to multiply the vector by.

    Returns default

    A new Vec2 instance representing the scaled vector.

  • Normalizes the vector to have a length of 1 while retaining its direction.

    This method divides the vector by its length. If the vector's length is zero, the resulting vector will also be a zero vector.

    Returns default

    A new Vec2 instance that is the normalized version of the vector.

  • Reflects this vector across a given normal vector.

    The reflection is calculated using the formula: reflected = this - 2 * (this · normal) * normal, where this is the original vector, normal is the vector across which to reflect, and · represents the dot product. The result is a new vector that is the reflection of the original vector.

    Parameters

    • normal: default

      The normal vector to reflect across.

    Returns default

    A new Vec2 instance representing the reflected vector.

  • Sets the x-coordinate.

    Parameters

    • x: number

      The new x-coordinate.

    Returns void

  • Sets the x and y coordinates.

    Parameters

    • x: number

      The new x-coordinate.

    • y: number

      The new y-coordinate.

    Returns void

  • Sets the y-coordinate.

    Parameters

    • y: number

      The new x-coordinate.

    Returns void

  • Calculates the squared length (magnitude) of the vector.

    The squared length is computed using the formula x^2 + y^2, where x and y are the components of the vector. This is often used when comparing the lengths of vectors, as it avoids the computational cost of calculating the square root.

    Returns number

    The squared length of the vector.

  • Subtracts the components of another vector from this vector.

    Parameters

    • v: default

      The vector to subtract from this vector.

    Returns default

    A new Vec2 instance representing the result of the subtraction.

  • Returns a string representation of the vector.

    This method returns a string that represents the vector in the format "Vector2(x, y)", where x and y are the components of the vector. This is useful for debugging and logging purposes.

    Returns string

    A string representation of the vector, e.g., "Vector2(3, 4)".

  • Unpacks the vector into an array of its x and y components.

    This method returns a tuple containing the x and y values of the vector, allowing for easy access to both components in a format that can be destructured or used in other operations.

    Returns [number, number]

    A tuple [x, y] representing the x and y components of the vector.