Feature Library

Placement solver: where a piece goes, and why not

1.51 GPU · 0.66 CPU ms
A still of the Placement solver: where a piece goes, and why not demo
Still
The live demo starts when it’s in view.Move over the site · click to place · drag posts or tiles · R turns · Copy, then paste

The answer a building game needs under the cursor: where a piece goes and whether it may go there.

It tries sockets first, then grids carried by placed pieces (tilted for roofs and ramps), then world grids, then bare ground, scores each and keeps one, with stable tie-breaks so a still cursor never flickers between two. A refusal stays at the socket you aimed for and says why in words: the volumes overlap, a port’s approach is blocked, the ground is too steep, the footpath is protected, nothing holds it up. Collision uses one oriented box per part, so a doorway stays open. On bare ground a machine stands level on feet, with props where the ground falls away. Rows and areas are dragged out with their total cost and checked against each other; a selection copies as a rigid blueprint that pastes turned, in one edit. Plain TypeScript, no dependencies, about 0.03 ms a cursor query.

What it costs a frame

1.51 ms
0.66 ms
GPU
1.51 ms
CPU, median
0.66 ms
CPU, 95th pct
1.51 ms
Draw calls
0
Weight
Moderate

Measured 25 November 2026 on RTX 4060 Laptop, 13th Gen Intel(R) Core(TM) i9-13900H, Chrome 158, 1920×1080 at 1×. 600 frames × 3 rounds, median kept (rounds ranged GPU 1.49–1.55, CPU 0.60–0.70 ms). What ran: Alone: the cursor circles the bare grid and every frame runs the solver for a post (explain, then the query with its validation), draws the board, the hosts, the ghost and a marker for every candidate. CPU is mostly the solver; GPU one shadow map and a small instanced scene. Phones: tiers are being measured next.

How steadily it runs on RTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · reference

Typical frame ?
16.86 ms
1% low ?
55 fps
Varies by ?
±0.47 ms
All metricsRTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · reference
p50p95p9916.02 ms18.13 ms +
How long each frame took. Most frames sit in the tall bars; a long tail to the right is hitching.

Frames run on a 60 Hz timer (a page demo), so most frames take about one tick, 16.7 ms: the spread around it is what matters.

Frames

Median (p50) ?
16.86 ms
p95 ?
17.73 ms
p99 ?
18.02 ms
Slowest ?
18.55 ms
Mean ?
16.87 ms
Standard deviation ?
0.47 ms
1% low ?
55 fps
0.1% low ?
54 fps
Hitches ?
0 of 600
Over budget ?
0 of 600

Main thread

Long tasks ?
0
Long frames ?
0
Event-loop delay ?
0.0 / 1.7 / 1.9 ms

Input

No input during this run.

Memory

JavaScript heap ?
33.6 → 33.3 MB
Growth ?
-0.26 MB

Browser

JavaScript a frame ?
0.61 ms
All tasks a frame ?
2.20 ms
Layout / style ?
0.51 / 0.30 ms
Main thread busy ?
13%

Garbage collection

Collections ?
18 (12 minor, 6 major)
Pauses ?
32.4 ms in all, longest 1.2

What it needs

Renders with
WebGPUNeeds WebGPU: current Chrome and Edge, Safari 26+, Firefox 141+ on Windows.
Your browser
Checking…Looking for WebGPU
Depends on
NothingNo other packages
With an agent
install("placement-solver")Your coding agent reads its pitfalls and installs it through the library’s MCP server. Set it up

What it’s for

  1. A base-building or colony game: slabs, walls, roofs and machines that snap under the cursor and refuse with a reason
  2. A factory or farm builder on uneven ground: machines on feet and props instead of a slab under everything
  3. Dragging out fences, pipes, rows of posts or a floor of tiles with the cost shown before the click
  4. Copying a finished building as a blueprint and pasting it turned, every join kept
  5. A level editor or a room planner: sockets, grids and collision that leaves openings open
  6. Scrapping safely: warn before removing the post a deck stands on

Pitfalls, in brief

  1. The demo needs WebGPU to draw its build site; the solver itself is plain TypeScript and runs anywhere (a server, a worker, a Canvas 2D or three.js page).
  2. Pass the visible hit as the aim, and the camera ray with it.
  3. Stand pieces on the ground the renderer draws.
  4. A still cursor must give one ghost.
  5. Socket direction decides the turn.
9 more in brief
  1. Keep visible gaps between collision volumes.
  2. Pinwheel walls round a room.
  3. Load paths come from compatible sockets, bearing faces, fitted feet and bracket fastenings.
  4. Clip a carried grid to the polygon of a triangular face.
  5. Machines on bare ground stand at the highest ground under them, clearance (30 cm) above it, on a foot at each corner: a base flush with the grass reads as floating.
  6. Terrace pieces (levels) quantize their top against the highest ground and report the gap along each edge instead of feet: draw a plinth or skirt down to the lowest ground under them, as the demo’s slabs do, or the low side hangs in the air.
  7. Paste the anchor where the copied slab would itself snap (query the slab at the cursor and use its pose): a grounded part must meet the terrain at its new level, and refitting each part on its own after a paste breaks the joins.
  8. Commit the preview you showed: world.commit revalidates it against the current world, so a socket that moved or was taken since the hover refuses as “The attachment moved”.
  9. Prepare placements once per world edit.
14 pitfalls in full, the hook-up guide, the module source and its tests come with the part.

Where it comes from

From HaTac. Fully custom code.

Hannes Andersson: The HaTac engine’s placement, spatial and ground-props modules and their measurements; the library’s host and demo.

placementbuilding gamebase buildingconstructionsnappingsocketssnap pointsgrid snappingtilted gridrooframpghost previewbuild previewrefusal reasonscollisionoriented boxesseparating axisclearanceprotected zonesterrainslopefeetpropsfoundations

Near it in the frame