@adatechnology/game-destruction-three 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -269,6 +269,78 @@ function worldBoxOf(local, matrix, axis, from, to) {
269
269
  return box;
270
270
  }
271
271
 
272
+ // src/fragment-support.ts
273
+ var SUPPORT_SHARE = 1e-4;
274
+ var createSupport = () => ({ count: 0, x: 0, y: 0, z: 0, edgeX: 0, edgeZ: 0 });
275
+ function writeCorners(params) {
276
+ const { e, half, corners } = params;
277
+ for (let i = 0; i < 8; i++) {
278
+ const sx = i & 1 ? half.x : -half.x;
279
+ const sy = i & 2 ? half.y : -half.y;
280
+ const sz = i & 4 ? half.z : -half.z;
281
+ corners[3 * i] = e[0] * sx + e[4] * sy + e[8] * sz;
282
+ corners[3 * i + 1] = e[1] * sx + e[5] * sy + e[9] * sz;
283
+ corners[3 * i + 2] = e[2] * sx + e[6] * sy + e[10] * sz;
284
+ }
285
+ }
286
+ function findSupport(params) {
287
+ const { corners, support } = params;
288
+ let low = Infinity;
289
+ for (let i = 0; i < 8; i++) low = Math.min(low, corners[3 * i + 1]);
290
+ const level = low + SUPPORT_SHARE * params.reach;
291
+ let count = 0, x = 0, y = 0, z = 0, first = -1, edgeX = 0, edgeZ = 0;
292
+ for (let i = 0; i < 8; i++) {
293
+ if (corners[3 * i + 1] > level) continue;
294
+ count++;
295
+ x += corners[3 * i];
296
+ y += corners[3 * i + 1];
297
+ z += corners[3 * i + 2];
298
+ if (first < 0) first = i;
299
+ else if (count === 2) {
300
+ edgeX = corners[3 * i] - corners[3 * first];
301
+ edgeZ = corners[3 * i + 2] - corners[3 * first + 2];
302
+ }
303
+ }
304
+ const along2 = Math.hypot(edgeX, edgeZ);
305
+ support.count = count;
306
+ support.x = x / count;
307
+ support.y = y / count;
308
+ support.z = z / count;
309
+ support.edgeX = along2 > 0 ? edgeX / along2 : 0;
310
+ support.edgeZ = along2 > 0 ? edgeZ / along2 : 0;
311
+ }
312
+ function inertiaAbout(params) {
313
+ const { e, half, x, y, z } = params;
314
+ const ax = x * e[0] + y * e[1] + z * e[2];
315
+ const ay = x * e[4] + y * e[5] + z * e[6];
316
+ const az = x * e[8] + y * e[9] + z * e[10];
317
+ const xx = half.x * half.x, yy = half.y * half.y, zz = half.z * half.z;
318
+ return (ax * ax * (yy + zz) + ay * ay * (xx + zz) + az * az * (xx + yy)) / 3;
319
+ }
320
+ function applyInverseInertia(params) {
321
+ const { e, half, v } = params;
322
+ const xx = half.x * half.x, yy = half.y * half.y, zz = half.z * half.z;
323
+ const ax = 3 * (v.x * e[0] + v.y * e[1] + v.z * e[2]) / (yy + zz);
324
+ const ay = 3 * (v.x * e[4] + v.y * e[5] + v.z * e[6]) / (xx + zz);
325
+ const az = 3 * (v.x * e[8] + v.y * e[9] + v.z * e[10]) / (xx + yy);
326
+ v.x = ax * e[0] + ay * e[4] + az * e[8];
327
+ v.y = ax * e[1] + ay * e[5] + az * e[9];
328
+ v.z = ax * e[2] + ay * e[6] + az * e[10];
329
+ }
330
+ function turnToLanding(params) {
331
+ const { corners, pivot, axisX, axisZ } = params;
332
+ let first = Infinity;
333
+ for (let i = 0; i < 8; i++) {
334
+ const px = corners[3 * i] - pivot.x;
335
+ const py = corners[3 * i + 1] - pivot.y;
336
+ const pz = corners[3 * i + 2] - pivot.z;
337
+ if (py <= SUPPORT_SHARE * params.reach) continue;
338
+ const qy = axisZ * px - axisX * pz;
339
+ first = Math.min(first, Math.atan2(qy, py) + Math.PI / 2);
340
+ }
341
+ return first;
342
+ }
343
+
272
344
  // src/fragment-instances.ts
273
345
  import { kitOriginOf, markKitOrigin } from "@adatechnology/game-contracts";
274
346
  import { BATCHED_LAYER } from "@adatechnology/game-render-three";
@@ -766,6 +838,67 @@ function slerpInto(out, a, ai, b, bi, t) {
766
838
  out.set(x / length, y / length, z / length, w / length);
767
839
  }
768
840
 
