Skip to main content

Box2

2D axis-aligned bounding box stored as [minX, minY, maxX, maxY]. Functions modify the first argument in-place unless noted.


Functions Reference

Creation

FunctionReturnsDescription
box2_create(minVec, maxVec)ArrayCreates a new Box2. Defaults to empty inverted bounds.
box2_set(b, minVec, maxVec)-Sets bounds directly.

Operations

FunctionReturnsDescription
box2_expand_by_point(b, x, y)-Expands the box to include the point.
box2_contains_point(b, x, y)booleanReturns true if point is inside the box.
box2_intersects_box(b, b2)booleanReturns true if boxes intersect.
box2_copy(b, src)-Copies bounds from src.
box2_clone(b)ArrayReturns copy [minX,minY,maxX,maxY].
box2_contains_box(b, b2)booleanReturns true if b fully contains b2.
box2_equals(b, b2)booleanStrict equality on all components.
box2_clamp_point(b, x, y, out?)Vector2Clamps point to box boundaries.
box2_distance_to_point(b, x, y)numberEuclidean distance to the box (0 if inside).
box2_expand_by_scalar(b, s)-Expands bounds by scalar in all directions.
box2_expand_by_vector(b, v)-Expands bounds by vector [x,y] in both directions.
box2_get_center(b, out?)Vector2Returns center point.
box2_get_size(b, out?)Vector2Returns width/height.
box2_get_parameter(b, p, out?)Vector2Returns normalized coordinates within the box.
box2_is_empty(b)booleanTrue if minX > maxX or minY > maxY.
box2_make_empty(b)-Sets to empty ([inf,inf,-inf,-inf]).
box2_intersect(b, b2)Box2Sets b to overlap with b2 (or empty if none).
box2_union(b, b2)Box2Sets b to the union of both boxes.
box2_translate(b, offset)-Moves box by offset.
box2_set_from_center_and_size(b, center, size)Box2Sets from center [x,y] and size [w,h].
box2_set_from_points(b, points)Box2Sets to enclose array of points [x,y].

Usage Examples

var b = box2_create();
box2_set(b, 0, 0, 10, 10);

box2_expand_by_point(b, 12, 5); // b = [0, 0, 12, 10]
var inside = box2_contains_point(b, 6, 6); // true