You are an expert browser-based physics engineer.
TASK: Output ONE (1) self-contained HTML file (inline ) that runs as-is in any modern desktop browser. No external libraries, assets, or CSS files. Return ONLY the ….
Goal: “Spinning Hexagon — Spawnable Balls”
Make a 900×900 canvas simulation where balls bounce inside a rotating regular hexagon. The user can spawn new balls with the mouse. Be precise—follow every spec below; do not change values.
Canvas & Timing
Canvas CSS size: 900×900; handle devicePixelRatio for crisp rendering.
Use requestAnimationFrame for rendering.
Fixed physics step = 1/120 s. If a frame lags, run multiple fixed steps, capped at 5 per RAF.
Interpolate render between sim steps for smooth motion.
Determinism & RNG
Read URL param ?seed=; default seed = 1.
Implement xorshift32 (uint32) seeded from ?seed. Provide rand01() ∈ [0,1).
Use this RNG for ALL initial randomness (colors chosen from a fixed palette by cycling, positions jitter, initial velocities for startup balls).
World Geometry (do not change)
World origin at canvas center.
Boundary: regular HEXAGON centered at (0,0), circumradius = 360 px.
Hexagon rotates CLOCKWISE at constant 40 deg/s about its center. Rotation is applied to the boundary, not to the balls’ local frames (i.e., walls move; balls are in world coords).
Forces & Integration
Gravity g = (0, +1100) px/s² (positive y downward).
Air drag OFF.
Integrator: semi-implicit (symplectic) Euler.
Balls (state & startup)
Ball state per item: {id, pos, vel, radius, mass, color}.
Mass = radius² (units arbitrary).
Restitution (all collisions) e = 0.88. No friction/spin modeling.
Startup: create exactly 6 balls with the following radii (px): [12,14,16,18,20,22].
Startup placement: uniformly jitter in a disk of radius 80 px around the center using RNG; if a ball would start outside the hexagon, nudge inward along the radial direction until inside.
Startup velocity: zero.
Spawning (user input)
LEFT CLICK: spawn ONE ball at the mouse position projected INSIDE the hexagon (if click is outside, clamp along the ray from center to click to lie 10 px inside the boundary).
Spawned ball:
radius = 14 px
color cycles through a fixed 12-color palette you define in code (e.g., 12 distinct hues); cycle index persists across spawns.
initial velocity = normalized vector from canvas center to mouse * 600 px/s.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
LEFT CLICK & DRAG: if the mouse is held, spawn at most 8 balls per second (throttle) at the current mouse position with the same rules.
RIGHT CLICK context menu must be prevented; do nothing on right click.
Collisions (specific)
Ball–wall only; ignore ball–ball to keep it lightweight.
For each fixed step (and each sub-step if you subdivide), integrate velocity and position, then resolve collisions against the CURRENTLY ROTATED hexagon.
Compute hexagon edge normals from its rotation angle each step.
Collision test: for each edge, if the ball center is beyond the edge by penetration depth d < 0 (using signed distance to the infinite line and inside-tests for the segment), resolve by:
Pushing the ball out along the edge normal by |d|.
Reflecting velocity on the normal with restitution e: v' = v − (1+e)*(v·n)*n.
Anti-tunneling: if |v| * dt > radius, subdivide that step into ceil(|v|*dt/radius) substeps, but never exceed the global cap of 5 substeps per RAF frame.
Controls
Space: pause/resume physics.
R: reset world to startup state using the SAME seed (deterministic).
C: clear ALL balls (keeps rotation and counters).
S: download a PNG screenshot of the canvas.
+/-: increase/decrease hexagon rotation speed by ±5 deg/s (apply immediately; show in overlay). Clamp to [10, 120] deg/s.
Overlay (top-left, monospace)
Show: FPS (smoothed over 0.5 s), physics steps per frame (avg), total balls, hexagon angle (deg, 0–360), rotation speed (deg/s), seed value, paused state.
Bottom-right: short controls legend (mouse + keys).
Rendering
Background #111. Hexagon outline #ffffff with 3 px stroke. Balls are filled disks with a 1 px #000 border. Draw in world space then transformed for rotation of the hexagon boundary (i.e., you don’t rotate balls).
Draw order: hexagon outline on top of balls (so containment is visually clear).
House Rules
No external libraries/CDNs. No inline SVG—use .
Keep the entire program under ~250 lines excluding blank lines (write tight, but comment key functions).
Use const/let; no frameworks or transpilers.
Provide small utility funcs: rng, vec2 ops, hexagon vertices generation by angle.
On load, center the canvas in the page with minimal styling; no fonts beyond default system monospace for the overlay.
Output format
Return ONLY a single valid HTML document containing , the , and a single with your implementation. No extra explanation.