Phaser 4TypeScriptXPBD physicsWeb AudioVitest
Керування
- Pick a material, then drag from a bolt on the rock or a joint you built to any grid point to add a member; it stops at the material's reach and shows its length and price
- Move drags a joint you built (its members follow), Delete removes a member or a joint, Mirror builds the same member on the other side, Undo and Redo step through every edit
- Test sends the vehicles across; members turn red as they near their limit and snap past it. Slow motion and Edit are in the bar; the design is kept
- Keyboard: arrow keys move a grid cursor, Enter starts and ends a member (it chains), 1 to 5 choose a material, B, M and D choose a tool, X toggles mirror, Ctrl+Z and Ctrl+Shift+Z undo and redo, Delete removes at the cursor, Space tests and stops
- Stars: one for a pass within budget, two at 80 % of it, three at par. Drawings, stars and unlocked levels are kept in this browser
- Add ?level=5 to the address to open a level, or ?demo=12 to watch a reference bridge being tested
Needs WebGL. Without it the poster is shown. Respects the reduced-motion setting: no camera shake, splinters, sparks, splashes or dust, and the interface does not animate; the simulation itself runs the same. Sound is off until you turn it on. Progress is saved in this browser.
Питання
How do I play Trestle?
Pick a material and drag from a bolt on the rock or a joint you built to add a member, then press Test to send the vehicles across. Members turn red near their limit and snap past it.
How many levels are there?
Twelve crossings in three chapters. Every level ships with a reference bridge that passes.
How do stars work?
One star for a pass within budget, two at 80% of it, and three at par.
Can I play with the keyboard?
Yes. Arrow keys move a grid cursor, Enter starts and ends a member, 1 to 5 choose a material, Ctrl+Z undoes and Space tests.
Does it save progress?
Drawings, stars and unlocked levels are kept in this browser.
Як це зроблено
Trestle is a bridge-building puzzle: you draw members between joints on a grid, then a vehicle drives across and the structure carries it or fails. Phaser 4 draws the canyon, the bridge and the vehicles; the physics, the levels and the editor rules are plain TypeScript with unit tests, and React owns the interface and the input.
The simulation
- Every joint is a particle and every member a distance constraint solved with XPBD (extended position-based dynamics): a fixed 60 Hz step split into 24 substeps with one constraint pass each, which is stiffer and steadier than many passes per step. Each member's compliance comes from its material's axial stiffness and length, so steel barely stretches and timber gives a little.
- The solver's Lagrange multiplier divided by the substep squared is the member's axial force. That number colours each member from calm to red and breaks it when it passes the material's strength in tension or compression. Cables only pull: they go slack and sag when pushed. A member also snaps once it is stretched or squashed by more than 12 %: one solver pass per substep is soft, and without that limit a collapsing bridge drew long rubber bands. Broken stubs keep a fixed length.
- Members add half their mass to each end joint, so a bridge also carries its own weight; a bare road sags and snaps before a vehicle reaches it.
- Vehicles are rigid frames of particles on two or three wheels. A wheel touching a road member is pushed out along the contact normal while the two joints of that member are pushed the other way, weighted by their inverse masses and by where the wheel sits along the member, which is how a heavy truck bends a weak deck. Drive and friction are a tangential correction capped by grip times the normal correction, so traction depends on load.
- The simulation is deterministic: the same level and design always give the same run. Vitest uses that to prove every level can be passed: each one ships with a reference bridge that must cross within budget, a lean bridge that must cross at par for three stars, and the bare road that must fail.
- Par prices come from a small offline optimiser (
core/tune.test.ts, run by hand) that removes or downgrades members of the reference one at a time and keeps each change only while the bridge still passes with 20 % headroom on every member.
The editor
- A design is just a list of members between grid points; joints are their ends, so moving a joint moves every member that meets there and deleting a joint deletes them. Undo and redo keep whole designs (they are small), up to 200 steps.
- Drags snap to existing joints when they are in reach and otherwise stop at the material's maximum length on the nearest grid point toward the pointer. A member through rock, below the water or duplicated is refused with a reason next to the pointer.
- Mirror builds every new member a second time across the centre line between the two road bolts.
- The whole board works from the keyboard: a grid cursor, Enter to start and chain members, number keys for materials and letters for tools; a live region reads out lengths, positions and results.
Drawing and performance
- The sky, the mesas, the canyon walls with their sandstone strata, the road, scrub and cacti are painted once per level with Canvas 2D into a texture the size of the screen. Phaser Graphics redraws only the bridge, the vehicles and the river glints each frame; particles (splinters, sparks, spray and dust) are pooled emitters fired with
explode(). - Broken members leave stubs at their joints and two tumbling halves that fall into the river.
- The canvas renders at the device pixel ratio (capped at 2, or 1.5 on low-end devices) and the loop sleeps when the page is hidden or the canvas is off screen.
- Under reduced motion there is no camera shake, no particles, no spinning debris and the interface does not animate; the simulation is the same.
- Sound is generated with Web Audio (pencil taps, timber cracks, steel pings, an engine hum that follows the speed) and stays off until you turn it on.
- Build mode shows one plain next-step line above the tray (lay road from a bolt, finish the road, brace and test, or how far over budget), the tools carry text labels, and each material card states its role (road, brace, pull only) and longest piece.
- The backdrop is painted once per level: three rows of mesas with lit caps, soft cloud banks, canyon walls that recede into haze as real rock, stone grain and ledges on the faces, and a river with wall reflections, a sun path, ripples, sparkles, foam and boulders on the banks.