Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
raea/people: passers-by go door to door round a block, legs keep their own busyness, no one jumps
shinyobjectz179f244e5d04
diff --git a/sdk/character/src/people/flow.ts b/sdk/character/src/people/flow.tsindex bbef8c9d..2357a754 100644--- a/sdk/character/src/people/flow.ts+++ b/sdk/character/src/people/flow.ts@@ -1,7 +1,9 @@// The people passing through (docs/character.feature CH52): a street carries more than the people who live on it.-// Each sidewalk holds walkers by its length, its busyness at its busiest hour and the hour's share of that; each is a-// person (a role drawn by the mix) walking the line at their own pace one way or the other, keeping to their right.-// Where a walker is at any minute is computed, not stepped, so any frame of a shot can be drawn alone.+// Each sidewalk holds walkers by its length, how busy it is at its busiest hour and the hour's share of that. A+// walker comes out of a door, walks a stretch of the sidewalk at their own pace one way or the other, keeping to their+// right, and goes in at the door nearest where the stretch ends; then someone else comes out. A sidewalk round a+// block is closed, so walkers turn its corners; one with no doors on a stretch starts and stops there. Where a walker+// is at any minute is computed, not stepped, so any frame of a shot can be drawn alone.import { walkSpeed } from "./day";import { makePerson, type Folk } from "./persona";import { hash, rng } from "./rng";@@ -11,14 +13,25 @@ import type { Minute, Person, Place, Role, V3 } from "./types";export interface Walk {id: string;path: V3[];+ /** walkers a metre at the busiest hour (each leg's own when `legs` is given) */perMetre: number;/** its width (metres, 3): walkers keep to their right within it */width?: number;+ /** a ring (round a block): the last point joins the first, and walkers go on round */+ closed?: boolean;+ /** stretches of their own busyness, in order: each ends at `to` metres along the walk */+ legs?: { to: number; perMetre: number }[];+ /** the doors along it, in metres along the walk: walkers come out of and go in at these */+ doors?: number[];}/** A city street's walkers by hour, against its busiest (5 pm): the morning and evening rush, the noon errands, the* empty night. Index h is h:00; a minute between is read between. */export const STREET_HOURS = [0.03, 0.02, 0.02, 0.02, 0.04, 0.1, 0.3, 0.6, 0.7, 0.55, 0.6, 0.75, 0.9, 0.85, 0.75, 0.8, 0.9, 1, 0.85, 0.5, 0.3, 0.2, 0.1, 0.05];+/** The share of a walker's round spent walking (the rest is indoors, between a door and the next) */+export const OUT_SHARE = 0.7;+/** How far round a corner past its own leg a walker may go (metres) */+export const SPILL = 25;export interface Passer {id: string;@@ -26,6 +39,8 @@ export interface Passer {at: V3;facing: number;person: Person;+ /** metres along the walk */+ s: number;}export interface FlowInput {@@ -53,41 +68,114 @@ export function busyAt(m: Minute, hours = STREET_HOURS): number {return hours[i] * (1 - k) + hours[(i + 1) % 24] * k;}+/** A walk's points with the closing point for a ring, and the metres along it to each. */+function measure(w: Walk) {+ const P = w.closed ? [...w.path, w.path[0]] : w.path;+ const cum = [0];+ for (let i = 1; i < P.length; i++) cum.push(cum[i - 1] + Math.hypot(P[i][0] - P[i - 1][0], P[i][2] - P[i - 1][2]));+ return { P, cum, L: cum[cum.length - 1] };+}+const walks = new WeakMap<Walk, ReturnType<typeof measure>>();++/** The point `s` metres along a walk and its direction there, turned smoothly through the TURN metres either side of+ * a corner (a walker's keep-right offset hangs off the direction: a sharp turn would jump them across the corner). */+const TURN = 1.5;+function along(m: ReturnType<typeof measure>, s: number, closed: boolean): { at: V3; tx: number; tz: number } {+ const { P, cum } = m, n = cum.length - 1;+ let lo = 0, hi = n;+ while (hi - lo > 1) { const mid = (lo + hi) >> 1; if (cum[mid] <= s) lo = mid; else hi = mid; }+ const dir = (i: number): [number, number] => {+ // segment i's direction (wrapping round a ring; held at an open walk's ends)+ const k = closed ? ((i % n) + n) % n : Math.min(n - 1, Math.max(0, i));+ const d = cum[k + 1] - cum[k] || 1;+ return [(P[k + 1][0] - P[k][0]) / d, (P[k + 1][2] - P[k][2]) / d];+ };+ const A = P[lo], B = P[hi], d = cum[hi] - cum[lo] || 1, u = Math.min(1, Math.max(0, (s - cum[lo]) / d));+ let [tx, tz] = dir(lo);+ const intoStart = s - cum[lo], toEnd = cum[hi] - s;+ // within TURN of either end of the segment, half-way to the neighbour's direction at the corner itself+ const blend = (o: [number, number], x: number) => { const w = 0.5 * Math.max(0, 1 - x / TURN); tx = tx * (1 - w) + o[0] * w; tz = tz * (1 - w) + o[1] * w; };+ if (intoStart < TURN && (closed || lo > 0)) blend(dir(lo - 1), intoStart);+ if (toEnd < TURN && (closed || hi < n)) blend(dir(lo + 1), toEnd);+ const l = Math.hypot(tx, tz) || 1;+ return { at: [A[0] + (B[0] - A[0]) * u, A[1] + (B[1] - A[1]) * u, A[2] + (B[2] - A[2]) * u], tx: tx / l, tz: tz / l };+}++const longest = (x: number, len: number) => Math.min(40 + 160 * x, len + SPILL);+/** One round's stretch on the leg [a, b]: out of a door (or anywhere on a leg with none), most walking the way with+ * more of the leg ahead of them (so a busy leg's walkers stay on it), as far as the round allows. */+function stretchOf(rc: ReturnType<typeof rng>, a: number, b: number, doors: number[], most: number) {+ const s0 = doors.length ? doors[Math.floor(rc.next() * doors.length)] : a + rc.next() * (b - a);+ const ahead = b - s0, behind = s0 - a, dir = rc.next() * (ahead + behind) < ahead ? 1 : -1;+ return { s0, dir, D: Math.max(8, Math.min(most, (dir > 0 ? ahead : behind) + SPILL * rc.next())) };+}+/** The mean share of its longest that a round's stretch spends on its own leg (from 256 draws of its own: the part+ * round a corner counts on the next leg, not this one) */+function stretchShare(a: number, b: number, doors: number[]): number {+ const r = rng(`stretch-${a}-${b}-${doors.length}`);+ let sum = 0;+ for (let k = 0; k < 256; k++) {+ const most = longest(r.next(), b - a), { s0, dir, D } = stretchOf(r, a, b, doors, most);+ const end = s0 + dir * D, on = Math.min(Math.max(s0, end), b) - Math.max(Math.min(s0, end), a);+ sum += Math.max(0, on) / most;+ }+ return sum / 256;+}+const stretch = new Map<string, number>();+/** Everyone walking the given sidewalks at minute `m` (fractional minutes are seconds): who, where, which way. */-export function passersBy(walks: Walk[], m: Minute, f: FlowInput): Passer[] {- const busy = busyAt(m, f.hours), out: Passer[] = [];+export function passersBy(list: Walk[], m: Minute, f: FlowInput): Passer[] {+ const busy = busyAt(m, f.hours), out: Passer[] = [], sec = m * 60;const roles = new Map(f.roles.map((r) => [r.id, r]));- for (const w of walks) {+ for (const w of list) {if (f.near && f.within !== undefined && !w.path.some((p) => Math.hypot(p[0] - f.near![0], p[2] - f.near![2]) <= f.within!)) continue;- const seg = w.path.slice(1).map((q, i) => Math.hypot(q[0] - w.path[i][0], q[2] - w.path[i][2]));- const L = seg.reduce((a, b) => a + b, 0);+ let g = walks.get(w);+ if (!g) walks.set(w, (g = measure(w)));+ const L = g.L;if (L < 2) continue;- // the line's slots at its busiest; a slot walks when its own draw is under the hour's share, so the walkers- // thin out and come back as the day goes without anyone jumping- const slots = Math.round(L * w.perMetre * (f.busy ?? 1));- for (let k = 0; k < slots; k++) {- const id = `${w.id}:${k}`, r = rng(`${f.seed}-${id}`);- if (r.next() >= busy) continue;- let p = people.get(`${f.seed}-${id}`);- if (!p) {- // who they are from a draw of their own, so a walker made earlier walks the same way- const role = roles.get(rng(`${f.seed}-${id}-who`).weighted(f.mix))!;- const home: Place = { id: `passing-${w.id}`, kind: "street", at: w.path[0] };- p = makePerson(id, role, f.folk, home, hash(`${f.seed}-${id}`));- people.set(`${f.seed}-${id}`, p);+ const legs = w.legs ?? [{ to: L, perMetre: w.perMetre }];+ let from = 0;+ legs.forEach((leg, j) => {+ const a = from, b = Math.min(L, leg.to), len = b - a;+ from = b;+ if (len <= 0) return;+ const doors = (w.doors ?? []).filter((d) => d >= a && d < b);+ // the leg's walkers at its busiest; a slot walks when its own draw is under the hour's share, so the walkers+ // thin out and come back as the day goes without anyone jumping. (Indoors part of each round: more slots.)+ // (a round's stretch is often shorter than its longest, the rest of the round indoors: more slots for that, by+ // the leg's own mean share, which its length and doors decide)+ const key = `${w.id}:${j}`;+ let share = stretch.get(key);+ if (share === undefined) stretch.set(key, (share = stretchShare(a, b, doors)));+ const slots = Math.round((len * leg.perMetre * (f.busy ?? 1)) / (OUT_SHARE * share));+ for (let k = 0; k < slots; k++) {+ const id = `${w.id}:${j}:${k}`, r = rng(`${f.seed}-${id}`);+ if (r.next() >= busy) continue;+ let p = people.get(`${f.seed}-${id}`);+ if (!p) {+ // who they are from a draw of their own, so a walker made earlier walks the same way+ const role = roles.get(rng(`${f.seed}-${id}-who`).weighted(f.mix))!;+ const home: Place = { id: `passing-${w.id}`, kind: "street", at: w.path[0] };+ p = makePerson(id, role, f.folk, home, hash(`${f.seed}-${id}`));+ people.set(`${f.seed}-${id}`, p);+ }+ const v = walkSpeed(p, f.cold ?? 0.5) * (0.9 + 0.2 * r.next());+ // a round: out of a door, along the leg (and round its corner by up to SPILL metres), in at a door, a while+ // inside. The longest stretch sets the round; a shorter one leaves them inside longer that round.+ const most = longest(r.next(), len), round = most / v / OUT_SHARE;+ const tau = sec + r.next() * round, n = Math.floor(tau / round), u = tau - n * round;+ const { s0, dir, D } = stretchOf(rng(`${f.seed}-${id}-${n}`), a, b, doors, most);+ if (u > D / v) continue;+ let s = s0 + dir * v * u;+ if (w.closed) s = ((s % L) + L) % L;+ else if (s < 0 || s > L) continue; // walked off an open walk's end: turned into a street it doesn't cover+ const { at, tx, tz } = along(g, s, !!w.closed);+ // keep right: anywhere from the centreline to near the right-hand edge (a busy pavement is two loose streams,+ // not two files)+ const off = dir * (w.width ?? 3) * (0.04 + 0.38 * r.next());+ out.push({ id, walk: w.id, at: [at[0] - tz * off, at[1], at[2] + tx * off], facing: Math.atan2(dir * tx, dir * tz), person: p, s });}- const dir = r.next() < 0.5 ? 1 : -1, phase = r.next();- const v = walkSpeed(p, f.cold ?? 0.5) * (0.9 + 0.2 * r.next());- // along the line, wrapping at its ends (a walker leaves at one end as another comes in at the other)- let s = (((phase * L + dir * v * m * 60) % L) + L) % L, i = 0;- while (i < seg.length - 1 && s > seg[i]) s -= seg[i++];- const A = w.path[i], B = w.path[i + 1], u = seg[i] ? s / seg[i] : 0;- const tx = (B[0] - A[0]) / (seg[i] || 1), tz = (B[2] - A[2]) / (seg[i] || 1);- // keep right: off the centreline by a quarter of the width or so, to the walker's right- const off = dir * (w.width ?? 3) * (0.12 + 0.2 * r.next());- const at: V3 = [A[0] + (B[0] - A[0]) * u - tz * off, A[1] + (B[1] - A[1]) * u, A[2] + (B[2] - A[2]) * u + tx * off];- out.push({ id, walk: w.id, at, facing: Math.atan2(dir * tx, dir * tz), person: p });- }+ });}return out;}diff --git a/sdk/character/test/people.test.ts b/sdk/character/test/people.test.tsindex be78c34e..c7779c5b 100644--- a/sdk/character/test/people.test.ts+++ b/sdk/character/test/people.test.ts@@ -214,8 +214,9 @@ test("passers-by: as many as the line and the hour say, on it, keeping right, thconst walk: Walk = { id: "w", path: [[0, 0, 0], [200, 0, 0], [200, 0, 100]], perMetre: 0.3, width: 3 };const f = { roles, folk, mix, seed: "flow" };const at17 = passersBy([walk], 17 * 60, f), at3 = passersBy([walk], 3 * 60, f);- // 300 m at 0.3 a metre at the busiest hour (5 pm), a fiftieth of that at 3 am- expect(Math.abs(at17.length - 90)).toBeLessThan(15);+ // 300 m at 0.3 a metre at the busiest hour (5 pm) less those walked off the open ends; a fiftieth of that at 3 am+ expect(at17.length).toBeGreaterThan(65);+ expect(at17.length).toBeLessThan(105);expect(at3.length).toBeLessThan(8);expect(passersBy([walk], 17 * 60, f)).toEqual(at17);for (const p of at17) {@@ -228,11 +229,42 @@ test("passers-by: as many as the line and the hour say, on it, keeping right, thconst east = leg.filter((p) => Math.sin(p.facing) > 0.5), west = leg.filter((p) => Math.sin(p.facing) < -0.5);expect(east.length + west.length).toBe(leg.length);expect(east.every((p) => p.at[2] > 0) && west.every((p) => p.at[2] < 0)).toBe(true);- // a second later each has moved about a walking pace along the line+ // a second later each still out has moved about a walking pace along the lineconst later = new Map(passersBy([walk], 17 * 60 + 1 / 60, f).map((p) => [p.id, p]));- for (const p of leg.slice(0, 20)) {- const q = later.get(p.id)!;+ for (const p of leg) {+ const q = later.get(p.id);+ if (!q) continue;const d = Math.hypot(q.at[0] - p.at[0], q.at[2] - p.at[2]);- if (d < 5) expect(d).toBeGreaterThan(0.7), expect(d).toBeLessThan(1.8);+ expect(d).toBeGreaterThan(0.7);+ expect(d).toBeLessThan(1.8);}});++test("passers-by round a block: no one jumps; they come out of doors and go in at doors; a busy leg stays busy", () => {+ // a 100 x 60 block: the avenue side (y = 0, 100 m) busy, the other three quiet; doors every 12 m+ const ring: Walk = {+ id: "b", closed: true, width: 4, perMetre: 0.08,+ path: [[0, 0, 0], [100, 0, 0], [100, 0, 60], [0, 0, 60]],+ legs: [{ to: 100, perMetre: 0.5 }, { to: 160, perMetre: 0.08 }, { to: 260, perMetre: 0.08 }, { to: 320, perMetre: 0.08 }],+ doors: Array.from({ length: 26 }, (_, i) => 6 + i * 12),+ };+ const f = { roles, folk, mix, seed: "ring" };+ let seen = 0, came = 0, went = 0;+ const near = (p: { s: number }) => ring.doors!.some((d) => Math.abs(d - p.s) < 0.15 || Math.abs(Math.abs(d - p.s) - 320) < 0.15);+ for (let t = 0; t < 48; t++) {+ const m = 17 * 60 + t / (24 * 60), now = passersBy([ring], m, f), next = passersBy([ring], m + 1 / (24 * 60), f);+ const a = new Map(now.map((p) => [p.id, p])), b = new Map(next.map((p) => [p.id, p]));+ for (const [id, p] of a) {+ const q = b.get(id);+ if (q) { seen++; expect(Math.hypot(q.at[0] - p.at[0], q.at[2] - p.at[2])).toBeLessThan(0.12); }+ else { went++; }+ }+ for (const [id, q] of b) if (!a.has(id)) { came++; expect(near(q)).toBe(true); }+ }+ expect(seen).toBeGreaterThan(1000);+ expect(came + went).toBeGreaterThan(0);+ // the avenue side holds more a metre than the quiet sides+ const at = passersBy([ring], 17 * 60, f);+ const avenue = at.filter((p) => p.s < 100).length / 100, quiet = at.filter((p) => p.s >= 100).length / 220;+ expect(avenue).toBeGreaterThan(2.5 * quiet);+});