Breakout·8

32 bricks · 3 lives · canvas 2d · zero deps

fps 60
|v| 3.44
unit grid-8 · crt 400 × 600 px
← →A / DspaceP or drag across the screen
01

collision resolution

No physics engine.
Circle ↔ AABB, solved by hand.

Every bounce is an explicit penetration test — clamp the ball's centre to the rectangle, compare overlap on each axis, resolve along the smaller one. Deepest overlap wins, one brick per substep.

  1. Clamped point distance

    cx = clamp(ball.x, x, x+w) — distance to that point ≤ r means contact. Cheap, exact, no sqrt needed for the axis test.

  2. Min-penetration axis

    penX vs penY decide the wall. Flip only that velocity component and push the ball out by the overlap — so a brick can never swallow the ball.

  3. Deepest overlap wins

    The ball's bounding box maps to a local grid window; candidates are scored and one is resolved per substep, so simultaneous hits never stack into a spiral.

  4. Substepping

    The ball moves in ≤2 px slices per frame, so at speed 4 it cannot tunnel through a 20 px brick — the classic breakout bug.

  5. Paddle angle

    Hit offset maps to ±60° off vertical, with a |vy| ≥ 0.35·|v| floor so the ball never flattens into an endless horizontal rally.

push axis = min(penX, penY)
top/bottom hit → flip vy · left/right hit → flip vx
−60° 0° +60° offset → angle
edge hits fan out · centre hits stay vertical

Field spec

implementation constants — mirrored in the code
Canvas400 × 600 px · logical space, DPR-scaled for crisp lines on retina
Grid8 cols × 4 rows · 50 × 20 px bricks · x = col·50, y = 70 + row·20
Bricks32 total · +10 pts each · max 320 · cyan, tinted per row
Paddle60 × 10 px · y = 580 · clamped to x ∈ [0, 340]
Ballr = 3 px · launch (2, −2.8) normalised, randomised sign
Speed|v| = 3.44 → 4.00, ramped by bricks destroyed · magnitude constant mid-flight
Lives3 · exit below the paddle costs one, ball re-attaches and waits for launch
FramerequestAnimationFrame, dt-clamped · ≤2 px substeps · 60 fps target
Mobiletouchstart / touchmove / touchend · touch-action:none · portrait & landscape ≥320 px