@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.
@@ -4112,7 +4112,6 @@ var GameDestructionThree = (() => {
4112
4112
  ...DEBRIS_EFFECT_FAMILIES.flatMap((family) => [stageCrack(family), breachDust(family), collapseDust(family)]),
4113
4113
  REBAR_EXPOSE
4114
4114
  ];
4115
- var FACING5 = [0, 0, 1];
4116
4115
  var ORIGIN2 = [0, 0, 0];
4117
4116
  var FORWARD4 = [0, 0, -1];
4118
4117
  var IMPACT_MATERIALS = ["concrete", "metal", "wood", "glass", "flesh", "water"];
@@ -4122,7 +4121,7 @@ var GameDestructionThree = (() => {
4122
4121
  team: "weapons",
4123
4122
  budget: { particles: 40, decals: 1 },
4124
4123
  loop: 2,
4125
- timeline: [call2("impact", material, ORIGIN2, FACING5, 1, 0)]
4124
+ timeline: [call2("impact", material, ORIGIN2, NORMAL, 1, 0)]
4126
4125
  });
4127
4126
  var muzzleEffect = (weapon) => ({
4128
4127
  id: `muzzle_${weapon}`,
@@ -7828,7 +7827,6 @@ var GameDestructionThree = (() => {
7828
7827
  var FIRE_FLICKER_SWAY = 0.25;
7829
7828
  var FIRE_FLICKER_JITTER = 0.08;
7830
7829
  var LENS_FADE = 0.16;
7831
- var FLOOR_NORMAL_Y = 0.9;
7832
7830
  function rotate2(q, v) {
7833
7831
  const [qx, qy, qz, qw] = q;
7834
7832
  const ix = qw * v[0] + qy * v[2] - qz * v[1];
@@ -7965,7 +7963,7 @@ var GameDestructionThree = (() => {
7965
7963
  const [x, y, z] = this.place([recipe2[0], recipe2[1], recipe2[2]]);
7966
7964
  const options = recipe2[5];
7967
7965
  const normal = options?.n ? this.turn(options.n) : void 0;
7968
- const onFloor = !normal || normal[1] > FLOOR_NORMAL_Y;
7966
+ const onFloor = !normal;
7969
7967
  args = [x, onFloor ? this.context.groundAt(x, z) : y, z, recipe2[3], recipe2[4], normal ? { ...options, n: normal } : options];
7970
7968
  }
7971
7969
  this.context.trace?.("decal", args);
@@ -10925,6 +10923,78 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
10925
10923
  return box;
10926
10924
  }
10927
10925
 
10926
+ // src/fragment-support.ts
10927
+ var SUPPORT_SHARE = 1e-4;
10928
+ var createSupport = () => ({ count: 0, x: 0, y: 0, z: 0, edgeX: 0, edgeZ: 0 });
10929
+ function writeCorners(params) {
10930
+ const { e, half, corners } = params;
10931
+ for (let i = 0; i < 8; i++) {
10932
+ const sx = i & 1 ? half.x : -half.x;
10933
+ const sy = i & 2 ? half.y : -half.y;
10934
+ const sz = i & 4 ? half.z : -half.z;
10935
+ corners[3 * i] = e[0] * sx + e[4] * sy + e[8] * sz;
10936
+ corners[3 * i + 1] = e[1] * sx + e[5] * sy + e[9] * sz;
10937
+ corners[3 * i + 2] = e[2] * sx + e[6] * sy + e[10] * sz;
10938
+ }
10939
+ }
10940
+ function findSupport(params) {
10941
+ const { corners, support } = params;
10942
+ let low = Infinity;
10943
+ for (let i = 0; i < 8; i++) low = Math.min(low, corners[3 * i + 1]);
10944
+ const level = low + SUPPORT_SHARE * params.reach;
10945
+ let count = 0, x = 0, y = 0, z = 0, first = -1, edgeX = 0, edgeZ = 0;
10946
+ for (let i = 0; i < 8; i++) {
10947
+ if (corners[3 * i + 1] > level) continue;
10948
+ count++;
10949
+ x += corners[3 * i];
10950
+ y += corners[3 * i + 1];
10951
+ z += corners[3 * i + 2];
10952
+ if (first < 0) first = i;
10953
+ else if (count === 2) {
10954
+ edgeX = corners[3 * i] - corners[3 * first];
10955
+ edgeZ = corners[3 * i + 2] - corners[3 * first + 2];
10956
+ }
10957
+ }
10958
+ const along2 = Math.hypot(edgeX, edgeZ);
10959
+ support.count = count;
10960
+ support.x = x / count;
10961
+ support.y = y / count;
10962
+ support.z = z / count;
10963
+ support.edgeX = along2 > 0 ? edgeX / along2 : 0;
10964
+ support.edgeZ = along2 > 0 ? edgeZ / along2 : 0;
10965
+ }
10966
+ function inertiaAbout(params) {
10967
+ const { e, half, x, y, z } = params;
10968
+ const ax = x * e[0] + y * e[1] + z * e[2];
10969
+ const ay = x * e[4] + y * e[5] + z * e[6];
10970
+ const az = x * e[8] + y * e[9] + z * e[10];
10971
+ const xx = half.x * half.x, yy = half.y * half.y, zz = half.z * half.z;
10972
+ return (ax * ax * (yy + zz) + ay * ay * (xx + zz) + az * az * (xx + yy)) / 3;
10973
+ }
10974
+ function applyInverseInertia(params) {
10975
+ const { e, half, v } = params;
10976
+ const xx = half.x * half.x, yy = half.y * half.y, zz = half.z * half.z;
10977
+ const ax = 3 * (v.x * e[0] + v.y * e[1] + v.z * e[2]) / (yy + zz);
10978
+ const ay = 3 * (v.x * e[4] + v.y * e[5] + v.z * e[6]) / (xx + zz);
10979
+ const az = 3 * (v.x * e[8] + v.y * e[9] + v.z * e[10]) / (xx + yy);
10980
+ v.x = ax * e[0] + ay * e[4] + az * e[8];
10981
+ v.y = ax * e[1] + ay * e[5] + az * e[9];
10982
+ v.z = ax * e[2] + ay * e[6] + az * e[10];
10983
+ }
10984
+ function turnToLanding(params) {
10985
+ const { corners, pivot, axisX, axisZ } = params;
10986
+ let first = Infinity;
10987
+ for (let i = 0; i < 8; i++) {
10988
+ const px = corners[3 * i] - pivot.x;
10989
+ const py = corners[3 * i + 1] - pivot.y;
10990
+ const pz = corners[3 * i + 2] - pivot.z;
10991
+ if (py <= SUPPORT_SHARE * params.reach) continue;
10992
+ const qy = axisZ * px - axisX * pz;
10993
+ first = Math.min(first, Math.atan2(qy, py) + Math.PI / 2);
10994
+ }
10995
+ return first;
10996
+ }
10997
+
10928
10998
  // src/fragment-instances.ts
10929
10999
  var INSTANCED_FRAGMENT_LAYER = 29;
10930
11000
  var FRAGMENT_INSTANCES_NAME = "fragment-instances";
@@ -11420,6 +11490,67 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
11420
11490
  out.set(x / length, y / length, z / length, w / length);
11421
11491
  }
11422
11492
 
11493
+ // src/support-hull.ts
11494
+ var cross2 = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
11495
+ function planHull(points) {
11496
+ const sorted = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
11497
+ if (sorted.length < 3) return sorted;
11498
+ const lower = [];
11499
+ for (const p of sorted) {
11500
+ while (lower.length >= 2 && cross2(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) lower.pop();
11501
+ lower.push(p);
11502
+ }
11503
+ const upper = [];
11504
+ for (let i = sorted.length - 1; i >= 0; i--) {
11505
+ const p = sorted[i];
11506
+ while (upper.length >= 2 && cross2(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) upper.pop();
11507
+ upper.push(p);
11508
+ }
11509
+ return [...lower.slice(0, -1), ...upper.slice(0, -1)];
11510
+ }
11511
+ function toSegment(p, a, b) {
11512
+ const dx = b[0] - a[0];
11513
+ const dz = b[1] - a[1];
11514
+ const length = dx * dx + dz * dz;
11515
+ const t = length > 0 ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dz) / length)) : 0;
11516
+ return Math.hypot(p[0] - a[0] - t * dx, p[1] - a[1] - t * dz);
11517
+ }
11518
+ function isOverSupport(points, centre, tolerance) {
11519
+ const hull = planHull(points);
11520
+ if (hull.length === 0) return false;
11521
+ if (hull.length === 1) return Math.hypot(centre[0] - hull[0][0], centre[1] - hull[0][1]) <= tolerance;
11522
+ let inside = hull.length >= 3;
11523
+ let nearest = Infinity;
11524
+ for (let i = 0; i < hull.length; i++) {
11525
+ const a = hull[i];
11526
+ const b = hull[(i + 1) % hull.length];
11527
+ if (hull.length >= 3 && cross2(a, b, centre) < 0) inside = false;
11528
+ nearest = Math.min(nearest, toSegment(centre, a, b));
11529
+ }
11530
+ return inside || nearest <= tolerance;
11531
+ }
11532
+ function clipConvex(subject, window) {
11533
+ let kept = [...subject];
11534
+ for (let e = 0; e < window.length && kept.length > 0; e++) {
11535
+ const a = window[e];
11536
+ const b = window[(e + 1) % window.length];
11537
+ const next = [];
11538
+ for (let i = 0; i < kept.length; i++) {
11539
+ const p = kept[i];
11540
+ const q = kept[(i + 1) % kept.length];
11541
+ const sp = cross2(a, b, p);
11542
+ const sq = cross2(a, b, q);
11543
+ if (sp >= 0) next.push(p);
11544
+ if (sp >= 0 !== sq >= 0) {
11545
+ const t = sp / (sp - sq);
11546
+ next.push([p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t]);
11547
+ }
11548
+ }
11549
+ kept = next;
11550
+ }
11551
+ return kept;
11552
+ }
11553
+
11423
11554
  // src/fragments.ts
11424
11555
  function rotationOf(q) {
11425
11556
  const { x, y, z, w } = q;
@@ -11442,6 +11573,20 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
11442
11573
  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];
11443
11574
  }
11444
11575
  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]);
11576
+ function shadowPolygon(shadow, cx, cz) {
11577
+ const corners = [];
11578
+ for (let k = 0; k < 8; k++) {
11579
+ const a = k & 1 ? 1 : -1, b = k & 2 ? 1 : -1, c = k & 4 ? 1 : -1;
11580
+ corners.push([cx + a * shadow[0] + b * shadow[2] + c * shadow[4], cz + a * shadow[1] + b * shadow[3] + c * shadow[5]]);
11581
+ }
11582
+ return planHull(corners);
11583
+ }
11584
+ function contactsOf(mine, under, x, z) {
11585
+ const own = shadowPolygon(mine, 0, 0);
11586
+ const points = [];
11587
+ for (const box of under) points.push(...clipConvex(own, shadowPolygon(box.shadow, box.x - x, box.z - z)));
11588
+ return points;
11589
+ }
11445
11590
  function shadowsOverlap(first, second, dx, dz) {
11446
11591
  for (const shadow of [first, second]) {
11447
11592
  for (let g = 0; g < 6; g += 2) {
@@ -11492,8 +11637,18 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
11492
11637
  var WALL_RESTITUTION = 0.25;
11493
11638
  var WALL_GLIDE = 0.7;
11494
11639
  var TUMBLE_REACH = 0.1;
11640
+ var GROUND_RESTITUTION = 0.25;
11641
+ var GROUND_GLIDE = 0.7;
11642
+ var GROUND_SPIN_KEPT = 0.55;
11643
+ var BOUNCE_LIFT_SHARE = 0.05;
11644
+ var ROUGHNESS_SHARE = 0.05;
11645
+ var SETTLE_FALL_HALVES = 2;
11646
+ var NUDGE_ENERGY_SHARE = 0.1;
11647
+ var DEBRIS_BOUNCE_SPEED = 0.6;
11648
+ var DEBRIS_LAY_RATE = 7;
11495
11649
  var LAID_GAP = 5e-3;
11496
11650
  var STAND_RATIO = 0.6;
11651
+ var FADE_SECONDS3 = 0.6;
11497
11652
  var SETTLE_SECONDS = 0.4;
11498
11653
  var isSlow = (f) => f.vel.lengthSq() < 0.012 && f.ang.lengthSq() < 0.2;
11499
11654
  var isBig = (size) => {
@@ -11524,6 +11679,11 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
11524
11679
  __publicField(this, "v");
11525
11680
  __publicField(this, "w");
11526
11681
  __publicField(this, "rng", createRng(FRAGMENT_SEED));
11682
+ /** 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. */
11683
+ __publicField(this, "corners", new Float64Array(24));
11684
+ __publicField(this, "support", createSupport());
11685
+ __publicField(this, "halves", { x: 0, y: 0, z: 0 });
11686
+ __publicField(this, "lever", { x: 0, y: 0, z: 0 });
11527
11687
  /** The fragments in flight drawn by instances, a draw per shared geometry and material (`FragmentInstances`). */
11528
11688
  __publicField(this, "instances");
11529
11689
  /** 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`. */
@@ -11825,6 +11985,13 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
11825
11985
  continue;
11826
11986
  }
11827
11987
  if (this.clock > f.deadline) {
11988
+ const held = this.heldUntil(f);
11989
+ if (held > f.deadline) {
11990
+ f.deadline = held;
11991
+ if (held < next) next = held;
11992
+ this.mortal[kept++] = f;
11993
+ continue;
11994
+ }
11828
11995
  f.isMortal = false;
11829
11996
  f.dying = true;
11830
11997
  f.mesh.matrixAutoUpdate = true;
@@ -11837,6 +12004,26 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
11837
12004
  this.mortal.length = kept;
11838
12005
  this.nextDeadline = next;
11839
12006
  }
12007
+ /**
12008
+ * 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
12009
+ * 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.
12010
+ */
12011
+ heldUntil(f) {
12012
+ const box = f.box ?? (f.box = restingBox(f));
12013
+ let until = f.deadline;
12014
+ for (const other of this.list) {
12015
+ if (other === f || other.rigid || other.body) continue;
12016
+ const on = other.box ?? (other.box = restingBox(other));
12017
+ const e = rotationOf(other.mesh.quaternion);
12018
+ 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);
12019
+ if (bottom <= f.mesh.position.y || bottom > box.top + SUPPORT_GAP) continue;
12020
+ if (Math.abs(on.x - box.x) >= on.reachX + box.reachX || Math.abs(on.z - box.z) >= on.reachZ + box.reachZ) continue;
12021
+ if (!shadowsOverlap(box.shadow, on.shadow, on.x - box.x, on.z - box.z)) continue;
12022
+ const gone = other.dying ? this.clock + other.fade * FADE_SECONDS3 : other.asleep ? other.deadline + FADE_SECONDS3 : this.clock + SETTLE_SECONDS;
12023
+ until = Math.max(until, gone);
12024
+ }
12025
+ return until;
12026
+ }
11840
12027
  /** What lies inside the disc looks at the ground again: it was dug or heaped under it. */
11841
12028
  wakeRegion(x, z, radius) {
11842
12029
  for (const f of this.list) {
@@ -11855,7 +12042,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
11855
12042
  const f = this.active[i];
11856
12043
  if (f.dying) {
11857
12044
  f.mesh.matrixAutoUpdate = true;
11858
- f.fade -= dt / 0.6;
12045
+ f.fade -= dt / FADE_SECONDS3;
11859
12046
  if (f.fade <= 0) {
11860
12047
  this.instances.remove(f.mesh);
11861
12048
  this.o.scene.remove(f.mesh);
@@ -12004,36 +12191,22 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
12004
12191
  if (mesh2.position.y - ext < gy) {
12005
12192
  contact = true;
12006
12193
  mesh2.position.y = gy + ext;
12007
- if (f.vel.y < -0.6) {
12008
- f.vel.y = -f.vel.y * 0.25;
12009
- f.vel.x *= 0.7;
12010
- f.vel.z *= 0.7;
12011
- f.ang.multiplyScalar(0.55);
12012
- const tumble = Math.min(1, TUMBLE_REACH / Math.max(h.x, h.y, h.z));
12013
- f.ang.x += (this.rng() - 0.5) * 2 * tumble;
12014
- f.ang.z += (this.rng() - 0.5) * 2 * tumble;
12194
+ if (pile > floor2) {
12195
+ turning = this.restOnDebris(f, { g, dt, gy, sc, tip });
12015
12196
  } else {
12016
- f.vel.y = 0;
12017
- const sliding = Math.hypot(f.vel.x, f.vel.z);
12018
- const kept = sliding > 0 ? Math.max(0, sliding - DEBRIS_FRICTION * g * dt) / sliding : 0;
12019
- f.vel.x *= kept;
12020
- f.vel.z *= kept;
12021
- f.ang.multiplyScalar(Math.exp(-6 * dt));
12197
+ writeCorners({ e, half: this.scaledHalf(f, sc), corners: this.corners });
12198
+ findSupport({ corners: this.corners, reach: Math.max(h.x, h.y, h.z) * sc, support: this.support });
12199
+ if (!f.contact && this.bounces(f, { g, dt, sc })) {
12200
+ this.bounce(f, sc);
12201
+ } else {
12202
+ f.vel.y = 0;
12203
+ turning = this.restOn(f, { g, dt, sc });
12204
+ }
12022
12205
  }
12023
12206
  } else {
12024
12207
  f.ang.multiplyScalar(Math.exp(-0.15 * dt));
12025
12208
  if (f.air > 0) this.flutter(f, dt);
12026
12209
  }
12027
- if (contact && tip) {
12028
- f.vel.x += tip.x * TIP_SPEED * dt * 10;
12029
- f.vel.z += tip.z * TIP_SPEED * dt * 10;
12030
- f.ang.x += tip.z * TIP_TURN * dt * 10;
12031
- f.ang.z -= tip.x * TIP_TURN * dt * 10;
12032
- } else if (contact && f.vel.lengthSq() < 4) {
12033
- turning = this.settle(f, dt);
12034
- this.m.makeRotationFromQuaternion(mesh2.quaternion);
12035
- mesh2.position.y = gy + (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
12036
- }
12037
12210
  f.contact = contact;
12038
12211
  if (contact && !turning && isSlow(f)) {
12039
12212
  f.still += dt;
@@ -12086,7 +12259,8 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
12086
12259
  for (const other of this.list) {
12087
12260
  if (other === f || other.dying) continue;
12088
12261
  const box = other.box ?? (other.box = restingBox(other));
12089
- if (box.top > bottom + PILE_STEP || box.top < top - PILE_STEP) continue;
12262
+ const beneath = !other.asleep && other.mesh.position.y < bottom;
12263
+ if (box.top > bottom + PILE_STEP && !beneath || box.top < top - PILE_STEP) continue;
12090
12264
  if (Math.abs(box.x - p.x) >= box.reachX + reachX || Math.abs(box.z - p.z) >= box.reachZ + reachZ) continue;
12091
12265
  if (!shadowsOverlap(mine, box.shadow, box.x - p.x, box.z - p.z)) continue;
12092
12266
  if (box.top > top + PILE_STEP) under.length = 0;
@@ -12095,13 +12269,16 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
12095
12269
  }
12096
12270
  this.offEdge = null;
12097
12271
  if (under.length > 0 && !under.some((box) => box.top >= top - PILE_STEP && shadowsOverlap(POINT, box.shadow, box.x - p.x, box.z - p.z))) {
12098
- let x = 0, z = 0;
12099
- for (const box of under) {
12100
- x += p.x - box.x;
12101
- z += p.z - box.z;
12272
+ const contacts = contactsOf(mine, under, p.x, p.z);
12273
+ if (!isOverSupport(contacts, [0, 0], 0)) {
12274
+ let x = 0, z = 0;
12275
+ for (const [cx, cz] of contacts) {
12276
+ x -= cx;
12277
+ z -= cz;
12278
+ }
12279
+ const d = Math.hypot(x, z);
12280
+ this.offEdge = d > 1e-6 ? { x: x / d, z: z / d } : { x: 1, z: 0 };
12102
12281
  }
12103
- const d = Math.hypot(x, z);
12104
- this.offEdge = d > 1e-6 ? { x: x / d, z: z / d } : { x: 1, z: 0 };
12105
12282
  }
12106
12283
  return top;
12107
12284
  }
@@ -12113,8 +12290,41 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
12113
12290
  f.ang.x += (this.rng() - 0.5) * SHEET_SWAY * 4 * dt;
12114
12291
  f.ang.z += (this.rng() - 0.5) * SHEET_SWAY * 4 * dt;
12115
12292
  }
12116
- /** Tip the fragment towards lying on its flattest face. True while its end is still more than `LAID_GAP` from lying. */
12117
- settle(f, dt) {
12293
+ /**
12294
+ * 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
12295
+ * it when its centre is past it (`tip`), else laid flat (`layOnDebris`). True while it is still being laid flat.
12296
+ */
12297
+ restOnDebris(f, params) {
12298
+ const { g, dt, gy, sc, tip } = params;
12299
+ const h = f.half;
12300
+ if (f.vel.y < -DEBRIS_BOUNCE_SPEED) {
12301
+ f.vel.y = -f.vel.y * GROUND_RESTITUTION;
12302
+ f.vel.x *= GROUND_GLIDE;
12303
+ f.vel.z *= GROUND_GLIDE;
12304
+ f.ang.multiplyScalar(GROUND_SPIN_KEPT);
12305
+ const tumble = Math.min(1, TUMBLE_REACH / Math.max(h.x, h.y, h.z));
12306
+ f.ang.x += (this.rng() - 0.5) * 2 * tumble;
12307
+ f.ang.z += (this.rng() - 0.5) * 2 * tumble;
12308
+ } else {
12309
+ f.vel.y = 0;
12310
+ this.slide(f, g, dt);
12311
+ f.ang.multiplyScalar(Math.exp(-6 * dt));
12312
+ }
12313
+ if (tip) {
12314
+ f.vel.x += tip.x * TIP_SPEED * dt * 10;
12315
+ f.vel.z += tip.z * TIP_SPEED * dt * 10;
12316
+ f.ang.x += tip.z * TIP_TURN * dt * 10;
12317
+ f.ang.z -= tip.x * TIP_TURN * dt * 10;
12318
+ return false;
12319
+ }
12320
+ if (f.vel.lengthSq() >= 4) return false;
12321
+ const turning = this.layOnDebris(f, dt);
12322
+ const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
12323
+ f.mesh.position.y = gy + (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
12324
+ return turning;
12325
+ }
12326
+ /** 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. */
12327
+ layOnDebris(f, dt) {
12118
12328
  const mesh2 = f.mesh;
12119
12329
  this.m.makeRotationFromQuaternion(mesh2.quaternion);
12120
12330
  const e = this.m.elements;
@@ -12134,9 +12344,204 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
12134
12344
  this.w.set(0, 1, 0);
12135
12345
  this.dq.setFromUnitVectors(this.v, this.w);
12136
12346
  this.q.copy(this.dq).multiply(mesh2.quaternion);
12137
- mesh2.quaternion.slerp(this.q, clamp6(dt * 7, 0, 1));
12347
+ mesh2.quaternion.slerp(this.q, Math.min(1, dt * DEBRIS_LAY_RATE));
12138
12348
  return Math.sqrt(Math.max(0, 1 - this.v.y * this.v.y)) * Math.max(half[0], half[1], half[2]) > LAID_GAP;
12139
12349
  }
12350
+ /** The fragment's halves (m) at its fade, into `halves`. */
12351
+ scaledHalf(f, sc) {
12352
+ this.halves.x = f.half.x * sc;
12353
+ this.halves.y = f.half.y * sc;
12354
+ this.halves.z = f.half.z * sc;
12355
+ return this.halves;
12356
+ }
12357
+ /** The speed (m/s, up) of the point it rests on (`support`): its centre's, and its spin's about the centre there. */
12358
+ supportSpeed(f) {
12359
+ const s = this.support;
12360
+ return f.vel.y + f.ang.z * s.x - f.ang.x * s.z;
12361
+ }
12362
+ /**
12363
+ * 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
12364
+ * step of gravity gives what lies on it.
12365
+ */
12366
+ bounces(f, params) {
12367
+ const { g, dt, sc } = params;
12368
+ const thinnest = Math.min(f.half.x, f.half.y, f.half.z) * sc;
12369
+ const least = Math.max(2 * g * dt, Math.sqrt(2 * g * BOUNCE_LIFT_SHARE * thinnest) / GROUND_RESTITUTION);
12370
+ return this.supportSpeed(f) < -least;
12371
+ }
12372
+ /**
12373
+ * 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
12374
+ * 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,
12375
+ * so a board metres long is turned as a long body is, not as a chip.
12376
+ */
12377
+ bounce(f, sc) {
12378
+ const s = this.support;
12379
+ const into = this.supportSpeed(f);
12380
+ const rough = ROUGHNESS_SHARE * Math.max(f.half.x, f.half.y, f.half.z) * sc;
12381
+ s.x += (this.rng() - 0.5) * 2 * rough;
12382
+ s.z += (this.rng() - 0.5) * 2 * rough;
12383
+ const turn2 = this.w.set(-s.z, 0, s.x);
12384
+ applyInverseInertia({ e: this.m.elements, half: this.scaledHalf(f, sc), v: turn2 });
12385
+ const share = 1 + (turn2.z * s.x - turn2.x * s.z);
12386
+ const impulse = -(1 + GROUND_RESTITUTION) * into / share;
12387
+ f.vel.y += impulse;
12388
+ f.ang.addScaledVector(turn2, impulse);
12389
+ f.vel.x *= GROUND_GLIDE;
12390
+ f.vel.z *= GROUND_GLIDE;
12391
+ f.ang.multiplyScalar(GROUND_SPIN_KEPT);
12392
+ }
12393
+ /**
12394
+ * 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
12395
+ * 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).
12396
+ */
12397
+ slide(f, g, dt) {
12398
+ const sliding = Math.hypot(f.vel.x, f.vel.z);
12399
+ const kept = sliding > 0 ? Math.max(0, sliding - DEBRIS_FRICTION * g * dt) / sliding : 0;
12400
+ f.vel.x *= kept;
12401
+ f.vel.z *= kept;
12402
+ }
12403
+ /**
12404
+ * 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
12405
+ * 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
12406
+ * while it is still turning over.
12407
+ */
12408
+ restOn(f, params) {
12409
+ const { g, dt, sc } = params;
12410
+ const s = this.support;
12411
+ const ang = f.ang;
12412
+ const dx = -s.x, dy = -s.y, dz = -s.z;
12413
+ f.vel.x -= ang.y * dz - ang.z * dy;
12414
+ f.vel.z -= ang.x * dy - ang.y * dx;
12415
+ this.slide(f, g, dt);
12416
+ const footX = f.vel.x;
12417
+ const footZ = f.vel.z;
12418
+ const half = this.scaledHalf(f, sc);
12419
+ const gyration = Math.sqrt(inertiaAbout({ e: this.m.elements, half, x: 0, y: 1, z: 0 }));
12420
+ if (gyration > 0) ang.y = Math.sign(ang.y) * Math.max(0, Math.abs(ang.y) - DEBRIS_FRICTION * g * dt / gyration);
12421
+ if (s.count >= 3) {
12422
+ ang.x = 0;
12423
+ ang.z = 0;
12424
+ } else if (s.count === 2) {
12425
+ const along2 = ang.x * s.edgeX + ang.z * s.edgeZ;
12426
+ ang.x = along2 * s.edgeX;
12427
+ ang.z = along2 * s.edgeZ;
12428
+ }
12429
+ const lever = this.lever;
12430
+ lever.x = dx;
12431
+ lever.y = dy;
12432
+ lever.z = dz;
12433
+ const slow = footX * footX + footZ * footZ < 2 * g * SETTLE_FALL_HALVES * Math.max(half.x, half.y, half.z);
12434
+ const turning = slow && this.settle(f, g, dt);
12435
+ f.vel.x = footX + (ang.y * lever.z - ang.z * lever.y);
12436
+ f.vel.y = ang.z * lever.x - ang.x * lever.z;
12437
+ f.vel.z = footZ + (ang.x * lever.y - ang.y * lever.x);
12438
+ return turning;
12439
+ }
12440
+ /**
12441
+ * 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
12442
+ * 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
12443
+ * 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
12444
+ * holds it. True while it still turns.
12445
+ */
12446
+ settle(f, g, dt) {
12447
+ const s = this.support;
12448
+ const e = this.m.elements;
12449
+ const half = this.halves;
12450
+ const reach2 = Math.max(half.x, half.y, half.z);
12451
+ const lever = this.lever;
12452
+ let torqueX = g * lever.z;
12453
+ let torqueZ = -g * lever.x;
12454
+ if (s.count >= 3) {
12455
+ torqueX = 0;
12456
+ torqueZ = 0;
12457
+ } else if (s.count === 2) {
12458
+ const along2 = torqueX * s.edgeX + torqueZ * s.edgeZ;
12459
+ torqueX = along2 * s.edgeX;
12460
+ torqueZ = along2 * s.edgeZ;
12461
+ }
12462
+ const torque = Math.hypot(torqueX, torqueZ);
12463
+ const ang = f.ang;
12464
+ if (torque <= g * SUPPORT_SHARE * reach2 && ang.x === 0 && ang.z === 0) {
12465
+ if (s.count >= 3 && !this.isUnsteady(f)) return false;
12466
+ if (!this.nudge(f, g)) return false;
12467
+ } else if (torque > 0) {
12468
+ const axisX = torqueX / torque;
12469
+ const axisZ = torqueZ / torque;
12470
+ const along2 = lever.x * axisX + lever.z * axisZ;
12471
+ 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;
12472
+ ang.x += axisX * torque / inertia * dt;
12473
+ ang.z += axisZ * torque / inertia * dt;
12474
+ }
12475
+ const spin = Math.hypot(ang.x, ang.z);
12476
+ if (spin === 0) return false;
12477
+ const landing2 = turnToLanding({ corners: this.corners, pivot: { x: -lever.x, y: -lever.y, z: -lever.z }, axisX: ang.x / spin, axisZ: ang.z / spin, reach: reach2 });
12478
+ if (spin * dt > landing2) {
12479
+ ang.x *= landing2 / (spin * dt);
12480
+ ang.z *= landing2 / (spin * dt);
12481
+ }
12482
+ return true;
12483
+ }
12484
+ /**
12485
+ * 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
12486
+ * its flattest.
12487
+ */
12488
+ isUnsteady(f) {
12489
+ const e = this.m.elements;
12490
+ const up = [Math.abs(e[1]), Math.abs(e[5]), Math.abs(e[9])];
12491
+ const half = [f.half.x, f.half.y, f.half.z];
12492
+ let small = 0;
12493
+ let standing = 0;
12494
+ for (let i = 1; i < 3; i++) {
12495
+ if (half[i] < half[small]) small = i;
12496
+ if (up[i] > up[standing]) standing = i;
12497
+ }
12498
+ if (f.body) return half[standing] > half[small] * (1 + SUPPORT_SHARE);
12499
+ const across = Math.min(...half.filter((_, i) => i !== standing));
12500
+ return across < STAND_RATIO * half[standing];
12501
+ }
12502
+ /**
12503
+ * 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 ω² =
12504
+ * 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.
12505
+ */
12506
+ nudge(f, g) {
12507
+ const e = this.m.elements;
12508
+ const half = this.halves;
12509
+ const tolerance = SUPPORT_SHARE * Math.max(half.x, half.y, half.z);
12510
+ let small = 0;
12511
+ if (half.y < half.x) small = 1;
12512
+ if (half.z < (small === 1 ? half.y : half.x)) small = 2;
12513
+ const sign = e[small * 4 + 1] >= 0 ? 1 : -1;
12514
+ const axisX = -e[small * 4 + 2] * sign;
12515
+ const axisZ = e[small * 4] * sign;
12516
+ const length = Math.hypot(axisX, axisZ);
12517
+ if (length < SUPPORT_SHARE) return false;
12518
+ const kx = axisX / length;
12519
+ const kz = axisZ / length;
12520
+ const c = this.corners;
12521
+ const floor2 = this.support.y + tolerance;
12522
+ let far2 = -Infinity;
12523
+ for (let i = 0; i < 8; i++) if (c[3 * i + 1] <= floor2) far2 = Math.max(far2, kx * c[3 * i + 2] - kz * c[3 * i]);
12524
+ let count = 0, x = 0, y = 0, z = 0;
12525
+ for (let i = 0; i < 8; i++) {
12526
+ if (c[3 * i + 1] > floor2 || kx * c[3 * i + 2] - kz * c[3 * i] < far2 - tolerance) continue;
12527
+ count++;
12528
+ x += c[3 * i];
12529
+ y += c[3 * i + 1];
12530
+ z += c[3 * i + 2];
12531
+ }
12532
+ const lever = this.lever;
12533
+ lever.x = -x / count;
12534
+ lever.y = -y / count;
12535
+ lever.z = -z / count;
12536
+ const along2 = lever.x * kx + lever.z * kz;
12537
+ const square = lever.x * lever.x + lever.y * lever.y + lever.z * lever.z - along2 * along2;
12538
+ const rise = Math.max(Math.sqrt(square) - lever.y, tolerance);
12539
+ const inertia = inertiaAbout({ e, half, x: kx, y: 0, z: kz }) + square;
12540
+ const spinOfInertia = Math.sqrt(2 * g * rise * (1 + NUDGE_ENERGY_SHARE) / inertia);
12541
+ f.ang.x = kx * spinOfInertia;
12542
+ f.ang.z = kz * spinOfInertia;
12543
+ return true;
12544
+ }
12140
12545
  /** Remove the fragments of one object (after `restore`). */
12141
12546
  removeHome(home) {
12142
12547
  for (let i = this.list.length - 1; i >= 0; i--) {
@@ -14876,6 +15281,10 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
14876
15281
  var TILE_KEY_ORIGIN = 32768;
14877
15282
  var MU_STATIC = 0.7;
14878
15283
  var ROLLING_RESISTANCE = 0.015;
15284
+ var roundPartsRadius = (parts) => {
15285
+ if (!parts || parts.length === 0 || !parts.every((part) => part.round && !part.hull)) return void 0;
15286
+ return Math.max(...parts.map((part) => (part.half[0] + part.half[2]) / 2));
15287
+ };
14879
15288
  var MU_DYNAMIC = 0.6;
14880
15289
  var GROUND = { frictionStatic: MU_STATIC, frictionDynamic: MU_DYNAMIC, restitution: 0, rolling: ROLLING_RESISTANCE };
14881
15290
  var tileRollingAt = (rollingAt, x, z) => rollingAt((Math.floor(x / TILE) + 0.5) * TILE, (Math.floor(z / TILE) + 0.5) * TILE);
@@ -15424,9 +15833,13 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
15424
15833
  out[round2.axis] = about;
15425
15834
  return out;
15426
15835
  }
15427
- /** A round piece rolls to a stop by its surface's rolling resistance (the adapter's rule; the engine has none). */
15836
+ /**
15837
+ * 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
15838
+ * 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.
15839
+ */
15428
15840
  rollingOf(b) {
15429
- return b.round ? { coefficient: b.friction?.rolling ?? ROLLING_RESISTANCE, radius: b.round.radius, damping: b.friction?.rollingDamping ?? PIECE_ROLLING_DAMPING } : void 0;
15841
+ const radius = b.round?.radius ?? roundPartsRadius(b.partBoxes);
15842
+ return radius === void 0 ? void 0 : { coefficient: b.friction?.rolling ?? ROLLING_RESISTANCE, radius, damping: b.friction?.rollingDamping ?? PIECE_ROLLING_DAMPING };
15430
15843
  }
15431
15844
  material(b) {
15432
15845
  return {
@@ -16387,46 +16800,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16387
16800
  return { loop: createPhysicsLoop(option), stepsItself: true };
16388
16801
  };
16389
16802
 
16390
- // src/support-hull.ts
16391
- var cross2 = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
16392
- function planHull(points) {
16393
- const sorted = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
16394
- if (sorted.length < 3) return sorted;
16395
- const lower = [];
16396
- for (const p of sorted) {
16397
- while (lower.length >= 2 && cross2(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) lower.pop();
16398
- lower.push(p);
16399
- }
16400
- const upper = [];
16401
- for (let i = sorted.length - 1; i >= 0; i--) {
16402
- const p = sorted[i];
16403
- while (upper.length >= 2 && cross2(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) upper.pop();
16404
- upper.push(p);
16405
- }
16406
- return [...lower.slice(0, -1), ...upper.slice(0, -1)];
16407
- }
16408
- function toSegment(p, a, b) {
16409
- const dx = b[0] - a[0];
16410
- const dz = b[1] - a[1];
16411
- const length = dx * dx + dz * dz;
16412
- const t = length > 0 ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dz) / length)) : 0;
16413
- return Math.hypot(p[0] - a[0] - t * dx, p[1] - a[1] - t * dz);
16414
- }
16415
- function isOverSupport(points, centre, tolerance) {
16416
- const hull = planHull(points);
16417
- if (hull.length === 0) return false;
16418
- if (hull.length === 1) return Math.hypot(centre[0] - hull[0][0], centre[1] - hull[0][1]) <= tolerance;
16419
- let inside = hull.length >= 3;
16420
- let nearest = Infinity;
16421
- for (let i = 0; i < hull.length; i++) {
16422
- const a = hull[i];
16423
- const b = hull[(i + 1) % hull.length];
16424
- if (hull.length >= 3 && cross2(a, b, centre) < 0) inside = false;
16425
- nearest = Math.min(nearest, toSegment(centre, a, b));
16426
- }
16427
- return inside || nearest <= tolerance;
16428
- }
16429
-
16430
16803
  // src/pieces.ts
16431
16804
  var BALANCE_TOLERANCE = 0.01;
16432
16805
  var MU_STATIC2 = 0.7;
@@ -21318,6 +21691,12 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
21318
21691
  var MASONRY_CRUMBLE_SPEED = 0.6;
21319
21692
  var MASONRY_TOPPLE = 0.4;
21320
21693
  var MASONRY_TOPPLE_MIN_HEIGHT = 0.5;
21694
+ var SPIN_REACH = 0.1;
21695
+ var bornSpin = (size, chipSpin, rnd2) => {
21696
+ const reach2 = Math.hypot(size[0] ?? 0, size[1] ?? 0, size[2] ?? 0) / 2;
21697
+ const scale = reach2 <= SPIN_REACH ? 1 : SPIN_REACH / reach2;
21698
+ return [(rnd2() - 0.5) * chipSpin * scale, (rnd2() - 0.5) * chipSpin * scale, (rnd2() - 0.5) * chipSpin * scale];
21699
+ };
21321
21700
  var TILE_CLUMPS = 160;
21322
21701
  var TILE_THICKNESS = 0.018;
21323
21702
  var PANEL_EDGE_PILLAR = 2;
@@ -21897,7 +22276,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
21897
22276
  return (above2 + p.st.mass * share) / load;
21898
22277
  };
21899
22278
  };
21900
- const contactsOf = (p, support) => {
22279
+ const contactsOf2 = (p, support) => {
21901
22280
  const box = contactBox(p, support);
21902
22281
  const broken = support.rt.broken;
21903
22282
  if (!broken?.length) return [box];
@@ -21942,7 +22321,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
21942
22321
  }
21943
22322
  const corners = [];
21944
22323
  for (const k of lower) {
21945
- for (const [lo, hi] of contactsOf(p, o.parts[k])) {
22324
+ for (const [lo, hi] of contactsOf2(p, o.parts[k])) {
21946
22325
  for (let c = 0; c < 4; c++) {
21947
22326
  const local2 = [c & 1 ? hi[0] : lo[0], (lo[1] + hi[1]) / 2, c & 2 ? hi[2] : lo[2]];
21948
22327
  const at3 = toWorld(p.pos, p.q, local2);
@@ -21998,13 +22377,13 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
21998
22377
  };
21999
22378
  const specSpans = /* @__PURE__ */ new WeakMap();
22000
22379
  const spanOver = (p, g, supports) => {
22001
- if (p.posed || supports.some((k) => k.posed || k.rt.broken?.length)) return spanOf(g, supports.flatMap((k) => contactsOf(p, k)));
22380
+ if (p.posed || supports.some((k) => k.posed || k.rt.broken?.length)) return spanOf(g, supports.flatMap((k) => contactsOf2(p, k)));
22002
22381
  let byKey = specSpans.get(p.spec);
22003
22382
  if (!byKey) specSpans.set(p.spec, byKey = /* @__PURE__ */ new Map());
22004
22383
  const key = supports.map((k) => k.spec.id).join("\0");
22005
22384
  const known = byKey.get(key);
22006
22385
  if (known !== void 0) return known;
22007
- const span = spanOf(g, supports.flatMap((k) => contactsOf(p, k)));
22386
+ const span = spanOf(g, supports.flatMap((k) => contactsOf2(p, k)));
22008
22387
  byKey.set(key, span);
22009
22388
  return span;
22010
22389
  };
@@ -22092,7 +22471,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
22092
22471
  }
22093
22472
  const d = p.dims;
22094
22473
  const L = [0, 1, 2].sort((a, b) => d[b] - d[a])[0];
22095
- const spans2 = supports.flatMap((k) => contactsOf(p, k)).map(([lo, hi]) => [lo[L], hi[L]]);
22474
+ const spans2 = supports.flatMap((k) => contactsOf2(p, k)).map(([lo, hi]) => [lo[L], hi[L]]);
22096
22475
  let far2 = 0;
22097
22476
  for (let i = 0; i <= 16; i++) {
22098
22477
  const x = (i / 16 - 0.5) * d[L];
@@ -23933,7 +24312,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
23933
24312
  size: [cell.size[0], cell.size[1], cell.size[2]],
23934
24313
  rotation: q,
23935
24314
  velocity: [dv[0] * k, dv[1] * k + 0.5 * rnd2(), dv[2] * k],
23936
- angularVelocity: [(rnd2() - 0.5) * 8, (rnd2() - 0.5) * 8, (rnd2() - 0.5) * 8],
24315
+ angularVelocity: bornSpin(cell.size, 8, rnd2),
23937
24316
  mass: mass * faceArea(cell) / total,
23938
24317
  ...marks.length > 0 ? { marks } : {},
23939
24318
  ...inner && laid ? { unit: true } : inner ? { inner } : {}
@@ -23972,7 +24351,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
23972
24351
  size,
23973
24352
  rotation: q,
23974
24353
  velocity: [out0[0] * k, out0[1] * k + 0.5 * rnd2(), out0[2] * k],
23975
- angularVelocity: [(rnd2() - 0.5) * 12, (rnd2() - 0.5) * 12, (rnd2() - 0.5) * 12],
24354
+ angularVelocity: bornSpin(size, 12, rnd2),
23976
24355
  mass: layer3.mat.density * size[0] * size[1] * size[2]
23977
24356
  });
23978
24357
  }
@@ -24298,7 +24677,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
24298
24677
  size,
24299
24678
  rotation: q,
24300
24679
  velocity: [vdir[0] * k, vdir[1] * k + 0.5 * rnd2(), vdir[2] * k],
24301
- angularVelocity: [(rnd2() - 0.5) * 20, (rnd2() - 0.5) * 20, (rnd2() - 0.5) * 20],
24680
+ angularVelocity: bornSpin(size, 20, rnd2),
24302
24681
  mass: mass / n,
24303
24682
  inner: true
24304
24683
  });
@@ -24704,7 +25083,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
24704
25083
  size: f.size,
24705
25084
  rotation: quat.mul(q, f.q),
24706
25085
  velocity: [dv[0] * k, dv[1] * k + 0.5 * rnd2(), dv[2] * k],
24707
- angularVelocity: [(rnd2() - 0.5) * 12, (rnd2() - 0.5) * 12, (rnd2() - 0.5) * 12],
25086
+ angularVelocity: bornSpin(f.size, 12, rnd2),
24708
25087
  mass: mass * f.weight / totW,
24709
25088
  ...marks.length > 0 ? { marks } : {},
24710
25089
  ...f.inner ? { inner: true } : {}
@@ -24843,7 +25222,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
24843
25222
  rotation: q,
24844
25223
  velocity: v,
24845
25224
  inner: true,
24846
- angularVelocity: [(rnd2() - 0.5) * 30, (rnd2() - 0.5) * 30, (rnd2() - 0.5) * 30],
25225
+ angularVelocity: bornSpin(size, 30, rnd2),
24847
25226
  mass
24848
25227
  });
24849
25228
  }
@@ -29166,7 +29545,8 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
29166
29545
  var UNDERMINED_STAND = 0.4;
29167
29546
  var UNDERMINED_KEEP = 0.6;
29168
29547
  var SOLIDS_MAX = 64;
29169
- var UNDERMINED_PUSH = 0.6;
29548
+ var UNDERMINED_FROUDE = 0.13;
29549
+ var GRAVITY5 = 9.81;
29170
29550
  var DEBRIS_ID = "debris#";
29171
29551
  var UNDERMINED_STILL = 0.05;
29172
29552
  var SOLID_WELD = 1e-4;
@@ -31026,13 +31406,13 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
31026
31406
  }
31027
31407
  /**
31028
31408
  * Plays the effect the event map names for a destruction event (`data/event-effects.json` of `game-vfx`, or `RendererOptions.eventEffects`) at a point.
31029
- * `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
31030
- * leave out the burst it replaces.
31409
+ * `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
31410
+ * whether one played, so the caller can leave out the burst it replaces.
31031
31411
  */
31032
- playEvent(event, point, play) {
31412
+ playEvent(event, point, play, normal) {
31033
31413
  const id = effectForEvent(play && event.material === void 0 ? { ...event, material: play.material } : event, this.o.eventEffects ?? EVENT_EFFECTS_MAP);
31034
31414
  if (!id) return false;
31035
- return this.vfx.play(id, point, { seed: hashNumbers(point[0], point[1], point[2], this.time), ...play ? { values: play.values } : {} }) !== null;
31415
+ return this.vfx.play(id, point, { seed: hashNumbers(point[0], point[1], point[2], this.time), ...play ? { values: play.values } : {}, ...normal ? { normal } : {} }) !== null;
31036
31416
  }
31037
31417
  /** The family and colours of a part's material, for the effects of the event map. */
31038
31418
  partPlay(objectId, partId) {
@@ -31052,7 +31432,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
31052
31432
  const core = def?.visual?.innerColor;
31053
31433
  const look = debrisLook(def, "spall", { partColor: this.partColor(pv, def) ?? void 0 });
31054
31434
  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 });
31055
- else this.playEvent({ kind: "spall", material: look.impact }, ev.point);
31435
+ else this.playEvent({ kind: "spall", material: look.impact }, ev.point, void 0, [normal.x, normal.y, normal.z]);
31056
31436
  if (core !== void 0) this.trickleFrom(ev.point, [normal.x, normal.y, normal.z], core, mv);
31057
31437
  if (mv.mesh.userData.drawnSpalls?.includes(ev.part)) return;
31058
31438
  this.visuals.addSpall(mv, wp, normal, Math.sqrt(ev.area / Math.PI), core ?? fallbackColor(def));
@@ -31772,7 +32152,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
31772
32152
  body.sleeping = false;
31773
32153
  body.still = 0;
31774
32154
  body.calmAt = null;
31775
- if (held.length > 0 && length > 1e-6) this.pieceSys.ruleImpulse(KIT_RULE.CHECK_UNDERMINED, body, [0, -body.mass * UNDERMINED_PUSH, 0], [centre[0] + gx, floor2, centre[2] + gz]);
32155
+ if (held.length > 0 && length > 1e-6) this.pieceSys.ruleImpulse(KIT_RULE.CHECK_UNDERMINED, body, [0, -body.mass * startDownSpeed(centre[1] - floor2), 0], [centre[0] + gx, floor2, centre[2] + gz]);
31776
32156
  if (!was) this.o.onLoosened?.(b.id, body, "ground");
31777
32157
  }
31778
32158
  }
@@ -31789,7 +32169,8 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
31789
32169
  if (this.standsWith(held, null, centre, feet.floor)) return;
31790
32170
  const inside = insideBy(convexHull2(held), [centre[0], centre[2]]);
31791
32171
  const reach2 = this.reachAlong(body, inside.out.x, inside.out.z);
31792
- this.pieceSys.ruleImpulse(KIT_RULE.GO_OVER_IF_BALANCED, body, [0, -body.mass * UNDERMINED_PUSH, 0], [centre[0] + inside.out.x * reach2, feet.floor, centre[2] + inside.out.z * reach2]);
32172
+ const mid = this.pieceSys.centerOf(body, new this.THREE.Vector3());
32173
+ this.pieceSys.ruleImpulse(KIT_RULE.GO_OVER_IF_BALANCED, body, [0, -body.mass * startDownSpeed(centre[1] - feet.floor), 0], [mid.x + inside.out.x * reach2, feet.floor, mid.z + inside.out.z * reach2]);
31793
32174
  }
31794
32175
  /** 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. */
31795
32176
  reachAlong(body, x, z) {
@@ -32854,6 +33235,9 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
32854
33235
  };
32855
33236
  return [...half(sorted), ...half([...sorted].reverse())];
32856
33237
  }
33238
+ function startDownSpeed(height) {
33239
+ return UNDERMINED_FROUDE * Math.sqrt(GRAVITY5 * height);
33240
+ }
32857
33241
  function centreOfBoxes(boxes) {
32858
33242
  const sum = [0, 0, 0];
32859
33243
  let total = 0;