@adatechnology/game-destruction-three 0.26.5 → 0.26.6

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
@@ -1578,6 +1578,8 @@ var rows = (r) => {
1578
1578
  const [x, y, z, w] = r;
1579
1579
  return [1 - 2 * (y * y + z * z), 2 * (x * y - z * w), 2 * (x * z + y * w), 2 * (x * y + z * w), 1 - 2 * (x * x + z * z), 2 * (y * z - x * w), 2 * (x * z - y * w), 2 * (y * z + x * w), 1 - 2 * (x * x + y * y)];
1580
1580
  };
1581
+ var HINGE_RELEASE = 40 * Math.PI / 180;
1582
+ var HINGE_SECONDS = 1.5;
1581
1583
  var isTurned = (box) => box.rotation !== void 0 && rows(box.rotation).some((v) => Math.abs(v) > 1e-6 && Math.abs(Math.abs(v) - 1) > 1e-6);
1582
1584
  var squareBoxOf = (box) => {
1583
1585
  const r = box.rotation;
@@ -3364,6 +3366,47 @@ var GroundSchedule = class {
3364
3366
  }
3365
3367
  };
3366
3368
 
3369
+ // src/merge-flush-boxes.ts
3370
+ var FLUSH = 1e-4;
3371
+ var sameTurn = (a, b) => {
3372
+ if (!a.rotation && !b.rotation) return true;
3373
+ if (!a.rotation || !b.rotation) return false;
3374
+ return a.rotation.every((v, i) => Math.abs(v - b.rotation[i]) < FLUSH);
3375
+ };
3376
+ var flushAxis = (a, b) => {
3377
+ if (!sameTurn(a, b)) return -1;
3378
+ let along2 = -1;
3379
+ for (let axis = 0; axis < 3; axis++) {
3380
+ const same2 = Math.abs(a.half[axis] - b.half[axis]) < FLUSH && Math.abs(a.center[axis] - b.center[axis]) < FLUSH;
3381
+ if (same2) continue;
3382
+ if (along2 >= 0) return -1;
3383
+ along2 = axis;
3384
+ }
3385
+ if (along2 < 0) return -1;
3386
+ return Math.abs(Math.abs(a.center[along2] - b.center[along2]) - a.half[along2] - b.half[along2]) < FLUSH ? along2 : -1;
3387
+ };
3388
+ function mergeFlushBoxes(boxes) {
3389
+ const out = [...boxes];
3390
+ for (let i = 0; i < out.length; i++) {
3391
+ for (let j = i + 1; j < out.length; j++) {
3392
+ const a = out[i];
3393
+ const axis = flushAxis(a, out[j]);
3394
+ if (axis < 0) continue;
3395
+ const b = out[j];
3396
+ const low = Math.min(a.center[axis] - a.half[axis], b.center[axis] - b.half[axis]);
3397
+ const high = Math.max(a.center[axis] + a.half[axis], b.center[axis] + b.half[axis]);
3398
+ const center = [a.center[0], a.center[1], a.center[2]];
3399
+ const half = [a.half[0], a.half[1], a.half[2]];
3400
+ center[axis] = (low + high) / 2;
3401
+ half[axis] = (high - low) / 2;
3402
+ out[i] = { center, half, ...a.rotation ? { rotation: a.rotation } : {} };
3403
+ out.splice(j, 1);
3404
+ j = i;
3405
+ }
3406
+ }
3407
+ return out;
3408
+ }
3409
+
3367
3410
  // src/engine-pieces.ts
3368
3411
  var GROUND_WORKER_FRAMES = 2;
3369
3412
  var wasCoarseOf = (tile) => !tile.slab && tile.collision.length < tile.heights.length;
@@ -3401,6 +3444,7 @@ var RING_SEGMENTS = 16;
3401
3444
  var TOPPLE = 3;
3402
3445
  var HOLD_MARGIN = 4;
