Creates a Vec2 with the given components. Defaults to (0, 0).
The x component (default 0).
The y component (default 0).
Angle of the vector in radians, measured counter-clockwise from the +x axis. Returns 0 for the zero vector.
Setting angle preserves the vector's magnitude and rotates it to the given angle.
The desired angle in radians. Must not be NaN.
Magnitude (Euclidean length) of the vector.
Setting length scales the vector to the given magnitude. Throws if the vector is zero-length.
The desired magnitude. Must not be NaN.
The x component.
The x component.
The y component.
The y component.
Add another Vec2 to this in-place (this += other).
The Vec2 to add.
this for chaining.
Return a new Vec2 equal to this + other × scalar.
The Vec2 to scale and add.
The multiplier applied to vector before addition.
If true, the returned Vec2 is auto-disposed after one use (default false).
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().
A new Vec2 with the same x and y values.
Return a new Vec2 with the same components.
If true, the returned Vec2 is auto-disposed after one use (default false).
A copy of this vector.
2D cross product (this.x × other.y − this.y × other.x). Returns a
scalar.
The other Vec2.
The scalar cross product.
Return this Vec2 to the object pool. Throws if already disposed or immutable.
Dot product of this and another Vec2.
The other Vec2.
this.x * other.x + this.y * other.y.
Check whether this Vec2 is component-wise equal to another, within an optional epsilon tolerance.
The Vec2 to compare against.
Maximum allowed difference per component (default 0).
true if both components differ by at most epsilon.
Returns the squared magnitude. Faster than length as it avoids a square
root.
x² + y².
Return a new Vec2 equal to this × scalar.
The multiplier.
If true, the returned Vec2 is auto-disposed after one use (default false).
A new Vec2 with components (this.x * scalar, this.y * scalar).
Multiply this Vec2 by a scalar in-place (this ×= scalar).
The multiplier.
this for chaining.
Normalise this vector to unit length in-place. Throws for zero-length vectors.
this for chaining.
Return the perpendicular vector, rotated 90° counter-clockwise.
If true, the returned Vec2 is auto-disposed after one use (default false).
A new Vec2 with components (-this.y, this.x).
Rotate this vector by angle radians in-place.
The rotation angle in radians.
this for chaining.
Copy another Vec2's components into this vector in-place.
The source Vec2 to copy from.
this for chaining.
Set both components at once in-place.
The new x component.
The new y component.
this for chaining.
Subtract another Vec2 from this in-place (this -= other).
The Vec2 to subtract.
this for chaining.
String representation in the form { x: … y: … }.
A human-readable string of this vector's components.
Return a new unit-length vector with the same direction. Throws for zero-length vectors.
If true, the returned Vec2 is auto-disposed after one use (default false).
A normalised copy of this vector.
StaticdistanceStaticdsqStaticeqCheck whether two Vec2s are component-wise equal, within an optional epsilon tolerance. Disposes weak arguments after comparison.
true if both components differ by at most epsilon.
StaticfromCreate a unit Vec2 pointing in the given direction (angle in radians).
Equivalent to Vec2.fromPolar(1, radians).
The angle in radians, measured counter-clockwise from the +x axis.
If true, the returned Vec2 is auto-disposed after one use (default false).
A unit Vec2 at the given angle.
StaticfromCreate a Vec2 from polar coordinates (length and angle in radians).
The magnitude of the resulting vector.
The angle in radians, measured counter-clockwise from the +x axis.
If true, the returned Vec2 is auto-disposed after one use (default false).
A new Vec2 with components (length * cos(angle), length * sin(angle)).
StaticgetAllocate a Vec2 from the public object pool. If weak is true, the vector
auto-disposes after a single API call.
The x component (default 0).
The y component (default 0).
If true, the returned Vec2 is auto-disposed after one use (default false).
A pooled or newly created Vec2.
StaticlerpLinearly interpolate between two Vec2s. Returns a + t * (b - a).
Disposes weak arguments after use.
A new Vec2 at the interpolated position.
StaticweakAllocate a weak Vec2 (auto-disposes after a single use).
The x component (default 0).
The y component (default 0).
A weak, pooled Vec2 that is automatically disposed after one API call.
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.