Cabinet №04 / vanilla js

BRICKBREAKER

A phosphor playfield on a single <canvas>. Axis-aligned bounding-box collisions, penetration-side resolution, paddle-angle reflection.

8 × 4 GRID50 × 20 BRICKS320 MAX
Score
000
BEST000
Balls
Bricks remaining32
Multi-hit chain×1
BRIC▮BREAKER
400 × 600 · CANVAS 2D
60 FPS
◀ ▶ KEYS · SPACE SERVE · P PAUSE
R RESTART · M MUTE · E DEMO
◀ DRAG ▶
01

Telemetry

Live
02

Field spec

FIELD400×600
BRICK GRID8×4 · 50×20
HUD BAND28px
PADDLE60×10 @ y580
PADDLE TRAVEL0–340
BALL RADIUS3
SERVE VECTOR2 : −2.8
SPEED BAND3.40 → 4.00
STEP16.67ms ×2
SCORE / BRICK+10
03

Controls

◀ ▶ / A D

Move the paddle (mouse also tracks)

DRAG

Touch anywhere on screen or the pad below

SPACE

Serve the ball, or pause mid-rally

P / ESC

Pause — auto-pauses on tab blur

R

Restart the rack

E

Demo mode — watch it self-play

04

Service notes — how the collisions are resolved

  1. 01

    AABB overlap, then side test

    The ball is expanded into a square bounding box. When it overlaps a brick, penetration is measured on both axes — px = (w/2 + r) − |dx|, py = (h/2 + r) − |dy|. The smaller axis wins: px < py means a side strike and flips vx; otherwise it's a top/bottom strike and flips vy. The ball is then pushed out along that axis so it can never sink into the rack.

  2. 02

    One collision per sub-step

    Every frame is split into two half-steps, each resolving at most one brick — the one with the least total penetration. That kills double-flips on corner hits while still letting a fast diagonal legitimately clear two neighbours in a single frame. Dead bricks are flagged and skipped immediately.

  3. 03

    Paddle angle from hit offset

    Contact is normalised to rel ∈ [−1, 1] across the paddle width and mapped to a ±60° exit angle: vx = sin(rel·θ)·speed, vy = −cos(rel·θ)·speed. Centre hits return almost vertically, edge hits bank hard. Paddle-side hits reflect horizontally instead, and a guard keeps |vy| ≥ 0.7 so the ball never degenerates into a horizontal loop.

  4. 04

    Speed is constant in magnitude

    Reflections only ever flip signs, so velocity length is preserved. The scalar climbs +0.015 per brick and +0.02 per paddle return, hard-clamped at 4.00 px/step, and resets to 3.40 when a ball is lost. Physics advances on a fixed-step accumulator, so 120 Hz phones and throttled desktops get identical trajectories.