841
+ // src/support-hull.ts
842
+ var cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
843
+ function planHull(points) {
844
+ const sorted = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
845
+ if (sorted.length < 3) return sorted;
846
+ const lower = [];
847
+ for (const p of sorted) {
848
+ while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) lower.pop();
849
+ lower.push(p);
850
+ }
851
+ const upper = [];
852
+ for (let i = sorted.length - 1; i >= 0; i--) {
853
+ const p = sorted[i];
854
+ while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) upper.pop();
855
+ upper.push(p);
856
+ }
857
+ return [...lower.slice(0, -1), ...upper.slice(0, -1)];
858
+ }
859
+ function toSegment(p, a, b) {
860
+ const dx = b[0] - a[0];
861
+ const dz = b[1] - a[1];
862
+ const length = dx * dx + dz * dz;
863
+ const t = length > 0 ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dz) / length)) : 0;
864
+ return Math.hypot(p[0] - a[0] - t * dx, p[1] - a[1] - t * dz);
865
+ }
866
+ function isOverSupport(points, centre, tolerance) {
867
+ const hull = planHull(points);
868
+ if (hull.length === 0) return false;
869
+ if (hull.length === 1) return Math.hypot(centre[0] - hull[0][0], centre[1] - hull[0][1]) <= tolerance;
870
+ let inside = hull.length >= 3;
871
+ let nearest = Infinity;
872
+ for (let i = 0; i < hull.length; i++) {
873
+ const a = hull[i];
874
+ const b = hull[(i + 1) % hull.length];
875
+ if (hull.length >= 3 && cross(a, b, centre) < 0) inside = false;
876
+ nearest = Math.min(nearest, toSegment(centre, a, b));
877
+ }
878
+ return inside || nearest <= tolerance;
879
+ }
880
+ function clipConvex(subject, window) {
881
+ let kept = [...subject];
882
+ for (let e = 0; e < window.length && kept.length > 0; e++) {
883
+ const a = window[e];
884
+ const b = window[(e + 1) % window.length];
885
+ const next = [];
886
+ for (let i = 0; i < kept.length; i++) {
887
+ const p = kept[i];
888
+ const q = kept[(i + 1) % kept.length];
889
+ const sp = cross(a, b, p);
890
+ const sq = cross(a, b, q);
891
+ if (sp >= 0) next.push(p);
892
+ if (sp >= 0 !== sq >= 0) {
893
+ const t = sp / (sp - sq);
894
+ next.push([p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t]);
895
+ }
896
+ }
897
+ kept = next;
898
+ }
899
+ return kept;
900
+ }
901
+
769
902
  // src/fragments.ts
