@adatechnology/game-destruction-three 0.21.0 → 0.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -759,6 +759,110 @@ var GroundSpots = class {
759
759
  }
760
760
  };
761
761
 
762
+ // src/piece-contacts.ts
763
+ var SEGMENT_BOX_ROUNDS = 4;
764
+ var PARALLEL = 1e-4;
765
+ var sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
766
+ var dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
767
+ var along = (a, d, t) => [a[0] + d[0] * t, a[1] + d[1] * t, a[2] + d[2] * t];
768
+ var clamp01 = (t) => t < 0 ? 0 : t > 1 ? 1 : t;
769
+ var nearestOnSegment = (from, to, p) => {
770
+ const d = sub(to, from);
771
+ const len2 = dot(d, d);
772
+ return len2 < 1e-12 ? from : along(from, d, clamp01(dot(sub(p, from), d) / len2));
773
+ };
774
+ var nearestOfSegments = (p1, q1, p2, q2) => {
775
+ const d1 = sub(q1, p1);
776
+ const d2 = sub(q2, p2);
777
+ const r = sub(p1, p2);
778
+ const a = dot(d1, d1);
779
+ const e = dot(d2, d2);
780
+ const f = dot(d2, r);
781
+ let s = 0;
782
+ let t = 0;
783
+ if (a < 1e-12 && e < 1e-12) return [p1, p2];
784
+ if (a < 1e-12) t = clamp01(f / e);
785
+ else {
786
+ const c = dot(d1, r);
787
+ if (e < 1e-12) s = clamp01(-c / a);
788
+ else {
789
+ const b = dot(d1, d2);
790
+ const denom = a * e - b * b;
791
+ if (denom > PARALLEL * a * e) s = clamp01((b * f - c * e) / denom);
792
+ else {
793
+ const s0 = clamp01(-c / a);
794
+ const s1 = clamp01((b - c) / a);
795
+ s = (s0 + s1) / 2;
796
+ }
797
+ t = (b * s + f) / e;
798
+ if (t < 0) {
799
+ t = 0;
800
+ s = clamp01(-c / a);
801
+ } else if (t > 1) {
802
+ t = 1;
803
+ s = clamp01((b - c) / a);
804
+ }
805
+ }
806
+ }
807
+ return [along(p1, d1, s), along(p2, d2, t)];
808
+ };
809
+ var toBox = (box, p) => {
810
+ const d = sub(p, box.c);
811
+ const e = box.e;
812
+ return [e[0] * d[0] + e[1] * d[1] + e[2] * d[2], e[4] * d[0] + e[5] * d[1] + e[6] * d[2], e[8] * d[0] + e[9] * d[1] + e[10] * d[2]];
813
+ };
814
+ var fromBox = (box, l) => {
815
+ const e = box.e;
816
+ return [box.c[0] + e[0] * l[0] + e[4] * l[1] + e[8] * l[2], box.c[1] + e[1] * l[0] + e[5] * l[1] + e[9] * l[2], box.c[2] + e[2] * l[0] + e[6] * l[1] + e[10] * l[2]];
817
+ };
818
+ var capsuleBox = (from, to, radius, box) => {
819
+ let p = nearestOnSegment(from, to, box.c);
820
+ let l = toBox(box, p);
821
+ for (let round = 0; round < SEGMENT_BOX_ROUNDS; round++) {
822
+ const clamped = [Math.max(-box.half[0], Math.min(box.half[0], l[0])), Math.max(-box.half[1], Math.min(box.half[1], l[1])), Math.max(-box.half[2], Math.min(box.half[2], l[2]))];
823
+ p = nearestOnSegment(from, to, fromBox(box, clamped));
824
+ l = toBox(box, p);
825
+ }
826
+ const q = [Math.max(-box.half[0], Math.min(box.half[0], l[0])), Math.max(-box.half[1], Math.min(box.half[1], l[1])), Math.max(-box.half[2], Math.min(box.half[2], l[2]))];
827
+ const gap = [l[0] - q[0], l[1] - q[1], l[2] - q[2]];
828
+ const distance = Math.hypot(gap[0], gap[1], gap[2]);
829
+ if (distance >= radius) return null;
830
+ const e = box.e;
831
+ if (distance > 1e-6) {
832
+ const n = [gap[0] / distance, gap[1] / distance, gap[2] / distance];
833
+ const normal = [e[0] * n[0] + e[4] * n[1] + e[8] * n[2], e[1] * n[0] + e[5] * n[1] + e[9] * n[2], e[2] * n[0] + e[6] * n[1] + e[10] * n[2]];
834
+ return { point: fromBox(box, q), normal, depth: radius - distance };
835
+ }
836
+ let axis = 0;
837
+ for (let k = 1; k < 3; k++) if (box.half[k] - Math.abs(l[k]) < box.half[axis] - Math.abs(l[axis])) axis = k;
838
+ const sign = l[axis] < 0 ? -1 : 1;
839
+ return { point: p, normal: [sign * e[axis * 4], sign * e[axis * 4 + 1], sign * e[axis * 4 + 2]], depth: radius + box.half[axis] - Math.abs(l[axis]) };
840
+ };
841
+ function shapeContact(a, b) {
842
+ if (a.kind === "capsule" && b.kind === "capsule") {
843
+ const [pa, pb] = nearestOfSegments(a.from, a.to, b.from, b.to);
844
+ const d = sub(pa, pb);
845
+ const distance = Math.hypot(d[0], d[1], d[2]);
846
+ const reach = a.radius + b.radius;
847
+ if (distance >= reach) return null;
848
+ const normal = distance > 1e-6 ? [d[0] / distance, d[1] / distance, d[2] / distance] : [1, 0, 0];
849
+ return { point: along(pb, normal, b.radius - (reach - distance) / 2), normal, depth: reach - distance };
850
+ }
851
+ if (a.kind === "capsule" && b.kind === "boxes") {
852
+ let best = null;
853
+ for (const box of b.boxes) {
854
+ const hit = capsuleBox(a.from, a.to, a.radius, box);
855
+ if (hit && (!best || hit.depth > best.depth)) best = hit;
856
+ }
857
+ return best;
858
+ }
859
+ if (a.kind === "boxes" && b.kind === "capsule") {
860
+ const hit = shapeContact(b, a);
861
+ return hit && { point: hit.point, normal: [-hit.normal[0], -hit.normal[1], -hit.normal[2]], depth: hit.depth };
862
+ }
863
+ return null;
864
+ }
865
+
762
866
  // src/pieces.ts
763
867
  var SLAM_MIN_SPEED = 2;
764
868
  var SLAM_BOUNCE = 0.2;
@@ -767,6 +871,8 @@ var SUB_DT = 1 / 120;
767
871
  var MU_STATIC = 0.7;
768
872
  var MU_DYNAMIC = 0.6;
769
873
  var SLIP_SPEED = 0.02;
874
+ var ROLLING_RESISTANCE = 0.015;
875
+ var ROUND_LYING = Math.SQRT1_2;
770
876
  var SLEEP_AFTER = 0.5;
771
877
  var CALM_DRIFT = 0.01;
772
878
  var CALM_TURN = 0.05;
@@ -787,6 +893,13 @@ var SUPPORT_CHECKS2 = 16;
787
893
  var SUPPORT_GAP2 = 0.02;
788
894
  var WAKE_ABOVE_SPEED = 0.05;
789
895
  var WAKE_ABOVE_SPIN = 0.1;
896
+ var SURFACE_CELL = 0.5;
897
+ var SURFACE_CELLS_ACROSS = 24;
898
+ var cellKey = (ix, iz) => (ix + 32768) * 65536 + (iz + 32768);
899
+ var MAX_COLLISION_STEPS = 8;
900
+ var MIN_ROUND_RADIUS = 5e-3;
901
+ var BOUNCE_MIN_SPEED = 0.1;
902
+ var KNOCK_FLAT = 0.5;
790
903
  var topFace = 1;