3403
3446
  var SIDE_DEAD = 0.01;
3447
+ var BOX_FOOT = 0.02;
3404
3448
  var PUSH_SPIN = 0.3;
3405
3449
  var STEADY_TURN = 0.1;
3406
3450
  var STEADY_SECONDS = 1.5;
@@ -3459,6 +3503,8 @@ var EnginePieces = class {
3459
3503
  __publicField(this, "pose", new Float64Array(7));
3460
3504
  __publicField(this, "velocity", new Float64Array(6));
3461
3505
  __publicField(this, "contactXYZ", new Float32Array(48));
3506
+ /** The pieces hung on a hinge: the fixed anchor at the screw and the joint that holds them to it. */
3507
+ __publicField(this, "hung", /* @__PURE__ */ new Map());
3462
3508
  __publicField(this, "outAxis", new Float64Array(3));
3463
3509
  __publicField(this, "wayAxis", new Float64Array(3));
3464
3510
  /** The centre `readBack` reads into (the sweep may move it). */
@@ -3509,6 +3555,8 @@ var EnginePieces = class {
3509
3555
  remove(b, wakeNeighbours = true) {
3510
3556
  const link = this.links.get(b);
3511
3557
  if (!link) return;
3558
+ const held = this.hung.get(b);
3559
+ if (held) this.letGo(b, held);
3512
3560
  this.world.removeBody(link.id);
3513
3561
  if (wakeNeighbours && (b.sleeping || link.sleeping)) this.wakeAround(b, link.pos);
3514
3562
  this.byUser.delete(link.user);
@@ -3649,6 +3697,7 @@ var EnginePieces = class {
3649
3697
  this.applyDueGround();
3650
3698
  this.layBlastGround();
3651
3699
  this.makeBodies(each);
3700
+ each((b) => this.hang(b));
3652
3701
  each((b) => this.sync(b, dt, h));
3653
3702
  if (this.schedule) this.layGroundLater(each, dt);
3654
3703
  else this.layGround(each, dt);
@@ -3682,8 +3731,44 @@ var EnginePieces = class {
3682
3731
  this.readActive(awake);
3683
3732
  awake.walk((b) => this.readBack(b, dt, time, this.active));
3684
3733
  } else for (const b of list) this.readBack(b, dt, time, null);
3734
+ this.wearHinges(dt);
3685
3735
  this.knock(time);
3686
3736
  }
3737
+ /** A piece with a hinge asked for (`PieceSystem.hinge`) gets a fixed anchor at the screw and a hinge joint to it, once its body exists. It swings in the vertical plane through the screw and its centre. */
3738
+ hang(b) {
3739
+ const hinge = b.hinge;
3740
+ const link = this.links.get(b);
3741
+ if (!hinge || !link || this.hung.has(b)) return;
3742
+ const c = this.centre(b);
3743
+ const dx = c.x - hinge.point[0];
3744
+ const dz = c.z - hinge.point[2];
3745
+ const along2 = Math.hypot(dx, dz);
3746
+ const axis = along2 < 1e-3 ? [0, 0, 1] : [dz / along2, 0, -dx / along2];
3747
+ const anchor = this.world.createBody({ shape: { kind: "box", half: [5e-3, 5e-3, 5e-3] }, motion: "static", position: hinge.point, user: 0 });
3748
+ this.world.ignorePair(anchor, link.id, true);
3749
+ const joint = this.world.createJoint({ kind: "hinge", a: anchor, b: link.id, point: hinge.point, axis, normal: [0, 1, 0], min: -Math.PI / 2, max: Math.PI / 2 });
3750
+ this.hung.set(b, { anchor, joint });
3751
+ }
3752
+ /** The screw tears out: past `HINGE_RELEASE` of turn, or after `HINGE_SECONDS`, the piece falls free. */
3753
+ wearHinges(dt) {
3754
+ for (const [b, held] of this.hung) {
3755
+ const hinge = b.hinge;
3756
+ if (hinge) {
3757
+ hinge.seconds -= dt;
3758
+ const [x, y, z, w] = hinge.from;
3759
+ const q = b.root.quaternion;
3760
+ const turned = 2 * Math.acos(Math.min(1, Math.abs(q.x * x + q.y * y + q.z * z + q.w * w)));
3761
+ if (turned < HINGE_RELEASE && hinge.seconds > 0) continue;
3762
+ }
3763
+ this.letGo(b, held);
3764
+ }
3765
+ }
3766
+ letGo(b, held) {
3767
+ this.world.removeJoint(held.joint);
3768
+ this.world.removeBody(held.anchor);
3769
+ this.hung.delete(b);
3770
+ b.hinge = void 0;
3771
+ }
3687
3772
  /** The engine's awake bodies in one crossing: a sleeping piece one of them wakes (a blow) is among the awake pieces from now on, and read back in this same step. */
3688
3773
  readActive(awake) {
3689
3774
  this.active.clear();
@@ -3767,7 +3852,7 @@ var EnginePieces = class {
3767
3852
  return { kind: "compound", children: [{ shape: { kind: "cylinder", halfHeight: b.halfExtents[round.axis], radius: round.radius }, position: [0, 0, 0], rotation }] };
3768
3853
  }
3769
3854
  if (b.partBoxes && b.partBoxes.length > 0) {
3770
- return { kind: "compound", children: b.partBoxes.map((part, i) => ({ shape: { kind: "box", half: part.half }, position: part.center, ...part.rotation ? { rotation: part.rotation } : {}, tag: i })) };
3855
+ return { kind: "compound", children: mergeFlushBoxes(b.partBoxes).map((part, i) => ({ shape: { kind: "box", half: part.half }, position: part.center, ...part.rotation ? { rotation: part.rotation } : {}, tag: i })) };
3771
3856
  }
3772
3857
  return { kind: "box", half: b.halfExtents };
3773
3858
  }
@@ -3833,8 +3918,7 @@ var EnginePieces = class {
3833
3918
  return {
3834
3919
  frictionStatic: b.friction?.static ?? MU_STATIC,
3835
3920
  frictionDynamic: b.friction?.dynamic ?? MU_DYNAMIC,
3836
- // a box lands dead (the kit never bounced them); a round piece bounces by its material
3837
- restitution: b.round ? b.restitution ?? SLAM_BOUNCE : 0,
3921
+ restitution: b.restitution ?? SLAM_BOUNCE,
3838
3922
  rolling: b.friction?.rolling ?? ROLLING_RESISTANCE
3839
3923
  };
3840
3924
  }
@@ -4079,8 +4163,16 @@ var EnginePieces = class {
4079
4163
  this.slip(b, link, false);
4080
4164
  return;
4081
4165
  }
4082
- const c = this.centre(b);
4083
4166
  const q = b.root.quaternion;
4167
+ const c = this.centre(b);
4168
+ let shiftX = 0, shiftY = 0, shiftZ = 0;
4169
+ if (b.massCentre) {
4170
+ this.w.set(b.massCentre[0], b.massCentre[1], b.massCentre[2]).applyQuaternion(q);
4171
+ shiftX = this.w.x;
4172
+ shiftY = this.w.y;
4173
+ shiftZ = this.w.z;
4174
+ c.add(this.w);
4175
+ }
4084
4176
  let low = Infinity;
4085
4177
  for (let i = 0; i < n; i++) low = Math.min(low, this.contactXYZ[i * 3 + 1] - c.y);
4086
4178
  let x0 = Infinity, x1 = -Infinity, z0 = Infinity, z1 = -Infinity;
@@ -4092,6 +4184,23 @@ var EnginePieces = class {
4092
4184
  z0 = Math.min(z0, rz);
4093
4185
  z1 = Math.max(z1, rz);
4094
4186
  }
4187
+ if (!b.contacts && !b.round && !b.apart) {
4188
+ const [hx, hy, hz] = b.halfExtents;
4189
+ const corner = this.w;
4190
+ let lowest = Infinity;
4191
+ for (let k = 0; k < 8; k++) lowest = Math.min(lowest, corner.set(k & 1 ? hx : -hx, k & 2 ? hy : -hy, k & 4 ? hz : -hz).applyQuaternion(q).y - shiftY);
4192
+ if (Math.abs(lowest - low) < BOX_FOOT) {
4193
+ for (let k = 0; k < 8; k++) {
4194
+ corner.set(k & 1 ? hx : -hx, k & 2 ? hy : -hy, k & 4 ? hz : -hz).applyQuaternion(q);
4195
+ if (corner.y - shiftY > lowest + BOX_FOOT) continue;
4196
+ x0 = Math.min(x0, corner.x - shiftX);
4197
+ x1 = Math.max(x1, corner.x - shiftX);
4198
+ z0 = Math.min(z0, corner.z - shiftZ);
4199
+ z1 = Math.max(z1, corner.z - shiftZ);
4200
+ }
4201
+ low = Math.min(low, lowest);
4202
+ }
4203
+ }
4095
4204
  const height = -low;
4096
4205
  const wx = x1 - x0;
4097
4206
  const wz = z1 - z0;
@@ -4493,7 +4602,7 @@ var EnginePieces = class {
4493
4602
  replaceGround(tile, made2, ix, iz, material) {
4494
4603
  if (tile) this.world.removeBody(tile.ground);
4495
4604
  if (made2.level === null) return this.world.createBody({ shape: this.heightField(made2, ix, iz), motion: "static", position: [0, 0, 0], material, user: 0 });
4496
- return this.world.createBody({ shape: { kind: "box", half: [TILE / 2, 0.5, TILE / 2] }, motion: "static", position: [(ix + 0.5) * TILE, made2.level - 0.5, (iz + 0.5) * TILE], material, user: 0 });
4605
+ return this.world.createBody({ shape: { kind: "box", half: [TILE / 2, 0.5, TILE / 2], convexRadius: 0 }, motion: "static", position: [(ix + 0.5) * TILE, made2.level - 0.5, (iz + 0.5) * TILE], material, user: 0 });
4497
4606
  }
4498
4607
  /** The solid under a height field, on its own body, swapped in place when the ground changed; none under a slab. Boxes only: a box costs nothing to make, a hull did (T3.8ag). */
4499
4608
  layVolume(t, tile, material) {
@@ -4517,7 +4626,7 @@ var EnginePieces = class {
4517
4626
  }
4518
4627
  stepBoxes(boxes) {
4519
4628
  const children = [];
4520
- for (let i = 0; i < boxes.length; i += 6) children.push({ shape: { kind: "box", half: [boxes[i + 3], boxes[i + 4], boxes[i + 5]] }, position: [boxes[i], boxes[i + 1], boxes[i + 2]] });
4629
+ for (let i = 0; i < boxes.length; i += 6) children.push({ shape: { kind: "box", half: [boxes[i + 3], boxes[i + 4], boxes[i + 5]], convexRadius: 0 }, position: [boxes[i], boxes[i + 1], boxes[i + 2]] });
4521
4630
  return { kind: "compound", children };
4522
4631
  }
4523
4632
  /** The host's fixed boxes, one static body per object (`EngineStatics`). */
@@ -4741,6 +4850,10 @@ var MU_DYNAMIC2 = 0.6;
4741
4850
  var SLIP_SPEED = 0.02;
4742
4851
  var ROLLING_RESISTANCE2 = 0.015;
4743
4852
  var ROUND_LYING = Math.SQRT1_2;
4853
+ var MIN_PIECE_HALF = 1e-3;
4854
+ var OWN_MIN_PIECE_HALF = 0.01;
4855
+ var OWN_MIN_MEASURED_HALF = 0.02;
4856
+ var minPieceHalf = (physics) => physics === "own" ? OWN_MIN_MEASURED_HALF : MIN_PIECE_HALF;
4744
4857
  var SLEEP_AFTER = 0.5;
4745
4858
  var STILL_SPEED2 = 0.12;
4746
4859
  var STILL_SPIN2 = 0.45;
@@ -4760,6 +4873,8 @@ var TIP_RISE_AFTER = 0.5;
4760
4873
  var TIP_LYING = 0.25;
4761
4874
  var TIP_SECONDS = 3;
4762
4875
  var STEP = 0.03;
4876
+ var WAKE_SAMPLES = 4;
4877
+ var WAKE_DROP = 0.05;
4763
4878
  var WARM_SLOTS = 8 + 12 * 7;
4764
4879
  var SOLVER_PASSES = 10;
4765
4880
  var SIDE_BOUNCE = 0.1;
@@ -4787,8 +4902,6 @@ var cellKey = (ix, iz) => (ix + 32768) * 65536 + (iz + 32768);
4787
4902
  var MAX_COLLISION_STEPS = 8;
4788
4903
  var MIN_ROUND_RADIUS = 5e-3;
4789
4904
  var BOUNCE_MIN_SPEED = 0.1;
4790
- var HINGE_RELEASE = 40 * Math.PI / 180;
4791
- var HINGE_SECONDS = 1.5;
4792
4905
  var BOX_IMPACT_SPEED = 1.5;
4793
4906
  var KNOCK_FLAT = 0.5;
4794
4907
  var SEPARATE_SPEED = 1.5;
@@ -4854,6 +4967,7 @@ var PieceSystem = class {
4854
4967
  __publicField(this, "m");
4855
4968
  __publicField(this, "hingeM");
4856
4969
  __publicField(this, "ax");
4970
+ __publicField(this, "wakeBox");
4857
4971
  __publicField(this, "solids", []);
4858
4972
  /** The engine stepping the pieces (`PieceSystemOptions.physics`), or null for the kit's own solver. */
4859
4973
  __publicField(this, "engine");
@@ -4944,6 +5058,7 @@ var PieceSystem = class {
4944
5058
  this.m = new THREE.Matrix4();
4945
5059
  this.hingeM = new THREE.Matrix4();
4946
5060
  this.ax = new THREE.Vector3();
5061
+ this.wakeBox = new THREE.Box3();
4947
5062
  const resolved = resolvePhysics(o.physics);
4948
5063
  this.loop = resolved?.loop ?? null;
4949
5064
  this.stepsItself = resolved?.stepsItself ?? false;
@@ -5108,10 +5223,9 @@ var PieceSystem = class {
5108
5223
  }
5109
5224
  /**
5110
5225
  * Hangs a piece on a hinge at `point` (world): that point of it stays where it is while the rest swings down about it, as a shelf that lost the side at one end swings on
5111
- * the screw at the other and pours what lay on it off its free end. It lets go past `HINGE_RELEASE` or `HINGE_SECONDS`. The kit's own solver only (`physics` swings nothing).
5226
+ * the screw at the other and pours what lay on it off its free end. It lets go past `HINGE_RELEASE` or `HINGE_SECONDS`. On the engine it is a hinge joint to a fixed anchor (`EnginePieces.hang`).
5112
5227
  */
5113
5228
  hinge(body, point) {
5114
- if (this.engine) return;
5115
5229
  const c = this.centerOf(body, this.p);
5116
5230
  const q = body.root.quaternion;
5117
5231
  const local = this.ax.set(point[0] - c.x, point[1] - c.y, point[2] - c.z).applyQuaternion(this.dq.copy(q).invert());
@@ -5130,6 +5244,7 @@ var PieceSystem = class {
5130
5244
  return existing;
5131
5245
  }
5132
5246
  const awake = this.awake;
5247
+ const least = this.engine ? MIN_PIECE_HALF : OWN_MIN_PIECE_HALF;
5133
5248
  let asleep = false;
5134
5249
  let inHand = false;
5135
5250
  const body = {
@@ -5139,7 +5254,7 @@ var PieceSystem = class {
5139
5254
  root,
5140
5255
  velocity: velocity ? [...velocity] : [0, 0, 0],
5141
5256
  angularVelocity: angular ? [...angular] : [0, 0, 0],
5142
- halfExtents: [Math.max(0.01, halfExtents[0]), Math.max(0.01, halfExtents[1]), Math.max(0.01, halfExtents[2])],
5257
+ halfExtents: [Math.max(least, halfExtents[0]), Math.max(least, halfExtents[1]), Math.max(least, halfExtents[2])],
5143
5258
  mass: Math.max(0.05, mass),
5144
5259
  // asleep or held is read and written as a plain flag; waking or taking it in hand puts the piece among the awake ones
5145
5260
  get sleeping() {
@@ -5339,12 +5454,31 @@ var PieceSystem = class {
5339
5454
  wakeRegion(x, z, radius) {
5340
5455
  this.engine?.groundChanged(x, z, radius);
5341
5456
  for (const b of this.list) {
5342
- if (!b.sleeping || b.held || Math.hypot(b.root.position.x - x, b.root.position.z - z) > radius) continue;
5457
+ if (!b.sleeping || b.held) continue;
5458
+ if (Math.hypot(b.root.position.x - x, b.root.position.z - z) > radius && !this.lostGroundIn(b, x, z, radius)) continue;
5343
5459
  b.sleeping = false;
5344
5460
  b.still = 0;
5345
5461
  b.calmAt = null;
5346
5462
  }
5347
5463
  }
5464
+ /**
5465
+ * A piece whose root is elsewhere (cut from a whole object, its root is that object's origin) that lies in the disc, with the ground gone from under part of its footprint:
5466
+ * where it lies is read from its drawn box, and only a support that went down wakes it (a piece the disc only passes over stays asleep).
5467
+ */
5468
+ lostGroundIn(b, x, z, radius) {
5469
+ const center = this.centerOf(b, this.ax);
5470
+ if (Math.hypot(center.x - x, center.z - z) > radius + Math.hypot(b.halfExtents[0], b.halfExtents[2])) return false;
5471
+ const box = this.wakeBox.setFromObject(b.root);
5472
+ for (let i = 0; i <= WAKE_SAMPLES; i++) {
5473
+ for (let k = 0; k <= WAKE_SAMPLES; k++) {
5474
+ const px = box.min.x + (box.max.x - box.min.x) * i / WAKE_SAMPLES, pz = box.min.z + (box.max.z - box.min.z) * k / WAKE_SAMPLES;
5475
+ if (Math.hypot(px - x, pz - z) > radius) continue;
5476
+ const support = Math.max(this.o.groundAt(px, pz), this.o.staticTopAt?.(px, pz, box.min.y, STEP) ?? -Infinity);
5477
+ if (support < box.min.y - WAKE_DROP) return true;
5478
+ }
5479
+ }
5480
+ return false;
5481
+ }
5348
5482
  /** A step is about to move the pieces: the roots go back to the step's pose and the awake ones are recorded as the previous one (`interpolate`). */
5349
5483
  beginTick() {
5350
5484
  const trail = this.trail;
@@ -6139,7 +6273,7 @@ var PieceSystem = class {
6139
6273
  let awake = 0;
6140
6274
  for (const b of this.list) {
6141
6275
  if (b.sleeping || b.held) continue;
6142
- if (b.balancing && this.hasComeToRest(b)) this.sleepByBudget(b);
6276
+ if (this.hasComeToRest(b) && (b.balancing || b.still >= SLEEP_AFTER && !b.hinge && !this.onSteepSupport(b))) this.sleepByBudget(b);
6143
6277
  else awake++;
6144
6278
  }
6145
6279
  for (const b of this.list) {
@@ -6215,7 +6349,9 @@ var PieceSystem = class {
6215
6349
  }
6216
6350
  /** Sleeps sooner than it would by itself, so it answers to every point it lies on (`restPoints`). */
6217
6351
  sleepByBudget(b) {
6352
+ const steady = b.steady;
6218
6353
  this.sleep(b);
6354
+ b.steady = steady;
6219
6355
  b.restPoints = this.restPointsOf(b);
6220
6356
  }
6221
6357
  hasComeToRest(b) {
@@ -8875,7 +9011,6 @@ var CONTACT_MESHES = 12;
8875
9011
  var PART_BOX_MESHES = 64;
8876
9012
  var ROUND_SAMPLES = 400;
8877
9013
  var ROUND_SIDES = 0.12;
8878
- var MIN_PIECE_HALF = 0.02;
8879
9014
  var ROUND_SLACK = 1.12;
8880
9015
  var RIM_POINTS = 12;
8881
9016
  var rimPoints = (round, half) => {
@@ -9451,7 +9586,8 @@ var Renderer = class {
9451
9586
  }
9452
9587
  if (total.isEmpty()) return { half: [0.25, 0.25, 0.25], center: new this.THREE.Vector3() };
9453
9588
  const size = total.getSize(new this.THREE.Vector3());
9454
- return { half: [Math.max(MIN_PIECE_HALF, size.x / 2), Math.max(MIN_PIECE_HALF, size.y / 2), Math.max(MIN_PIECE_HALF, size.z / 2)], center: total.getCenter(new this.THREE.Vector3()) };
9589
+ const least = minPieceHalf(this.o.physics);
9590
+ return { half: [Math.max(least, size.x / 2), Math.max(least, size.y / 2), Math.max(least, size.z / 2)], center: total.getCenter(new this.THREE.Vector3()) };
9455
9591
  }
9456
9592
  /**
9457
9593
  * A hosted body's boxes (`hostedSolids`: its damage zones) cut to the box of what its root draws, in the root's frame: the zones reach past the drawn body (17 cm each
@@ -10965,6 +11101,7 @@ var Renderer = class {
10965
11101
  old.halfExtents = half;
10966
11102
  this.pieceSys.setCenter(old, center);
10967
11103
  old.mass = Math.max(MIN_PIECE_MASS_KG, this.massOf(objectId) - (old.massOffset ?? 0));
11104
+ old.apart = true;
10968
11105
  if (old.contacts || old.round) {
10969
11106
  this.fitSurface(b, old);
10970
11107
  if (!old.round) this.setContacts(b, old, this.meshesOf(b).filter((mv) => this.visuals.shown(mv)));
@@ -10972,6 +11109,7 @@ var Renderer = class {
10972
11109
  if (!this.splittingAtLoad) old.sleeping = false;
10973
11110
  } else if (kitOriginOf3(b.root)?.kind === "furniture" && !this.splittingAtLoad) {
10974
11111
  const piece = this.makePiece(objectId);
11112
+ piece.apart = true;
10975
11113
  this.tipIfTopHeavy(b, true);
10976
11114
  this.o.onLoosened?.(objectId, piece, "apart");
10977
11115
  }
@@ -11223,6 +11361,7 @@ var Renderer = class {
11223
11361
  }
11224
11362
  const piece = this.pieceSys.create(newId, group, half, center, this.massOf(newId), vel, ang);
11225
11363
  nb.piece = piece;
11364
+ piece.apart = true;
11226
11365
  this.fitSurface(nb, piece);
11227
11366
  this.fitPartBoxes(nb, piece);
11228
11367
  const resting = this.splittingAtLoad && this.pieceSys.restIfSupported(piece);
@@ -11248,6 +11387,7 @@ var Renderer = class {
11248
11387
  body.halfExtents = half;
11249
11388
  this.pieceSys.setCenter(body, center);
11250
11389
  body.mass = Math.max(MIN_PIECE_MASS_KG, this.massOf(objectId) - (body.massOffset ?? 0));
11390
+ body.apart = true;
11251
11391
  if (!structural) {
11252
11392
  this.fitSurface(b, body);
11253
11393
  if (!body.round) this.setContacts(b, body, left);
@@ -11279,6 +11419,7 @@ var Renderer = class {
11279
11419
  away = hull.length >= 3 ? inside.out : null;
11280
11420
  }
11281
11421
  const piece = this.makePiece(objectId);
11422
+ (b.piece ?? piece).apart = true;
11282
11423
  this.setContacts(b, b.piece ?? piece, left);
11283
11424
  const loose = b.piece;
11284
11425
  if (away && loose) {
@@ -11330,6 +11471,7 @@ var Renderer = class {
11330
11471
  }
11331
11472
  if (!this.standsOn(objectId, wholeBoxes) || this.standsOn(objectId, boxes)) return false;
11332
11473
  const piece = this.makePiece(objectId);
11474
+ (b.piece ?? piece).apart = true;
11333
11475
  this.setContacts(b, b.piece ?? piece, this.meshesOf(b).filter((mv) => this.visuals.shown(mv)));
11334
11476
  this.tipIfTopHeavy(b);
11335
11477
  this.o.onLoosened?.(objectId, piece, "apart");
@@ -11412,6 +11554,38 @@ var Renderer = class {
11412
11554
  }
11413
11555
  body.contacts = points.length > 0 && meshes.length <= CONTACT_MESHES ? points : void 0;
11414
11556
  body.partBoxes = boxes.length > 1 || boxes.length === 1 && isTurned(boxes[0]) ? boxes : void 0;
11557
+ body.massCentre = this.massCentreOf(b, body);
11558
+ }
11559
+ /** Where the weight of what is drawn of `b` is, from the middle of its box (`PieceBody.massCentre`): each part's box centre weighed by the part's mass. */
11560
+ massCentreOf(b, body) {
11561
+ if (b.parts.size < 2) return void 0;
11562
+ const inv = specFrameInverse(this.THREE, b.root);
11563
+ const m4 = new this.THREE.Matrix4();
11564
+ const lb = new this.THREE.Box3();
11565
+ const box = new this.THREE.Box3();
11566
+ const middle = new this.THREE.Vector3();
11567
+ let weight = 0;
11568
+ const sum = [0, 0, 0];
11569
+ for (const [partId, pv] of b.parts) {
11570
+ const mass = pv.gone === "none" ? this.world.part(b.id, partId)?.mass ?? 0 : 0;
11571
+ if (mass <= 0) continue;
11572
+ box.makeEmpty();
11573
+ for (const mv of pv.meshes) {
11574
+ if (!this.visuals.shown(mv)) continue;
11575
+ mv.mesh.updateWorldMatrix(true, false);
11576
+ this.visuals.localBox(mv, lb);
11577
+ if (!lb.isEmpty()) box.union(lb.applyMatrix4(m4.multiplyMatrices(inv, mv.mesh.matrixWorld)));
11578
+ }
11579
+ if (box.isEmpty()) continue;
11580
+ box.getCenter(middle);
11581
+ sum[0] += middle.x * mass;
11582
+ sum[1] += middle.y * mass;
11583
+ sum[2] += middle.z * mass;
11584
+ weight += mass;
11585
+ }
11586
+ if (weight <= 0) return void 0;
11587
+ const [cx, cy, cz] = body.localCenter;
11588
+ return [sum[0] / weight - cx, sum[1] / weight - cy, sum[2] / weight - cz];
11415
11589
  }
11416
11590
  /** `fitOrientedBoxes` of a ranged part's points, taken from what it gave before when the points and the triangles are the very same numbers. */
11417
11591
  fitRanged(mv, points) {