770
903
  function rotationOf(q) {
771
904
  const { x, y, z, w } = q;
@@ -788,6 +921,20 @@ function shadowOf(e, half) {
788
921
  return [e[0] * half.x, e[2] * half.x, e[4] * half.y, e[6] * half.y, e[8] * half.z, e[10] * half.z];
789
922
  }
790
923
  var reachAlong = (shadow, nx, nz) => Math.abs(nx * shadow[0] + nz * shadow[1]) + Math.abs(nx * shadow[2] + nz * shadow[3]) + Math.abs(nx * shadow[4] + nz * shadow[5]);
924
+ function shadowPolygon(shadow, cx, cz) {
925
+ const corners = [];
926
+ for (let k = 0; k < 8; k++) {
927
+ const a = k & 1 ? 1 : -1, b = k & 2 ? 1 : -1, c = k & 4 ? 1 : -1;
928
+ corners.push([cx + a * shadow[0] + b * shadow[2] + c * shadow[4], cz + a * shadow[1] + b * shadow[3] + c * shadow[5]]);
929
+ }
930
+ return planHull(corners);
931
+ }
932
+ function contactsOf(mine, under, x, z) {
933
+ const own = shadowPolygon(mine, 0, 0);
934
+ const points = [];
935
+ for (const box of under) points.push(...clipConvex(own, shadowPolygon(box.shadow, box.x - x, box.z - z)));
936
+ return points;
937
+ }
791
938
  function shadowsOverlap(first, second, dx, dz) {
792
939
  for (const shadow of [first, second]) {
793
940
  for (let g = 0; g < 6; g += 2) {
@@ -838,8 +985,18 @@ var SWEEP_MIN_SPEED = 1;
838
985
  var WALL_RESTITUTION = 0.25;
839
986
  var WALL_GLIDE = 0.7;
840
987
  var TUMBLE_REACH = 0.1;
988
+ var GROUND_RESTITUTION = 0.25;
989
+ var GROUND_GLIDE = 0.7;
990
+ var GROUND_SPIN_KEPT = 0.55;
991
+ var BOUNCE_LIFT_SHARE = 0.05;
992
+ var ROUGHNESS_SHARE = 0.05;
993
+ var SETTLE_FALL_HALVES = 2;
994
+ var NUDGE_ENERGY_SHARE = 0.1;
995
+ var DEBRIS_BOUNCE_SPEED = 0.6;
996
+ var DEBRIS_LAY_RATE = 7;
841
997
  var LAID_GAP = 5e-3;
842
998
  var STAND_RATIO = 0.6;
999
+ var FADE_SECONDS = 0.6;
843
1000
  var SETTLE_SECONDS = 0.4;
844
1001
  var isSlow = (f) => f.vel.lengthSq() < 0.012 && f.ang.lengthSq() < 0.2;
845
1002
  var isBig = (size) => {
@@ -870,6 +1027,11 @@ var FragmentSystem = class {
870
1027
  __publicField(this, "v");
871
1028
  __publicField(this, "w");
872
1029
  __publicField(this, "rng", createRng(FRAGMENT_SEED));
1030
+ /** The corners of the fragment being stepped (`writeCorners`), what of them it rests on, its halves at its fade, and its centre over that support: kept between calls. */
1031
+ __publicField(this, "corners", new Float64Array(24));
1032
+ __publicField(this, "support", createSupport());
1033
+ __publicField(this, "halves", { x: 0, y: 0, z: 0 });
1034
+ __publicField(this, "lever", { x: 0, y: 0, z: 0 });
873
1035
  /** The fragments in flight drawn by instances, a draw per shared geometry and material (`FragmentInstances`). */
874
1036
  __publicField(this, "instances");
875
1037
  /** The instanced probe of the last `warmMeshes` (a new one each call, as the other probes are new meshes), freed by the next call or by `dispose`. */
@@ -1171,6 +1333,13 @@ var FragmentSystem = class {
1171
1333
  continue;
1172
1334
  }
1173
1335
  if (this.clock > f.deadline) {
1336
+ const held = this.heldUntil(f);
1337
+ if (held > f.deadline) {
1338
+ f.deadline = held;
1339
+ if (held < next) next = held;
1340
+ this.mortal[kept++] = f;
1341
+ continue;
1342
+ }
1174
1343
  f.isMortal = false;
1175
1344
  f.dying = true;
1176
1345
  f.mesh.matrixAutoUpdate = true;
@@ -1183,6 +1352,26 @@ var FragmentSystem = class {
1183
1352
  this.mortal.length = kept;
1184
1353
  this.nextDeadline = next;
1185
1354
  }
1355
+ /**
1356
+ * When `f` may start to fade: once every chip lying on it (light and small, its bottom on f's top, their floor areas sharing ground) has shrunk away; never while one that
1357
+ * never fades lies on it, a settling time from now while one still moves on it, and at `f.deadline` when nothing lies on it.
1358
+ */
1359
+ heldUntil(f) {
1360
+ const box = f.box ?? (f.box = restingBox(f));
1361
+ let until = f.deadline;
1362
+ for (const other of this.list) {
1363
+ if (other === f || other.rigid || other.body) continue;
1364
+ const on = other.box ?? (other.box = restingBox(other));
1365
+ const e = rotationOf(other.mesh.quaternion);
1366
+ const bottom = other.mesh.position.y - (Math.abs(e[1]) * other.half.x + Math.abs(e[5]) * other.half.y + Math.abs(e[9]) * other.half.z);
1367
+ if (bottom <= f.mesh.position.y || bottom > box.top + SUPPORT_GAP) continue;
1368
+ if (Math.abs(on.x - box.x) >= on.reachX + box.reachX || Math.abs(on.z - box.z) >= on.reachZ + box.reachZ) continue;
1369
+ if (!shadowsOverlap(box.shadow, on.shadow, on.x - box.x, on.z - box.z)) continue;
1370
+ const gone = other.dying ? this.clock + other.fade * FADE_SECONDS : other.asleep ? other.deadline + FADE_SECONDS : this.clock + SETTLE_SECONDS;
1371
+ until = Math.max(until, gone);
1372
+ }
1373
+ return until;
1374
+ }
1186
1375
  /** What lies inside the disc looks at the ground again: it was dug or heaped under it. */
1187
1376
  wakeRegion(x, z, radius) {
1188
1377
  for (const f of this.list) {
@@ -1201,7 +1390,7 @@ var FragmentSystem = class {
1201
1390
  const f = this.active[i];
1202
1391
  if (f.dying) {
1203
1392
  f.mesh.matrixAutoUpdate = true;
1204
- f.fade -= dt / 0.6;
1393
+ f.fade -= dt / FADE_SECONDS;
1205
1394
  if (f.fade <= 0) {
1206
1395
  this.instances.remove(f.mesh);
1207
1396
  this.o.scene.remove(f.mesh);
@@ -1350,36 +1539,22 @@ var FragmentSystem = class {
1350
1539
  if (mesh.position.y - ext < gy) {
1351
1540
  contact = true;
1352
1541
  mesh.position.y = gy + ext;
1353
- if (f.vel.y < -0.6) {
1354
- f.vel.y = -f.vel.y * 0.25;
1355
- f.vel.x *= 0.7;
1356
- f.vel.z *= 0.7;
1357
- f.ang.multiplyScalar(0.55);
1358
- const tumble = Math.min(1, TUMBLE_REACH / Math.max(h.x, h.y, h.z));
1359
- f.ang.x += (this.rng() - 0.5) * 2 * tumble;
1360
- f.ang.z += (this.rng() - 0.5) * 2 * tumble;
1542
+ if (pile > floor) {
1543
+ turning = this.restOnDebris(f, { g, dt, gy, sc, tip });
1361
1544
  } else {
1362
- f.vel.y = 0;
1363
- const sliding = Math.hypot(f.vel.x, f.vel.z);
1364
- const kept = sliding > 0 ? Math.max(0, sliding - DEBRIS_FRICTION * g * dt) / sliding : 0;
1365
- f.vel.x *= kept;
1366
- f.vel.z *= kept;
1367
- f.ang.multiplyScalar(Math.exp(-6 * dt));
1545
+ writeCorners({ e, half: this.scaledHalf(f, sc), corners: this.corners });
1546
+ findSupport({ corners: this.corners, reach: Math.max(h.x, h.y, h.z) * sc, support: this.support });
1547
+ if (!f.contact && this.bounces(f, { g, dt, sc })) {
1548
+ this.bounce(f, sc);
1549
+ } else {
1550
+ f.vel.y = 0;
1551
+ turning = this.restOn(f, { g, dt, sc });
1552
+ }
1368
1553
  }
1369
1554
  } else {
1370
1555
  f.ang.multiplyScalar(Math.exp(-0.15 * dt));
1371
1556
  if (f.air > 0) this.flutter(f, dt);
1372
1557
  }
1373
- if (contact && tip) {
1374
- f.vel.x += tip.x * TIP_SPEED * dt * 10;
1375
- f.vel.z += tip.z * TIP_SPEED * dt * 10;
1376
- f.ang.x += tip.z * TIP_TURN * dt * 10;
1377
- f.ang.z -= tip.x * TIP_TURN * dt * 10;
1378
- } else if (contact && f.vel.lengthSq() < 4) {
1379
- turning = this.settle(f, dt);
1380
- this.m.makeRotationFromQuaternion(mesh.quaternion);
1381
- mesh.position.y = gy + (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
1382
- }
1383
1558
  f.contact = contact;
1384
1559
  if (contact && !turning && isSlow(f)) {
1385
1560
  f.still += dt;
@@ -1432,7 +1607,8 @@ var FragmentSystem = class {
1432
1607
  for (const other of this.list) {
1433
1608
  if (other === f || other.dying) continue;
1434
1609
  const box = other.box ?? (other.box = restingBox(other));
1435
- if (box.top > bottom + PILE_STEP || box.top < top - PILE_STEP) continue;
1610
+ const beneath = !other.asleep && other.mesh.position.y < bottom;
1611
+ if (box.top > bottom + PILE_STEP && !beneath || box.top < top - PILE_STEP) continue;
1436
1612
  if (Math.abs(box.x - p.x) >= box.reachX + reachX || Math.abs(box.z - p.z) >= box.reachZ + reachZ) continue;
1437
1613
  if (!shadowsOverlap(mine, box.shadow, box.x - p.x, box.z - p.z)) continue;
1438
1614
  if (box.top > top + PILE_STEP) under.length = 0;
@@ -1441,13 +1617,16 @@ var FragmentSystem = class {
1441
1617
  }
1442
1618
  this.offEdge = null;
1443
1619
  if (under.length > 0 && !under.some((box) => box.top >= top - PILE_STEP && shadowsOverlap(POINT, box.shadow, box.x - p.x, box.z - p.z))) {
1444
- let x = 0, z = 0;
1445
- for (const box of under) {
1446
- x += p.x - box.x;
1447
- z += p.z - box.z;
1620
+ const contacts = contactsOf(mine, under, p.x, p.z);
1621
+ if (!isOverSupport(contacts, [0, 0], 0)) {
1622
+ let x = 0, z = 0;
1623
+ for (const [cx, cz] of contacts) {
1624
+ x -= cx;
1625
+ z -= cz;
1626
+ }
1627
+ const d = Math.hypot(x, z);
1628
+ this.offEdge = d > 1e-6 ? { x: x / d, z: z / d } : { x: 1, z: 0 };
1448
1629
  }
1449
- const d = Math.hypot(x, z);
1450
- this.offEdge = d > 1e-6 ? { x: x / d, z: z / d } : { x: 1, z: 0 };
1451
1630
  }
1452
1631
  return top;
1453
1632
  }
@@ -1459,8 +1638,41 @@ var FragmentSystem = class {
1459
1638
  f.ang.x += (this.rng() - 0.5) * SHEET_SWAY * 4 * dt;
1460
1639
  f.ang.z += (this.rng() - 0.5) * SHEET_SWAY * 4 * dt;
1461
1640
  }
1462
- /** Tip the fragment towards lying on its flattest face. True while its end is still more than `LAID_GAP` from lying. */
1463
- settle(f, dt) {
1641
+ /**
1642
+ * On other debris, by the pile's rules (`DEBRIS_BOUNCE_SPEED`): a bounce with a tumble by the speed of its end, Coulomb friction, its spin damped, off the edge of what holds
1643
+ * it when its centre is past it (`tip`), else laid flat (`layOnDebris`). True while it is still being laid flat.
1644
+ */
1645
+ restOnDebris(f, params) {
1646
+ const { g, dt, gy, sc, tip } = params;
1647
+ const h = f.half;
1648
+ if (f.vel.y < -DEBRIS_BOUNCE_SPEED) {
1649
+ f.vel.y = -f.vel.y * GROUND_RESTITUTION;
1650
+ f.vel.x *= GROUND_GLIDE;
1651
+ f.vel.z *= GROUND_GLIDE;
1652
+ f.ang.multiplyScalar(GROUND_SPIN_KEPT);
1653
+ const tumble = Math.min(1, TUMBLE_REACH / Math.max(h.x, h.y, h.z));
1654
+ f.ang.x += (this.rng() - 0.5) * 2 * tumble;
1655
+ f.ang.z += (this.rng() - 0.5) * 2 * tumble;
1656
+ } else {
1657
+ f.vel.y = 0;
1658
+ this.slide(f, g, dt);
1659
+ f.ang.multiplyScalar(Math.exp(-6 * dt));
1660
+ }
1661
+ if (tip) {
1662
+ f.vel.x += tip.x * TIP_SPEED * dt * 10;
1663
+ f.vel.z += tip.z * TIP_SPEED * dt * 10;
1664
+ f.ang.x += tip.z * TIP_TURN * dt * 10;
1665
+ f.ang.z -= tip.x * TIP_TURN * dt * 10;
1666
+ return false;
1667
+ }
1668
+ if (f.vel.lengthSq() >= 4) return false;
1669
+ const turning = this.layOnDebris(f, dt);
1670
+ const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
1671
+ f.mesh.position.y = gy + (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
1672
+ return turning;
1673
+ }
1674
+ /** Tip the fragment lying on other debris towards lying on its flattest face. True while its end is still more than `LAID_GAP` from lying. */
1675
+ layOnDebris(f, dt) {
1464
1676
  const mesh = f.mesh;
1465
1677
  this.m.makeRotationFromQuaternion(mesh.quaternion);
1466
1678
  const e = this.m.elements;
@@ -1480,9 +1692,204 @@ var FragmentSystem = class {
1480
1692
  this.w.set(0, 1, 0);
1481
1693
  this.dq.setFromUnitVectors(this.v, this.w);
1482
1694
  this.q.copy(this.dq).multiply(mesh.quaternion);
1483
- mesh.quaternion.slerp(this.q, clamp(dt * 7, 0, 1));
1695
+ mesh.quaternion.slerp(this.q, Math.min(1, dt * DEBRIS_LAY_RATE));
1484
1696
  return Math.sqrt(Math.max(0, 1 - this.v.y * this.v.y)) * Math.max(half[0], half[1], half[2]) > LAID_GAP;
1485
1697
  }
1698
+ /** The fragment's halves (m) at its fade, into `halves`. */
1699
+ scaledHalf(f, sc) {
1700
+ this.halves.x = f.half.x * sc;
1701
+ this.halves.y = f.half.y * sc;
1702
+ this.halves.z = f.half.z * sc;
1703
+ return this.halves;
1704
+ }
1705
+ /** The speed (m/s, up) of the point it rests on (`support`): its centre's, and its spin's about the centre there. */
1706
+ supportSpeed(f) {
1707
+ const s = this.support;
1708
+ return f.vel.y + f.ang.z * s.x - f.ang.x * s.z;
1709
+ }
1710
+ /**
1711
+ * It hits the floor hard enough to bounce: the point it comes down on goes into it faster than the rebound that lifts it `BOUNCE_LIFT_SHARE` of its thinnest half, and than one
1712
+ * step of gravity gives what lies on it.
1713
+ */
1714
+ bounces(f, params) {
1715
+ const { g, dt, sc } = params;
1716
+ const thinnest = Math.min(f.half.x, f.half.y, f.half.z) * sc;
1717
+ const least = Math.max(2 * g * dt, Math.sqrt(2 * g * BOUNCE_LIFT_SHARE * thinnest) / GROUND_RESTITUTION);
1718
+ return this.supportSpeed(f) < -least;
1719
+ }
1720
+ /**
1721
+ * The hit on the floor as an impulse at the point it comes down on (`support`): j = (1 + e) v / (1 + n · ((I⁻¹ (r × n)) × r)) per unit of mass, which sends the centre up
1722
+ * and turns it by its own inertia about that point. A box landing flat on a face is not turned; one coming down on a corner tumbles as much as its size and the hit give,
1723
+ * so a board metres long is turned as a long body is, not as a chip.
1724
+ */
1725
+ bounce(f, sc) {
1726
+ const s = this.support;
1727
+ const into = this.supportSpeed(f);
1728
+ const rough = ROUGHNESS_SHARE * Math.max(f.half.x, f.half.y, f.half.z) * sc;
1729
+ s.x += (this.rng() - 0.5) * 2 * rough;
1730
+ s.z += (this.rng() - 0.5) * 2 * rough;
1731
+ const turn2 = this.w.set(-s.z, 0, s.x);
1732
+ applyInverseInertia({ e: this.m.elements, half: this.scaledHalf(f, sc), v: turn2 });
1733
+ const share = 1 + (turn2.z * s.x - turn2.x * s.z);
1734
+ const impulse = -(1 + GROUND_RESTITUTION) * into / share;
1735
+ f.vel.y += impulse;
1736
+ f.ang.addScaledVector(turn2, impulse);
1737
+ f.vel.x *= GROUND_GLIDE;
1738
+ f.vel.z *= GROUND_GLIDE;
1739
+ f.ang.multiplyScalar(GROUND_SPIN_KEPT);
1740
+ }
1741
+ /**
1742
+ * Coulomb friction on the floor: the sliding speed falls by mu g each second, so a piece leaving at v slides v^2 / (2 mu g), as a body of the piece system does (a snapped
1743
+ * shovel handle thrown at 7.5 m/s comes to rest 5.7 m away, near the blade, not a metre from where it broke).
1744
+ */
1745
+ slide(f, g, dt) {
1746
+ const sliding = Math.hypot(f.vel.x, f.vel.z);
1747
+ const kept = sliding > 0 ? Math.max(0, sliding - DEBRIS_FRICTION * g * dt) / sliding : 0;
1748
+ f.vel.x *= kept;
1749
+ f.vel.z *= kept;
1750
+ }
1751
+ /**
1752
+ * On the floor and not bouncing: friction acts on its foot (the point it rests on), which slides or holds while the piece turns over it; it turns only as what it rests on lets
1753
+ * it (not at all on a face, only about the edge on an edge), its spin about the vertical falls by the friction under it, and gravity tips it over its foot (`settle`). True
1754
+ * while it is still turning over.
1755
+ */
1756
+ restOn(f, params) {
1757
+ const { g, dt, sc } = params;
1758
+ const s = this.support;
1759
+ const ang = f.ang;
1760
+ const dx = -s.x, dy = -s.y, dz = -s.z;
1761
+ f.vel.x -= ang.y * dz - ang.z * dy;
1762
+ f.vel.z -= ang.x * dy - ang.y * dx;
1763
+ this.slide(f, g, dt);
1764
+ const footX = f.vel.x;
1765
+ const footZ = f.vel.z;
1766
+ const half = this.scaledHalf(f, sc);
1767
+ const gyration = Math.sqrt(inertiaAbout({ e: this.m.elements, half, x: 0, y: 1, z: 0 }));
1768
+ if (gyration > 0) ang.y = Math.sign(ang.y) * Math.max(0, Math.abs(ang.y) - DEBRIS_FRICTION * g * dt / gyration);
1769
+ if (s.count >= 3) {
1770
+ ang.x = 0;
1771
+ ang.z = 0;
1772
+ } else if (s.count === 2) {
1773
+ const along2 = ang.x * s.edgeX + ang.z * s.edgeZ;
1774
+ ang.x = along2 * s.edgeX;
1775
+ ang.z = along2 * s.edgeZ;
1776
+ }
1777
+ const lever = this.lever;
1778
+ lever.x = dx;
1779
+ lever.y = dy;
1780
+ lever.z = dz;
1781
+ const slow = footX * footX + footZ * footZ < 2 * g * SETTLE_FALL_HALVES * Math.max(half.x, half.y, half.z);
1782
+ const turning = slow && this.settle(f, g, dt);
1783
+ f.vel.x = footX + (ang.y * lever.z - ang.z * lever.y);
1784
+ f.vel.y = ang.z * lever.x - ang.x * lever.z;
1785
+ f.vel.z = footZ + (ang.x * lever.y - ang.y * lever.x);
1786
+ return turning;
1787
+ }
1788
+ /**
1789
+ * Gravity tips it over what it rests on (`support`), as a rigid body tips over its foot: the turn m g × the lever from the foot to the centre (`lever`), over its inertia about
1790
+ * the foot (its own, more the lever squared), so the time to lie down goes as √(L / g), never at a fixed rate. On a face it does not turn; balanced on a face too narrow for
1791
+ * it, or on an edge, it is let over (`nudge`, which moves `lever` to that edge). Its turn this step stops where the face it falls onto reaches the floor, and that face then
1792
+ * holds it. True while it still turns.
1793
+ */
1794
+ settle(f, g, dt) {
1795
+ const s = this.support;
1796
+ const e = this.m.elements;
1797
+ const half = this.halves;
1798
+ const reach = Math.max(half.x, half.y, half.z);
1799
+ const lever = this.lever;
1800
+ let torqueX = g * lever.z;
1801
+ let torqueZ = -g * lever.x;
1802
+ if (s.count >= 3) {
1803
+ torqueX = 0;
1804
+ torqueZ = 0;
1805
+ } else if (s.count === 2) {
1806
+ const along2 = torqueX * s.edgeX + torqueZ * s.edgeZ;
1807
+ torqueX = along2 * s.edgeX;
1808
+ torqueZ = along2 * s.edgeZ;
1809
+ }
1810
+ const torque = Math.hypot(torqueX, torqueZ);
1811
+ const ang = f.ang;
1812
+ if (torque <= g * SUPPORT_SHARE * reach && ang.x === 0 && ang.z === 0) {
1813
+ if (s.count >= 3 && !this.isUnsteady(f)) return false;
1814
+ if (!this.nudge(f, g)) return false;
1815
+ } else if (torque > 0) {
1816
+ const axisX = torqueX / torque;
1817
+ const axisZ = torqueZ / torque;
1818
+ const along2 = lever.x * axisX + lever.z * axisZ;
1819
+ const inertia = inertiaAbout({ e, half, x: axisX, y: 0, z: axisZ }) + lever.x * lever.x + lever.y * lever.y + lever.z * lever.z - along2 * along2;
1820
+ ang.x += axisX * torque / inertia * dt;
1821
+ ang.z += axisZ * torque / inertia * dt;
1822
+ }
1823
+ const spin = Math.hypot(ang.x, ang.z);
1824
+ if (spin === 0) return false;
1825
+ const landing = turnToLanding({ corners: this.corners, pivot: { x: -lever.x, y: -lever.y, z: -lever.z }, axisX: ang.x / spin, axisZ: ang.z / spin, reach });
1826
+ if (spin * dt > landing) {
1827
+ ang.x *= landing / (spin * dt);
1828
+ ang.z *= landing / (spin * dt);
1829
+ }
1830
+ return true;
1831
+ }
1832
+ /**
1833
+ * It stands on a face it would not stay on: a sheet on its edge or a board on its end (the face narrower than `STAND_RATIO` of its height), or a big piece on any face but
1834
+ * its flattest.
1835
+ */
1836
+ isUnsteady(f) {
1837
+ const e = this.m.elements;
1838
+ const up = [Math.abs(e[1]), Math.abs(e[5]), Math.abs(e[9])];
1839
+ const half = [f.half.x, f.half.y, f.half.z];
1840
+ let small = 0;
1841
+ let standing = 0;
1842
+ for (let i = 1; i < 3; i++) {
1843
+ if (half[i] < half[small]) small = i;
1844
+ if (up[i] > up[standing]) standing = i;
1845
+ }
1846
+ if (f.body) return half[standing] > half[small] * (1 + SUPPORT_SHARE);
1847
+ const across = Math.min(...half.filter((_, i) => i !== standing));
1848
+ return across < STAND_RATIO * half[standing];
1849
+ }
1850
+ /**
1851
+ * Let over the edge towards lying on its flattest face, from balance: the spin about that edge that lifts its centre over it with `NUDGE_ENERGY_SHARE` to spare (½ I ω² =
1852
+ * m g Δh), so it falls from there by its own weight, as a piece a breath tipped over. The edge becomes the lever (`lever`). False when there is no way down to that face.
1853
+ */
1854
+ nudge(f, g) {
1855
+ const e = this.m.elements;
1856
+ const half = this.halves;
1857
+ const tolerance = SUPPORT_SHARE * Math.max(half.x, half.y, half.z);
1858
+ let small = 0;
1859
+ if (half.y < half.x) small = 1;
1860
+ if (half.z < (small === 1 ? half.y : half.x)) small = 2;
1861
+ const sign = e[small * 4 + 1] >= 0 ? 1 : -1;
1862
+ const axisX = -e[small * 4 + 2] * sign;
1863
+ const axisZ = e[small * 4] * sign;
1864
+ const length = Math.hypot(axisX, axisZ);
1865
+ if (length < SUPPORT_SHARE) return false;
1866
+ const kx = axisX / length;
1867
+ const kz = axisZ / length;
1868
+ const c = this.corners;
1869
+ const floor = this.support.y + tolerance;
1870
+ let far2 = -Infinity;
1871
+ for (let i = 0; i < 8; i++) if (c[3 * i + 1] <= floor) far2 = Math.max(far2, kx * c[3 * i + 2] - kz * c[3 * i]);
1872
+ let count = 0, x = 0, y = 0, z = 0;
1873
+ for (let i = 0; i < 8; i++) {
1874
+ if (c[3 * i + 1] > floor || kx * c[3 * i + 2] - kz * c[3 * i] < far2 - tolerance) continue;
1875
+ count++;
1876
+ x += c[3 * i];
1877
+ y += c[3 * i + 1];
1878
+ z += c[3 * i + 2];
1879
+ }
1880
+ const lever = this.lever;
1881
+ lever.x = -x / count;
1882
+ lever.y = -y / count;
1883
+ lever.z = -z / count;
1884
+ const along2 = lever.x * kx + lever.z * kz;
1885
+ const square = lever.x * lever.x + lever.y * lever.y + lever.z * lever.z - along2 * along2;
1886
+ const rise = Math.max(Math.sqrt(square) - lever.y, tolerance);
1887
+ const inertia = inertiaAbout({ e, half, x: kx, y: 0, z: kz }) + square;
1888
+ const spinOfInertia = Math.sqrt(2 * g * rise * (1 + NUDGE_ENERGY_SHARE) / inertia);
1889
+ f.ang.x = kx * spinOfInertia;
1890
+ f.ang.z = kz * spinOfInertia;
1891
+ return true;
1892
+ }
1486
1893
  /** Remove the fragments of one object (after `restore`). */
1487
1894
  removeHome(home) {
1488
1895
  for (let i = this.list.length - 1; i >= 0; i--) {
@@ -3938,6 +4345,10 @@ var TILE_KEY = 65536;
3938
4345
  var TILE_KEY_ORIGIN = 32768;
3939
4346
  var MU_STATIC = 0.7;
3940
4347
  var ROLLING_RESISTANCE = 0.015;
4348
+ var roundPartsRadius = (parts) => {
4349
+ if (!parts || parts.length === 0 || !parts.every((part) => part.round && !part.hull)) return void 0;
4350
+ return Math.max(...parts.map((part) => (part.half[0] + part.half[2]) / 2));
4351
+ };
3941
4352
  var MU_DYNAMIC = 0.6;
3942
4353
  var GROUND = { frictionStatic: MU_STATIC, frictionDynamic: MU_DYNAMIC, restitution: 0, rolling: ROLLING_RESISTANCE };
3943
4354
  var tileRollingAt = (rollingAt, x, z) => rollingAt((Math.floor(x / TILE) + 0.5) * TILE, (Math.floor(z / TILE) + 0.5) * TILE);
@@ -4486,9 +4897,13 @@ var EnginePieces = class {
4486
4897
  out[round.axis] = about;
4487
4898
  return out;
4488
4899
  }
4489
- /** A round piece rolls to a stop by its surface's rolling resistance (the adapter's rule; the engine has none). */
4900
+ /**
4901
+ * A round piece rolls to a stop by its surface's rolling resistance (the adapter's rule; the engine has none). Round is its shape: the `round` of the piece, or parts
4902
+ * that are all cylinders (a valve knob cut off a shower), which roll on the widest of them; without it such a knob rolled 9 m and was still rolling 8 s later.
4903
+ */
4490
4904
  rollingOf(b) {
4491
- return b.round ? { coefficient: b.friction?.rolling ?? ROLLING_RESISTANCE, radius: b.round.radius, damping: b.friction?.rollingDamping ?? PIECE_ROLLING_DAMPING } : void 0;
4905
+ const radius = b.round?.radius ?? roundPartsRadius(b.partBoxes);
4906
+ return radius === void 0 ? void 0 : { coefficient: b.friction?.rolling ?? ROLLING_RESISTANCE, radius, damping: b.friction?.rollingDamping ?? PIECE_ROLLING_DAMPING };
4492
4907
  }
4493
4908
  material(b) {
4494
4909
  return {
@@ -5450,46 +5865,6 @@ var resolvePhysics = (option) => {
5450
5865
  return { loop: createPhysicsLoop(option), stepsItself: true };
5451
5866
  };
5452
5867
 
5453
- // src/support-hull.ts
5454
- var cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
5455
- function planHull(points) {
5456
- const sorted = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
5457
- if (sorted.length < 3) return sorted;
5458
- const lower = [];
5459
- for (const p of sorted) {
5460
- while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) lower.pop();
5461
- lower.push(p);
5462
- }
5463
- const upper = [];
5464
- for (let i = sorted.length - 1; i >= 0; i--) {
5465
- const p = sorted[i];
5466
- while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) upper.pop();
5467
- upper.push(p);
5468
- }
5469
- return [...lower.slice(0, -1), ...upper.slice(0, -1)];
5470
- }
5471
- function toSegment(p, a, b) {
5472
- const dx = b[0] - a[0];
5473
- const dz = b[1] - a[1];
5474
- const length = dx * dx + dz * dz;
5475
- const t = length > 0 ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dz) / length)) : 0;
5476
- return Math.hypot(p[0] - a[0] - t * dx, p[1] - a[1] - t * dz);
5477
- }
5478
- function isOverSupport(points, centre, tolerance) {
5479
- const hull = planHull(points);
5480
- if (hull.length === 0) return false;
5481
- if (hull.length === 1) return Math.hypot(centre[0] - hull[0][0], centre[1] - hull[0][1]) <= tolerance;
5482
- let inside = hull.length >= 3;
5483
- let nearest = Infinity;
5484
- for (let i = 0; i < hull.length; i++) {
5485
- const a = hull[i];
5486
- const b = hull[(i + 1) % hull.length];
5487
- if (hull.length >= 3 && cross(a, b, centre) < 0) inside = false;
5488
- nearest = Math.min(nearest, toSegment(centre, a, b));
5489
- }
5490
- return inside || nearest <= tolerance;
5491
- }
5492
-
5493
5868
  // src/pieces.ts
5494
5869
  var BALANCE_TOLERANCE = 0.01;
5495
5870
  var MU_STATIC2 = 0.7;
@@ -8949,7 +9324,8 @@ var UNDERMINED_RIM_RISE = 0.1;
8949
9324
  var UNDERMINED_STAND = 0.4;
8950
9325
  var UNDERMINED_KEEP = 0.6;
8951
9326
  var SOLIDS_MAX = 64;
8952
- var UNDERMINED_PUSH = 0.6;
9327
+ var UNDERMINED_FROUDE = 0.13;
9328
+ var GRAVITY2 = 9.81;
8953
9329
  var DEBRIS_ID = "debris#";
8954
9330
  var UNDERMINED_STILL = 0.05;
8955
9331
  var SOLID_WELD = 1e-4;
@@ -10809,13 +11185,13 @@ var Renderer = class {
10809
11185
  }
10810
11186
  /**
10811
11187
  * Plays the effect the event map names for a destruction event (`data/event-effects.json` of `game-vfx`, or `RendererOptions.eventEffects`) at a point.
10812
- * `play` gives the family of the material (the map's `{material}`) and the colours of its debris and dust. Answers whether one played, so the caller can
10813
- * leave out the burst it replaces.
11188
+ * `play` gives the family of the material (the map's `{material}`) and the colours of its debris and dust, `normal` the face it happened on (world). Answers
11189
+ * whether one played, so the caller can leave out the burst it replaces.
10814
11190
  */
10815
- playEvent(event, point, play) {
11191
+ playEvent(event, point, play, normal) {
10816
11192
  const id = effectForEvent(play && event.material === void 0 ? { ...event, material: play.material } : event, this.o.eventEffects ?? EVENT_EFFECTS_MAP);
10817
11193
  if (!id) return false;
10818
- return this.vfx.play(id, point, { seed: hashNumbers(point[0], point[1], point[2], this.time), ...play ? { values: play.values } : {} }) !== null;
11194
+ return this.vfx.play(id, point, { seed: hashNumbers(point[0], point[1], point[2], this.time), ...play ? { values: play.values } : {}, ...normal ? { normal } : {} }) !== null;
10819
11195
  }
10820
11196
  /** The family and colours of a part's material, for the effects of the event map. */
10821
11197
  partPlay(objectId, partId) {
@@ -10835,7 +11211,7 @@ var Renderer = class {
10835
11211
  const core = def?.visual?.innerColor;
10836
11212
  const look = debrisLook(def, "spall", { partColor: this.partColor(pv, def) ?? void 0 });
10837
11213
  if (core !== void 0 || look.impact === void 0) this.vfx.burst(look.debris, ev.point, [normal.x, normal.y, normal.z], 5, 2.5, { color: look.color });
10838
- else this.playEvent({ kind: "spall", material: look.impact }, ev.point);
11214
+ else this.playEvent({ kind: "spall", material: look.impact }, ev.point, void 0, [normal.x, normal.y, normal.z]);
10839
11215
  if (core !== void 0) this.trickleFrom(ev.point, [normal.x, normal.y, normal.z], core, mv);
10840
11216
  if (mv.mesh.userData.drawnSpalls?.includes(ev.part)) return;
10841
11217
  this.visuals.addSpall(mv, wp, normal, Math.sqrt(ev.area / Math.PI), core ?? fallbackColor(def));
@@ -11555,7 +11931,7 @@ var Renderer = class {
11555
11931
  body.sleeping = false;
11556
11932
  body.still = 0;
11557
11933
  body.calmAt = null;
11558
- if (held.length > 0 && length > 1e-6) this.pieceSys.ruleImpulse(KIT_RULE.CHECK_UNDERMINED, body, [0, -body.mass * UNDERMINED_PUSH, 0], [centre[0] + gx, floor, centre[2] + gz]);
11934
+ if (held.length > 0 && length > 1e-6) this.pieceSys.ruleImpulse(KIT_RULE.CHECK_UNDERMINED, body, [0, -body.mass * startDownSpeed(centre[1] - floor), 0], [centre[0] + gx, floor, centre[2] + gz]);
11559
11935
  if (!was) this.o.onLoosened?.(b.id, body, "ground");
11560
11936
  }
11561
11937
  }
@@ -11572,7 +11948,8 @@ var Renderer = class {
11572
11948
  if (this.standsWith(held, null, centre, feet.floor)) return;
11573
11949
  const inside = insideBy(convexHull(held), [centre[0], centre[2]]);
11574
11950
  const reach = this.reachAlong(body, inside.out.x, inside.out.z);
11575
- this.pieceSys.ruleImpulse(KIT_RULE.GO_OVER_IF_BALANCED, body, [0, -body.mass * UNDERMINED_PUSH, 0], [centre[0] + inside.out.x * reach, feet.floor, centre[2] + inside.out.z * reach]);
11951
+ const mid = this.pieceSys.centerOf(body, new this.THREE.Vector3());
11952
+ this.pieceSys.ruleImpulse(KIT_RULE.GO_OVER_IF_BALANCED, body, [0, -body.mass * startDownSpeed(centre[1] - feet.floor), 0], [mid.x + inside.out.x * reach, feet.floor, mid.z + inside.out.z * reach]);
11576
11953
  }
11577
11954
  /** How far (m) the box of a piece reaches from its middle along the level direction (x, z), turned as it is: the support of its box that way. */
11578
11955
  reachAlong(body, x, z) {
@@ -12637,6 +13014,9 @@ function convexHull(points) {
12637
13014
  };
12638
13015
  return [...half(sorted), ...half([...sorted].reverse())];
12639
13016
  }
13017
+ function startDownSpeed(height) {
13018
+ return UNDERMINED_FROUDE * Math.sqrt(GRAVITY2 * height);
13019
+ }
12640
13020
  function centreOfBoxes(boxes) {
12641
13021
  const sum = [0, 0, 0];
12642
13022
  let total = 0;