791
904
  var boxTopAt = (s, x, z) => {
792
905
  const e = s.e;
@@ -819,8 +932,11 @@ var faceNormal = (s, axis) => {
819
932
  };
820
933
  var pairFriction = (a, b) => ({
821
934
  static: Math.sqrt((a?.static ?? MU_STATIC) * (b?.static ?? MU_STATIC)),
822
- dynamic: Math.sqrt((a?.dynamic ?? MU_DYNAMIC) * (b?.dynamic ?? MU_DYNAMIC))
935
+ dynamic: Math.sqrt((a?.dynamic ?? MU_DYNAMIC) * (b?.dynamic ?? MU_DYNAMIC)),
936
+ // the softer of the two sets how much a round thing rolling on the other sinks in and is held back
937
+ rolling: Math.max(a?.rolling ?? 0, b?.rolling ?? ROLLING_RESISTANCE)
823
938
  });
939
+ var pairRestitution = (a, b) => Math.sqrt((a ?? SLAM_BOUNCE) * (b ?? SLAM_BOUNCE));
824
940
  var PieceSystem = class {
825
941
  constructor(THREE, o) {
826
942
  __publicField(this, "THREE", THREE);
@@ -830,9 +946,20 @@ var PieceSystem = class {
830
946
  __publicField(this, "p");
831
947
  __publicField(this, "q");
832
948
  __publicField(this, "dq");
949
+ __publicField(this, "nextQ");
833
950
  __publicField(this, "m");
834
951
  __publicField(this, "ax");
835
952
  __publicField(this, "solids", []);
953
+ /** The boxes other pieces land on: a piece's own parts when it has them (`partBoxes`), else its box. */
954
+ __publicField(this, "surfaces", []);
955
+ /** The surfaces by the cells of a grid on the ground (`SURFACE_CELL`) their bounding circle covers: what is under a point is looked for in its cell only. */
956
+ __publicField(this, "surfaceGrid", /* @__PURE__ */ new Map());
957
+ /** The surfaces wider than `SURFACE_CELLS_ACROSS` cells (a slab come down whole): looked at for every point. */
958
+ __publicField(this, "wideSurfaces", []);
959
+ /** Each piece's box of the step (`solids`), by its body. */
960
+ __publicField(this, "solidOf", /* @__PURE__ */ new Map());
961
+ /** Each piece's surfaces of the step (its parts' boxes, or its box), by its body. */
962
+ __publicField(this, "surfacesOf", /* @__PURE__ */ new Map());
836
963
  __publicField(this, "supportTurn", 0);
837
964
  /** The surface normal and the friction of what the last `supportAt` found under the point (the ground and fixed tops are level: up, and no piece). */
838
965
  __publicField(this, "supportNormal", UP);
@@ -840,6 +967,7 @@ var PieceSystem = class {
840
967
  this.p = new THREE.Vector3();
841
968
  this.q = new THREE.Quaternion();
842
969
  this.dq = new THREE.Quaternion();
970
+ this.nextQ = new THREE.Quaternion();
843
971
  this.m = new THREE.Matrix4();
844
972
  this.ax = new THREE.Vector3();
845
973
  }
@@ -890,10 +1018,33 @@ var PieceSystem = class {
890
1018
  }
891
1019
  remove(objectId) {
892
1020
  const i = this.list.findIndex((b) => b.objectId === objectId);
893
- if (i >= 0) this.list.splice(i, 1);
1021
+ if (i < 0) return;
1022
+ const [gone] = this.list.splice(i, 1);
1023
+ if (gone) this.forgetSolids(gone);
894
1024
  }
895
1025
  clear() {
896
1026
  this.list.length = 0;
1027
+ this.solids = [];
1028
+ this.surfaces = [];
1029
+ this.solidOf.clear();
1030
+ this.surfacesOf.clear();
1031
+ this.surfaceGrid.clear();
1032
+ this.wideSurfaces.length = 0;
1033
+ }
1034
+ /** Drops a piece from the boxes of the step (`solids`, `surfaces` and their grid). */
1035
+ forgetSolids(body) {
1036
+ this.solidOf.delete(body);
1037
+ this.surfacesOf.delete(body);
1038
+ this.solids = this.solids.filter((s) => s.body !== body);
1039
+ this.surfaces = this.surfaces.filter((s) => s.body !== body);
1040
+ for (const [key, cell] of this.surfaceGrid) {
1041
+ const kept = cell.filter((s) => s.body !== body);
1042
+ if (kept.length === 0) this.surfaceGrid.delete(key);
1043
+ else if (kept.length !== cell.length) this.surfaceGrid.set(key, kept);
1044
+ }
1045
+ const wide = this.wideSurfaces.filter((s) => s.body !== body);
1046
+ this.wideSurfaces.length = 0;
1047
+ this.wideSurfaces.push(...wide);
897
1048
  }
898
1049
  /** `clear` and the clock back to 0. */
899
1050
  reset() {
@@ -934,9 +1085,17 @@ var PieceSystem = class {
934
1085
  mulInvInertiaWith(b, e, x, y, z) {
935
1086
  const [hx, hy, hz] = b.halfExtents;
936
1087
  const k = 3 / b.mass;
937
- const ix = k / (hy * hy + hz * hz);
938
- const iy = k / (hx * hx + hz * hz);
939
- const iz = k / (hx * hx + hy * hy);
1088
+ let ix = k / (hy * hy + hz * hz);
1089
+ let iy = k / (hx * hx + hz * hz);
1090
+ let iz = k / (hx * hx + hy * hy);
1091
+ const round = b.round;
1092
+ if (round) {
1093
+ const about = 1 / (round.inertia * b.mass * round.radius * round.radius);
1094
+ if (round.kind === "sphere") ix = iy = iz = about;
1095
+ else if (round.axis === 0) ix = about;
1096
+ else if (round.axis === 1) iy = about;
1097
+ else iz = about;
1098
+ }
940
1099
  const lx = (e[0] * x + e[1] * y + e[2] * z) * ix;
941
1100
  const ly = (e[4] * x + e[5] * y + e[6] * z) * iy;
942
1101
  const lz = (e[8] * x + e[9] * y + e[10] * z) * iz;
@@ -991,12 +1150,106 @@ var PieceSystem = class {
991
1150
  }
992
1151
  }
993
1152
  update(dt) {
994
- this.time += dt;
1153
+ const { steps, near } = this.collisionSteps(dt);
1154
+ if (steps === 1) {
1155
+ this.time += dt;
1156
+ this.step(dt);
1157
+ return;
1158
+ }
1159
+ const start = this.time;
1160
+ this.time = start + dt;
1161
+ const far = (b) => !near.has(b);
1162
+ this.step(dt, { moves: far, meets: (a, b) => far(a) && far(b) });
1163
+ for (let i = 0; i < steps; i++) {
1164
+ this.time = start + dt * (i + 1) / steps;
1165
+ this.step(dt / steps, { moves: (b) => near.has(b), meets: (a, b) => near.has(a) || near.has(b) });
1166
+ }
1167
+ }
1168
+ /**
1169
+ * Steps of an update: one, or more while a round piece and a piece it could meet within the update close in fast enough to pass through each other between two (closing at
1170
+ * their two speeds they cover at most the round one's radius per step: two cans at 3.6 m/s close 12 cm in a frame, a can is 7 cm across; a plank thrown at a can still counts).
1171
+ * A round piece with nothing in reach takes one step, so its sweep against the world (the costly part) runs once; `near` holds the pieces of the pairs that need more.
1172
+ */
1173
+ collisionSteps(dt) {
1174
+ let need = 1;
1175
+ const near = /* @__PURE__ */ new Set();
1176
+ const p = this.p;
1177
+ const speedOf = (b) => b.held ? 0 : Math.hypot(b.velocity[0], b.velocity[1], b.velocity[2]);
1178
+ for (const b of this.list) {
1179
+ if (!b.round || b.held) continue;
1180
+ const radius = Math.max(MIN_ROUND_RADIUS, b.round.radius);
1181
+ const speed = b.sleeping ? 0 : speedOf(b);
1182
+ const c = this.centerOf(b, p);
1183
+ const cx = c.x, cy = c.y, cz = c.z;
1184
+ const rb = Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]);
1185
+ for (const o of this.list) {
1186
+ if (o === b || b.sleeping && o.sleeping) continue;
1187
+ const closing = speed + (o.sleeping ? 0 : speedOf(o));
1188
+ if (closing * dt < radius * 0.5) continue;
1189
+ const reach = closing * dt + rb + Math.hypot(o.halfExtents[0], o.halfExtents[1], o.halfExtents[2]);
1190
+ const oc = this.centerOf(o, p);
1191
+ if (Math.abs(oc.x - cx) > reach || Math.abs(oc.y - cy) > reach || Math.abs(oc.z - cz) > reach) continue;
1192
+ const pair = Math.ceil(closing * dt / radius);
1193
+ if (pair < 2) continue;
1194
+ need = Math.max(need, pair);
1195
+ near.add(b);
1196
+ near.add(o);
1197
+ }
1198
+ }
1199
+ return { steps: Math.max(1, Math.min(MAX_COLLISION_STEPS, need)), near };
1200
+ }
1201
+ /** A step of `dt`: the pieces `only.moves` takes move (all when absent) and the pairs `only.meets` takes meet, so each pair meets in one of the steps of an update. */
1202
+ step(dt, only) {
995
1203
  this.solids = this.list.map((body) => {
996
1204
  const c = this.centerOf(body, new this.THREE.Vector3());
997
1205
  const h = body.halfExtents;
998
1206
  return { body, c: [c.x, c.y, c.z], e: [...new this.THREE.Matrix4().makeRotationFromQuaternion(body.root.quaternion).elements], half: h, r: Math.hypot(h[0], h[1], h[2]) };
999
1207
  });
1208
+ this.surfaces = this.solids.flatMap((whole) => {
1209
+ const parts = whole.body.partBoxes;
1210
+ if (!parts) return [whole];
1211
+ const e = whole.e;
1212
+ return parts.map(({ center, half }) => ({
1213
+ body: whole.body,
1214
+ c: [
1215
+ whole.c[0] + e[0] * center[0] + e[4] * center[1] + e[8] * center[2],
1216
+ whole.c[1] + e[1] * center[0] + e[5] * center[1] + e[9] * center[2],
1217
+ whole.c[2] + e[2] * center[0] + e[6] * center[1] + e[10] * center[2]
1218
+ ],
1219
+ e,
1220
+ half,
1221
+ r: Math.hypot(half[0], half[1], half[2])
1222
+ }));
1223
+ });
1224
+ this.solidOf.clear();
1225
+ for (const one of this.solids) this.solidOf.set(one.body, one);
1226
+ this.surfacesOf.clear();
1227
+ for (const one of this.surfaces) {
1228
+ const list = this.surfacesOf.get(one.body);
1229
+ if (list) list.push(one);
1230
+ else this.surfacesOf.set(one.body, [one]);
1231
+ }
1232
+ this.surfaceGrid.clear();
1233
+ this.wideSurfaces.length = 0;
1234
+ for (const one of this.surfaces) {
1235
+ const x0 = Math.floor((one.c[0] - one.r) / SURFACE_CELL);
1236
+ const x1 = Math.floor((one.c[0] + one.r) / SURFACE_CELL);
1237
+ const z0 = Math.floor((one.c[2] - one.r) / SURFACE_CELL);
1238
+ const z1 = Math.floor((one.c[2] + one.r) / SURFACE_CELL);
1239
+ if (x1 - x0 > SURFACE_CELLS_ACROSS || z1 - z0 > SURFACE_CELLS_ACROSS) {
1240
+ this.wideSurfaces.push(one);
1241
+ continue;
1242
+ }
1243
+ for (let ix = x0; ix <= x1; ix++) {
1244
+ for (let iz = z0; iz <= z1; iz++) {
1245
+ const key = cellKey(ix, iz);
1246
+ const cell = this.surfaceGrid.get(key);
1247
+ if (cell) cell.push(one);
1248
+ else this.surfaceGrid.set(key, [one]);
1249
+ }
1250
+ }
1251
+ }
1252
+ if (this.wideSurfaces.length > 0) for (const cell of this.surfaceGrid.values()) cell.push(...this.wideSurfaces);
1000
1253
  this.wakeUnsupported();
1001
1254
  for (const b of this.list) {
1002
1255
  if (b.held) {
@@ -1007,14 +1260,14 @@ var PieceSystem = class {
1007
1260
  b.calmAt = null;
1008
1261
  continue;
1009
1262
  }
1010
- if (b.sleeping) continue;
1263
+ if (b.sleeping || only && !only.moves(b)) continue;
1011
1264
  this.integrate(b, dt);
1012
1265
  if (b.sleeping) continue;
1013
1266
  const v = b.velocity;
1014
1267
  const w = b.angularVelocity;
1015
1268
  if (Math.hypot(v[0], v[1], v[2]) > WAKE_ABOVE_SPEED || Math.hypot(w[0], w[1], w[2]) > WAKE_ABOVE_SPIN) this.wakeAbove(b);
1016
1269
  }
1017
- this.separate(dt);
1270
+ this.collide(dt, only?.meets);
1018
1271
  }
1019
1272
  /** A few sleeping pieces each update (`SUPPORT_CHECKS`, in turn) look under them again: one whose table broke, or whose shelf fell over, falls. */
1020
1273
  wakeUnsupported() {
@@ -1043,11 +1296,15 @@ var PieceSystem = class {
1043
1296
  }
1044
1297
  return false;
1045
1298
  }
1299
+ /** The surfaces whose cell holds `(x, z)`, the wide ones (`wideSurfaces`) among them. */
1300
+ surfacesAt(x, z) {
1301
+ return this.surfaceGrid.get(cellKey(Math.floor(x / SURFACE_CELL), Math.floor(z / SURFACE_CELL))) ?? this.wideSurfaces;
1302
+ }
1046
1303
  /** The highest top of a piece (other than `self`) under `(x, z)` that a point at height `y` stands on (at most `step` above it), -Infinity when none; the boxes of the last update. */
1047
1304
  topAt(x, z, y, step, self) {
1048
1305
  let top = -Infinity;
1049
- for (const s of this.solids) {
1050
- if (s.body.objectId === self || s.body.held || Math.abs(x - s.c[0]) > s.r || Math.abs(z - s.c[2]) > s.r) continue;
1306
+ for (const s of this.surfacesAt(x, z)) {
1307
+ if (s.body.objectId === self || s.body.held && !s.body.carrying || Math.abs(x - s.c[0]) > s.r || Math.abs(z - s.c[2]) > s.r) continue;
1051
1308
  const t = boxTopAt(s, x, z);
1052
1309
  if (t > top && t <= y + step) top = t;
1053
1310
  }
@@ -1055,25 +1312,50 @@ var PieceSystem = class {
1055
1312
  }
1056
1313
  /** What lay asleep on `b` (a box over its top) wakes at once when `b` moves: a can on a table that goes over goes with it. */
1057
1314
  wakeAbove(b) {
1058
- const below = this.solids.find((s) => s.body === b);
1315
+ const below = this.solidOf.get(b);
1059
1316
  if (!below) return;
1060
1317
  const top = below.c[1] + below.half[0] * Math.abs(below.e[1]) + below.half[1] * Math.abs(below.e[5]) + below.half[2] * Math.abs(below.e[9]);
1061
- for (const s of this.solids) {
1062
- const o = s.body;
1063
- if (!o.sleeping || o.held || o === b) continue;
1064
- if (s.c[1] < below.c[1] || s.c[1] - s.r > top + STEP || Math.hypot(s.c[0] - below.c[0], s.c[2] - below.c[2]) > s.r + below.r) continue;
1318
+ const x0 = Math.floor((below.c[0] - below.r) / SURFACE_CELL);
1319
+ const x1 = Math.floor((below.c[0] + below.r) / SURFACE_CELL);
1320
+ const z0 = Math.floor((below.c[2] - below.r) / SURFACE_CELL);
1321
+ const z1 = Math.floor((below.c[2] + below.r) / SURFACE_CELL);
1322
+ const wide = x1 - x0 > SURFACE_CELLS_ACROSS || z1 - z0 > SURFACE_CELLS_ACROSS;
1323
+ const look = (part) => {
1324
+ const o = part.body;
1325
+ const s = this.solidOf.get(o);
1326
+ if (!s || !o.sleeping || o.held || o === b) return;
1327
+ if (s.c[1] < below.c[1] || s.c[1] - s.r > top + STEP || Math.hypot(s.c[0] - below.c[0], s.c[2] - below.c[2]) > s.r + below.r) return;
1328
+ if (boxTopAt(below, s.c[0], s.c[2]) === -Infinity && !this.restsOn(o, b, true)) return;
1065
1329
  o.sleeping = false;
1066
1330
  o.still = 0;
1067
1331
  o.calmAt = null;
1332
+ };
1333
+ if (wide) for (const s of this.solids) look(s);
1334
+ else for (let ix = x0; ix <= x1; ix++) for (let iz = z0; iz <= z1; iz++) for (const part of this.surfaceGrid.get(cellKey(ix, iz)) ?? []) look(part);
1335
+ }
1336
+ /**
1337
+ * Whether `o` rests on a top of `below` (its parts, or its box): a corner of `o` (its contacts or its box) is over that top and within `STEP` of it. With `lowest`, only its
1338
+ * lowest corners count (within `STEP` of its lowest): a can rolling by another is beside it, not on it; without, a board leaning on a seat by its high end rests on the seat.
1339
+ */
1340
+ restsOn(o, below, lowest) {
1341
+ const c = this.centerOf(o, this.p);
1342
+ const e = this.m.makeRotationFromQuaternion(o.root.quaternion).elements;
1343
+ const points = this.samples(o, false).map(([sx, sy, sz]) => [c.x + e[0] * sx + e[4] * sy + e[8] * sz, c.y + e[1] * sx + e[5] * sy + e[9] * sz, c.z + e[2] * sx + e[6] * sy + e[10] * sz]);
1344
+ let low = Infinity;
1345
+ for (const p of points) low = Math.min(low, p[1]);
1346
+ for (const [x, y, z] of points) {
1347
+ if (lowest && y > low + STEP) continue;
1348
+ for (const s of this.surfacesAt(x, z)) if (s.body === below && Math.abs(y - boxTopAt(s, x, z)) <= STEP) return true;
1068
1349
  }
1350
+ return false;
1069
1351
  }
1070
1352
  /** Where a corner of `self` at `(x, y, z)` lands: the ground, the top of another piece or of something fixed under it. */
1071
1353
  supportAt(self, x, y, z, step) {
1072
1354
  let top = this.o.groundAt(x, z);
1073
1355
  let on = null;
1074
1356
  let face = 1;
1075
- for (const s of this.solids) {
1076
- if (s.body === self || s.body.held || Math.abs(x - s.c[0]) > s.r || Math.abs(z - s.c[2]) > s.r) continue;
1357
+ for (const s of this.surfacesAt(x, z)) {
1358
+ if (s.body === self || s.body.held && !s.body.carrying || Math.abs(x - s.c[0]) > s.r || Math.abs(z - s.c[2]) > s.r) continue;
1077
1359
  const t = boxTopAt(s, x, z);
1078
1360
  if (t > top && t <= y + step) {
1079
1361
  top = t;
@@ -1098,9 +1380,10 @@ var PieceSystem = class {
1098
1380
  const into = -(v[0] * n[0] + v[1] * n[1] + v[2] * n[2]);
1099
1381
  if (into <= 0) return;
1100
1382
  const k = Math.min(1, hit.distance / travel);
1383
+ const bounce = pairRestitution(b.restitution, hit.restitution);
1101
1384
  for (let i = 0; i < 3; i++) {
1102
1385
  pos[i] = start[i] + (pos[i] - start[i]) * k;
1103
- v[i] += (1 + SLAM_BOUNCE) * into * n[i];
1386
+ v[i] += (1 + bounce) * into * n[i];
1104
1387
  }
1105
1388
  const w = b.angularVelocity;
1106
1389
  w[0] *= 0.5;
@@ -1139,9 +1422,16 @@ var PieceSystem = class {
1139
1422
  gLow = Math.min(gLow, g);
1140
1423
  gHigh = Math.max(gHigh, g);
1141
1424
  }
1142
- const local = gHigh - gLow > STEP ? this.samples(b, true) : corners;
1143
- const prev = local.map(() => [0, 0, 0]);
1425
+ let local = b.round ? this.roundSamples(b, q) : gHigh - gLow > STEP ? this.samples(b, true) : corners;
1426
+ let prev = local.map(() => [0, 0, 0]);
1144
1427
  for (let s = 0; s < steps; s++) {
1428
+ if (b.round) {
1429
+ const wl0 = Math.hypot(w[0], w[1], w[2]);
1430
+ const next = this.nextQ.copy(q);
1431
+ if (wl0 > 1e-6) next.premultiply(this.dq.setFromAxisAngle(this.ax.set(w[0] / wl0, w[1] / wl0, w[2] / wl0), wl0 * h)).normalize();
1432
+ local = this.roundSamples(b, next);
1433
+ if (prev.length !== local.length) prev = local.map(() => [0, 0, 0]);
1434
+ }
1145
1435
  this.m.makeRotationFromQuaternion(q);
1146
1436
  const e0 = this.m.elements;
1147
1437
  for (let i = 0; i < local.length; i++) {
@@ -1159,7 +1449,7 @@ var PieceSystem = class {
1159
1449
  this.dq.setFromAxisAngle(this.ax.set(w[0] / wl, w[1] / wl, w[2] / wl), wl * h);
1160
1450
  q.premultiply(this.dq).normalize();
1161
1451
  }
1162
- const damp = Math.exp(-0.15 * h);
1452
+ const damp = b.round ? 1 : Math.exp(-0.15 * h);
1163
1453
  w[0] *= damp;
1164
1454
  w[1] *= damp;
1165
1455
  w[2] *= damp;
@@ -1227,6 +1517,12 @@ var PieceSystem = class {
1227
1517
  }
1228
1518
  }
1229
1519
  if (lowest < Infinity) b.floor = lowest;
1520
+ if (b.round && ns.length > 0) {
1521
+ const sum = [0, 0, 0];
1522
+ for (const one of ns) for (let k = 0; k < 3; k++) sum[k] += one[k];
1523
+ const len = Math.hypot(sum[0], sum[1], sum[2]);
1524
+ b.surfaceNormal = len > 1e-6 ? [sum[0] / len, sum[1] / len, sum[2] / len] : UP;
1525
+ } else if (b.round && rs.length === 0) b.surfaceNormal = UP;
1230
1526
  if (side) {
1231
1527
  pos[0] -= side.dir[0] * side.back;
1232
1528
  pos[2] -= side.dir[1] * side.back;
@@ -1255,14 +1551,15 @@ var PieceSystem = class {
1255
1551
  landPoint = [lx, pos[1] - Math.max(...rs.map((r) => -r[1])), lz];
1256
1552
  }
1257
1553
  const pointV = (r) => [v[0] + w[1] * r[2] - w[2] * r[1], v[1] + w[2] * r[0] - w[0] * r[2], v[2] + w[0] * r[1] - w[1] * r[0]];
1258
- const dot = (a, c2) => a[0] * c2[0] + a[1] * c2[1] + a[2] * c2[2];
1554
+ const dot2 = (a, c2) => a[0] * c2[0] + a[1] * c2[1] + a[2] * c2[2];
1259
1555
  const goal = rs.map((r, k) => {
1260
- const vn = dot(pointV(r), ns[k]);
1556
+ const vn = dot2(pointV(r), ns[k]);
1261
1557
  return vn < 0 ? -restitution * vn : 0;
1262
1558
  });
1263
1559
  const slipping = rs.map((r, k) => {
1264
1560
  const pv = pointV(r);
1265
- const vn = dot(pv, ns[k]);
1561
+ pv[1] += this.o.gravity * h;
1562
+ const vn = dot2(pv, ns[k]);
1266
1563
  return Math.hypot(pv[0] - vn * ns[k][0], pv[1] - vn * ns[k][1], pv[2] - vn * ns[k][2]) > SLIP_SPEED;
1267
1564
  });
1268
1565
  const vx0 = v[0];
@@ -1293,13 +1590,13 @@ var PieceSystem = class {
1293
1590
  for (let k = 0; k < rs.length; k++) {
1294
1591
  const r = rs[k];
1295
1592
  const n = ns[k];
1296
- const vn = dot(pointV(r), n);
1593
+ const vn = dot2(pointV(r), n);
1297
1594
  const total = Math.max(0, accN[k] + (goal[k] - vn) / reach(r, n));
1298
1595
  const j = total - accN[k];
1299
1596
  accN[k] = total;
1300
1597
  if (j !== 0) push(r, [n[0] * j, n[1] * j, n[2] * j]);
1301
1598
  const pv = pointV(r);
1302
- const pn = dot(pv, n);
1599
+ const pn = dot2(pv, n);
1303
1600
  const vt = [pv[0] - pn * n[0], pv[1] - pn * n[1], pv[2] - pn * n[2]];
1304
1601
  const speed = Math.hypot(vt[0], vt[1], vt[2]);
1305
1602
  const f = [accT[k][0], accT[k][1], accT[k][2]];
@@ -1344,7 +1641,7 @@ var PieceSystem = class {
1344
1641
  const height = -low;
1345
1642
  const wx = x1 - x0;
1346
1643
  const wz = z1 - z0;
1347
- unstable = Math.min(wx, wz) < NARROW * height || x0 > 0 || x1 < 0 || z0 > 0 || z1 < 0;
1644
+ unstable = !b.round && (Math.min(wx, wz) < NARROW * height || x0 > 0 || x1 < 0 || z0 > 0 || z1 < 0);
1348
1645
  if (b.tipping) {
1349
1646
  b.tipping.seconds -= h;
1350
1647
  const [hx, hy, hz] = b.halfExtents;
@@ -1363,13 +1660,19 @@ var PieceSystem = class {
1363
1660
  if (wx <= wz) w[2] -= dirX * k;
1364
1661
  else w[0] += dirZ * k;
1365
1662
  }
1366
- const rd = Math.exp((rs.length >= 3 ? -3 : -1.5) * h);
1367
- w[0] *= rd;
1368
- w[1] *= rd;
1369
- w[2] *= rd;
1370
- const fd = Math.exp(-0.3 * h);
1371
- v[0] *= fd;
1372
- v[2] *= fd;
1663
+ if (b.round) {
1664
+ let rolling = 0;
1665
+ for (const mu of mus) rolling = Math.max(rolling, mu.rolling ?? ROLLING_RESISTANCE);
1666
+ this.rollingResistance(b, q, w, h, rolling);
1667
+ } else {
1668
+ const rd = Math.exp((rs.length >= 3 ? -3 : -1.5) * h);
1669
+ w[0] *= rd;
1670
+ w[1] *= rd;
1671
+ w[2] *= rd;
1672
+ const fd = Math.exp(-0.3 * h);
1673
+ v[0] *= fd;
1674
+ v[2] *= fd;
1675
+ }
1373
1676
  }
1374
1677
  if (this.o.sweep && !b.held) this.sweepOn(b, start, pos);
1375
1678
  b.root.quaternion.copy(q);
@@ -1405,63 +1708,241 @@ var PieceSystem = class {
1405
1708
  }
1406
1709
  /** Whether the centre `c` of `upper` is over the top of `lower`'s box (as it was at the start of the update). */
1407
1710
  onTopOf(upper, lower, c) {
1408
- const s = this.solids.find((one) => one.body === lower);
1711
+ const s = this.solidOf.get(lower);
1409
1712
  if (!s) return false;
1410
1713
  const top = boxTopAt(s, c.x, c.z);
1411
1714
  return top > -Infinity && c.y >= top - 0.01;
1412
1715
  }
1716
+ /**
1717
+ * The points a round piece touches the ground by as it is turned now (`q`): its rims (`contacts`), which hold it standing on an end, and the exact lowest point of each
1718
+ * rim (of the ball), so lying on its side it rests on a line and rolls smoothly instead of over the corners of a polygon.
1719
+ */
1720
+ roundSamples(b, q) {
1721
+ const round = b.round;
1722
+ const rims = b.contacts ?? [];
1723
+ if (!round) return rims;
1724
+ const e = this.m.makeRotationFromQuaternion(q).elements;
1725
+ const n = b.surfaceNormal ?? UP;
1726
+ const down = [-(e[0] * n[0] + e[1] * n[1] + e[2] * n[2]), -(e[4] * n[0] + e[5] * n[1] + e[6] * n[2]), -(e[8] * n[0] + e[9] * n[1] + e[10] * n[2])];
1727
+ if (round.kind === "sphere") return [...rims, [down[0] * round.radius, down[1] * round.radius, down[2] * round.radius]];
1728
+ const k = round.axis;
1729
+ const across = [down[0], down[1], down[2]];
1730
+ across[k] = 0;
1731
+ const len = Math.hypot(across[0], across[1], across[2]);
1732
+ if (len < 1e-3) return rims;
1733
+ const lowest = (at, radius) => {
1734
+ const p = [across[0] / len * radius, across[1] / len * radius, across[2] / len * radius];
1735
+ p[k] = at;
1736
+ return p;
1737
+ };
1738
+ const half = b.halfExtents[k];
1739
+ const low = (round.profile ?? [[-half, round.radius], [half, round.radius]]).map(([at, radius]) => lowest(at, radius));
1740
+ return len > ROUND_LYING ? low : [...rims, ...low];
1741
+ }
1742
+ /**
1743
+ * Slows the spin of a round piece on the ground by its rolling resistance: a torque C_rr N r about the axis it rolls on (N = m g n_y on a surface of normal n), so with
1744
+ * I = k m r^2 the spin loses C_rr g n_y / (k r) per second, and down a slope of angle t it rolls at g (sin t - C_rr cos t) / (1 + k). Standing on its rim, a can's spin
1745
+ * about the rest dies down; lying, it keeps turning about the line it touches by (a tapered bottle rounds its narrow end).
1746
+ */
1747
+ rollingResistance(b, q, w, h, rolling) {
1748
+ const round = b.round;
1749
+ if (!round) return;
1750
+ const e = this.m.makeRotationFromQuaternion(q).elements;
1751
+ const cylinder = round.kind === "cylinder";
1752
+ const a = cylinder ? [e[round.axis * 4], e[round.axis * 4 + 1], e[round.axis * 4 + 2]] : [0, 0, 0];
1753
+ const along2 = cylinder ? w[0] * a[0] + w[1] * a[1] + w[2] * a[2] : 0;
1754
+ const roll = cylinder ? [a[0] * along2, a[1] * along2, a[2] * along2] : [w[0], w[1], w[2]];
1755
+ const rest = [w[0] - roll[0], w[1] - roll[1], w[2] - roll[2]];
1756
+ const spin = Math.hypot(roll[0], roll[1], roll[2]);
1757
+ const upright = b.surfaceNormal?.[1] ?? 1;
1758
+ const slow = spin > 0 ? Math.max(0, spin - rolling * this.o.gravity * upright * h / (round.inertia * round.radius)) / spin : 0;
1759
+ const lying = cylinder && Math.abs(a[1]) < ROUND_LYING;
1760
+ const damp = lying ? 1 : Math.exp(-3 * h);
1761
+ for (let i = 0; i < 3; i++) w[i] = roll[i] * slow + rest[i] * damp;
1762
+ }
1413
1763
  /** Half the height of the box of a piece as it is turned now. */
1414
1764
  halfHeight(b) {
1415
1765
  const e = this.m.makeRotationFromQuaternion(b.root.quaternion).elements;
1416
1766
  return Math.abs(e[1]) * b.halfExtents[0] + Math.abs(e[5]) * b.halfExtents[1] + Math.abs(e[9]) * b.halfExtents[2];
1417
1767
  }
1418
- /** Push overlapping pieces apart (horizontal bounding circles), gently, so they do not interpenetrate forever. */
1419
- separate(dt) {
1768
+ /**
1769
+ * Pieces against each other. Candidates by sweep and prune along x on their bounding spheres (sorted once per step: fifty cans cost a sort and a handful of pairs, not
1770
+ * 1225); a pair with a round piece in it meets by its shapes (`shapeContact`) and exchanges an impulse, two boxes are only pushed apart (`separatePair`).
1771
+ */
1772
+ collide(dt, meets) {
1420
1773
  const n = this.list.length;
1421
1774
  if (n < 2) return;
1422
- const maxMove = 1.5 * dt;
1423
- const ca = new this.THREE.Vector3();
1424
- const cb = new this.THREE.Vector3();
1425
- for (let i = 0; i < n; i++) {
1426
- const a = this.list[i];
1427
- for (let j = i + 1; j < n; j++) {
1775
+ const centers = this.list.map((b) => {
1776
+ const c = this.centerOf(b, this.p);
1777
+ return [c.x, c.y, c.z];
1778
+ });
1779
+ const radii = this.list.map((b) => Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]));
1780
+ const order = this.list.map((_, i) => i).sort((i, j) => centers[i][0] - radii[i] - (centers[j][0] - radii[j]));
1781
+ for (let oi = 0; oi < n; oi++) {
1782
+ const i = order[oi];
1783
+ const reach = centers[i][0] + radii[i];
1784
+ for (let oj = oi + 1; oj < n; oj++) {
1785
+ const j = order[oj];
1786
+ if (centers[j][0] - radii[j] > reach) break;
1787
+ const a = this.list[i];
1428
1788
  const b = this.list[j];
1429
- const fixedA = a.held || a.sleeping;
1430
- const fixedB = b.held || b.sleeping;
1431
- if (fixedA && fixedB) continue;
1432
- const ra = Math.hypot(a.halfExtents[0], a.halfExtents[1], a.halfExtents[2]);
1433
- const rb = Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]);
1434
- this.centerOf(a, ca);
1435
- this.centerOf(b, cb);
1436
- if (Math.abs(ca.y - cb.y) > ra + rb) continue;
1437
- if (Math.abs(ca.y - cb.y) >= (this.halfHeight(a) + this.halfHeight(b)) * 0.9) continue;
1438
- if (this.onTopOf(b, a, cb) || this.onTopOf(a, b, ca)) continue;
1439
- const rh = (Math.max(a.halfExtents[0], a.halfExtents[2]) + Math.max(b.halfExtents[0], b.halfExtents[2])) * 0.8;
1440
- let dx = cb.x - ca.x;
1441
- let dz = cb.z - ca.z;
1442
- let d = Math.hypot(dx, dz);
1443
- if (d >= rh) continue;
1444
- if (d < 1e-5) {
1445
- dx = 1;
1446
- dz = 0;
1447
- d = 1;
1448
- }
1449
- const push = Math.min(maxMove, (rh - d) * 0.5);
1450
- const wa = fixedA ? 0 : fixedB ? 1 : b.mass / (a.mass + b.mass);
1451
- const wb = fixedB ? 0 : fixedA ? 1 : a.mass / (a.mass + b.mass);
1452
- const ux = dx / (d || 1);
1453
- const uz = dz / (d || 1);
1454
- if (!fixedA) {
1455
- a.root.position.x -= ux * push * 2 * wa;
1456
- a.root.position.z -= uz * push * 2 * wa;
1457
- }
1458
- if (!fixedB) {
1459
- b.root.position.x += ux * push * 2 * wb;
1460
- b.root.position.z += uz * push * 2 * wb;
1461
- }
1789
+ if ((a.held || a.sleeping) && (b.held || b.sleeping)) continue;
1790
+ if (meets && !meets(a, b)) continue;
1791
+ const r = radii[i] + radii[j];
1792
+ const ca = centers[i];
1793
+ const cb = centers[j];
1794
+ if (Math.abs(ca[1] - cb[1]) > r || Math.abs(ca[2] - cb[2]) > r) continue;
1795
+ if (!a.round && !b.round && Math.abs(ca[1] - cb[1]) >= (this.halfHeight(a) + this.halfHeight(b)) * 0.9) continue;
1796
+ if (this.onTopOf(b, a, new this.THREE.Vector3(...cb)) || this.onTopOf(a, b, new this.THREE.Vector3(...ca))) continue;
1797
+ if (a.round || b.round) this.knock(a, b, ca, cb);
1798
+ else if (this.partsOverlap(a, b) && !this.restsOn(a, b, false) && !this.restsOn(b, a, false)) this.separatePair(a, b, ca, cb, dt);
1462
1799
  }
1463
1800
  }
1464
1801
  }
1802
+ /** The shape a piece meets others by (`ContactShape`), as it stands now: a round one a capsule round its axis, any other the boxes of its parts or its box. */
1803
+ contactShape(b, c) {
1804
+ const e = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
1805
+ const round = b.round;
1806
+ if (round) {
1807
+ if (round.kind === "sphere") return { kind: "capsule", from: c, to: c, radius: round.radius };
1808
+ const reach = Math.max(0, b.halfExtents[round.axis] - round.radius);
1809
+ const k = round.axis * 4;
1810
+ const d = [e[k] * reach, e[k + 1] * reach, e[k + 2] * reach];
1811
+ return { kind: "capsule", from: [c[0] - d[0], c[1] - d[1], c[2] - d[2]], to: [c[0] + d[0], c[1] + d[1], c[2] + d[2]], radius: round.radius };
1812
+ }
1813
+ const parts = b.partBoxes ?? [{ center: [0, 0, 0], half: b.halfExtents }];
1814
+ return {
1815
+ kind: "boxes",
1816
+ boxes: parts.map(({ center, half }) => ({
1817
+ c: [c[0] + e[0] * center[0] + e[4] * center[1] + e[8] * center[2], c[1] + e[1] * center[0] + e[5] * center[1] + e[9] * center[2], c[2] + e[2] * center[0] + e[6] * center[1] + e[10] * center[2]],
1818
+ e,
1819
+ half
1820
+ }))
1821
+ };
1822
+ }
1823
+ /**
1824
+ * Two pieces that met, one of them round: they are put apart along the normal (by their inverse masses) and, closing in, exchange an impulse
1825
+ * j = -(1 + e) v_n / (1/m_a + 1/m_b + the turning of each), with the restitution e of their two materials, and the friction of their surfaces across it. The impulses are
1826
+ * equal and opposite, so the momentum of the pair is kept. A held piece is not moved (the hand holds it); a sleeping one wakes when it is knocked.
1827
+ */
1828
+ knock(a, b, ca, cb) {
1829
+ const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
1830
+ if (!hit) return;
1831
+ const n = hit.normal;
1832
+ if (Math.abs(n[1]) > SLAM_FLOOR_NORMAL) return;
1833
+ const ra = [hit.point[0] - ca[0], hit.point[1] - ca[1], hit.point[2] - ca[2]];
1834
+ const rb = [hit.point[0] - cb[0], hit.point[1] - cb[1], hit.point[2] - cb[2]];
1835
+ const pointV = (body, r) => {
1836
+ const v2 = body.velocity;
1837
+ const w = body.angularVelocity;
1838
+ return [v2[0] + w[1] * r[2] - w[2] * r[1], v2[1] + w[2] * r[0] - w[0] * r[2], v2[2] + w[0] * r[1] - w[1] * r[0]];
1839
+ };
1840
+ const relative = () => {
1841
+ const va = pointV(a, ra);
1842
+ const vb = pointV(b, rb);
1843
+ return [va[0] - vb[0], va[1] - vb[1], va[2] - vb[2]];
1844
+ };
1845
+ const vn0 = relative();
1846
+ const closing = vn0[0] * n[0] + vn0[1] * n[1] + vn0[2] * n[2];
1847
+ const wake = (body) => {
1848
+ body.sleeping = false;
1849
+ body.still = 0;
1850
+ body.calmAt = null;
1851
+ };
1852
+ if (closing < -WAKE_ABOVE_SPEED) {
1853
+ if (a.sleeping) wake(a);
1854
+ if (b.sleeping) wake(b);
1855
+ }
1856
+ const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
1857
+ const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
1858
+ if (ima + imb === 0) return;
1859
+ const ea = [...this.m.makeRotationFromQuaternion(a.root.quaternion).elements];
1860
+ const eb = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
1861
+ const flat = Math.hypot(n[0], n[2]);
1862
+ const out = flat > KNOCK_FLAT ? [n[0] / flat, 0, n[2] / flat] : n;
1863
+ const depth = Math.max(0, hit.depth - SLOP) / (flat > KNOCK_FLAT ? flat : 1);
1864
+ for (const [body, share, sign] of [[a, ima, 1], [b, imb, -1]]) {
1865
+ if (share === 0) continue;
1866
+ const move = depth * share * sign / (ima + imb);
1867
+ body.root.position.x += out[0] * move;
1868
+ body.root.position.y += out[1] * move;
1869
+ body.root.position.z += out[2] * move;
1870
+ }
1871
+ if (closing >= 0) return;
1872
+ const turning = (body, e, r, d) => {
1873
+ const t2 = this.mulInvInertiaWith(body, e, r[1] * d[2] - r[2] * d[1], r[2] * d[0] - r[0] * d[2], r[0] * d[1] - r[1] * d[0]);
1874
+ return (t2[1] * r[2] - t2[2] * r[1]) * d[0] + (t2[2] * r[0] - t2[0] * r[2]) * d[1] + (t2[0] * r[1] - t2[1] * r[0]) * d[2];
1875
+ };
1876
+ const resist = (d) => ima + imb + (ima > 0 ? turning(a, ea, ra, d) : 0) + (imb > 0 ? turning(b, eb, rb, d) : 0);
1877
+ const give = (j2) => {
1878
+ if (ima > 0) {
1879
+ for (let i = 0; i < 3; i++) a.velocity[i] += j2[i] * ima;
1880
+ const dw = this.mulInvInertiaWith(a, ea, ra[1] * j2[2] - ra[2] * j2[1], ra[2] * j2[0] - ra[0] * j2[2], ra[0] * j2[1] - ra[1] * j2[0]);
1881
+ for (let i = 0; i < 3; i++) a.angularVelocity[i] += dw[i];
1882
+ }
1883
+ if (imb > 0) {
1884
+ for (let i = 0; i < 3; i++) b.velocity[i] -= j2[i] * imb;
1885
+ const dw = this.mulInvInertiaWith(b, eb, rb[1] * j2[2] - rb[2] * j2[1], rb[2] * j2[0] - rb[0] * j2[2], rb[0] * j2[1] - rb[1] * j2[0]);
1886
+ for (let i = 0; i < 3; i++) b.angularVelocity[i] -= dw[i];
1887
+ }
1888
+ };
1889
+ const bounce = -closing > BOUNCE_MIN_SPEED ? pairRestitution(a.restitution, b.restitution) : 0;
1890
+ const j = -(1 + bounce) * closing / resist(n);
1891
+ give([n[0] * j, n[1] * j, n[2] * j]);
1892
+ const v = relative();
1893
+ const vn = v[0] * n[0] + v[1] * n[1] + v[2] * n[2];
1894
+ const vt = [v[0] - vn * n[0], v[1] - vn * n[1], v[2] - vn * n[2]];
1895
+ const slide = Math.hypot(vt[0], vt[1], vt[2]);
1896
+ if (slide < 1e-6) return;
1897
+ const t = [vt[0] / slide, vt[1] / slide, vt[2] / slide];
1898
+ const jt = Math.min(slide / resist(t), pairFriction(a.friction, b.friction ?? null).dynamic * j);
1899
+ give([-t[0] * jt, -t[1] * jt, -t[2] * jt]);
1900
+ if (-closing >= SLAM_MIN_SPEED && this.time - Math.max(a.lastHit, b.lastHit) > 0.15) {
1901
+ a.lastHit = b.lastHit = this.time;
1902
+ this.o.onKnock?.(a, b, hit.point, -closing);
1903
+ }
1904
+ }
1905
+ /** Whether a part of `a` and a part of `b` (their boxes of the step, as world-aligned boxes around them) overlap. */
1906
+ partsOverlap(a, b) {
1907
+ const extent = (s, k) => Math.abs(s.e[k]) * s.half[0] + Math.abs(s.e[k + 4]) * s.half[1] + Math.abs(s.e[k + 8]) * s.half[2];
1908
+ for (const pa of this.surfacesOf.get(a) ?? []) {
1909
+ for (const pb of this.surfacesOf.get(b) ?? []) {
1910
+ let apart = false;
1911
+ for (let k = 0; k < 3 && !apart; k++) apart = Math.abs(pa.c[k] - pb.c[k]) > extent(pa, k) + extent(pb, k);
1912
+ if (!apart) return true;
1913
+ }
1914
+ }
1915
+ return false;
1916
+ }
1917
+ /** Two boxes overlapping side by side (horizontal bounding circles): pushed apart, gently, so they do not interpenetrate forever. */
1918
+ separatePair(a, b, ca, cb, dt) {
1919
+ const fixedA = a.held || a.sleeping;
1920
+ const fixedB = b.held || b.sleeping;
1921
+ const maxMove = 1.5 * dt;
1922
+ const rh = (Math.max(a.halfExtents[0], a.halfExtents[2]) + Math.max(b.halfExtents[0], b.halfExtents[2])) * 0.8;
1923
+ let dx = cb[0] - ca[0];
1924
+ let dz = cb[2] - ca[2];
1925
+ let d = Math.hypot(dx, dz);
1926
+ if (d >= rh) return;
1927
+ if (d < 1e-5) {
1928
+ dx = 1;
1929
+ dz = 0;
1930
+ d = 1;
1931
+ }
1932
+ const push = Math.min(maxMove, (rh - d) * 0.5);
1933
+ const wa = fixedA ? 0 : fixedB ? 1 : b.mass / (a.mass + b.mass);
1934
+ const wb = fixedB ? 0 : fixedA ? 1 : a.mass / (a.mass + b.mass);
1935
+ const ux = dx / (d || 1);
1936
+ const uz = dz / (d || 1);
1937
+ if (!fixedA) {
1938
+ a.root.position.x -= ux * push * 2 * wa;
1939
+ a.root.position.z -= uz * push * 2 * wa;
1940
+ }
1941
+ if (!fixedB) {
1942
+ b.root.position.x += ux * push * 2 * wb;
1943
+ b.root.position.z += uz * push * 2 * wb;
1944
+ }
1945
+ }
1465
1946
  };
1466
1947
 
1467
1948
  // src/rebar.ts
@@ -1762,6 +2243,23 @@ function specFromObject3D(THREE, root, options = {}) {
1762
2243
  return { spec, parts: meshesOf };
1763
2244
  }
1764
2245
 
2246
+ // src/friction.ts
2247
+ var SURFACE_BY_KEY = [
2248
+ [/rubber|tire|tyre/, { static: 1, dynamic: 0.8, rolling: 0.03, restitution: 0.8 }],
2249
+ [/foam|sponge|fabric|cloth|leather|textile|carpet/, { static: 0.6, dynamic: 0.5, rolling: 0.08, restitution: 0.2 }],
2250
+ [/concrete|brick|stone|masonry|cement|asphalt|plaster/, { static: 0.7, dynamic: 0.6, rolling: 0.012, restitution: 0.4 }],
2251
+ [/steel|iron|metal|alumin|copper|brass|tin/, { static: 0.6, dynamic: 0.4, rolling: 2e-3, restitution: 0.6 }],
2252
+ [/glass/, { static: 0.4, dynamic: 0.35, rolling: 2e-3, restitution: 0.7 }],
2253
+ [/ceramic|porcelain|tile/, { static: 0.5, dynamic: 0.4, rolling: 5e-3, restitution: 0.5 }],
2254
+ [/wood|mdf|plywood|board|cork/, { static: 0.4, dynamic: 0.3, rolling: 0.01, restitution: 0.5 }],
2255
+ [/plastic|polycarbonate|acrylic|nylon|pvc/, { static: 0.4, dynamic: 0.3, rolling: 0.01, restitution: 0.5 }],
2256
+ [/paper|cardboard|carton/, { static: 0.4, dynamic: 0.3, rolling: 0.02, restitution: 0.2 }]
2257
+ ];
2258
+ function surfaceOf(materialId, def) {
2259
+ const keys = [materialId, ...def?.tags ?? []].join(" ").toLowerCase();
2260
+ return SURFACE_BY_KEY.find(([key]) => key.test(keys))?.[1];
2261
+ }
2262
+
1765
2263
  // src/renderer.ts
1766
2264
  import { kitOriginOf } from "@adatechnology/game-contracts";
1767
2265
 
@@ -2870,6 +3368,63 @@ var FEET_BAND = 0.03;
2870
3368
  var TIP_MIN_HEIGHT = 0.3;
2871
3369
  var TIP_NARROW = 0.3;
2872
3370
  var CONTACT_MESHES = 12;
3371
+ var PART_BOX_MESHES = 64;
3372
+ var ROUND_SAMPLES = 400;
3373
+ var ROUND_SIDES = 0.12;
3374
+ var ROUND_SLACK = 1.12;
3375
+ var RIM_POINTS = 12;
3376
+ var rimPoints = (round, half) => {
3377
+ const out = [];
3378
+ const circle = (axis, at, radius) => {
3379
+ const [i, j] = axis === 0 ? [1, 2] : axis === 1 ? [0, 2] : [0, 1];
3380
+ for (let k = 0; k < RIM_POINTS; k++) {
3381
+ const a = k / RIM_POINTS * Math.PI * 2;
3382
+ const p = [0, 0, 0];
3383
+ p[axis] = at;
3384
+ p[i] = Math.cos(a) * radius;
3385
+ p[j] = Math.sin(a) * radius;
3386
+ out.push(p);
3387
+ }
3388
+ };
3389
+ if (round.kind === "sphere") for (const axis of [0, 1, 2]) circle(axis, 0, round.radius);
3390
+ else {
3391
+ const ends = round.profile ?? [];
3392
+ circle(round.axis, -half[round.axis], ends[0]?.[1] ?? round.radius);
3393
+ circle(round.axis, half[round.axis], ends[ends.length - 1]?.[1] ?? round.radius);
3394
+ }
3395
+ return out;
3396
+ };
3397
+ var PROFILE_RINGS = 8;
3398
+ var SHELL_CYLINDER = 1;
3399
+ var SOLID_CYLINDER = 0.5;
3400
+ var SHELL_SPHERE = 2 / 3;
3401
+ var SOLID_SPHERE = 0.4;
3402
+ var LIQUID_KG_PER_L = 1;
3403
+ var profileOf = (points, axis, half, radius) => {
3404
+ const [i, j] = axis === 0 ? [1, 2] : axis === 1 ? [0, 2] : [0, 1];
3405
+ const widest = new Array(PROFILE_RINGS).fill(0);
3406
+ for (const p of points) {
3407
+ const ring = Math.min(PROFILE_RINGS - 1, Math.max(0, Math.floor((p[axis] + half) / (2 * half) * PROFILE_RINGS)));
3408
+ widest[ring] = Math.max(widest[ring], Math.hypot(p[i], p[j]));
3409
+ }
3410
+ const rings = [];
3411
+ for (let k = 0; k < PROFILE_RINGS; k++) {
3412
+ if (widest[k] <= 0) continue;
3413
+ const at = k === 0 ? -half : k === PROFILE_RINGS - 1 ? half : -half + (k + 0.5) / PROFILE_RINGS * 2 * half;
3414
+ rings.push([at, Math.min(radius, widest[k])]);
3415
+ }
3416
+ const hull = [];
3417
+ for (const ring of rings) {
3418
+ while (hull.length >= 2) {
3419
+ const [a0, r0] = hull[hull.length - 2];
3420
+ const [a1, r1] = hull[hull.length - 1];
3421
+ if ((r1 - r0) * (ring[0] - a0) <= (ring[1] - r0) * (a1 - a0)) hull.pop();
3422
+ else break;
3423
+ }
3424
+ hull.push(ring);
3425
+ }
3426
+ return hull;
3427
+ };
2873
3428
  var BROKEN_GAP = 0.03;
2874
3429
  var BROKEN_SAMPLES = [[0.5, 0.5], [0.1, 0.1], [0.9, 0.1], [0.1, 0.9], [0.9, 0.9]];
2875
3430
  var UNCHECKED_KINDS = /* @__PURE__ */ new Set(["structure", "room", "ground"]);
@@ -3070,7 +3625,12 @@ var Renderer = class {
3070
3625
  scene: o.scene,
3071
3626
  onHardLanding: (p, point, speed) => this.hardLanding(p, point, speed),
3072
3627
  sweep: (p, from, to) => this.sweepPiece(p, from, to),
3073
- onSlam: (p, hit, speed) => this.slam(p, hit, speed)
3628
+ onSlam: (p, hit, speed) => this.slam(p, hit, speed),
3629
+ // two pieces knocked together: each sounds as its material does (a can on a can, a bottle on a table's leg)
3630
+ onKnock: (a, b, point, speed) => {
3631
+ this.landingSound(a.objectId, point, clamp(speed / 10, 0.2, 1));
3632
+ this.landingSound(b.objectId, point, clamp(speed / 10, 0.2, 1));
3633
+ }
3074
3634
  });
3075
3635
  this.unsubscribe = this.world.on("*", (ev) => {
3076
3636
  if (ev.type === "explode") this.coverWhenWentOff.set(ev, this.coverAround(ev));
@@ -3249,8 +3809,129 @@ var Renderer = class {
3249
3809
  const { half, center } = this.measure(b.root, this.meshesOf(b));
3250
3810
  const piece = this.pieceSys.create(objectId, b.root, half, center, options.mass ?? this.massOf(objectId), options.velocity);
3251
3811
  b.piece = piece;
3812
+ this.fitSurface(b, piece, options.inertia);
3813
+ this.fitPartBoxes(b, piece);
3252
3814
  return piece;
3253
3815
  }
3816
+ /**
3817
+ * What lands on or runs into a piece of several meshes meets its parts as drawn (`partBoxes`: a bench's slats, a table's top and legs), from the moment it is made a piece, not
3818
+ * the box around them all: a board laid on a bench rests on its seat, a can rolls under a table between its legs. Its own contacts with the ground are left as they were.
3819
+ */
3820
+ fitPartBoxes(b, body) {
3821
+ if (body.round) return;
3822
+ const contacts = body.contacts;
3823
+ this.setContacts(b, body, this.meshesOf(b).filter((mv) => this.visuals.shown(mv)));
3824
+ body.contacts = contacts;
3825
+ }
3826
+ /** What the piece's surface is (the friction of the material it stands on) and whether it is round (`round`: it rolls on rings of contacts). */
3827
+ fitSurface(b, body, inertia) {
3828
+ const box = new this.THREE.Box3();
3829
+ let low = Infinity;
3830
+ let material = null;
3831
+ for (const [partId, pv] of b.parts) {
3832
+ if (pv.gone !== "none") continue;
3833
+ for (const mv of pv.meshes) {
3834
+ if (!this.visuals.shown(mv)) continue;
3835
+ this.visuals.worldBox(mv, box);
3836
+ if (box.isEmpty() || box.min.y >= low - 5e-3) continue;
3837
+ low = box.min.y;
3838
+ material = this.world.part(b.id, partId)?.material ?? pv.material ?? null;
3839
+ }
3840
+ }
3841
+ const surface = material ? surfaceOf(material, this.world.materials.get(material)) : void 0;
3842
+ body.friction = surface && { static: surface.static, dynamic: surface.dynamic, rolling: surface.rolling };
3843
+ body.restitution = surface?.restitution;
3844
+ const wasRound = body.round !== void 0;
3845
+ if (inertia !== void 0) body.inertiaGiven = inertia;
3846
+ const round = this.roundOf(b, body);
3847
+ body.round = round ?? void 0;
3848
+ if (round) {
3849
+ body.contacts = rimPoints(round, body.halfExtents);
3850
+ body.partBoxes = void 0;
3851
+ } else if (wasRound) body.contacts = void 0;
3852
+ }
3853
+ /**
3854
+ * k of a round piece's moment of inertia about its axis (I = k m r^2), from where its mass is. A piece that is not hollow is a solid (k 1/2 for a cylinder, 2/5 for a ball).
3855
+ * A hollow one (`userData.hollow`) with nothing inside (no `destructionContains` liquid, no `destructionPowder`) is all wall, a thin shell (k 1, 2/3): an empty can. One with
3856
+ * contents weighs its walls as their area times their thickness (`destructionThickness`) times density, or, without a thickness, as what is left of its mass besides the liquid
3857
+ * (`LIQUID_KG_PER_L`), and the rest as a solid: a full 350 ml can comes to about 0.53 and rolls down a slope at g sin t / 1.53, an empty one at g sin t / 2.
3858
+ */
3859
+ inertiaOf(b, body, kind, axis, radius) {
3860
+ const shell = kind === "sphere" ? SHELL_SPHERE : SHELL_CYLINDER;
3861
+ const solid = kind === "sphere" ? SOLID_SPHERE : SOLID_CYLINDER;
3862
+ let hollow = false;
3863
+ let filled = false;
3864
+ let liters = 0;
3865
+ let thickness = 0;
3866
+ let material = null;
3867
+ for (const [partId, pv] of b.parts) {
3868
+ if (pv.gone !== "none") continue;
3869
+ for (const mv of pv.meshes) {
3870
+ const ud = mv.mesh.userData;
3871
+ if (ud.hollow !== true) continue;
3872
+ hollow = true;
3873
+ if (ud.destructionPowder) filled = true;
3874
+ if (ud.destructionContains) {
3875
+ filled = true;
3876
+ const left = this.world.part(b.id, partId)?.liters;
3877
+ liters += left ?? (typeof ud.destructionContains.liters === "number" ? ud.destructionContains.liters : 0);
3878
+ }
3879
+ const t = typeof ud.destructionThickness === "number" ? ud.destructionThickness : 0;
3880
+ if (t > thickness) {
3881
+ thickness = t;
3882
+ material = this.world.part(b.id, partId)?.material ?? pv.material;
3883
+ }
3884
+ }
3885
+ }
3886
+ if (!hollow) return solid;
3887
+ if (!filled) return shell;
3888
+ const density = material ? this.world.materials.get(material)?.density : void 0;
3889
+ const area = kind === "sphere" ? 4 * Math.PI * radius * radius : 2 * Math.PI * radius * 2 * body.halfExtents[axis] + 2 * Math.PI * radius * radius;
3890
+ const walls = density && thickness > 0 ? area * thickness * density : body.mass - liters * LIQUID_KG_PER_L;
3891
+ const share = clamp(walls / body.mass, 0, 1);
3892
+ return share * shell + (1 - share) * solid;
3893
+ }
3894
+ /**
3895
+ * Whether what is drawn of the piece is round about one of its axes (a can, a bottle, a plate) or all of them (a ball): its points, in its frame from the centre of
3896
+ * mass, all lie within the circle the box's two other sides draw (a box's corners would stick out of it by sqrt 2). Null for anything else.
3897
+ */
3898
+ roundOf(b, body) {
3899
+ const half = body.halfExtents;
3900
+ const inv = specFrameInverse(this.THREE, b.root);
3901
+ const m4 = new this.THREE.Matrix4();
3902
+ const p = new this.THREE.Vector3();
3903
+ const [cx, cy, cz] = body.localCenter;
3904
+ const points = [];
3905
+ for (const mv of this.meshesOf(b)) {
3906
+ if (!this.visuals.shown(mv) || mv.host) continue;
3907
+ const pos = mv.mesh.geometry.getAttribute("position");
3908
+ if (!pos) continue;
3909
+ mv.mesh.updateWorldMatrix(true, false);
3910
+ m4.multiplyMatrices(inv, mv.mesh.matrixWorld);
3911
+ const stride = Math.max(1, Math.floor(pos.count / ROUND_SAMPLES));
3912
+ for (let i = 0; i < pos.count; i += stride) {
3913
+ p.fromBufferAttribute(pos, i).applyMatrix4(m4);
3914
+ points.push([p.x - cx, p.y - cy, p.z - cz]);
3915
+ }
3916
+ }
3917
+ if (points.length < 12) return null;
3918
+ const fits = (axis2) => {
3919
+ const [i, j] = axis2 === 0 ? [1, 2] : axis2 === 1 ? [0, 2] : [0, 1];
3920
+ if (Math.abs(half[i] - half[j]) > ROUND_SIDES * Math.max(half[i], half[j])) return null;
3921
+ const radius2 = (half[i] + half[j]) / 2;
3922
+ for (const q of points) if (Math.hypot(q[i], q[j]) > radius2 * ROUND_SLACK) return null;
3923
+ return radius2;
3924
+ };
3925
+ const radii = [0, 1, 2].map(fits);
3926
+ if (radii.every((r) => r !== null)) {
3927
+ const radius2 = (half[0] + half[1] + half[2]) / 3;
3928
+ return { kind: "sphere", axis: 1, radius: radius2, inertia: body.inertiaGiven ?? this.inertiaOf(b, body, "sphere", 1, radius2) };
3929
+ }
3930
+ const axis = [0, 1, 2].find((k) => radii[k] !== null);
3931
+ if (axis === void 0) return null;
3932
+ const radius = radii[axis] ?? 0;
3933
+ return { kind: "cylinder", axis, radius, inertia: body.inertiaGiven ?? this.inertiaOf(b, body, "cylinder", axis, radius), profile: profileOf(points, axis, half[axis], radius) };
3934
+ }
3254
3935
  pieces() {
3255
3936
  return [...this.pieceSys.list];
3256
3937
  }
@@ -3274,7 +3955,11 @@ var Renderer = class {
3274
3955
  const box = { min: [from[0] - core, from[1] - core, from[2] - core], max: [from[0] + core, from[1] + core, from[2] + core] };
3275
3956
  const hit = this.world.sweepBox(box, d, travel, { ignore: p.objectId, spacing: Math.max(0.05, core) });
3276
3957
  if (!hit) return null;
3277
- return { object: hit.object, part: hit.part, distance: hit.distance, point: hit.point, normal: hit.normal };
3958
+ const other = this.pieceSys.get(hit.object);
3959
+ if (other && (p.round || other.round)) return null;
3960
+ const material = this.world.part(hit.object, hit.part)?.material;
3961
+ const restitution = material === void 0 ? void 0 : surfaceOf(material, this.world.materials.get(material))?.restitution;
3962
+ return { object: hit.object, part: hit.part, distance: hit.distance, point: hit.point, normal: hit.normal, ...restitution === void 0 ? {} : { restitution } };
3278
3963
  }
3279
3964
  /** A piece thrown into a wall: the wall takes the blow of its weight where it met it, the piece takes it too, and each sounds as its material does. */
3280
3965
  slam(p, hit, speed) {
@@ -4047,6 +4732,10 @@ var Renderer = class {
4047
4732
  old.halfExtents = half;
4048
4733
  this.pieceSys.setCenter(old, center);
4049
4734
  old.mass = Math.max(0.05, this.massOf(objectId));
4735
+ if (old.contacts || old.round) {
4736
+ this.fitSurface(b, old);
4737
+ if (!old.round) this.setContacts(b, old, this.meshesOf(b).filter((mv) => this.visuals.shown(mv)));
4738
+ } else if (old.partBoxes) this.fitPartBoxes(b, old);
4050
4739
  old.sleeping = false;
4051
4740
  } else if (kitOriginOf(b.root)?.kind === "furniture") {
4052
4741
  const piece = this.makePiece(objectId);
@@ -4100,6 +4789,7 @@ var Renderer = class {
4100
4789
  }
4101
4790
  const piece = this.pieceSys.create(newId, group, half, center, this.massOf(newId), vel, ang);
4102
4791
  nb.piece = piece;
4792
+ this.fitSurface(nb, piece);
4103
4793
  if (kitOriginOf(from.root)?.kind === "furniture") this.tipIfTopHeavy(nb);
4104
4794
  this.o.onPiece?.(piece);
4105
4795
  }
@@ -4120,7 +4810,10 @@ var Renderer = class {
4120
4810
  body.halfExtents = half;
4121
4811
  this.pieceSys.setCenter(body, center);
4122
4812
  body.mass = Math.max(0.05, this.massOf(objectId));
4123
- if (!structural) this.setContacts(b, body, left);
4813
+ if (!structural) {
4814
+ this.fitSurface(b, body);
4815
+ if (!body.round) this.setContacts(b, body, left);
4816
+ }
4124
4817
  body.sleeping = false;
4125
4818
  return;
4126
4819
  }
@@ -4232,11 +4925,13 @@ var Renderer = class {
4232
4925
  }
4233
4926
  /**
4234
4927
  * The piece touches the ground by the corners of each part left of it, not by the box around them all: on one leg, or on the legs of one side, its centre of mass is off
4235
- * what holds it and it goes over until the top meets the floor. Past `CONTACT_MESHES` meshes it keeps the corners of its box.
4928
+ * what holds it and it goes over until the top meets the floor. Past `CONTACT_MESHES` meshes it keeps the corners of its box; up to `PART_BOX_MESHES`, what lands on it lands
4929
+ * on its parts.
4236
4930
  */
4237
4931
  setContacts(b, body, meshes) {
4238
- if (meshes.length > CONTACT_MESHES) {
4932
+ if (meshes.length > PART_BOX_MESHES) {
4239
4933
  body.contacts = void 0;
4934
+ body.partBoxes = void 0;
4240
4935
  return;
4241
4936
  }
4242
4937
  const inv = specFrameInverse(this.THREE, b.root);
@@ -4244,14 +4939,20 @@ var Renderer = class {
4244
4939
  const lb = new this.THREE.Box3();
4245
4940
  const [cx, cy, cz] = body.localCenter;
4246
4941
  const points = [];
4942
+ const boxes = [];
4247
4943
  for (const mv of meshes) {
4248
4944
  mv.mesh.updateWorldMatrix(true, false);
4249
4945
  this.visuals.localBox(mv, lb);
4250
4946
  if (lb.isEmpty()) continue;
4251
4947
  lb.applyMatrix4(m4.multiplyMatrices(inv, mv.mesh.matrixWorld));
4252
4948
  for (let i = 0; i < 8; i++) points.push([(i & 1 ? lb.max.x : lb.min.x) - cx, (i & 2 ? lb.max.y : lb.min.y) - cy, (i & 4 ? lb.max.z : lb.min.z) - cz]);
4949
+ boxes.push({
4950
+ center: [(lb.min.x + lb.max.x) / 2 - cx, (lb.min.y + lb.max.y) / 2 - cy, (lb.min.z + lb.max.z) / 2 - cz],
4951
+ half: [Math.max(5e-3, (lb.max.x - lb.min.x) / 2), Math.max(5e-3, (lb.max.y - lb.min.y) / 2), Math.max(5e-3, (lb.max.z - lb.min.z) / 2)]
4952
+ });
4253
4953
  }
4254
- body.contacts = points.length > 0 ? points : void 0;
4954
+ body.contacts = points.length > 0 && meshes.length <= CONTACT_MESHES ? points : void 0;
4955
+ body.partBoxes = boxes.length > 1 ? boxes : void 0;
4255
4956
  }
4256
4957
  /**
4257
4958
  * A piece of furniture standing on feet too close for its height (a board on two legs in a line, a lone leg) does not stay up: the box of a piece hides that its weight is up