nape-js API
    Preparing search index...

    Class Vec2

    2D vector used for positions, velocities, forces, and other 2D quantities.

    Supports object pooling via Vec2.get() / dispose(), weak references that auto-dispose after a single use, and immutability guards.

    Converted from nape-compiled.js lines 23448–27180.

    Index

    Constructors

    • Creates a Vec2 with the given components. Defaults to (0, 0).

      Parameters

      • x: number = 0

        The x component (default 0).

      • y: number = 0

        The y component (default 0).

      Returns Vec2

    Accessors

    • get angle(): number

      Angle of the vector in radians, measured counter-clockwise from the +x axis. Returns 0 for the zero vector.

      Returns number

    • set angle(value: number): void

      Setting angle preserves the vector's magnitude and rotates it to the given angle.

      Parameters

      • value: number

        The desired angle in radians. Must not be NaN.

      Returns void

    • get length(): number

      Magnitude (Euclidean length) of the vector.

      Returns number

    • set length(value: number): void

      Setting length scales the vector to the given magnitude. Throws if the vector is zero-length.

      Parameters

      • value: number

        The desired magnitude. Must not be NaN.

      Returns void

    Methods

    • Return a new Vec2 equal to this + other.

      Parameters

      • vector: Vec2

        The Vec2 to add.

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A new Vec2 with components (this.x + other.x, this.y + other.y).

    • Add another Vec2 to this in-place (this += other).

      Parameters

      • vector: Vec2

        The Vec2 to add.

      Returns this

      this for chaining.

    • Return a new Vec2 equal to this + other × scalar.

      Parameters

      • vector: Vec2

        The Vec2 to scale and add.

      • scalar: number

        The multiplier applied to vector before addition.

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A new Vec2 with components (this.x + vector.x * scalar, this.y + vector.y * scalar).

    • Return a new Vec2 with the same components. Alias for copy().

      Returns Vec2

      A new Vec2 with the same x and y values.

    • Return a new Vec2 with the same components.

      Parameters

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A copy of this vector.

    • 2D cross product (this.x × other.y − this.y × other.x). Returns a scalar.

      Parameters

      • vector: Vec2

        The other Vec2.

      Returns number

      The scalar cross product.

    • Return this Vec2 to the object pool. Throws if already disposed or immutable.

      Returns void

    • Dot product of this and another Vec2.

      Parameters

      • vector: Vec2

        The other Vec2.

      Returns number

      this.x * other.x + this.y * other.y.

    • Check whether this Vec2 is component-wise equal to another, within an optional epsilon tolerance.

      Parameters

      • other: Vec2

        The Vec2 to compare against.

      • epsilon: number = 0

        Maximum allowed difference per component (default 0).

      Returns boolean

      true if both components differ by at most epsilon.

    • Returns the squared magnitude. Faster than length as it avoids a square root.

      Returns number

      x² + y².

    • Return a new Vec2 equal to this × scalar.

      Parameters

      • scalar: number

        The multiplier.

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A new Vec2 with components (this.x * scalar, this.y * scalar).

    • Multiply this Vec2 by a scalar in-place (this ×= scalar).

      Parameters

      • scalar: number

        The multiplier.

      Returns this

      this for chaining.

    • Normalise this vector to unit length in-place. Throws for zero-length vectors.

      Returns this

      this for chaining.

    • Return the perpendicular vector, rotated 90° counter-clockwise.

      Parameters

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A new Vec2 with components (-this.y, this.x).

    • Reflect vec about this vector as a normal axis.

      Parameters

      • vec: Vec2

        The Vec2 to reflect.

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A new Vec2 that is the reflection of vec about this vector.

    • Rotate this vector by angle radians in-place.

      Parameters

      • angle: number

        The rotation angle in radians.

      Returns this

      this for chaining.

    • Copy another Vec2's components into this vector in-place.

      Parameters

      • vector: Vec2

        The source Vec2 to copy from.

      Returns this

      this for chaining.

    • Set both components at once in-place.

      Parameters

      • x: number

        The new x component.

      • y: number

        The new y component.

      Returns this

      this for chaining.

    • Return a new Vec2 equal to this − other.

      Parameters

      • vector: Vec2

        The Vec2 to subtract.

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A new Vec2 with components (this.x - other.x, this.y - other.y).

    • Subtract another Vec2 from this in-place (this -= other).

      Parameters

      • vector: Vec2

        The Vec2 to subtract.

      Returns this

      this for chaining.

    • String representation in the form { x: … y: … }.

      Returns string

      A human-readable string of this vector's components.

    • Return a new unit-length vector with the same direction. Throws for zero-length vectors.

      Parameters

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A normalised copy of this vector.

    • Euclidean distance between two Vec2s.

      Parameters

      • a: Vec2

        The first Vec2.

      • b: Vec2

        The second Vec2.

      Returns number

      The distance sqrt((a.x - b.x)² + (a.y - b.y)²).

    • Squared Euclidean distance between two Vec2s. Avoids a square root when only comparison is needed.

      Parameters

      • a: Vec2

        The first Vec2.

      • b: Vec2

        The second Vec2.

      Returns number

      The squared distance (a.x - b.x)² + (a.y - b.y)².

    • Check whether two Vec2s are component-wise equal, within an optional epsilon tolerance. Disposes weak arguments after comparison.

      Parameters

      • a: Vec2

        The first Vec2.

      • b: Vec2

        The second Vec2.

      • epsilon: number = 0

        Maximum allowed difference per component (default 0).

      Returns boolean

      true if both components differ by at most epsilon.

    • Create a unit Vec2 pointing in the given direction (angle in radians). Equivalent to Vec2.fromPolar(1, radians).

      Parameters

      • radians: number

        The angle in radians, measured counter-clockwise from the +x axis.

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A unit Vec2 at the given angle.

    • Create a Vec2 from polar coordinates (length and angle in radians).

      Parameters

      • length: number

        The magnitude of the resulting vector.

      • angle: number

        The angle in radians, measured counter-clockwise from the +x axis.

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A new Vec2 with components (length * cos(angle), length * sin(angle)).

      const v = Vec2.fromPolar(1, Math.PI / 4); // 45-degree unit vector
      
    • Allocate a Vec2 from the public object pool. If weak is true, the vector auto-disposes after a single API call.

      Parameters

      • x: number = 0

        The x component (default 0).

      • y: number = 0

        The y component (default 0).

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A pooled or newly created Vec2.

      const v = Vec2.get(3, 4);
      // use v ...
      v.dispose();
    • Linearly interpolate between two Vec2s. Returns a + t * (b - a). Disposes weak arguments after use.

      Parameters

      • a: Vec2

        The start Vec2 (t = 0).

      • b: Vec2

        The end Vec2 (t = 1).

      • t: number

        The interpolation factor (typically 0–1, but not clamped).

      • weak: boolean = false

        If true, the returned Vec2 is auto-disposed after one use (default false).

      Returns Vec2

      A new Vec2 at the interpolated position.

    • Allocate a weak Vec2 (auto-disposes after a single use).

      Parameters

      • x: number = 0

        The x component (default 0).

      • y: number = 0

        The y component (default 0).

      Returns Vec2

      A weak, pooled Vec2 that is automatically disposed after one API call.