加载应用…
云澜 Clyune Clyune Component Gallery · 中文

Box Collisions · How it works & code

AABB collision detection + impulse and friction, boxes can stack.

Runtime logic

Visiting `/physics/box-collide`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'box-collide' })`. mount resolves the variant to family `box-collide`; `withControls` builds the control panel (6 presets + sliders). `createCanvasStage` creates the `<canvas>` (HiDPI ≤ 2×, ResizeObserver) and the info panel. The family’s `build(S)` initialises state and returns the per-frame callback. Every `requestAnimationFrame`: read controls, advance a speed-scaled clock (dt ≤ 50 ms), run the frame callback; returned readouts go to the info panel. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. Changing parameters: colour/speed/size apply live; structural ones remount; leaving the page cancels rAF, removes listeners and frees resources.

Environment

Canvas 2D: 2D canvas drawing; requestAnimationFrame: Display-synced animation loop; ResizeObserver: Canvas follows container size; Pointer Events: Unified mouse/touch/pen events; devicePixelRatio: Crisp HiDPI rendering (capped at 2×) · react 18.3.1, react-router-dom 6.30.6, vite 5.4.21

Dependencies

react 18.3.1

Imported APIs

withControls — Wraps the demo in the shared control panel (presets, sliders, reset); hot-updates or remounts on change; paramsFromSpec — Turns the spec (colour/speed/size/count/extras) into control-panel parameter definitions; createCanvasStage — Shared Canvas 2D stage: canvas + info panel, HiDPI, one rAF loop, pointer state, idle auto-pointer; rng — Seeded deterministic PRNG (same seed, same result); hueOf — Hue of a colour (0–360°); clamp — Clamps a value to a range; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.onDown — Pointer down; S.onUp — Pointer up; S.clear — Clears with the background colour (translucent = trails); S.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.strokeRect() — Strokes a rectangle; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path

Source code

frontend/src/demos/kits/kit-physics.js: FAM.box-collide (L341-L392)

Code walkthrough

L343-343: Control definitions (colour/speed/size/count/extras): Box Colors, Box Size, Number of Boxes, restitution e, gravity g, Friction μ. L344-344: Preset buttons: name → parameter overrides (6). L345-353: Info-panel content: live rows(v), formula, notes, params, steps. L354-391: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `fill`, `stroke`, `strokeRect`; math via `Math.abs`, `Math.round`, `Math.sign`, `Math.min`, `Math.max`, `Math.hypot`; returns readouts `n`, `p`, `K`, `pairs`, `e`, `mu`, `pen` … to the info panel.

Principles

AABB Intersection:|x₁ − x₂| Separating along the smallest penetrating axis; impulse j = −1 + e) · v_rel / (1 / m₁ + 1 / m₂) Cut the friction |j_t| ≤ μ · j,m ∝ w · h AABB (轴对齐包围盒)是最简单的碰撞检测:两个不旋转的矩形,只要在 x 和 y Both directions overlap, which is collision. After the collision The shallowest through 的方向把它们推开,并在这个方向上施加冲量,让相对速度按恢复系数 e Bounce back; cut back with friction so the box can fold up. Large boxes are of high quality and harder to push. The pointer is a pusher, holding down the box to drag and throw.

Open the interactive demo · GitHub