SOURCE / PINNED RELEASE
Made of little things.
Drift City
- Release
- 098731aa0f0f…
- Author-recorded commit
- 2822ea0a4f5b…
- License
- LICENSE
- Author’s source reference
- nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-a4c0039c855
Archive hash verified: b3323e408235502b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.
import * as THREE from 'three';
import { TUMBLES, type Prop, type World } from './world.js';
/**
* Things knocked loose, in two styles:
*
* - Toppling: posts, signs, trees, hydrants, fences tip over away from the car about their
* foot, sliding a little, bounce once as they land and lie where they fell. Bushes are
* light, so they're flung further and roll right over.
* - Tumbling: light things (cones, boxes, trash cans, benches, hay bales) and panels torn
* off cars fly off spinning, bounce along the ground and come to rest.
*/
interface Topple {
obj: THREE.Object3D;
prop: Prop;
vx: number;
vz: number;
axis: THREE.Vector3;
angle: number;
spin: number;
rest: number;
settled: boolean;
}
interface Tumble {
obj: THREE.Object3D;
vel: THREE.Vector3;
spin: THREE.Vector3;
/** How far its centre sits above the ground when it's lying there. */
lie: number;
settled: boolean;
}
const MAX = 220;
const GRAVITY = 16;
const _q = new THREE.Quaternion();
const _v = new THREE.Vector3();
const _box = new THREE.Box3();
function dispose(obj: THREE.Object3D): void {
obj.removeFromParent();
if (obj.userData.disposable && obj instanceof THREE.Mesh) obj.geometry.dispose();
}
export class Debris {
readonly root = new THREE.Group();
private readonly topples: Topple[] = [];
private readonly tumbles: Tumble[] = [];
/** A prop knocked by something moving at (vx, vz): it topples or tumbles, by its kind. */
add(obj: THREE.Object3D, prop: Prop, vx: number, vz: number): void {
if (TUMBLES.has(prop.kind)) {
const speed = Math.hypot(vx, vz);
// Light things go flying, ahead of the car and up; heavy ones (benches, hay) less so.
const heavy = prop.kind === 'bench' || prop.kind === 'hay';
const k = heavy ? 0.55 : 1.05;
const up = Math.min(heavy ? 3 : 8, 1.5 + speed * (heavy ? 0.12 : 0.28));
const r = () => (Math.random() - 0.5) * 2;
this.fling(obj, new THREE.Vector3(vx * k + r() * 1.5, up, vz * k + r() * 1.5), new THREE.Vector3(r() * 9, r() * 6, r() * 9).multiplyScalar(heavy ? 0.4 : 1));
return;
}
const speed = Math.hypot(vx, vz) || 1;
const dx = vx / speed;
const dz = vz / speed;
// Tipping towards d means turning about up × d.
const axis = new THREE.Vector3(dz, 0, -dx);
const bush = prop.kind === 'bush';
const fling = bush ? 0.55 : prop.kind === 'lamp' || prop.kind === 'hydrant' ? 0.2 : 0.3;
this.root.add(obj);
this.topples.push({
obj,
prop,
vx: vx * fling,
vz: vz * fling,
axis,
angle: 0,
spin: ((bush ? 1.2 : 0.35) * Math.min(speed, 30)) / Math.max(1, prop.h),
rest: bush ? Math.PI * (0.75 + Math.random() * 0.2) : Math.PI / 2 - 0.08,
settled: false,
});
this.trim();
}
/**
* Send something flying with a velocity and a spin (radians a second about each axis).
* It's re-centred on its middle first, so it tumbles about its middle.
*/
fling(obj: THREE.Object3D, vel: THREE.Vector3, spin: THREE.Vector3): void {
if (obj instanceof THREE.Mesh && obj.userData.disposable) {
obj.geometry.computeBoundingBox();
const c = obj.geometry.boundingBox!.getCenter(_v);
obj.geometry.translate(-c.x, -c.y, -c.z);
obj.position.add(c);
}
this.root.attach(obj);
_box.setFromObject(obj);
const size = _box.getSize(_v);
this.tumbles.push({ obj, vel, spin, lie: Math.max(0.05, Math.min(size.x, size.y, size.z) / 2), settled: false });
this.trim();
}
private trim(): void {
while (this.topples.length + this.tumbles.length > MAX) {
const old = this.topples.length > this.tumbles.length ? this.topples.shift() : this.tumbles.shift();
if (old) dispose(old.obj);
}
}
update(dt: number, world: World): void {
for (const p of this.topples) {
if (p.settled) continue;
const m = p.obj;
m.position.x += p.vx * dt;
m.position.z += p.vz * dt;
const drag = Math.exp(-2.5 * dt);
p.vx *= drag;
p.vz *= drag;
m.position.y = world.height(m.position.x, m.position.z, m.position.y + 1) - 0.1;
// Falls faster the further over it leans, like anything top-heavy.
p.spin += (14 / Math.max(1.5, p.prop.h)) * Math.max(0.3, Math.sin(p.angle + 0.12)) * dt;
p.angle += p.spin * dt;
if (p.angle < 0) {
p.angle = 0;
p.spin = Math.abs(p.spin) * 0.3;
}
if (p.angle >= p.rest) {
p.angle = p.rest;
p.spin *= -0.25;
if (Math.abs(p.spin) < 0.25 && Math.hypot(p.vx, p.vz) < 0.3) p.settled = true;
}
m.quaternion.copy(_q.setFromAxisAngle(p.axis, p.angle));
}
for (const t of this.tumbles) {
if (t.settled) continue;
const m = t.obj;
t.vel.y -= GRAVITY * dt;
m.position.addScaledVector(t.vel, dt);
const w = t.spin.length();
if (w > 1e-4) m.quaternion.premultiply(_q.setFromAxisAngle(_v.copy(t.spin).divideScalar(w), w * dt));
const floor = world.height(m.position.x, m.position.z, m.position.y + 1) + t.lie;
if (m.position.y < floor) {
m.position.y = floor;
if (t.vel.y < 0) t.vel.y *= -0.32;
t.vel.x *= 0.62;
t.vel.z *= 0.62;
t.spin.multiplyScalar(0.6);
if (Math.abs(t.vel.y) < 0.8 && Math.hypot(t.vel.x, t.vel.z) < 0.4) t.settled = true;
}
}
}
clear(): void {
for (const p of this.topples) dispose(p.obj);
for (const t of this.tumbles) dispose(t.obj);
this.topples.length = 0;
this.tumbles.length = 0;
}
}
