Skip to main content

Vector2

2D vector functions using arrays [x, y]. All angles are in degrees. Functions modify the first vector in-place for zero allocations and high performance.


Creating Vectors

// Create a new vector
var v = vec2_create(3, 4);

// Create a zero vector
var zero = vec2_create(); // [0, 0]

Functions Reference

Creation

FunctionReturnsDescription
vec2_create(x?, y?)ArrayCreates a new vec2 with given components (default: 0, 0)

Setters

FunctionDescription
vec2_set(vec, x, y)Sets the vector components
vec2_set_scalar(vec, s)Sets all components to the same value
vec2_set_x(vec, x)Sets the x component
vec2_set_y(vec, y)Sets the y component
vec2_set_component(vec, index, value)Sets component by index (0=x, 1=y)

Getters

FunctionReturnsDescription
vec2_get_component(vec, index)numberGets component by index (0=x, 1=y)

Clone / Copy

FunctionReturnsDescription
vec2_clone(vec)ArrayReturns a new independent copy
vec2_copy(dest, src)-Copies src values into dest

Addition

FunctionDescription
vec2_add(vec, v)Adds v to vec
vec2_add_scalar(vec, s)Adds scalar to all components
vec2_add_scaled_vector(vec, v, s)Adds v * s to vec
vec2_add_vectors(dest, a, b)Sets dest = a + b

Subtraction

FunctionDescription
vec2_sub(vec, v)Subtracts v from vec
vec2_sub_scalar(vec, s)Subtracts scalar from all components
vec2_sub_vectors(dest, a, b)Sets dest = a - b

Multiplication

FunctionDescription
vec2_multiply(vec, v)Component-wise multiplication
vec2_multiply_scalar(vec, s)Multiplies all components by scalar

Division

FunctionDescription
vec2_divide(vec, v)Component-wise division
vec2_divide_scalar(vec, s)Divides all components by scalar

Dot / Cross

FunctionReturnsDescription
vec2_dot(vec, v)numberDot product
vec2_cross(vec, v)number2D cross product (scalar)

Length

FunctionReturnsDescription
vec2_length(vec)numberEuclidean length
vec2_length_sq(vec)numberSquared length (faster, no sqrt)
vec2_manhattan_length(vec)numberManhattan (taxicab) length
vec2_set_length(vec, len)-Sets vector to specific length

Normalize / Negate

FunctionDescription
vec2_normalize(vec)Converts to unit vector (length = 1)
vec2_negate(vec)Inverts: x = -x, y = -y

Distance

FunctionReturnsDescription
vec2_distance_to(vec, v)numberEuclidean distance
vec2_distance_to_squared(vec, v)numberSquared distance (faster)
vec2_manhattan_distance_to(vec, v)numberManhattan distance

Min / Max / Clamp

FunctionDescription
vec2_min(vec, v)Component-wise minimum
vec2_max(vec, v)Component-wise maximum
vec2_clamp(vec, min, max)Clamps between min and max vectors
vec2_clamp_scalar(vec, min, max)Clamps between scalar values
vec2_clamp_length(vec, min, max)Clamps vector length between min and max

Rounding

FunctionDescription
vec2_floor(vec)Rounds components down
vec2_ceil(vec)Rounds components up
vec2_round(vec)Rounds components to nearest
vec2_round_to_zero(vec)Rounds towards zero

Equality

FunctionReturnsDescription
vec2_equals(vec, v)booleanTrue if vectors are exactly equal

Interpolation

FunctionDescription
vec2_lerp(vec, v, alpha)Lerp towards v by alpha (0..1)
vec2_lerp_vectors(dest, v1, v2, alpha)Sets dest = lerp(v1, v2, alpha)

Angle (in degrees)

FunctionReturnsDescription
vec2_angle(vec)numberAngle from positive X-axis
vec2_angle_to(vec, v)numberAngle between two vectors

Rotation (in degrees)

FunctionDescription
vec2_rotate_around(vec, center, angle)Rotates around center point

Random

FunctionDescription
vec2_random(vec)Sets components to random values [0, 1)

Array Conversion

FunctionReturnsDescription
vec2_from_array(vec, array, offset?)-Sets from array at offset
vec2_to_array(vec, array?, offset?)ArrayCopies to array at offset
vec2_from_buffer_attribute(vec, attr, index)-Sets from flat buffer by vertex index

Matrix Transformation

FunctionDescription
vec2_apply_matrix3(vec, m)Multiplies by 3x3 matrix (column-major)

Extra Utility

FunctionDescription
vec2_abs(vec)Sets components to absolute values
vec2_reflect(vec, normal)Reflects across normal vector
vec2_project(vec, onto)Projects onto another vector
vec2_perp(vec)Rotates 90° CCW (perpendicular)

Usage Examples

Basic Operations

var a = vec2_create(3, 4);
var b = vec2_create(1, 2);

vec2_add(a, b); // a = [4, 6]
vec2_multiply_scalar(a, 2); // a = [8, 12]
vec2_normalize(a); // a = unit vector

Distance and Length

var pos = vec2_create(10, 0);
var target = vec2_create(5, 0);

var dist = vec2_distance_to(pos, target); // 5
var len = vec2_length(pos); // 10

Interpolation

var start = vec2_create(0, 0);
var end = vec2_create(100, 100);

vec2_lerp(start, end, 0.5); // start = [50, 50]

Rotation

var v = vec2_create(1, 0);
var center = vec2_create(0, 0);

vec2_rotate_around(v, center, 90); // v ≈ [0, 1]

Buffer Attribute (for vertex data)

// Flat buffer with vertices: [x0,y0, x1,y1, x2,y2, ...]
var positions = [10, 20, 30, 40, 50, 60];
var v = vec2_create();

vec2_from_buffer_attribute(v, positions, 0); // v = [10, 20]
vec2_from_buffer_attribute(v, positions, 1); // v = [30, 40]
vec2_from_buffer_attribute(v, positions, 2); // v = [50, 60]