Bash.tv → GitHub → itch.io

Detect collisions in 2D games. Hitboxes vs hurtboxes, AABB, circles, circle-vs-box, rotated boxes and convex polygons (SAT), swept AABB for fast objects, ray and segment tests, grid raycasts (DDA) for line of sight and hitscan, collision layers and masks, broad phase vs narrow phase, and choosing hitbox sizes that feel fair. Includes a tested collide.js. Use when adding collisions, hitboxes, bu...

Download skill (.zip) Files on GitHub

Collision detection

Drop-in: assets/collide.js (23 tests passed: edges, swept hits, rays, DDA, SAT, layers). Response (separating, sliding, bouncing) lives in game-physics; many-object speed in game-spatial-partitioning.

Need Function
Boxes (most games) aabb(a, b) with {x, y, w, h} top-left. Touching edges don’t count
How far to push out aabbPenetration(a, b) → smallest-axis {x, y}
Round things circles(a, b), circleContact(a, b) → normal + depth, circleRect(c, r)
Fast objects vs thin walls sweptAABB(a, vx * dt, vy * dt, wall) → time of impact + normal
Rotated boxes, triangles polygons(A, B) (SAT) with rectPoly(cx, cy, w, h, angle)
Hitscan, lasers, line of sight rayRect, segments, raycastGrid(ox, oy, dx, dy, tile, isSolid)
Who can hit whom LAYER bits + canCollide(a, b) (player bullets ignore the player, pickups ignore enemies…)

Hitboxes that feel fair

Order each step

  1. Move things (physics). 2. Broad phase: candidates from a spatial grid or the tilemap. 3. Narrow phase: exact tests.
  2. Collect results, then apply damage, pickups and removals (never mutate arrays mid-check). 5. Resolve overlaps (game-physics).

Tiles

For tile maps, don’t test against every tile: convert the box to the tile range it covers (floor(x / T) … floor((x + w) / T)) and check only those (game-physics moveAndSlide does this with substeps, so it never tunnels).

Pitfalls