@adatechnology/game-destruction-three 1.3.0 → 1.3.2

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) {
@@ -809,7 +956,8 @@ function restingBox(f) {
809
956
  z: p.z,
810
957
  reachX: Math.abs(e[0]) * x + Math.abs(e[4]) * y + Math.abs(e[8]) * z,
811
958
  reachZ: Math.abs(e[2]) * x + Math.abs(e[6]) * y + Math.abs(e[10]) * z,
812
- top: p.y + Math.abs(e[1]) * x + Math.abs(e[5]) * y + Math.abs(e[9]) * z
959
+ top: p.y + Math.abs(e[1]) * x + Math.abs(e[5]) * y + Math.abs(e[9]) * z,
960
+ bottom: p.y - Math.abs(e[1]) * x - Math.abs(e[5]) * y - Math.abs(e[9]) * z
813
961
  };
814
962
  }
815
963
  var BUILDERS = {
@@ -832,14 +980,27 @@ var SUPPORT_GAP = 0.02;
832
980
  var PILE_STEP = 0.01;
833
981
  var TIP_SPEED = 0.6;
834
982
  var TIP_TURN = 3;
983
+ var RIDGE_TURN = Math.PI * (3 - Math.sqrt(5));
984
+ var SIDE_BY_SIDE = 1e-4;
835
985
  var POINT = [0, 0, 0, 0, 0, 0];
836
986
  var SHARD_SHAPES = 6;
837
987
  var SWEEP_MIN_SPEED = 1;
838
988
  var WALL_RESTITUTION = 0.25;
989
+ var ACROSS_MIN_STEP = 1e-4;
839
990
  var WALL_GLIDE = 0.7;
840
991
  var TUMBLE_REACH = 0.1;
992
+ var GROUND_RESTITUTION = 0.25;
993
+ var GROUND_GLIDE = 0.7;
994
+ var GROUND_SPIN_KEPT = 0.55;
995
+ var BOUNCE_LIFT_SHARE = 0.05;
996
+ var ROUGHNESS_SHARE = 0.05;
997
+ var SETTLE_FALL_HALVES = 2;
998
+ var NUDGE_ENERGY_SHARE = 0.1;
999
+ var DEBRIS_BOUNCE_SPEED = 0.6;
1000
+ var DEBRIS_LAY_RATE = 7;
841
1001
  var LAID_GAP = 5e-3;
842
1002
  var STAND_RATIO = 0.6;
1003
+ var FADE_SECONDS = 0.6;
843
1004
  var SETTLE_SECONDS = 0.4;
844
1005
  var isSlow = (f) => f.vel.lengthSq() < 0.012 && f.ang.lengthSq() < 0.2;
845
1006
  var isBig = (size) => {
@@ -870,6 +1031,13 @@ var FragmentSystem = class {
870
1031
  __publicField(this, "v");
871
1032
  __publicField(this, "w");
872
1033
  __publicField(this, "rng", createRng(FRAGMENT_SEED));
1034
+ /** 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. */
1035
+ __publicField(this, "corners", new Float64Array(24));
1036
+ /** The corners of `corners` from the lowest (`ridgeUnder`). */
1037
+ __publicField(this, "lowCorners", [0, 0, 0, 0, 0, 0, 0, 0]);
1038
+ __publicField(this, "support", createSupport());
1039
+ __publicField(this, "halves", { x: 0, y: 0, z: 0 });
1040
+ __publicField(this, "lever", { x: 0, y: 0, z: 0 });
873
1041
  /** The fragments in flight drawn by instances, a draw per shared geometry and material (`FragmentInstances`). */
874
1042
  __publicField(this, "instances");
875
1043
  /** 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`. */
@@ -881,8 +1049,20 @@ var FragmentSystem = class {
881
1049
  __publicField(this, "supportTurn", 0);
882
1050
  /** The pose before the last update and after it, of the fragments that were awake (`interpolate`); null when the option is off. */
883
1051
  __publicField(this, "trail");
1052
+ /** Fragments crushed so far under a moving piece that came down on them with no room left (`meetMover`): they fade where they were. */
1053
+ __publicField(this, "crushed", 0);
1054
+ /** The pieces moving this update (`FragmentSystemOptions.movers`). */
1055
+ __publicField(this, "moving", []);
1056
+ /** Where a fragment was when its step began, and that point carried by a moving piece. */
1057
+ __publicField(this, "before");
1058
+ __publicField(this, "carried");
1059
+ /** The move of a piece since the last update, and its inverse. */
1060
+ __publicField(this, "shift");
1061
+ __publicField(this, "unshift");
884
1062
  __publicField(this, "markGeometry", null);
885
1063
  __publicField(this, "markMaterial", null);
1064
+ /** The floor under the fragment `pileUnder` is asked about (`floorUnder`, or the ground when that is all that was measured): what two pieces at one height must both stand on. */
1065
+ __publicField(this, "pileFloor", 0);
886
1066
  /** Set by `pileUnder`: the way off the edge when the fragment's centre is past what it lies on, else null. */
887
1067
  __publicField(this, "offEdge", null);
888
1068
  this.box = new THREE.BoxGeometry(1, 1, 1);
@@ -891,6 +1071,10 @@ var FragmentSystem = class {
891
1071
  this.m = new THREE.Matrix4();
892
1072
  this.v = new THREE.Vector3();
893
1073
  this.w = new THREE.Vector3();
1074
+ this.before = new THREE.Vector3();
1075
+ this.carried = new THREE.Vector3();
1076
+ this.shift = new THREE.Matrix4();
1077
+ this.unshift = new THREE.Matrix4();
894
1078
  this.instances = new FragmentInstances(THREE, o.scene);
895
1079
  this.trail = o.interpolate ? new PoseTrail() : null;
896
1080
  }
@@ -1059,6 +1243,8 @@ var FragmentSystem = class {
1059
1243
  const live = {
1060
1244
  mesh,
1061
1245
  floor: null,
1246
+ ridgeTries: 0,
1247
+ ridgeWay: null,
1062
1248
  home,
1063
1249
  vel: new this.THREE.Vector3(f.velocity[0], f.velocity[1], f.velocity[2]),
1064
1250
  ang: new this.THREE.Vector3(f.angularVelocity[0], f.angularVelocity[1], f.angularVelocity[2]),
@@ -1171,6 +1357,13 @@ var FragmentSystem = class {
1171
1357
  continue;
1172
1358
  }
1173
1359
  if (this.clock > f.deadline) {
1360
+ const held = this.heldUntil(f);
1361
+ if (held > f.deadline) {
1362
+ f.deadline = held;
1363
+ if (held < next) next = held;
1364
+ this.mortal[kept++] = f;
1365
+ continue;
1366
+ }
1174
1367
  f.isMortal = false;
1175
1368
  f.dying = true;
1176
1369
  f.mesh.matrixAutoUpdate = true;
@@ -1183,6 +1376,26 @@ var FragmentSystem = class {
1183
1376
  this.mortal.length = kept;
1184
1377
  this.nextDeadline = next;
1185
1378
  }
1379
+ /**
1380
+ * 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
1381
+ * 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.
1382
+ */
1383
+ heldUntil(f) {
1384
+ const box = f.box ?? (f.box = restingBox(f));
1385
+ let until = f.deadline;
1386
+ for (const other of this.list) {
1387
+ if (other === f || other.rigid || other.body) continue;
1388
+ const on = other.box ?? (other.box = restingBox(other));
1389
+ const e = rotationOf(other.mesh.quaternion);
1390
+ 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);
1391
+ if (bottom <= f.mesh.position.y || bottom > box.top + SUPPORT_GAP) continue;
1392
+ if (Math.abs(on.x - box.x) >= on.reachX + box.reachX || Math.abs(on.z - box.z) >= on.reachZ + box.reachZ) continue;
1393
+ if (!shadowsOverlap(box.shadow, on.shadow, on.x - box.x, on.z - box.z)) continue;
1394
+ const gone = other.dying ? this.clock + other.fade * FADE_SECONDS : other.asleep ? other.deadline + FADE_SECONDS : this.clock + SETTLE_SECONDS;
1395
+ until = Math.max(until, gone);
1396
+ }
1397
+ return until;
1398
+ }
1186
1399
  /** What lies inside the disc looks at the ground again: it was dug or heaped under it. */
1187
1400
  wakeRegion(x, z, radius) {
1188
1401
  for (const f of this.list) {
@@ -1193,7 +1406,9 @@ var FragmentSystem = class {
1193
1406
  update(dt) {
1194
1407
  this.tick++;
1195
1408
  this.clock += dt;
1409
+ this.moving = this.o.movers?.() ?? [];
1196
1410
  this.wakeUnsupported();
1411
+ this.wakeMoved();
1197
1412
  this.wakeRigid();
1198
1413
  this.beginTick();
1199
1414
  const g = this.o.gravity;
@@ -1201,7 +1416,7 @@ var FragmentSystem = class {
1201
1416
  const f = this.active[i];
1202
1417
  if (f.dying) {
1203
1418
  f.mesh.matrixAutoUpdate = true;
1204
- f.fade -= dt / 0.6;
1419
+ f.fade -= dt / FADE_SECONDS;
1205
1420
  if (f.fade <= 0) {
1206
1421
  this.instances.remove(f.mesh);
1207
1422
  this.o.scene.remove(f.mesh);
@@ -1227,6 +1442,23 @@ var FragmentSystem = class {
1227
1442
  if (f.rigid) this.o.rigid?.remove(f.rigid);
1228
1443
  f.rigid = null;
1229
1444
  }
1445
+ /** A fragment asleep in the box of a piece that moves wakes in the same update: what it lay on, or what lies on it, is going. */
1446
+ wakeMoved() {
1447
+ if (this.moving.length === 0) return;
1448
+ for (const f of this.list) if (f.asleep && !f.dying && !f.rigid && this.nearAnyMover(f)) this.rouse(f);
1449
+ }
1450
+ /** Whether the fragment is in the box of the moving piece (or within its reach of it). */
1451
+ nearMover(f, mover) {
1452
+ const p = f.mesh.position;
1453
+ const reach = Math.max(f.half.x, f.half.y, f.half.z);
1454
+ const b = mover.box;
1455
+ return p.x >= b[0] - reach && p.x <= b[3] + reach && p.y >= b[1] - reach && p.y <= b[4] + reach && p.z >= b[2] - reach && p.z <= b[5] + reach;
1456
+ }
1457
+ /** Whether the fragment is in the box of any piece moving this update. */
1458
+ nearAnyMover(f) {
1459
+ for (const mover of this.moving) if (this.nearMover(f, mover)) return true;
1460
+ return false;
1461
+ }
1230
1462
  /** The debris moved by a rigid body that the body's world woke (something fell on it, a blast, its ground dug): it is awake here too. */
1231
1463
  wakeRigid() {
1232
1464
  for (const f of this.list) if (f.rigid && f.asleep && !f.dying && !f.rigid.sleeping) this.rouse(f);
@@ -1308,29 +1540,77 @@ var FragmentSystem = class {
1308
1540
  const ext = Math.abs(e[1]) * f.half.x + Math.abs(e[5]) * f.half.y + Math.abs(e[9]) * f.half.z;
1309
1541
  f.floor = null;
1310
1542
  const bottom = f.mesh.position.y - ext;
1311
- const pile = this.pileUnder(f, bottom);
1312
- const offEdge = this.offEdge;
1313
1543
  const ground = this.o.groundAt(f.mesh.position.x, f.mesh.position.z);
1544
+ this.pileFloor = ground;
1545
+ let pile = this.pileUnder(f, bottom);
1314
1546
  if (bottom <= ground + SUPPORT_GAP && pile <= ground) continue;
1315
1547
  const floor = this.floorUnder(f);
1548
+ if (floor > ground) {
1549
+ this.pileFloor = floor;
1550
+ pile = this.pileUnder(f, bottom);
1551
+ }
1552
+ const offEdge = this.offEdge;
1316
1553
  if (bottom > Math.max(floor, pile) + SUPPORT_GAP || pile > floor && offEdge) this.rouse(f);
1317
1554
  }
1318
1555
  }
1319
- /** What a fragment lands on: measured where it is, and again only once it moved `FLOOR_REMEASURE` m off that spot (a ray down is not free). */
1556
+ /** What a fragment lands on: measured where it is, and again only once it moved half its size (at most `FLOOR_REMEASURE` m) off that spot (a ray down is not free). */
1320
1557
  floorUnder(f) {
1321
1558
  const p = f.mesh.position;
1322
1559
  if (!this.o.surfaceAt) return this.o.groundAt(p.x, p.z);
1323
1560
  const at = f.floor;
1324
- if (!at || Math.abs(at.x - p.x) > FLOOR_REMEASURE || Math.abs(at.z - p.z) > FLOOR_REMEASURE || p.y < at.y - FLOOR_REMEASURE) {
1561
+ const away = Math.min(FLOOR_REMEASURE, Math.max(f.half.x, f.half.y, f.half.z));
1562
+ if (!at || Math.abs(at.x - p.x) > away || Math.abs(at.z - p.z) > away || p.y < at.y - FLOOR_REMEASURE) {
1325
1563
  f.floor = { x: p.x, z: p.z, y: this.o.surfaceAt(p.x, p.y, p.z) };
1326
1564
  }
1327
1565
  return f.floor.y;
1328
1566
  }
1567
+ /**
1568
+ * Whether the fragment, come down on `floor` (what is built under its centre), lies only on its centre: none of its four lowest corners is over anything that high, so it
1569
+ * balances on an edge narrower than itself. Then the way it slides off (towards its lowest corner, or one more `RIDGE_TURN` round each time it lands so again, kept while it
1570
+ * slides), else null.
1571
+ * Asked once per measure of the floor (`floorUnder`), at the first contact.
1572
+ */
1573
+ ridgeUnder(f, e, floor) {
1574
+ const at = f.floor;
1575
+ const surfaceAt = this.o.surfaceAt;
1576
+ if (!at || !surfaceAt) return null;
1577
+ if (at.ridge !== void 0) return at.ridge;
1578
+ at.ridge = null;
1579
+ const p = f.mesh.position;
1580
+ if (floor <= this.o.groundAt(p.x, p.z) + SUPPORT_GAP) return f.ridgeWay = null;
1581
+ const c = this.corners;
1582
+ writeCorners({ e, half: f.half, corners: c });
1583
+ const low = this.lowCorners;
1584
+ for (let i = 0; i < 8; i++) {
1585
+ let k = i;
1586
+ while (k > 0 && c[3 * low[k - 1] + 1] > c[3 * i + 1]) {
1587
+ low[k] = low[k - 1];
1588
+ k--;
1589
+ }
1590
+ low[k] = i;
1591
+ }
1592
+ for (let k = 0; k < 4; k++) {
1593
+ const i = low[k];
1594
+ if (surfaceAt(p.x + c[3 * i], p.y, p.z + c[3 * i + 2]) >= floor - SUPPORT_GAP) return f.ridgeWay = null;
1595
+ }
1596
+ if (f.ridgeWay) return at.ridge = f.ridgeWay;
1597
+ const lowest = low[0];
1598
+ const lx = c[3 * lowest], lz = c[3 * lowest + 2];
1599
+ const tilt = c[3 * low[3] + 1] - c[3 * lowest + 1];
1600
+ const d = Math.hypot(lx, lz);
1601
+ f.ridgeTries++;
1602
+ const angle = f.seq * RIDGE_TURN + f.ridgeTries * RIDGE_TURN;
1603
+ f.ridgeWay = tilt > SUPPORT_GAP && d > 1e-6 && f.ridgeTries === 1 ? { x: lx / d, z: lz / d } : { x: Math.cos(angle), z: Math.sin(angle) };
1604
+ return at.ridge = f.ridgeWay;
1605
+ }
1329
1606
  step(f, dt, g) {
1330
1607
  const mesh = f.mesh;
1331
1608
  f.box = void 0;
1332
1609
  f.vel.y -= g * dt;
1610
+ this.before.copy(mesh.position);
1333
1611
  if (!this.hitWall(f, dt)) mesh.position.addScaledVector(f.vel, dt);
1612
+ const stirred = this.nearAnyMover(f);
1613
+ if (stirred) f.floor = null;
1334
1614
  const w = f.ang.length();
1335
1615
  if (w > 1e-5) {
1336
1616
  this.dq.setFromAxisAngle(this.v.copy(f.ang).multiplyScalar(1 / w), w * dt);
@@ -1342,6 +1622,7 @@ var FragmentSystem = class {
1342
1622
  const sc = f.dying ? f.fade : 1;
1343
1623
  const ext = (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
1344
1624
  const floor = this.floorUnder(f);
1625
+ this.pileFloor = floor;
1345
1626
  const pile = this.pileUnder(f, mesh.position.y - ext + Math.max(0, -f.vel.y) * dt);
1346
1627
  const gy = Math.max(floor, pile);
1347
1628
  const tip = pile > floor ? this.offEdge : null;
@@ -1350,35 +1631,31 @@ var FragmentSystem = class {
1350
1631
  if (mesh.position.y - ext < gy) {
1351
1632
  contact = true;
1352
1633
  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;
1361
- } else {
1634
+ const ridge = pile > floor ? null : this.ridgeUnder(f, e, floor);
1635
+ if (pile > floor) {
1636
+ turning = this.restOnDebris(f, { g, dt, gy, sc, tip });
1637
+ } else if (ridge) {
1362
1638
  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));
1639
+ f.vel.x += ridge.x * TIP_SPEED * dt * 10;
1640
+ f.vel.z += ridge.z * TIP_SPEED * dt * 10;
1641
+ f.floor = null;
1642
+ turning = true;
1643
+ } else {
1644
+ writeCorners({ e, half: this.scaledHalf(f, sc), corners: this.corners });
1645
+ findSupport({ corners: this.corners, reach: Math.max(h.x, h.y, h.z) * sc, support: this.support });
1646
+ if (!f.contact && this.bounces(f, { g, dt, sc })) {
1647
+ this.bounce(f, sc);
1648
+ } else {
1649
+ f.vel.y = 0;
1650
+ turning = this.restOn(f, { g, dt, sc });
1651
+ }
1368
1652
  }
1369
1653
  } else {
1370
1654
  f.ang.multiplyScalar(Math.exp(-0.15 * dt));
1371
1655
  if (f.air > 0) this.flutter(f, dt);
1372
1656
  }
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;
1657
+ if (stirred) {
1658
+ for (const mover of this.moving) if (this.nearMover(f, mover)) this.meetMover(f, mover, dt);
1382
1659
  }
1383
1660
  f.contact = contact;
1384
1661
  if (contact && !turning && isSlow(f)) {
@@ -1395,11 +1672,12 @@ var FragmentSystem = class {
1395
1672
  hitWall(f, dt) {
1396
1673
  const sweep = this.o.sweep;
1397
1674
  if (!sweep) return false;
1398
- const speed = f.vel.length();
1399
- if (speed < SWEEP_MIN_SPEED) return false;
1400
- if (f.contact && f.vel.y <= 0 && Math.hypot(f.vel.x, f.vel.z) < SWEEP_MIN_SPEED) return false;
1675
+ const across = f.vel.length() < SWEEP_MIN_SPEED || f.contact && f.vel.y <= 0 && Math.hypot(f.vel.x, f.vel.z) < SWEEP_MIN_SPEED;
1676
+ const vy = across ? 0 : f.vel.y;
1677
+ const speed = Math.hypot(f.vel.x, vy, f.vel.z);
1678
+ if (speed * dt < ACROSS_MIN_STEP) return false;
1401
1679
  const p = f.mesh.position;
1402
- const dir = [f.vel.x / speed, f.vel.y / speed, f.vel.z / speed];
1680
+ const dir = [f.vel.x / speed, vy / speed, f.vel.z / speed];
1403
1681
  const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
1404
1682
  const reach = (x, y, z) => (Math.abs(e[0] * x + e[1] * y + e[2] * z) * f.half.x + Math.abs(e[4] * x + e[5] * y + e[6] * z) * f.half.y + Math.abs(e[8] * x + e[9] * y + e[10] * z) * f.half.z) * (f.dying ? f.fade : 1);
1405
1683
  const ahead = reach(dir[0], dir[1], dir[2]);
@@ -1416,6 +1694,68 @@ var FragmentSystem = class {
1416
1694
  f.floor = null;
1417
1695
  return true;
1418
1696
  }
1697
+ /**
1698
+ * The fragment's way this step as the moving piece sees it: from where the piece carried the fragment's start to where the fragment got. A face of the piece on that way
1699
+ * stops it on the face, moving as the piece moves there, with what it had across the face kept as off a wall: a panel coming down on a sheet takes it along.
1700
+ */
1701
+ meetMover(f, mover, dt) {
1702
+ const p = f.mesh.position;
1703
+ const shift = this.shift.fromArray(mover.delta);
1704
+ const from = this.carried.copy(this.before).applyMatrix4(shift);
1705
+ const rx = p.x - from.x, ry = p.y - from.y, rz = p.z - from.z;
1706
+ const span = Math.hypot(rx, ry, rz);
1707
+ if (span < 1e-6) return;
1708
+ const dir = [rx / span, ry / span, rz / span];
1709
+ const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
1710
+ const reach = (x, y, z) => (Math.abs(e[0] * x + e[1] * y + e[2] * z) * f.half.x + Math.abs(e[4] * x + e[5] * y + e[6] * z) * f.half.y + Math.abs(e[8] * x + e[9] * y + e[10] * z) * f.half.z) * (f.dying ? f.fade : 1);
1711
+ const hit = mover.sweep([from.x, from.y, from.z], dir, span + reach(dir[0], dir[1], dir[2]));
1712
+ if (!hit) return;
1713
+ const nx = hit.normal[0] ?? 0, ny = hit.normal[1] ?? 0, nz = hit.normal[2] ?? 0;
1714
+ if (rx * nx + ry * ny + rz * nz >= 0) return;
1715
+ const px = hit.point[0] ?? p.x, py = hit.point[1] ?? p.y, pz = hit.point[2] ?? p.z;
1716
+ const was = this.v.set(px, py, pz).applyMatrix4(this.unshift.copy(shift).invert());
1717
+ const cx = (px - was.x) / dt, cy = (py - was.y) / dt, cz = (pz - was.z) / dt;
1718
+ this.alongFace(f, nx, ny, nz);
1719
+ const off2 = Math.min(f.half.x, f.half.y, f.half.z) * (f.dying ? f.fade : 1) + 2e-3;
1720
+ const qx = px + nx * off2, qy = py + ny * off2, qz = pz + nz * off2;
1721
+ if (this.pinched(from, qx, qy, qz, off2)) {
1722
+ p.copy(from);
1723
+ f.vel.set(cx, cy, cz);
1724
+ this.crushed++;
1725
+ f.dying = true;
1726
+ this.activate(f);
1727
+ return;
1728
+ }
1729
+ p.set(qx, qy, qz);
1730
+ const ux = f.vel.x - cx, uy = f.vel.y - cy, uz = f.vel.z - cz;
1731
+ const inward = Math.min(0, ux * nx + uy * ny + uz * nz);
1732
+ const tx = ux - inward * nx, ty = uy - inward * ny, tz = uz - inward * nz;
1733
+ f.vel.set(cx + tx * WALL_GLIDE - inward * WALL_RESTITUTION * nx, cy + ty * WALL_GLIDE - inward * WALL_RESTITUTION * ny, cz + tz * WALL_GLIDE - inward * WALL_RESTITUTION * nz);
1734
+ f.ang.multiplyScalar(0.55);
1735
+ }
1736
+ /**
1737
+ * Whether a fragment the piece carried to `p` and pushed on to (x, y, z), `off` its reach from the face, gets there through something else or ends below what is under it there.
1738
+ */
1739
+ pinched(p, x, y, z, off2) {
1740
+ const floor = this.o.surfaceAt ? this.o.surfaceAt(x, y, z) : this.o.groundAt(x, z);
1741
+ if (y - off2 < floor) return true;
1742
+ const sweep = this.o.sweep;
1743
+ const dx = x - p.x, dy = y - p.y, dz = z - p.z;
1744
+ const length = Math.hypot(dx, dy, dz);
1745
+ if (!sweep || length < 1e-6) return false;
1746
+ return sweep([p.x, p.y, p.z], [dx / length, dy / length, dz / length], length) !== null;
1747
+ }
1748
+ /** Turns the fragment the least way that lays its thinnest side on the face of normal (nx, ny, nz). */
1749
+ alongFace(f, nx, ny, nz) {
1750
+ const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
1751
+ const half = [f.half.x, f.half.y, f.half.z];
1752
+ let thin = 0;
1753
+ for (let i = 1; i < 3; i++) if (half[i] < half[thin]) thin = i;
1754
+ const side = e[thin * 4] * nx + e[thin * 4 + 1] * ny + e[thin * 4 + 2] * nz >= 0 ? 1 : -1;
1755
+ this.v.set(e[thin * 4] * side, e[thin * 4 + 1] * side, e[thin * 4 + 2] * side);
1756
+ this.dq.setFromUnitVectors(this.v, this.w.set(nx, ny, nz));
1757
+ f.mesh.quaternion.premultiply(this.dq).normalize();
1758
+ }
1419
1759
  /**
1420
1760
  * The top of the debris lying under a falling fragment: a piece lands on the pile, not through it, so two flat pieces never end up inside each other. Only pieces
1421
1761
  * below it count (a resting one keeps its box), those whose top is at or below where the faller's bottom was a step ago plus a small step.
@@ -1426,13 +1766,16 @@ var FragmentSystem = class {
1426
1766
  const reachX = Math.abs(e[0]) * f.half.x + Math.abs(e[4]) * f.half.y + Math.abs(e[8]) * f.half.z;
1427
1767
  const reachZ = Math.abs(e[2]) * f.half.x + Math.abs(e[6]) * f.half.y + Math.abs(e[10]) * f.half.z;
1428
1768
  const mine = shadowOf(e, f.half);
1769
+ const lowest = p.y - (Math.abs(e[1]) * f.half.x + Math.abs(e[5]) * f.half.y + Math.abs(e[9]) * f.half.z);
1429
1770
  let top = Number.NEGATIVE_INFINITY;
1430
1771
  const under = this.under;
1431
1772
  under.length = 0;
1432
1773
  for (const other of this.list) {
1433
1774
  if (other === f || other.dying) continue;
1434
1775
  const box = other.box ?? (other.box = restingBox(other));
1435
- if (box.top > bottom + PILE_STEP || box.top < top - PILE_STEP) continue;
1776
+ const beneath = !other.asleep && other.mesh.position.y < bottom;
1777
+ if (box.top > bottom + PILE_STEP && !beneath || box.top < top - PILE_STEP) continue;
1778
+ if (box.bottom >= lowest - SIDE_BY_SIDE && (other.seq > f.seq || box.bottom > this.pileFloor + SUPPORT_GAP)) continue;
1436
1779
  if (Math.abs(box.x - p.x) >= box.reachX + reachX || Math.abs(box.z - p.z) >= box.reachZ + reachZ) continue;
1437
1780
  if (!shadowsOverlap(mine, box.shadow, box.x - p.x, box.z - p.z)) continue;
1438
1781
  if (box.top > top + PILE_STEP) under.length = 0;
@@ -1441,13 +1784,16 @@ var FragmentSystem = class {
1441
1784
  }
1442
1785
  this.offEdge = null;
1443
1786
  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;
1787
+ const contacts = contactsOf(mine, under, p.x, p.z);
1788
+ if (!isOverSupport(contacts, [0, 0], 0)) {
1789
+ let x = 0, z = 0;
1790
+ for (const [cx, cz] of contacts) {
1791
+ x -= cx;
1792
+ z -= cz;
1793
+ }
1794
+ const d = Math.hypot(x, z);
1795
+ this.offEdge = d > 1e-6 ? { x: x / d, z: z / d } : { x: 1, z: 0 };
1448
1796
  }
1449
- const d = Math.hypot(x, z);
1450
- this.offEdge = d > 1e-6 ? { x: x / d, z: z / d } : { x: 1, z: 0 };
1451
1797
  }
1452
1798
  return top;
1453
1799
  }
@@ -1459,8 +1805,41 @@ var FragmentSystem = class {
1459
1805
  f.ang.x += (this.rng() - 0.5) * SHEET_SWAY * 4 * dt;
1460
1806
  f.ang.z += (this.rng() - 0.5) * SHEET_SWAY * 4 * dt;
1461
1807
  }
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) {
1808
+ /**
1809
+ * 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
1810
+ * it when its centre is past it (`tip`), else laid flat (`layOnDebris`). True while it is still being laid flat.
1811
+ */
1812
+ restOnDebris(f, params) {
1813
+ const { g, dt, gy, sc, tip } = params;
1814
+ const h = f.half;
1815
+ if (f.vel.y < -DEBRIS_BOUNCE_SPEED) {
1816
+ f.vel.y = -f.vel.y * GROUND_RESTITUTION;
1817
+ f.vel.x *= GROUND_GLIDE;
1818
+ f.vel.z *= GROUND_GLIDE;
1819
+ f.ang.multiplyScalar(GROUND_SPIN_KEPT);
1820
+ const tumble = Math.min(1, TUMBLE_REACH / Math.max(h.x, h.y, h.z));
1821
+ f.ang.x += (this.rng() - 0.5) * 2 * tumble;
1822
+ f.ang.z += (this.rng() - 0.5) * 2 * tumble;
1823
+ } else {
1824
+ f.vel.y = 0;
1825
+ this.slide(f, g, dt);
1826
+ f.ang.multiplyScalar(Math.exp(-6 * dt));
1827
+ }
1828
+ if (tip) {
1829
+ f.vel.x += tip.x * TIP_SPEED * dt * 10;
1830
+ f.vel.z += tip.z * TIP_SPEED * dt * 10;
1831
+ f.ang.x += tip.z * TIP_TURN * dt * 10;
1832
+ f.ang.z -= tip.x * TIP_TURN * dt * 10;
1833
+ return false;
1834
+ }
1835
+ if (f.vel.lengthSq() >= 4) return false;
1836
+ const turning = this.layOnDebris(f, dt);
1837
+ const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
1838
+ f.mesh.position.y = gy + (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
1839
+ return turning;
1840
+ }
1841
+ /** 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. */
1842
+ layOnDebris(f, dt) {
1464
1843
  const mesh = f.mesh;
1465
1844
  this.m.makeRotationFromQuaternion(mesh.quaternion);
1466
1845
  const e = this.m.elements;
@@ -1480,9 +1859,204 @@ var FragmentSystem = class {
1480
1859
  this.w.set(0, 1, 0);
1481
1860
  this.dq.setFromUnitVectors(this.v, this.w);
1482
1861
  this.q.copy(this.dq).multiply(mesh.quaternion);
1483
- mesh.quaternion.slerp(this.q, clamp(dt * 7, 0, 1));
1862
+ mesh.quaternion.slerp(this.q, Math.min(1, dt * DEBRIS_LAY_RATE));
1484
1863
  return Math.sqrt(Math.max(0, 1 - this.v.y * this.v.y)) * Math.max(half[0], half[1], half[2]) > LAID_GAP;
1485
1864
  }
1865
+ /** The fragment's halves (m) at its fade, into `halves`. */
1866
+ scaledHalf(f, sc) {
1867
+ this.halves.x = f.half.x * sc;
1868
+ this.halves.y = f.half.y * sc;
1869
+ this.halves.z = f.half.z * sc;
1870
+ return this.halves;
1871
+ }
1872
+ /** The speed (m/s, up) of the point it rests on (`support`): its centre's, and its spin's about the centre there. */
1873
+ supportSpeed(f) {
1874
+ const s = this.support;
1875
+ return f.vel.y + f.ang.z * s.x - f.ang.x * s.z;
1876
+ }
1877
+ /**
1878
+ * 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
1879
+ * step of gravity gives what lies on it.
1880
+ */
1881
+ bounces(f, params) {
1882
+ const { g, dt, sc } = params;
1883
+ const thinnest = Math.min(f.half.x, f.half.y, f.half.z) * sc;
1884
+ const least = Math.max(2 * g * dt, Math.sqrt(2 * g * BOUNCE_LIFT_SHARE * thinnest) / GROUND_RESTITUTION);
1885
+ return this.supportSpeed(f) < -least;
1886
+ }
1887
+ /**
1888
+ * 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
1889
+ * 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,
1890
+ * so a board metres long is turned as a long body is, not as a chip.
1891
+ */
1892
+ bounce(f, sc) {
1893
+ const s = this.support;
1894
+ const into = this.supportSpeed(f);
1895
+ const rough = ROUGHNESS_SHARE * Math.max(f.half.x, f.half.y, f.half.z) * sc;
1896
+ s.x += (this.rng() - 0.5) * 2 * rough;
1897
+ s.z += (this.rng() - 0.5) * 2 * rough;
1898
+ const turn2 = this.w.set(-s.z, 0, s.x);
1899
+ applyInverseInertia({ e: this.m.elements, half: this.scaledHalf(f, sc), v: turn2 });
1900
+ const share = 1 + (turn2.z * s.x - turn2.x * s.z);
1901
+ const impulse = -(1 + GROUND_RESTITUTION) * into / share;
1902
+ f.vel.y += impulse;
1903
+ f.ang.addScaledVector(turn2, impulse);
1904
+ f.vel.x *= GROUND_GLIDE;
1905
+ f.vel.z *= GROUND_GLIDE;
1906
+ f.ang.multiplyScalar(GROUND_SPIN_KEPT);
1907
+ }
1908
+ /**
1909
+ * 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
1910
+ * 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).
1911
+ */
1912
+ slide(f, g, dt) {
1913
+ const sliding = Math.hypot(f.vel.x, f.vel.z);
1914
+ const kept = sliding > 0 ? Math.max(0, sliding - DEBRIS_FRICTION * g * dt) / sliding : 0;
1915
+ f.vel.x *= kept;
1916
+ f.vel.z *= kept;
1917
+ }
1918
+ /**
1919
+ * 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
1920
+ * 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
1921
+ * while it is still turning over.
1922
+ */
1923
+ restOn(f, params) {
1924
+ const { g, dt, sc } = params;
1925
+ const s = this.support;
1926
+ const ang = f.ang;
1927
+ const dx = -s.x, dy = -s.y, dz = -s.z;
1928
+ f.vel.x -= ang.y * dz - ang.z * dy;
1929
+ f.vel.z -= ang.x * dy - ang.y * dx;
1930
+ this.slide(f, g, dt);
1931
+ const footX = f.vel.x;
1932
+ const footZ = f.vel.z;
1933
+ const half = this.scaledHalf(f, sc);
1934
+ const gyration = Math.sqrt(inertiaAbout({ e: this.m.elements, half, x: 0, y: 1, z: 0 }));
1935
+ if (gyration > 0) ang.y = Math.sign(ang.y) * Math.max(0, Math.abs(ang.y) - DEBRIS_FRICTION * g * dt / gyration);
1936
+ if (s.count >= 3) {
1937
+ ang.x = 0;
1938
+ ang.z = 0;
1939
+ } else if (s.count === 2) {
1940
+ const along2 = ang.x * s.edgeX + ang.z * s.edgeZ;
1941
+ ang.x = along2 * s.edgeX;
1942
+ ang.z = along2 * s.edgeZ;
1943
+ }
1944
+ const lever = this.lever;
1945
+ lever.x = dx;
1946
+ lever.y = dy;
1947
+ lever.z = dz;
1948
+ const slow = footX * footX + footZ * footZ < 2 * g * SETTLE_FALL_HALVES * Math.max(half.x, half.y, half.z);
1949
+ const turning = slow && this.settle(f, g, dt);
1950
+ f.vel.x = footX + (ang.y * lever.z - ang.z * lever.y);
1951
+ f.vel.y = ang.z * lever.x - ang.x * lever.z;
1952
+ f.vel.z = footZ + (ang.x * lever.y - ang.y * lever.x);
1953
+ return turning;
1954
+ }
1955
+ /**
1956
+ * 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
1957
+ * 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
1958
+ * 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
1959
+ * holds it. True while it still turns.
1960
+ */
1961
+ settle(f, g, dt) {
1962
+ const s = this.support;
1963
+ const e = this.m.elements;
1964
+ const half = this.halves;
1965
+ const reach = Math.max(half.x, half.y, half.z);
1966
+ const lever = this.lever;
1967
+ let torqueX = g * lever.z;
1968
+ let torqueZ = -g * lever.x;
1969
+ if (s.count >= 3) {
1970
+ torqueX = 0;
1971
+ torqueZ = 0;
1972
+ } else if (s.count === 2) {
1973
+ const along2 = torqueX * s.edgeX + torqueZ * s.edgeZ;
1974
+ torqueX = along2 * s.edgeX;
1975
+ torqueZ = along2 * s.edgeZ;
1976
+ }
1977
+ const torque = Math.hypot(torqueX, torqueZ);
1978
+ const ang = f.ang;
1979
+ if (torque <= g * SUPPORT_SHARE * reach && ang.x === 0 && ang.z === 0) {
1980
+ if (s.count >= 3 && !this.isUnsteady(f)) return false;
1981
+ if (!this.nudge(f, g)) return false;
1982
+ } else if (torque > 0) {
1983
+ const axisX = torqueX / torque;
1984
+ const axisZ = torqueZ / torque;
1985
+ const along2 = lever.x * axisX + lever.z * axisZ;
1986
+ 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;
1987
+ ang.x += axisX * torque / inertia * dt;
1988
+ ang.z += axisZ * torque / inertia * dt;
1989
+ }
1990
+ const spin = Math.hypot(ang.x, ang.z);
1991
+ if (spin === 0) return false;
1992
+ 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 });
1993
+ if (spin * dt > landing) {
1994
+ ang.x *= landing / (spin * dt);
1995
+ ang.z *= landing / (spin * dt);
1996
+ }
1997
+ return true;
1998
+ }
1999
+ /**
2000
+ * 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
2001
+ * its flattest.
2002
+ */
2003
+ isUnsteady(f) {
2004
+ const e = this.m.elements;
2005
+ const up = [Math.abs(e[1]), Math.abs(e[5]), Math.abs(e[9])];
2006
+ const half = [f.half.x, f.half.y, f.half.z];
2007
+ let small = 0;
2008
+ let standing = 0;
2009
+ for (let i = 1; i < 3; i++) {
2010
+ if (half[i] < half[small]) small = i;
2011
+ if (up[i] > up[standing]) standing = i;
2012
+ }
2013
+ if (f.body) return half[standing] > half[small] * (1 + SUPPORT_SHARE);
2014
+ const across = Math.min(...half.filter((_, i) => i !== standing));
2015
+ return across < STAND_RATIO * half[standing];
2016
+ }
2017
+ /**
2018
+ * 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 ω² =
2019
+ * 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.
2020
+ */
2021
+ nudge(f, g) {
2022
+ const e = this.m.elements;
2023
+ const half = this.halves;
2024
+ const tolerance = SUPPORT_SHARE * Math.max(half.x, half.y, half.z);
2025
+ let small = 0;
2026
+ if (half.y < half.x) small = 1;
2027
+ if (half.z < (small === 1 ? half.y : half.x)) small = 2;
2028
+ const sign = e[small * 4 + 1] >= 0 ? 1 : -1;
2029
+ const axisX = -e[small * 4 + 2] * sign;
2030
+ const axisZ = e[small * 4] * sign;
2031
+ const length = Math.hypot(axisX, axisZ);
2032
+ if (length < SUPPORT_SHARE) return false;
2033
+ const kx = axisX / length;
2034
+ const kz = axisZ / length;
2035
+ const c = this.corners;
2036
+ const floor = this.support.y + tolerance;
2037
+ let far2 = -Infinity;
2038
+ 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]);
2039
+ let count = 0, x = 0, y = 0, z = 0;
2040
+ for (let i = 0; i < 8; i++) {
2041
+ if (c[3 * i + 1] > floor || kx * c[3 * i + 2] - kz * c[3 * i] < far2 - tolerance) continue;
2042
+ count++;
2043
+ x += c[3 * i];
2044
+ y += c[3 * i + 1];
2045
+ z += c[3 * i + 2];
2046
+ }
2047
+ const lever = this.lever;
2048
+ lever.x = -x / count;
2049
+ lever.y = -y / count;
2050
+ lever.z = -z / count;
2051
+ const along2 = lever.x * kx + lever.z * kz;
2052
+ const square = lever.x * lever.x + lever.y * lever.y + lever.z * lever.z - along2 * along2;
2053
+ const rise = Math.max(Math.sqrt(square) - lever.y, tolerance);
2054
+ const inertia = inertiaAbout({ e, half, x: kx, y: 0, z: kz }) + square;
2055
+ const spinOfInertia = Math.sqrt(2 * g * rise * (1 + NUDGE_ENERGY_SHARE) / inertia);
2056
+ f.ang.x = kx * spinOfInertia;
2057
+ f.ang.z = kz * spinOfInertia;
2058
+ return true;
2059
+ }
1486
2060
  /** Remove the fragments of one object (after `restore`). */
1487
2061
  removeHome(home) {
1488
2062
  for (let i = this.list.length - 1; i >= 0; i--) {
@@ -3938,6 +4512,10 @@ var TILE_KEY = 65536;
3938
4512
  var TILE_KEY_ORIGIN = 32768;
3939
4513
  var MU_STATIC = 0.7;
3940
4514
  var ROLLING_RESISTANCE = 0.015;
4515
+ var roundPartsRadius = (parts) => {
4516
+ if (!parts || parts.length === 0 || !parts.every((part) => part.round && !part.hull)) return void 0;
4517
+ return Math.max(...parts.map((part) => (part.half[0] + part.half[2]) / 2));
4518
+ };
3941
4519
  var MU_DYNAMIC = 0.6;
3942
4520
  var GROUND = { frictionStatic: MU_STATIC, frictionDynamic: MU_DYNAMIC, restitution: 0, rolling: ROLLING_RESISTANCE };
3943
4521
  var tileRollingAt = (rollingAt, x, z) => rollingAt((Math.floor(x / TILE) + 0.5) * TILE, (Math.floor(z / TILE) + 0.5) * TILE);
@@ -4486,9 +5064,13 @@ var EnginePieces = class {
4486
5064
  out[round.axis] = about;
4487
5065
  return out;
4488
5066
  }
4489
- /** A round piece rolls to a stop by its surface's rolling resistance (the adapter's rule; the engine has none). */
5067
+ /**
5068
+ * 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
5069
+ * 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.
5070
+ */
4490
5071
  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;
5072
+ const radius = b.round?.radius ?? roundPartsRadius(b.partBoxes);
5073
+ return radius === void 0 ? void 0 : { coefficient: b.friction?.rolling ?? ROLLING_RESISTANCE, radius, damping: b.friction?.rollingDamping ?? PIECE_ROLLING_DAMPING };
4492
5074
  }
4493
5075
  material(b) {
4494
5076
  return {
@@ -5450,46 +6032,6 @@ var resolvePhysics = (option) => {
5450
6032
  return { loop: createPhysicsLoop(option), stepsItself: true };
5451
6033
  };
5452
6034
 
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
6035
  // src/pieces.ts
5494
6036
  var BALANCE_TOLERANCE = 0.01;
5495
6037
  var MU_STATIC2 = 0.7;
@@ -8805,8 +9347,10 @@ var MIN_FRAGMENTS_PER_SLICE = 8;
8805
9347
  var STATIC_REFRESH = 0.5;
8806
9348
  var STATIC_REFRESH_PER_UPDATE = 8;
8807
9349
  var SURFACE_RAYS = 4;
8808
- var SURFACE_INSIDE = 5e-3;
9350
+ var SURFACE_INSIDE = 1e-3;
8809
9351
  var SURFACE_STEP = 0.15;
9352
+ var MOVER_MIN_MOVE = 1e-3;
9353
+ var MOVER_RAYS = 4;
8810
9354
  var STATIC_SPAN = 3;
8811
9355
  var VERTICES_PER_UPDATE = 3e3;
8812
9356
  var FEET_BAND = 0.03;
@@ -8949,7 +9493,8 @@ var UNDERMINED_RIM_RISE = 0.1;
8949
9493
  var UNDERMINED_STAND = 0.4;
8950
9494
  var UNDERMINED_KEEP = 0.6;
8951
9495
  var SOLIDS_MAX = 64;
8952
- var UNDERMINED_PUSH = 0.6;
9496
+ var UNDERMINED_FROUDE = 0.13;
9497
+ var GRAVITY2 = 9.81;
8953
9498
  var DEBRIS_ID = "debris#";
8954
9499
  var UNDERMINED_STILL = 0.05;
8955
9500
  var SOLID_WELD = 1e-4;
@@ -9216,6 +9761,15 @@ var Renderer = class {
9216
9761
  __publicField(this, "stopContactHits", null);
9217
9762
  __publicField(this, "box");
9218
9763
  __publicField(this, "tmpQ");
9764
+ /** The pieces that moved this update, as the fragments see them (`collectMovers`), and the record kept per piece. */
9765
+ __publicField(this, "moving", []);
9766
+ __publicField(this, "movers", /* @__PURE__ */ new WeakMap());
9767
+ __publicField(this, "moverFrom");
9768
+ __publicField(this, "moverTo");
9769
+ __publicField(this, "moverBox");
9770
+ __publicField(this, "moverBoxMoved");
9771
+ /** The box of a geometry's positions, made once (`drawnBoxOf`). */
9772
+ __publicField(this, "geometryBoxes", /* @__PURE__ */ new WeakMap());
9219
9773
  __publicField(this, "v1");
9220
9774
  __publicField(this, "v2");
9221
9775
  __publicField(this, "v3");
@@ -9244,6 +9798,10 @@ var Renderer = class {
9244
9798
  this.workCosts = resolveWorkUnitCosts(o.workBudget?.costs);
9245
9799
  this.box = new THREE.Box3();
9246
9800
  this.tmpQ = new THREE.Quaternion();
9801
+ this.moverFrom = new THREE.Matrix4();
9802
+ this.moverTo = new THREE.Matrix4();
9803
+ this.moverBox = new THREE.Box3();
9804
+ this.moverBoxMoved = new THREE.Box3();
9247
9805
  this.v1 = new THREE.Vector3();
9248
9806
  this.v2 = new THREE.Vector3();
9249
9807
  this.v3 = new THREE.Vector3();
@@ -9273,6 +9831,8 @@ var Renderer = class {
9273
9831
  sweep: (from, direction, distance) => typeof this.world.raycast === "function" ? this.world.raycast([from[0], from[1], from[2]], [direction[0], direction[1], direction[2]], distance) : null,
9274
9832
  maxAwake: o.maxAwakeFragments ?? Infinity,
9275
9833
  ...o.interpolate ? { interpolate: true } : {},
9834
+ // the loose pieces that moved: the scraps on and in them go with them, and a part coming at a scrap meets it
9835
+ movers: () => this.moving,
9276
9836
  rigid: {
9277
9837
  add: (mesh, half, mass, velocity, angular) => {
9278
9838
  const body = this.pieceSys.create(`${DEBRIS_ID}${++this.debrisMade}`, mesh, [half[0], half[1], half[2]], new THREE.Vector3(), mass, [velocity[0], velocity[1], velocity[2]], [angular[0], angular[1], angular[2]]);
@@ -9876,6 +10436,8 @@ var Renderer = class {
9876
10436
  this.pieceSys.update(dt);
9877
10437
  this.measureCost();
9878
10438
  this.updateFading(dt);
10439
+ this.collectMovers();
10440
+ this.syncTransforms();
9879
10441
  this.fragments.update(dt);
9880
10442
  this.updateActive(dt);
9881
10443
  this.updateLeaks(dt);
@@ -10809,13 +11371,13 @@ var Renderer = class {
10809
11371
  }
10810
11372
  /**
10811
11373
  * 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.
11374
+ * `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
11375
+ * whether one played, so the caller can leave out the burst it replaces.
10814
11376
  */
10815
- playEvent(event, point, play) {
11377
+ playEvent(event, point, play, normal) {
10816
11378
  const id = effectForEvent(play && event.material === void 0 ? { ...event, material: play.material } : event, this.o.eventEffects ?? EVENT_EFFECTS_MAP);
10817
11379
  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;
11380
+ return this.vfx.play(id, point, { seed: hashNumbers(point[0], point[1], point[2], this.time), ...play ? { values: play.values } : {}, ...normal ? { normal } : {} }) !== null;
10819
11381
  }
10820
11382
  /** The family and colours of a part's material, for the effects of the event map. */
10821
11383
  partPlay(objectId, partId) {
@@ -10835,7 +11397,7 @@ var Renderer = class {
10835
11397
  const core = def?.visual?.innerColor;
10836
11398
  const look = debrisLook(def, "spall", { partColor: this.partColor(pv, def) ?? void 0 });
10837
11399
  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);
11400
+ else this.playEvent({ kind: "spall", material: look.impact }, ev.point, void 0, [normal.x, normal.y, normal.z]);
10839
11401
  if (core !== void 0) this.trickleFrom(ev.point, [normal.x, normal.y, normal.z], core, mv);
10840
11402
  if (mv.mesh.userData.drawnSpalls?.includes(ev.part)) return;
10841
11403
  this.visuals.addSpall(mv, wp, normal, Math.sqrt(ev.area / Math.PI), core ?? fallbackColor(def));
@@ -11377,6 +11939,7 @@ var Renderer = class {
11377
11939
  }
11378
11940
  }
11379
11941
  this.bounds.set(newId, nb);
11942
+ nb.lastWorld = Float64Array.from(group.matrixWorld.elements);
11380
11943
  for (const pv of nb.parts.values()) this.tagPart(pv, newId);
11381
11944
  this.followSplit(from, nb);
11382
11945
  const meshes = this.meshesOf(nb);
@@ -11555,7 +12118,7 @@ var Renderer = class {
11555
12118
  body.sleeping = false;
11556
12119
  body.still = 0;
11557
12120
  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]);
12121
+ 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
12122
  if (!was) this.o.onLoosened?.(b.id, body, "ground");
11560
12123
  }
11561
12124
  }
@@ -11572,7 +12135,8 @@ var Renderer = class {
11572
12135
  if (this.standsWith(held, null, centre, feet.floor)) return;
11573
12136
  const inside = insideBy(convexHull(held), [centre[0], centre[2]]);
11574
12137
  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]);
12138
+ const mid = this.pieceSys.centerOf(body, new this.THREE.Vector3());
12139
+ 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
12140
  }
11577
12141
  /** 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
12142
  reachAlong(body, x, z) {
@@ -11677,6 +12241,103 @@ var Renderer = class {
11677
12241
  }
11678
12242
  return "";
11679
12243
  }
12244
+ /**
12245
+ * The loose pieces that moved since the world was last told where they are (`FragmentSystemOptions.movers`), read before it is told: each with the box it took then and
12246
+ * takes now, its move, and a ray through its own parts.
12247
+ */
12248
+ collectMovers() {
12249
+ const out = this.moving;
12250
+ out.length = 0;
12251
+ for (const b of this.bounds.values()) {
12252
+ const piece = b.piece;
12253
+ const was = b.lastWorld;
12254
+ if (!piece || piece.sleeping || !b.sync || !was || !this.world.has(b.id)) continue;
12255
+ b.root.updateWorldMatrix(true, false);
12256
+ const now = b.root.matrixWorld.elements;
12257
+ let same2 = true;
12258
+ for (let i = 0; i < 16 && same2; i++) same2 = now[i] === was[i];
12259
+ if (same2) continue;
12260
+ let solid = this.movers.get(b);
12261
+ if (!solid) {
12262
+ solid = this.moverOf(b.id);
12263
+ this.movers.set(b, solid);
12264
+ }
12265
+ const before = this.moverFrom.fromArray(was);
12266
+ this.moverTo.copy(b.root.matrixWorld).multiply(before.invert()).toArray(solid.delta);
12267
+ const box = solid.box;
12268
+ box.fill(Infinity, 0, 3);
12269
+ box.fill(-Infinity, 3, 6);
12270
+ this.drawnBoxOf(b.root, this.moverBox);
12271
+ const back = this.moverFrom.fromArray(solid.delta).invert();
12272
+ if (this.farthestCorner(back) < MOVER_MIN_MOVE) continue;
12273
+ this.growBox(box);
12274
+ this.growBox(box, back);
12275
+ out.push(solid);
12276
+ }
12277
+ }
12278
+ /**
12279
+ * The world box of what is drawn under `root`, from the box of each mesh's positions, kept per geometry here: never by `Box3.setFromObject`, which writes a bounding box
12280
+ * into each geometry it reads and so changes what the piece is made of for anyone who reads it whole.
12281
+ */
12282
+ drawnBoxOf(root, out) {
12283
+ out.makeEmpty();
12284
+ root.updateWorldMatrix(true, true);
12285
+ root.traverseVisible((node) => {
12286
+ const mesh = node;
12287
+ const position = mesh.isMesh ? mesh.geometry?.getAttribute("position") : void 0;
12288
+ if (!position) return;
12289
+ let local = this.geometryBoxes.get(mesh.geometry);
12290
+ if (!local) {
12291
+ local = new this.THREE.Box3().setFromBufferAttribute(position);
12292
+ this.geometryBoxes.set(mesh.geometry, local);
12293
+ }
12294
+ out.union(this.moverBoxMoved.copy(local).applyMatrix4(mesh.matrixWorld));
12295
+ });
12296
+ }
12297
+ /** How far (m) the corner of `moverBox` that moves most goes under `matrix`. */
12298
+ farthestCorner(matrix) {
12299
+ if (this.moverBox.isEmpty()) return 0;
12300
+ const { min, max } = this.moverBox;
12301
+ let most = 0;
12302
+ for (let i = 0; i < 8; i++) {
12303
+ const corner = this.v1.set(i & 1 ? max.x : min.x, i & 2 ? max.y : min.y, i & 4 ? max.z : min.z);
12304
+ const x = corner.x, y = corner.y, z = corner.z;
12305
+ corner.applyMatrix4(matrix);
12306
+ most = Math.max(most, Math.hypot(corner.x - x, corner.y - y, corner.z - z));
12307
+ }
12308
+ return most;
12309
+ }
12310
+ /** `box` grown by `moverBox`, under `matrix` when one is given. */
12311
+ growBox(box, matrix) {
12312
+ if (this.moverBox.isEmpty()) return;
12313
+ const at = matrix ? this.moverBoxMoved.copy(this.moverBox).applyMatrix4(matrix) : this.moverBox;
12314
+ box[0] = Math.min(box[0], at.min.x);
12315
+ box[1] = Math.min(box[1], at.min.y);
12316
+ box[2] = Math.min(box[2], at.min.z);
12317
+ box[3] = Math.max(box[3], at.max.x);
12318
+ box[4] = Math.max(box[4], at.max.y);
12319
+ box[5] = Math.max(box[5], at.max.z);
12320
+ }
12321
+ /** A moving piece as the fragments see it, made once per bound object: its sweep meets only its own parts, the first of them past whatever else is on the way. */
12322
+ moverOf(objectId) {
12323
+ return {
12324
+ box: [0, 0, 0, 0, 0, 0],
12325
+ delta: new Array(16).fill(0),
12326
+ sweep: (from, direction, distance) => {
12327
+ let start = [from[0], from[1], from[2]];
12328
+ let left = distance;
12329
+ for (let k = 0; k < MOVER_RAYS && left > 0; k++) {
12330
+ const hit = this.world.raycast(start, [direction[0], direction[1], direction[2]], left);
12331
+ if (!hit) return null;
12332
+ if (hit.object === objectId) return { point: [start[0] + direction[0] * hit.distance, start[1] + direction[1] * hit.distance, start[2] + direction[2] * hit.distance], normal: hit.normal };
12333
+ const past = hit.distance + SURFACE_INSIDE;
12334
+ start = [start[0] + direction[0] * past, start[1] + direction[1] * past, start[2] + direction[2] * past];
12335
+ left -= past;
12336
+ }
12337
+ return null;
12338
+ }
12339
+ };
12340
+ }
11680
12341
  /** The top of the ground or of a standing part of the world under (x, z), below height y: a ray straight down through the destruction world. */
11681
12342
  surfaceAt(x, y, z) {
11682
12343
  const ground = this.groundAt(x, z);
@@ -12637,6 +13298,9 @@ function convexHull(points) {
12637
13298
  };
12638
13299
  return [...half(sorted), ...half([...sorted].reverse())];
12639
13300
  }
13301
+ function startDownSpeed(height) {
13302
+ return UNDERMINED_FROUDE * Math.sqrt(GRAVITY2 * height);
13303
+ }
12640
13304
  function centreOfBoxes(boxes) {
12641
13305
  const sum = [0, 0, 0];
12642
13306
  let total = 0;