@adatechnology/game-destruction-three 0.29.3 → 1.0.0

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
@@ -3694,7 +3694,6 @@ var MU_STATIC = 0.7;
3694
3694
  var ROLLING_RESISTANCE = 0.015;
3695
3695
  var MU_DYNAMIC = 0.6;
3696
3696
  var GROUND = { frictionStatic: MU_STATIC, frictionDynamic: MU_DYNAMIC, restitution: 0, rolling: ROLLING_RESISTANCE };
3697
- var groundTileKey = (x, z) => (Math.floor(x / TILE) + TILE_KEY_ORIGIN) * TILE_KEY + (Math.floor(z / TILE) + TILE_KEY_ORIGIN);
3698
3697
  var tileRollingAt = (rollingAt, x, z) => rollingAt((Math.floor(x / TILE) + 0.5) * TILE, (Math.floor(z / TILE) + 0.5) * TILE);
3699
3698
  var STILL_SPEED = 0.12;
3700
3699
  var STILL_SPIN = 0.45;
@@ -5114,9 +5113,8 @@ var PhysicsOptionError = class extends Error {
5114
5113
  }
5115
5114
  };
5116
5115
  var resolvePhysics = (option) => {
5117
- if (option === "own") return null;
5118
5116
  if (option === void 0 || typeof option !== "object") {
5119
- throw new PhysicsOptionError("the `physics` option is required: pass a PhysicsLoop, a world from the host, or 'own' for the kit's own solver");
5117
+ throw new PhysicsOptionError("the `physics` option is required: pass a PhysicsLoop or a world from the host");
5120
5118
  }
5121
5119
  if (isPhysicsLoop(option)) return { loop: option, stepsItself: false };
5122
5120
  if (typeof option.stepFixed !== "function") {
@@ -5166,44 +5164,20 @@ function isOverSupport(points, centre, tolerance) {
5166
5164
  }
5167
5165
 
5168
5166
  // src/pieces.ts
5169
- var SLAM_MIN_SPEED2 = 2;
5170
- var SLAM_BOUNCE2 = 0.2;
5171
5167
  var BALANCE_TOLERANCE = 0.01;
5172
- var SLAM_FLOOR_NORMAL2 = 0.7;
5173
- var SUB_DT = 1 / 120;
5174
5168
  var MU_STATIC2 = 0.7;
5175
5169
  var MU_DYNAMIC2 = 0.6;
5176
- var SLIP_SPEED = 0.02;
5177
5170
  var ROLLING_RESISTANCE2 = 0.015;
5178
- var ROUND_LYING = Math.SQRT1_2;
5179
5171
  var MIN_PIECE_HALF = 1e-3;
5180
- var OWN_MIN_PIECE_HALF = 0.01;
5181
- var OWN_MIN_MEASURED_HALF = 0.02;
5182
- var minPieceHalf = (physics) => physics === "own" ? OWN_MIN_MEASURED_HALF : MIN_PIECE_HALF;
5183
5172
  var SLEEP_AFTER = 0.5;
5184
5173
  var STILL_SPEED2 = 0.12;
5185
5174
  var STILL_SPIN2 = 0.45;
5186
- var CALM_DRIFT = 0.01;
5187
- var CALM_TURN = 0.05;
5188
- var CALM_SLEEP_AFTER = 1.5;
5189
- var STEADY_TURN2 = 0.1;
5190
5175
  var STEADY_SECONDS2 = 1.5;
5191
- var HARD_LANDING2 = 3;
5192
5176
  var SLOP = 2e-3;
5193
- var NARROW2 = 0.3;
5194
- var SUPPORT_MARGIN = 0.05;
5195
- var TOPPLE2 = 3;
5196
- var TIP_ACCELERATION = 30;
5197
- var TIP_RISING = 0.02;
5198
- var TIP_RISE_AFTER = 0.5;
5199
- var TIP_LYING = 0.25;
5200
5177
  var TIP_SECONDS = 3;
5201
5178
  var STEP = 0.03;
5202
5179
  var WAKE_SAMPLES = 4;
5203
5180
  var WAKE_DROP = 0.05;
5204
- var WARM_SLOTS = 8 + 12 * 7;
5205
- var SOLVER_PASSES = 10;
5206
- var SIDE_BOUNCE = 0.1;
5207
5181
  var EDGE_POINTS = [-0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75];
5208
5182
  var EDGE_SPACING = 0.05;
5209
5183
  var MAX_EDGE_POINTS = 31;
@@ -5220,15 +5194,9 @@ var STEEP_MARGIN = 1.1;
5220
5194
  var SLOPE_MIN_NORMAL = 0.5;
5221
5195
  var LIES_FLAT = 0.985;
5222
5196
  var KEEP_EVERY = 0.5;
5223
- var WAKE_ABOVE_SPEED = 0.05;
5224
- var WAKE_ABOVE_SPIN = 0.1;
5225
5197
  var SURFACE_CELL = 0.5;
5226
5198
  var SURFACE_CELLS_ACROSS = 24;
5227
5199
  var cellKey = (ix, iz) => (ix + 32768) * 65536 + (iz + 32768);
5228
- var MAX_COLLISION_STEPS = 8;
5229
- var MIN_ROUND_RADIUS = 5e-3;
5230
- var BOUNCE_MIN_SPEED = 0.1;
5231
- var BOX_IMPACT_SPEED = 1.5;
5232
5200
  var KNOCK_FLAT = 0.5;
5233
5201
  var SEPARATE_SPEED = 1.5;
5234
5202
  var LIFT_DEPTH = STEP;
@@ -5277,8 +5245,6 @@ var pairFriction = (a, b) => ({
5277
5245
  // both lose energy in the contact: a log on grass is held by its bark and by the blades
5278
5246
  rollingDamping: (a?.rollingDamping ?? PIECE_ROLLING_DAMPING) + (b?.rollingDamping ?? GROUND_ROLLING_DAMPING)
5279
5247
  });
5280
- var pairRestitution = (a, b) => Math.sqrt((a ?? SLAM_BOUNCE2) * (b ?? SLAM_BOUNCE2));
5281
- var turnBetween = (a, q) => 2 * Math.acos(Math.min(1, Math.abs(a[0] * q.x + a[1] * q.y + a[2] * q.z + a[3] * q.w)));
5282
5248
  var PieceSystem = class {
5283
5249
  constructor(THREE, o) {
5284
5250
  __publicField(this, "THREE", THREE);
@@ -5287,19 +5253,14 @@ var PieceSystem = class {
5287
5253
  __publicField(this, "time", 0);
5288
5254
  __publicField(this, "generations", 0);
5289
5255
  __publicField(this, "p");
5290
- __publicField(this, "q");
5291
5256
  __publicField(this, "dq");
5292
- __publicField(this, "nextQ");
5293
5257
  __publicField(this, "m");
5294
- __publicField(this, "hingeM");
5295
5258
  __publicField(this, "ax");
5296
5259
  __publicField(this, "wakeBox");
5297
5260
  __publicField(this, "solids", []);
5298
- /** The engine stepping the pieces (`PieceSystemOptions.physics`), or null for the kit's own solver. */
5261
+ /** The engine stepping the pieces (`PieceSystemOptions.physics`). */
5299
5262
  __publicField(this, "engine");
5300
- /** The host's ground worker, disposed with the system: by the engine, or here when there is none (the kit's own solver lays no tiles). */
5301
- __publicField(this, "groundWorker");
5302
- /** The loop the system steps itself (a world was handed), the loop it only registers on (a loop was handed), or null on `'own'`. */
5263
+ /** The loop the system steps itself (a world was handed) or only registers on (a loop was handed). */
5303
5264
  __publicField(this, "loop");
5304
5265
  __publicField(this, "stepsItself");
5305
5266
  /** The phases' way out of the loop, and whether this frame's `prepare` ran (it runs once, in the first fixed step). */
@@ -5349,12 +5310,8 @@ var PieceSystem = class {
5349
5310
  __publicField(this, "supportNormal", UP);
5350
5311
  __publicField(this, "supportFriction", null);
5351
5312
  __publicField(this, "supportBody", null);
5352
- /** The terrain's surfaces by rolling resistance and damping (`terrainSurface`), one object each. */
5353
- __publicField(this, "terrainSurfaces", /* @__PURE__ */ new Map());
5354
- /** The terrain's surface by ground tile (`groundTileKey`) this update, null for the default: read once per tile, not per contact per substep. */
5355
- __publicField(this, "surfaceByTile", /* @__PURE__ */ new Map());
5356
- /** A piece is asleep on both sides (the engine's too) or, on the kit's own solver, asleep and not held: nothing of it changes until it wakes. */
5357
- __publicField(this, "isSettled", (body) => this.engine ? this.engine.isSettled(body) : body.sleeping && !body.held);
5313
+ /** A piece is asleep on both sides (the engine's too): nothing of it changes until it wakes. */
5314
+ __publicField(this, "isSettled", (body) => this.engine.isSettled(body));
5358
5315
  __publicField(this, "measureAwake", (body) => {
5359
5316
  const rec = this.measured(body);
5360
5317
  const from = rec.from;
@@ -5378,18 +5335,15 @@ var PieceSystem = class {
5378
5335
  if (rec.from !== from || rec.list.length !== count || rec.list[0] !== first) this.reshaped = true;
5379
5336
  });
5380
5337
  this.p = new THREE.Vector3();
5381
- this.q = new THREE.Quaternion();
5382
5338
  this.dq = new THREE.Quaternion();
5383
- this.nextQ = new THREE.Quaternion();
5384
5339
  this.m = new THREE.Matrix4();
5385
- this.hingeM = new THREE.Matrix4();
5386
5340
  this.ax = new THREE.Vector3();
5387
5341
  this.wakeBox = new THREE.Box3();
5388
5342
  const resolved = resolvePhysics(o.physics);
5389
- this.loop = resolved?.loop ?? null;
5390
- this.stepsItself = resolved?.stepsItself ?? false;
5343
+ this.loop = resolved.loop;
5344
+ this.stepsItself = resolved.stepsItself;
5391
5345
  this.trail = o.interpolate ? new PoseTrail() : null;
5392
- this.engine = resolved ? new EnginePieces(THREE, resolved.loop.world, {
5346
+ this.engine = new EnginePieces(THREE, resolved.loop.world, {
5393
5347
  groundAt: o.terrainAt ?? o.groundAt,
5394
5348
  ...o.terrainRollingAt ? { groundRollingAt: o.terrainRollingAt } : {},
5395
5349
  ...o.terrainRollingDampingAt ? { groundRollingDampingAt: o.terrainRollingDampingAt } : {},
@@ -5409,13 +5363,12 @@ var PieceSystem = class {
5409
5363
  ...o.heldIgnores ? { heldIgnores: o.heldIgnores } : {},
5410
5364
  ...o.groundWorker ? { groundWorker: o.groundWorker } : {},
5411
5365
  ...o.groundWorkerFrames !== void 0 ? { groundWorkerFrames: o.groundWorkerFrames } : {}
5412
- }) : null;
5413
- this.groundWorker = o.groundWorker ?? null;
5414
- if (resolved && !resolved.stepsItself) this.joinLoop(resolved.loop, this.engine);
5366
+ });
5367
+ if (!resolved.stepsItself) this.joinLoop(resolved.loop, this.engine);
5415
5368
  }
5416
- /** Contact blows on the fixed parts become `blunt` hits of `destruction` (`engine-impacts.ts`); null when the pieces are not in the engine's loop. Returns the way to stop it. */
5369
+ /** Contact blows on the fixed parts become `blunt` hits of `destruction` (`engine-impacts.ts`). Returns the way to stop it. */
5417
5370
  contactHits(destruction, options) {
5418
- return this.engine && this.loop ? createEngineImpacts(this.loop, this.engine.statics, destruction, options) : null;
5371
+ return createEngineImpacts(this.loop, this.engine.statics, destruction, options);
5419
5372
  }
5420
5373
  /**
5421
5374
  * The pieces a blast at `point` can reach: the bodies the engine's `blastQuery` finds within `radius` (the pieces among them), in the order of `list`, and the pieces that have no
@@ -5423,7 +5376,6 @@ var PieceSystem = class {
5423
5376
  */
5424
5377
  piecesInBlast(blastQuery, point, radius) {
5425
5378
  const engine = this.engine;
5426
- if (!engine) return [...this.list];
5427
5379
  const found = blastQuery.run({ origin: point, radius });
5428
5380
  const reached = /* @__PURE__ */ new Set();
5429
5381
  for (let i = 0; i < found.bodyCount; i++) {
@@ -5432,9 +5384,9 @@ var PieceSystem = class {
5432
5384
  }
5433
5385
  return this.list.filter((piece) => reached.has(piece) || !engine.hasBody(piece));
5434
5386
  }
5435
- /** The engine's fixed bodies as the broad phase of the world's rays, or null when the pieces are not in the engine. */
5387
+ /** The engine's fixed bodies as the broad phase of the world's rays. */
5436
5388
  rayBroadPhase() {
5437
- return this.engine?.rayBroadPhase() ?? null;
5389
+ return this.engine.rayBroadPhase();
5438
5390
  }
5439
5391
  /**
5440
5392
  * The loop mode: the system's work goes into the loop's phases. `prepare` (sync, kinematic targets, the kit's rules that came before the step) runs in the first fixed step of
@@ -5484,8 +5436,7 @@ var PieceSystem = class {
5484
5436
  dispose() {
5485
5437
  this.trail?.clear();
5486
5438
  for (const leave of this.leaveLoop.splice(0)) leave();
5487
- if (this.engine) this.engine.dispose();
5488
- else this.groundWorker?.dispose();
5439
+ this.engine.dispose();
5489
5440
  }
5490
5441
  /**
5491
5442
  * Pairs born overlapping (both made in the same update, or one made inside the other) deeper than a contact (`LIFT_DEPTH`: chunks of one wall) go apart along the ground by
@@ -5596,7 +5547,7 @@ var PieceSystem = class {
5596
5547
  return existing;
5597
5548
  }
5598
5549
  const awake = this.awake;
5599
- const least = this.engine ? MIN_PIECE_HALF : OWN_MIN_PIECE_HALF;
5550
+ const least = MIN_PIECE_HALF;
5600
5551
  let asleep = false;
5601
5552
  let inHand = false;
5602
5553
  const body = {
@@ -5637,13 +5588,13 @@ var PieceSystem = class {
5637
5588
  this.bornAt.set(body, this.time);
5638
5589
  return body;
5639
5590
  }
5640
- /** What the loop's last frame cost (ms) and how many bodies the world had awake: the pair the cost of a body is read from. Null on the kit's own solver. */
5591
+ /** What the loop's last frame cost (ms) and how many bodies the world had awake: the pair the cost of a body is read from. */
5641
5592
  stepCost() {
5642
- return this.loop ? { stepMs: this.loop.frame.stepMs, active: this.loop.world.activeCount } : null;
5593
+ return { stepMs: this.loop.frame.stepMs, active: this.loop.world.activeCount };
5643
5594
  }
5644
- /** Bodies awake in the physics world, or the pieces not asleep on the kit's own solver. */
5595
+ /** Bodies awake in the physics world. */
5645
5596
  activeBodies() {
5646
- return this.loop ? this.loop.world.activeCount : this.list.filter((b) => !b.sleeping).length;
5597
+ return this.loop.world.activeCount;
5647
5598
  }
5648
5599
  /** Move the centre of mass (after a part broke off). */
5649
5600
  setCenter(b, c) {
@@ -5663,12 +5614,12 @@ var PieceSystem = class {
5663
5614
  }
5664
5615
  if (gone) {
5665
5616
  this.forgetSolids(gone);
5666
- this.engine?.remove(gone);
5617
+ this.engine.remove(gone);
5667
5618
  }
5668
5619
  }
5669
5620
  clear() {
5670
5621
  this.trail?.clear();
5671
- this.engine?.clear();
5622
+ this.engine.clear();
5672
5623
  this.list.length = 0;
5673
5624
  this.awake.clear();
5674
5625
  this.layoutDirty = true;
@@ -5782,29 +5733,13 @@ var PieceSystem = class {
5782
5733
  }
5783
5734
  return out;
5784
5735
  }
5785
- /**
5786
- * An impulse along the unit normal `n` at `r` (from the centre of mass, world) that stops the point closing in along `n` (with `bounce`); `e` holds the body's rotation.
5787
- */
5788
- contactImpulse(b, e, r, n, bounce) {
5789
- const v = b.velocity;
5790
- const w = b.angularVelocity;
5791
- const vn = (v[0] + w[1] * r[2] - w[2] * r[1]) * n[0] + (v[1] + w[2] * r[0] - w[0] * r[2]) * n[1] + (v[2] + w[0] * r[1] - w[1] * r[0]) * n[2];
5792
- if (vn >= 0) return;
5793
- const a = this.mulInvInertiaWith(b, e, r[1] * n[2] - r[2] * n[1], r[2] * n[0] - r[0] * n[2], r[0] * n[1] - r[1] * n[0]);
5794
- const k = (a[1] * r[2] - a[2] * r[1]) * n[0] + (a[2] * r[0] - a[0] * r[2]) * n[1] + (a[0] * r[1] - a[1] * r[0]) * n[2];
5795
- const j = -(1 + bounce) * vn / (1 / b.mass + k);
5796
- for (let i = 0; i < 3; i++) {
5797
- v[i] += j * n[i] / b.mass;
5798
- w[i] += a[i] * j;
5799
- }
5800
- }
5801
5736
  /** A blast at (x, z) of `radius`: the ground within its reach is laid at the next update, before the pieces it makes need it (`EnginePieces.layAhead`). */
5802
5737
  layGroundAt(x, z, radius) {
5803
- this.engine?.layAhead(x, z, radius);
5738
+ this.engine.layAhead(x, z, radius);
5804
5739
  }
5805
5740
  /** What lies inside the disc looks at the ground again: it was dug or heaped under it. */
5806
5741
  wakeRegion(x, z, radius) {
5807
- this.engine?.groundChanged(x, z, radius);
5742
+ this.engine.groundChanged(x, z, radius);
5808
5743
  for (const b of this.list) {
5809
5744
  if (!b.sleeping || b.held) continue;
5810
5745
  if (Math.hypot(b.root.position.x - x, b.root.position.z - z) > radius && !this.lostGroundIn(b, x, z, radius)) continue;
@@ -5847,7 +5782,7 @@ var PieceSystem = class {
5847
5782
  this.trail?.blend(alpha);
5848
5783
  }
5849
5784
  update(dt) {
5850
- if (this.trail && !(this.engine && this.loop && !this.stepsItself)) {
5785
+ if (this.trail && this.stepsItself) {
5851
5786
  this.beginTick();
5852
5787
  this.advance(dt);
5853
5788
  this.trail.commit();
@@ -5856,96 +5791,21 @@ var PieceSystem = class {
5856
5791
  this.advance(dt);
5857
5792
  }
5858
5793
  advance(dt) {
5859
- if (this.engine && this.loop) {
5860
- if (!this.stepsItself) {
5861
- this.time = this.loop.frame.time;
5862
- this.keepFar();
5863
- return;
5864
- }
5865
- this.time += dt;
5866
- this.engine.wakePending(this.list, this.awake);
5867
- this.measure();
5868
- this.wakeUnsupported(true);
5869
- if (dt > 0) {
5870
- this.engine.prepare(this.list, dt, dt, this.apartAll(dt), this.awake);
5871
- this.loop.step(dt);
5872
- this.engine.finish(this.list, dt, this.time, this.awake);
5873
- }
5874
- this.measure();
5875
- this.keepBudget();
5794
+ if (!this.stepsItself) {
5795
+ this.time = this.loop.frame.time;
5876
5796
  this.keepFar();
5877
5797
  return;
5878
5798
  }
5879
- const { steps, near: near2 } = this.collisionSteps(dt);
5880
- if (steps === 1) {
5881
- this.time += dt;
5882
- this.step(dt);
5883
- return;
5884
- }
5885
- const start = this.time;
5886
- this.time = start + dt;
5887
- const far2 = (b) => !near2.has(b);
5888
- this.step(dt, { moves: far2, meets: (a, b) => far2(a) && far2(b) });
5889
- for (let i = 0; i < steps; i++) {
5890
- this.time = start + dt * (i + 1) / steps;
5891
- this.step(dt / steps, { moves: (b) => near2.has(b), meets: (a, b) => near2.has(a) || near2.has(b) });
5892
- }
5893
- }
5894
- /**
5895
- * 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
5896
- * 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).
5897
- * 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.
5898
- */
5899
- collisionSteps(dt) {
5900
- let need = 1;
5901
- const near2 = /* @__PURE__ */ new Set();
5902
- const p = this.p;
5903
- const speedOf = (b) => b.held ? 0 : Math.hypot(b.velocity[0], b.velocity[1], b.velocity[2]);
5904
- for (const b of this.list) {
5905
- if (!b.round || b.held) continue;
5906
- const radius = Math.max(MIN_ROUND_RADIUS, b.round.radius);
5907
- const speed = b.sleeping ? 0 : speedOf(b);
5908
- const c = this.centerOf(b, p);
5909
- const cx = c.x, cy = c.y, cz = c.z;
5910
- const rb = Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]);
5911
- for (const o of this.list) {
5912
- if (o === b || b.sleeping && o.sleeping) continue;
5913
- const closing = speed + (o.sleeping ? 0 : speedOf(o));
5914
- if (closing * dt < radius * 0.5) continue;
5915
- const reach = closing * dt + rb + Math.hypot(o.halfExtents[0], o.halfExtents[1], o.halfExtents[2]);
5916
- const oc = this.centerOf(o, p);
5917
- if (Math.abs(oc.x - cx) > reach || Math.abs(oc.y - cy) > reach || Math.abs(oc.z - cz) > reach) continue;
5918
- const pair = Math.ceil(closing * dt / radius);
5919
- if (pair < 2) continue;
5920
- need = Math.max(need, pair);
5921
- near2.add(b);
5922
- near2.add(o);
5923
- }
5924
- }
5925
- return { steps: Math.max(1, Math.min(MAX_COLLISION_STEPS, need)), near: near2 };
5926
- }
5927
- /** 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. */
5928
- step(dt, only) {
5929
- this.surfaceByTile.clear();
5799
+ this.time += dt;
5800
+ this.engine.wakePending(this.list, this.awake);
5930
5801
  this.measure();
5931
- this.wakeUnsupported();
5932
- for (const b of this.list) {
5933
- if (b.held) {
5934
- b.velocity = [0, 0, 0];
5935
- b.angularVelocity = [0, 0, 0];
5936
- b.sleeping = false;
5937
- b.still = 0;
5938
- b.calmAt = null;
5939
- continue;
5940
- }
5941
- if (b.sleeping || only && !only.moves(b)) continue;
5942
- this.integrate(b, dt);
5943
- if (b.sleeping) continue;
5944
- const v = b.velocity;
5945
- const w = b.angularVelocity;
5946
- 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);
5802
+ this.wakeUnsupported(true);
5803
+ if (dt > 0) {
5804
+ this.engine.prepare(this.list, dt, dt, this.apartAll(dt), this.awake);
5805
+ this.loop.step(dt);
5806
+ this.engine.finish(this.list, dt, this.time, this.awake);
5947
5807
  }
5948
- this.collide(dt, only?.meets);
5808
+ this.measure();
5949
5809
  this.keepBudget();
5950
5810
  this.keepFar();
5951
5811
  }
@@ -6096,29 +5956,6 @@ var PieceSystem = class {
6096
5956
  }
6097
5957
  return top;
6098
5958
  }
6099
- /** 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. */
6100
- wakeAbove(b) {
6101
- const below = this.solidOf.get(b);
6102
- if (!below) return;
6103
- 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]);
6104
- const x0 = Math.floor((below.c[0] - below.r) / SURFACE_CELL);
6105
- const x1 = Math.floor((below.c[0] + below.r) / SURFACE_CELL);
6106
- const z0 = Math.floor((below.c[2] - below.r) / SURFACE_CELL);
6107
- const z1 = Math.floor((below.c[2] + below.r) / SURFACE_CELL);
6108
- const wide = x1 - x0 > SURFACE_CELLS_ACROSS || z1 - z0 > SURFACE_CELLS_ACROSS;
6109
- const look = (part) => {
6110
- const o = part.body;
6111
- const s = this.solidOf.get(o);
6112
- if (!s || !o.sleeping || o.held || o === b) return;
6113
- 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;
6114
- if (boxTopAt(below, s.c[0], s.c[2]) === -Infinity && !this.restsOn(o, b, true)) return;
6115
- o.sleeping = false;
6116
- o.still = 0;
6117
- o.calmAt = null;
6118
- };
6119
- if (wide) for (const s of this.solids) look(s);
6120
- 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);
6121
- }
6122
5959
  /**
6123
5960
  * 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
6124
5961
  * 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.
@@ -6135,36 +5972,6 @@ var PieceSystem = class {
6135
5972
  }
6136
5973
  return false;
6137
5974
  }
6138
- /**
6139
- * The terrain's own surface under a round piece's contact at (x, z) that stands at `y` on the ground `raw` (`groundAt` there), when it is the terrain and not a top above it
6140
- * (`terrainAt`, which `groundAt` equals to the bit where nothing is on it); null otherwise (the default surface). Read as the engine reads it (`tileRollingAt`), once per tile
6141
- * per update, and one object per value, so a substep makes none.
6142
- */
6143
- terrainSurface(x, y, z, raw) {
6144
- const rollingAt = this.o.terrainRollingAt;
6145
- if (!rollingAt || y !== raw || this.o.terrainAt && y !== this.o.terrainAt(x, z)) return null;
6146
- const key = groundTileKey(x, z);
6147
- let surface = this.surfaceByTile.get(key);
6148
- if (surface === void 0) {
6149
- surface = this.tileSurface(rollingAt, x, z);
6150
- this.surfaceByTile.set(key, surface);
6151
- }
6152
- return surface;
6153
- }
6154
- /** The terrain's surface of the tile at (x, z) (`tileRollingAt`), or null when it says nothing (the default surface). */
6155
- tileSurface(rollingAt, x, z) {
6156
- const dampingAt = this.o.terrainRollingDampingAt;
6157
- const rolling = tileRollingAt(rollingAt, x, z);
6158
- const rollingDamping = dampingAt ? tileRollingAt(dampingAt, x, z) : void 0;
6159
- if (rolling === void 0 && rollingDamping === void 0) return null;
6160
- const id = `${rolling ?? ROLLING_RESISTANCE2}|${rollingDamping ?? GROUND_ROLLING_DAMPING}`;
6161
- let surface = this.terrainSurfaces.get(id);
6162
- if (!surface) {
6163
- surface = { static: MU_STATIC2, dynamic: MU_DYNAMIC2, rolling: rolling ?? ROLLING_RESISTANCE2, rollingDamping: rollingDamping ?? GROUND_ROLLING_DAMPING };
6164
- this.terrainSurfaces.set(id, surface);
6165
- }
6166
- return surface;
6167
- }
6168
5975
  /** Where a corner of `self` at `(x, y, z)` lands: the ground, the top of another piece or of something fixed under it. */
6169
5976
  supportAt(self, x, y, z, step) {
6170
5977
  let top = this.o.groundAt(x, z);
@@ -6186,391 +5993,6 @@ var PieceSystem = class {
6186
5993
  this.supportBody = on?.body ?? null;
6187
5994
  return Math.max(top, fixed);
6188
5995
  }
6189
- /** Stops a piece that ran into something on its way from `start` to `pos` (`sweep`): moves it back to where it met it and takes the speed into it. */
6190
- sweepOn(b, start, pos) {
6191
- const travel = Math.hypot(pos[0] - start[0], pos[1] - start[1], pos[2] - start[2]);
6192
- if (travel < 1e-3) return;
6193
- const hit = this.o.sweep?.(b, start, pos);
6194
- if (!hit || hit.normal[1] > SLAM_FLOOR_NORMAL2) return;
6195
- const v = b.velocity;
6196
- const n = hit.normal;
6197
- const into = -(v[0] * n[0] + v[1] * n[1] + v[2] * n[2]);
6198
- if (into <= 0) return;
6199
- const k = Math.min(1, hit.distance / travel);
6200
- const bounce = pairRestitution(b.restitution, hit.restitution);
6201
- for (let i = 0; i < 3; i++) {
6202
- pos[i] = start[i] + (pos[i] - start[i]) * k;
6203
- v[i] += (1 + bounce) * into * n[i];
6204
- }
6205
- const w = b.angularVelocity;
6206
- w[0] *= 0.5;
6207
- w[1] *= 0.5;
6208
- w[2] *= 0.5;
6209
- if (into >= SLAM_MIN_SPEED2 && this.time - b.lastHit > 0.15) {
6210
- b.lastHit = this.time;
6211
- this.o.onSlam?.(b, hit, into);
6212
- }
6213
- }
6214
- /**
6215
- * The impulse that stops the hinge point of a piece: the point's velocity v + w x r goes to zero through J = -K^-1 (v + w x r), with K = 1/m - [r]x I^-1 [r]x, so what
6216
- * gravity gave the centre turns into a swing about the hinge.
6217
- */
6218
- holdOnHinge(b, hinge, q) {
6219
- const e = this.hingeM.makeRotationFromQuaternion(q).elements;
6220
- const [lx, ly, lz] = hinge.local;
6221
- const r = [e[0] * lx + e[4] * ly + e[8] * lz, e[1] * lx + e[5] * ly + e[9] * lz, e[2] * lx + e[6] * ly + e[10] * lz];
6222
- const v = b.velocity;
6223
- const w = b.angularVelocity;
6224
- const point = [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]];
6225
- const k = [0, 1, 2].map((i) => {
6226
- const d = [i === 0 ? 1 : 0, i === 1 ? 1 : 0, i === 2 ? 1 : 0];
6227
- const t = this.mulInvInertiaWith(b, 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]);
6228
- return [d[0] / b.mass + t[1] * r[2] - t[2] * r[1], d[1] / b.mass + t[2] * r[0] - t[0] * r[2], d[2] / b.mass + t[0] * r[1] - t[1] * r[0]];
6229
- });
6230
- const j = solve3(k, [-point[0], -point[1], -point[2]]);
6231
- if (!j) return;
6232
- for (let i = 0; i < 3; i++) v[i] += j[i] / b.mass;
6233
- const dw = this.mulInvInertiaWith(b, e, r[1] * j[2] - r[2] * j[1], r[2] * j[0] - r[0] * j[2], r[0] * j[1] - r[1] * j[0]);
6234
- for (let i = 0; i < 3; i++) w[i] += dw[i];
6235
- }
6236
- /** Puts the hinge point back where it hangs (what the step's rounding moved it by), moving the centre. */
6237
- keepOnHinge(hinge, pos, q) {
6238
- const e = this.hingeM.makeRotationFromQuaternion(q).elements;
6239
- const [lx, ly, lz] = hinge.local;
6240
- pos[0] = hinge.point[0] - (e[0] * lx + e[4] * ly + e[8] * lz);
6241
- pos[1] = hinge.point[1] - (e[1] * lx + e[5] * ly + e[9] * lz);
6242
- pos[2] = hinge.point[2] - (e[2] * lx + e[6] * ly + e[10] * lz);
6243
- }
6244
- /** The screw tears out: past `HINGE_RELEASE` of turn, or after `HINGE_SECONDS`, the piece falls free. */
6245
- wearHinge(b, hinge, q, dt) {
6246
- hinge.seconds -= dt;
6247
- const [x, y, z, w] = hinge.from;
6248
- const turned = 2 * Math.acos(Math.min(1, Math.abs(q.x * x + q.y * y + q.z * z + q.w * w)));
6249
- if (turned >= HINGE_RELEASE || hinge.seconds <= 0) b.hinge = void 0;
6250
- }
6251
- integrate(b, dt) {
6252
- const c = this.centerOf(b, this.p);
6253
- const pos = [c.x, c.y, c.z];
6254
- const start = [c.x, c.y, c.z];
6255
- const q = this.q.copy(b.root.quaternion);
6256
- const v = b.velocity;
6257
- const w = b.angularVelocity;
6258
- const invM = 1 / b.mass;
6259
- const steps = clamp(Math.ceil(dt / SUB_DT), 1, 8);
6260
- const h = dt / steps;
6261
- let landing = 0;
6262
- let landPoint = [pos[0], pos[1], pos[2]];
6263
- let contactedLast = false;
6264
- const propped = b.propped;
6265
- const proppedAt = b.proppedAt;
6266
- b.propped = void 0;
6267
- b.proppedAt = void 0;
6268
- let touched = false;
6269
- let unstable = false;
6270
- const corners = this.samples(b, false);
6271
- this.m.makeRotationFromQuaternion(q);
6272
- let gLow = Infinity;
6273
- let gHigh = -Infinity;
6274
- for (const [sx, sy, sz] of corners) {
6275
- const e0 = this.m.elements;
6276
- const y = pos[1] + e0[1] * sx + e0[5] * sy + e0[9] * sz;
6277
- const x = pos[0] + e0[0] * sx + e0[4] * sy + e0[8] * sz;
6278
- const z = pos[2] + e0[2] * sx + e0[6] * sy + e0[10] * sz;
6279
- const g = Math.max(this.o.groundAt(x, z), this.o.staticTopAt?.(x, z, y, STEP) ?? -Infinity);
6280
- gLow = Math.min(gLow, g);
6281
- gHigh = Math.max(gHigh, g);
6282
- }
6283
- let local = b.round ? this.roundSamples(b, q) : gHigh - gLow > STEP ? this.samples(b, true) : corners;
6284
- let prev = local.map(() => [0, 0, 0]);
6285
- for (let s = 0; s < steps; s++) {
6286
- if (b.round) {
6287
- const wl0 = Math.hypot(w[0], w[1], w[2]);
6288
- const next = this.nextQ.copy(q);
6289
- if (wl0 > 1e-6) next.premultiply(this.dq.setFromAxisAngle(this.ax.set(w[0] / wl0, w[1] / wl0, w[2] / wl0), wl0 * h)).normalize();
6290
- local = this.roundSamples(b, next);
6291
- if (prev.length !== local.length) prev = local.map(() => [0, 0, 0]);
6292
- }
6293
- this.m.makeRotationFromQuaternion(q);
6294
- const e0 = this.m.elements;
6295
- for (let i = 0; i < local.length; i++) {
6296
- const [sx, sy, sz] = local[i];
6297
- prev[i][0] = pos[0] + e0[0] * sx + e0[4] * sy + e0[8] * sz;
6298
- prev[i][1] = pos[1] + e0[1] * sx + e0[5] * sy + e0[9] * sz;
6299
- prev[i][2] = pos[2] + e0[2] * sx + e0[6] * sy + e0[10] * sz;
6300
- }
6301
- v[1] -= this.o.gravity * h;
6302
- if (b.hinge) this.holdOnHinge(b, b.hinge, q);
6303
- if (propped) {
6304
- const into = v[0] * propped[0] + v[1] * propped[1] + v[2] * propped[2];
6305
- if (into < 0) for (let k = 0; k < 3; k++) v[k] -= into * propped[k];
6306
- }
6307
- pos[0] += v[0] * h;
6308
- pos[1] += v[1] * h;
6309
- pos[2] += v[2] * h;
6310
- const wl = Math.hypot(w[0], w[1], w[2]);
6311
- if (wl > 1e-6) {
6312
- this.dq.setFromAxisAngle(this.ax.set(w[0] / wl, w[1] / wl, w[2] / wl), wl * h);
6313
- q.premultiply(this.dq).normalize();
6314
- }
6315
- if (b.hinge) this.keepOnHinge(b.hinge, pos, q);
6316
- const damp = b.round ? 1 : Math.exp(-0.15 * h);
6317
- w[0] *= damp;
6318
- w[1] *= damp;
6319
- w[2] *= damp;
6320
- this.m.makeRotationFromQuaternion(q);
6321
- const e = this.m.elements;
6322
- const rs = [];
6323
- const ids = [];
6324
- const ns = [];
6325
- const mus = [];
6326
- let maxPen = 0;
6327
- let penNormal = UP;
6328
- const rise = STEP + Math.max(0, -v[1]) * h * 2;
6329
- let side = null;
6330
- let lowest = Infinity;
6331
- for (let i = 0; i < local.length; i++) {
6332
- const [sx, sy, sz] = local[i];
6333
- const r = [e[0] * sx + e[4] * sy + e[8] * sz, e[1] * sx + e[5] * sy + e[9] * sz, e[2] * sx + e[6] * sy + e[10] * sz];
6334
- const px = pos[0] + r[0];
6335
- const pz = pos[2] + r[2];
6336
- const cy = pos[1] + r[1];
6337
- const was = prev[i];
6338
- const raw = this.o.groundAt(px, pz);
6339
- if (raw > cy - SLOP && was[1] < raw - STEP) {
6340
- if (this.o.groundAt(was[0], was[2]) >= raw - STEP) {
6341
- const floor = b.floor;
6342
- if (floor !== void 0 && floor - cy > -SLOP && floor < raw - STEP) {
6343
- rs.push(r);
6344
- ids.push(i);
6345
- ns.push(UP);
6346
- mus.push(pairFriction(b.friction, b.round ? this.terrainSurface(px, floor, pz, floor) : null));
6347
- maxPen = Math.max(maxPen, floor - cy);
6348
- }
6349
- continue;
6350
- }
6351
- const dx = px - was[0];
6352
- const dz = pz - was[2];
6353
- const len = Math.hypot(dx, dz);
6354
- if (len > 1e-6) {
6355
- let lo = 0;
6356
- let hi = 1;
6357
- for (let it = 0; it < 6; it++) {
6358
- const mid = (lo + hi) / 2;
6359
- if (this.o.groundAt(was[0] + dx * mid, was[2] + dz * mid) >= raw - STEP) hi = mid;
6360
- else lo = mid;
6361
- }
6362
- const back = (1 - lo) * len + 1e-3;
6363
- if (!side || back > side.back) side = { back, dir: [dx / len, dz / len], r };
6364
- }
6365
- continue;
6366
- }
6367
- this.supportNormal = UP;
6368
- this.supportFriction = null;
6369
- const gy = i < 8 || b.contacts ? this.supportAt(b, px, cy, pz, rise) : Math.max(raw, this.o.staticTopAt?.(px, pz, cy, rise) ?? -Infinity);
6370
- const pen = gy - cy;
6371
- if (pen > -SLOP) {
6372
- rs.push(r);
6373
- ids.push(i);
6374
- ns.push(this.supportNormal);
6375
- mus.push(pairFriction(b.friction, this.supportFriction ?? (b.round ? this.terrainSurface(px, gy, pz, raw) : null)));
6376
- if (pen > maxPen) {
6377
- maxPen = pen;
6378
- penNormal = this.supportNormal;
6379
- }
6380
- lowest = Math.min(lowest, gy);
6381
- }
6382
- }
6383
- if (lowest < Infinity) b.floor = lowest;
6384
- if (b.round && ns.length > 0) {
6385
- const sum = [0, 0, 0];
6386
- for (const one of ns) for (let k = 0; k < 3; k++) sum[k] += one[k];
6387
- const len = Math.hypot(sum[0], sum[1], sum[2]);
6388
- b.surfaceNormal = len > 1e-6 ? [sum[0] / len, sum[1] / len, sum[2] / len] : UP;
6389
- } else if (b.round && rs.length === 0) b.surfaceNormal = UP;
6390
- if (side) {
6391
- pos[0] -= side.dir[0] * side.back;
6392
- pos[2] -= side.dir[1] * side.back;
6393
- this.contactImpulse(b, e, side.r, [-side.dir[0], 0, -side.dir[1]], SIDE_BOUNCE);
6394
- }
6395
- contactedLast = rs.length > 0;
6396
- if (contactedLast) touched = true;
6397
- if (!contactedLast) continue;
6398
- const out = Math.max(0, maxPen - SLOP * 0.5) * penNormal[1];
6399
- pos[0] += penNormal[0] * out;
6400
- pos[1] += penNormal[1] * out;
6401
- pos[2] += penNormal[2] * out;
6402
- let impact2 = 0;
6403
- for (const r of rs) impact2 = Math.max(impact2, -(v[1] + w[2] * r[0] - w[0] * r[2]));
6404
- const restitution = impact2 > 2 ? 0.25 : 0;
6405
- if (impact2 > landing) {
6406
- landing = impact2;
6407
- let sx = 0;
6408
- let sz = 0;
6409
- for (const r of rs) {
6410
- sx += r[0];
6411
- sz += r[2];
6412
- }
6413
- const lx = pos[0] + sx / rs.length;
6414
- const lz = pos[2] + sz / rs.length;
6415
- landPoint = [lx, pos[1] - Math.max(...rs.map((r) => -r[1])), lz];
6416
- }
6417
- 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]];
6418
- const dot2 = (a, c2) => a[0] * c2[0] + a[1] * c2[1] + a[2] * c2[2];
6419
- const goal = rs.map((r, k) => {
6420
- const vn = dot2(pointV(r), ns[k]);
6421
- return vn < 0 ? -restitution * vn : 0;
6422
- });
6423
- const slipping = rs.map((r, k) => {
6424
- const pv = pointV(r);
6425
- pv[1] += this.o.gravity * h;
6426
- const vn = dot2(pv, ns[k]);
6427
- return Math.hypot(pv[0] - vn * ns[k][0], pv[1] - vn * ns[k][1], pv[2] - vn * ns[k][2]) > SLIP_SPEED;
6428
- });
6429
- const vx0 = v[0];
6430
- const vz0 = v[2];
6431
- if (!b.warm || b.warm.length !== WARM_SLOTS * 4) b.warm = new Float64Array(WARM_SLOTS * 4);
6432
- const warm = b.warm;
6433
- const accN = ids.map((id) => id < WARM_SLOTS ? warm[id * 4] : 0);
6434
- const accT = ids.map((id) => id < WARM_SLOTS ? [warm[id * 4 + 1], warm[id * 4 + 2], warm[id * 4 + 3]] : [0, 0, 0]);
6435
- const push = (r, j) => {
6436
- v[0] += j[0] * invM;
6437
- v[1] += j[1] * invM;
6438
- v[2] += j[2] * invM;
6439
- const dw = this.mulInvInertiaWith(b, e, r[1] * j[2] - r[2] * j[1], r[2] * j[0] - r[0] * j[2], r[0] * j[1] - r[1] * j[0]);
6440
- w[0] += dw[0];
6441
- w[1] += dw[1];
6442
- w[2] += dw[2];
6443
- };
6444
- const reach = (r, d) => {
6445
- const a = this.mulInvInertiaWith(b, 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]);
6446
- return invM + (a[1] * r[2] - a[2] * r[1]) * d[0] + (a[2] * r[0] - a[0] * r[2]) * d[1] + (a[0] * r[1] - a[1] * r[0]) * d[2];
6447
- };
6448
- for (let k = 0; k < rs.length; k++) {
6449
- const n = ns[k];
6450
- const t = accT[k];
6451
- if (accN[k] !== 0 || t[0] !== 0 || t[1] !== 0 || t[2] !== 0) push(rs[k], [n[0] * accN[k] + t[0], n[1] * accN[k] + t[1], n[2] * accN[k] + t[2]]);
6452
- }
6453
- for (let it = 0; it < SOLVER_PASSES; it++) {
6454
- for (let k = 0; k < rs.length; k++) {
6455
- const r = rs[k];
6456
- const n = ns[k];
6457
- const vn = dot2(pointV(r), n);
6458
- const total = Math.max(0, accN[k] + (goal[k] - vn) / reach(r, n));
6459
- const j = total - accN[k];
6460
- accN[k] = total;
6461
- if (j !== 0) push(r, [n[0] * j, n[1] * j, n[2] * j]);
6462
- const pv = pointV(r);
6463
- const pn = dot2(pv, n);
6464
- const vt = [pv[0] - pn * n[0], pv[1] - pn * n[1], pv[2] - pn * n[2]];
6465
- const speed = Math.hypot(vt[0], vt[1], vt[2]);
6466
- const f = [accT[k][0], accT[k][1], accT[k][2]];
6467
- if (speed > 1e-6) {
6468
- const d = [vt[0] / speed, vt[1] / speed, vt[2] / speed];
6469
- const jt = speed / reach(r, d);
6470
- f[0] -= d[0] * jt;
6471
- f[1] -= d[1] * jt;
6472
- f[2] -= d[2] * jt;
6473
- } else if (f[0] === 0 && f[1] === 0 && f[2] === 0) continue;
6474
- const most = (slipping[k] ? mus[k].dynamic : mus[k].static) * accN[k];
6475
- const fl = Math.hypot(f[0], f[1], f[2]);
6476
- if (fl > most) {
6477
- f[0] *= most / fl;
6478
- f[1] *= most / fl;
6479
- f[2] *= most / fl;
6480
- }
6481
- const dj = [f[0] - accT[k][0], f[1] - accT[k][1], f[2] - accT[k][2]];
6482
- accT[k] = f;
6483
- if (dj[0] !== 0 || dj[1] !== 0 || dj[2] !== 0) push(r, dj);
6484
- }
6485
- }
6486
- pos[0] += (v[0] - vx0) * h;
6487
- pos[2] += (v[2] - vz0) * h;
6488
- warm.fill(0);
6489
- for (let k = 0; k < rs.length; k++) {
6490
- const id = ids[k];
6491
- if (id >= WARM_SLOTS) continue;
6492
- warm[id * 4] = accN[k];
6493
- warm[id * 4 + 1] = accT[k][0];
6494
- warm[id * 4 + 2] = accT[k][1];
6495
- warm[id * 4 + 3] = accT[k][2];
6496
- }
6497
- let x0 = Infinity, x1 = -Infinity, z0 = Infinity, z1 = -Infinity, low = 0;
6498
- for (const r of rs) {
6499
- x0 = Math.min(x0, r[0]);
6500
- x1 = Math.max(x1, r[0]);
6501
- z0 = Math.min(z0, r[2]);
6502
- z1 = Math.max(z1, r[2]);
6503
- low = Math.min(low, r[1]);
6504
- }
6505
- if (proppedAt) {
6506
- x0 = Math.min(x0, proppedAt[0]);
6507
- x1 = Math.max(x1, proppedAt[0]);
6508
- z0 = Math.min(z0, proppedAt[2]);
6509
- z1 = Math.max(z1, proppedAt[2]);
6510
- }
6511
- const height = -low;
6512
- const wx = x1 - x0;
6513
- const wz = z1 - z0;
6514
- const pastX = x0 > -SUPPORT_MARGIN * wx || x1 < SUPPORT_MARGIN * wx;
6515
- const pastZ = z0 > -SUPPORT_MARGIN * wz || z1 < SUPPORT_MARGIN * wz;
6516
- unstable = !b.round && (Math.min(wx, wz) < NARROW2 * height || pastX || pastZ);
6517
- if (b.tipping) {
6518
- b.tipping.seconds -= h;
6519
- const [hx, hy, hz] = b.halfExtents;
6520
- const upHalf = Math.abs(e[1]) * hx + Math.abs(e[5]) * hy + Math.abs(e[9]) * hz;
6521
- const rising = TIP_SECONDS - b.tipping.seconds > TIP_RISE_AFTER && v[1] > TIP_RISING;
6522
- if (upHalf < TIP_LYING || b.tipping.seconds <= 0 || rising) b.tipping = null;
6523
- else {
6524
- w[0] += b.tipping.x * TIP_ACCELERATION * h;
6525
- w[2] += b.tipping.z * TIP_ACCELERATION * h;
6526
- unstable = true;
6527
- }
6528
- }
6529
- if (unstable) {
6530
- const dirX = x0 + x1 > 0 ? -1 : 1;
6531
- const dirZ = z0 + z1 > 0 ? -1 : 1;
6532
- const k = TOPPLE2 * Math.min(height, 1) * h;
6533
- const overZ = pastX === pastZ ? wx <= wz : pastX;
6534
- if (overZ) w[2] -= dirX * k;
6535
- else w[0] += dirZ * k;
6536
- }
6537
- if (b.round) {
6538
- let rolling = 0;
6539
- let damping = 0;
6540
- for (const mu of mus) {
6541
- rolling = Math.max(rolling, mu.rolling ?? ROLLING_RESISTANCE2);
6542
- damping = Math.max(damping, mu.rollingDamping ?? 0);
6543
- }
6544
- this.rollingResistance(b, q, w, h, { rolling, damping });
6545
- } else {
6546
- const rd = Math.exp((rs.length >= 3 ? -3 : -1.5) * h);
6547
- w[0] *= rd;
6548
- w[1] *= rd;
6549
- w[2] *= rd;
6550
- const fd = Math.exp(-0.3 * h);
6551
- v[0] *= fd;
6552
- v[2] *= fd;
6553
- }
6554
- }
6555
- if (b.hinge) this.wearHinge(b, b.hinge, q, dt);
6556
- if (this.o.sweep && !b.held) this.sweepOn(b, start, pos);
6557
- b.root.quaternion.copy(q);
6558
- const off2 = this.ax.copy(b.centerV).applyQuaternion(q);
6559
- b.root.position.set(pos[0] - off2.x, pos[1] - off2.y, pos[2] - off2.z);
6560
- b.root.updateMatrixWorld(true);
6561
- b.onGround = contactedLast;
6562
- if (landing > HARD_LANDING2 && this.time - b.lastHit > 0.15) {
6563
- b.lastHit = this.time;
6564
- this.o.onHardLanding(b, landPoint, landing);
6565
- }
6566
- const still = touched && !unstable && Math.hypot(v[0], v[1], v[2]) < STILL_SPEED2 && Math.hypot(w[0], w[1], w[2]) < STILL_SPIN2;
6567
- if (still) b.still += dt;
6568
- else b.still = 0;
6569
- b.balancing = unstable;
6570
- this.keepsTilt(b, q, dt);
6571
- const held = (touched || propped !== void 0) && !b.tipping;
6572
- if (!b.hinge && (b.still >= SLEEP_AFTER || this.staysInPlace(b, held, pos, q, dt)) && !this.onSteepSupport(b)) this.sleep(b);
6573
- }
6574
5996
  /**
6575
5997
  * Whether `b` lies flat on a settled top (asleep) tilted past what the friction of the two holds (a laptop on a table top gone over at 42 degrees, held by 0.49 up to 26),
6576
5998
  * with every corner that touches on it: it slides, it does not sleep there, and one asleep there wakes. Held by anything else (the floor, a flat top, a side it leans on, a
@@ -6604,15 +6026,6 @@ var PieceSystem = class {
6604
6026
  b.velocity[0] = b.velocity[1] = b.velocity[2] = 0;
6605
6027
  b.angularVelocity[0] = b.angularVelocity[1] = b.angularVelocity[2] = 0;
6606
6028
  }
6607
- /** How long the piece has kept its tilt (within `STEADY_TURN`): one going over leaves it within a moment, one held up at an angle keeps it. */
6608
- keepsTilt(b, q, dt) {
6609
- const at = b.steady;
6610
- if (at && turnBetween(at.quaternion, q) <= STEADY_TURN2) {
6611
- at.seconds += dt;
6612
- return;
6613
- }
6614
- b.steady = { quaternion: [q.x, q.y, q.z, q.w], seconds: 0 };
6615
- }
6616
6029
  /**
6617
6030
  * Over `maxAwake` pieces awake, the oldest that have come to rest sleep where they lie: on the ground, slower than `STILL_SPEED` and `STILL_SPIN`, not falling over (`tip`),
6618
6031
  * and one held up at an angle (a board leaning on a seat) only once it kept its tilt for `STEADY_SECONDS`; that one sleeps then under the budget too, since nothing else puts
@@ -6723,118 +6136,6 @@ var PieceSystem = class {
6723
6136
  const y = c.y + e[1] * sx + e[5] * sy + e[9] * sz;
6724
6137
  return this.supportAt(b, c.x + e[0] * sx + e[4] * sy + e[8] * sz, y, c.z + e[2] * sx + e[6] * sy + e[10] * sz, STEP) >= y - SUPPORT_GAP2;
6725
6138
  }
6726
- /** Whether the piece has kept to the same spot on the ground for `CALM_SLEEP_AFTER` (see `CALM_DRIFT`). */
6727
- staysInPlace(b, touched, center, q, dt) {
6728
- const at = b.calmAt;
6729
- const turned = at ? turnBetween(at.quaternion, q) : Infinity;
6730
- if (!touched || !at || Math.hypot(center[0] - at.center[0], center[1] - at.center[1], center[2] - at.center[2]) > CALM_DRIFT || turned > CALM_TURN) {
6731
- b.calmAt = touched ? { center: [center[0], center[1], center[2]], quaternion: [q.x, q.y, q.z, q.w] } : null;
6732
- b.calm = 0;
6733
- return false;
6734
- }
6735
- b.calm += dt;
6736
- return b.calm >= CALM_SLEEP_AFTER;
6737
- }
6738
- /** Whether the centre `c` of `upper` is over the top of `lower`'s box (as it was at the start of the update). */
6739
- onTopOf(upper, lower, c) {
6740
- const s = this.solidOf.get(lower);
6741
- if (!s) return false;
6742
- const top = boxTopAt(s, c.x, c.z);
6743
- return top > -Infinity && c.y >= top - 0.01;
6744
- }
6745
- /**
6746
- * 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
6747
- * 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.
6748
- */
6749
- roundSamples(b, q) {
6750
- const round = b.round;
6751
- const rims = b.contacts ?? [];
6752
- if (!round) return rims;
6753
- const e = this.m.makeRotationFromQuaternion(q).elements;
6754
- const n = b.surfaceNormal ?? UP;
6755
- 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])];
6756
- if (round.kind === "sphere") return [...rims, [down[0] * round.radius, down[1] * round.radius, down[2] * round.radius]];
6757
- const k = round.axis;
6758
- const across = [down[0], down[1], down[2]];
6759
- across[k] = 0;
6760
- const len = Math.hypot(across[0], across[1], across[2]);
6761
- if (len < 1e-3) return rims;
6762
- const lowest = (at, radius) => {
6763
- const p = [across[0] / len * radius, across[1] / len * radius, across[2] / len * radius];
6764
- p[k] = at;
6765
- return p;
6766
- };
6767
- const half = b.halfExtents[k];
6768
- const low = (round.profile ?? [[-half, round.radius], [half, round.radius]]).map(([at, radius]) => lowest(at, radius));
6769
- return len > ROUND_LYING ? low : [...rims, ...low];
6770
- }
6771
- /**
6772
- * 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
6773
- * 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
6774
- * about the rest dies down; lying, it keeps turning about the line it touches by (a tapered bottle rounds its narrow end). Its rolling damping (`PieceFriction.rollingDamping`,
6775
- * the pair's) then takes exp(-d h) of what is left, as the grass or the sand drag more the faster it rolls.
6776
- */
6777
- rollingResistance(b, q, w, h, { rolling, damping }) {
6778
- const round = b.round;
6779
- if (!round) return;
6780
- const e = this.m.makeRotationFromQuaternion(q).elements;
6781
- const cylinder = round.kind === "cylinder";
6782
- const a = cylinder ? [e[round.axis * 4], e[round.axis * 4 + 1], e[round.axis * 4 + 2]] : [0, 0, 0];
6783
- const along2 = cylinder ? w[0] * a[0] + w[1] * a[1] + w[2] * a[2] : 0;
6784
- const roll = cylinder ? [a[0] * along2, a[1] * along2, a[2] * along2] : [w[0], w[1], w[2]];
6785
- const rest = [w[0] - roll[0], w[1] - roll[1], w[2] - roll[2]];
6786
- const spin = Math.hypot(roll[0], roll[1], roll[2]);
6787
- const upright = b.surfaceNormal?.[1] ?? 1;
6788
- const slow = spin > 0 ? Math.max(0, spin - rolling * this.o.gravity * upright * h / (round.inertia * round.radius)) / spin * Math.exp(-damping * h) : 0;
6789
- const lying = cylinder && Math.abs(a[1]) < ROUND_LYING;
6790
- const damp = lying ? 1 : Math.exp(-3 * h);
6791
- for (let i = 0; i < 3; i++) w[i] = roll[i] * slow + rest[i] * damp;
6792
- }
6793
- /** Half the height of the box of a piece as it is turned now. */
6794
- halfHeight(b) {
6795
- const e = this.m.makeRotationFromQuaternion(b.root.quaternion).elements;
6796
- return Math.abs(e[1]) * b.halfExtents[0] + Math.abs(e[5]) * b.halfExtents[1] + Math.abs(e[9]) * b.halfExtents[2];
6797
- }
6798
- /**
6799
- * 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
6800
- * 1225); a pair with a round piece in it meets by its shapes (`shapeContact`) and exchanges an impulse, two boxes are put apart by their separating axes (`separatePair`).
6801
- */
6802
- collide(dt, meets) {
6803
- const n = this.list.length;
6804
- if (n < 2) return;
6805
- const centers = this.list.map((b) => {
6806
- const c = this.centerOf(b, this.p);
6807
- return [c.x, c.y, c.z];
6808
- });
6809
- const radii = this.list.map((b) => Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]));
6810
- const order = this.list.map((_, i) => i).sort((i, j) => centers[i][0] - radii[i] - (centers[j][0] - radii[j]));
6811
- for (let oi = 0; oi < n; oi++) {
6812
- const i = order[oi];
6813
- const reach = centers[i][0] + radii[i];
6814
- for (let oj = oi + 1; oj < n; oj++) {
6815
- const j = order[oj];
6816
- if (centers[j][0] - radii[j] > reach) break;
6817
- const a = this.list[i];
6818
- const b = this.list[j];
6819
- if ((a.held || a.sleeping) && (b.held || b.sleeping)) continue;
6820
- if (meets && !meets(a, b)) continue;
6821
- const r = radii[i] + radii[j];
6822
- const ca = centers[i];
6823
- const cb = centers[j];
6824
- if (Math.abs(ca[1] - cb[1]) > r || Math.abs(ca[2] - cb[2]) > r) continue;
6825
- const stacked = !a.round && !b.round && Math.abs(ca[1] - cb[1]) >= (this.halfHeight(a) + this.halfHeight(b)) * 0.9;
6826
- const onTop = this.onTopOf(b, a, new this.THREE.Vector3(...cb)) || this.onTopOf(a, b, new this.THREE.Vector3(...ca));
6827
- if (a.round || b.round) {
6828
- if (!stacked && !onTop) this.knock(a, b, ca, cb);
6829
- continue;
6830
- }
6831
- if (!this.partsOverlap(a, b)) continue;
6832
- this.boxImpact(a, b, ca, cb, dt);
6833
- this.separatePair(a, b, ca, cb, dt, stacked || onTop || this.restsOn(a, b, false) || this.restsOn(b, a, false));
6834
- }
6835
- }
6836
- this.applyPushes(dt);
6837
- }
6838
6139
  /**
6839
6140
  * Moves each piece by what its pairs put it apart this step (`pushes`), at most `SEPARATE_SPEED` along the ground and as much up: a chunk in five others is not pushed five
6840
6141
  * times as fast, and nothing is ever moved down (into what holds it). A piece still being put apart is moving, though its speed is nil: it does not fall asleep in the other.
@@ -6869,129 +6170,6 @@ var PieceSystem = class {
6869
6170
  last.push[2] = push[2];
6870
6171
  return last.share;
6871
6172
  }
6872
- /** 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. */
6873
- contactShape(b, c) {
6874
- const e = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
6875
- const round = b.round;
6876
- if (round) {
6877
- if (round.kind === "sphere") return { kind: "capsule", from: c, to: c, radius: round.radius };
6878
- const reach = Math.max(0, b.halfExtents[round.axis] - round.radius);
6879
- const k = round.axis * 4;
6880
- const d = [e[k] * reach, e[k + 1] * reach, e[k + 2] * reach];
6881
- 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 };
6882
- }
6883
- const parts = b.partBoxes ?? [{ center: [0, 0, 0], half: b.halfExtents }];
6884
- return {
6885
- kind: "boxes",
6886
- boxes: parts.map(squareBoxOf).map(({ center, half }) => ({
6887
- 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]],
6888
- e,
6889
- half
6890
- }))
6891
- };
6892
- }
6893
- /**
6894
- * 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
6895
- * 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
6896
- * 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.
6897
- */
6898
- knock(a, b, ca, cb) {
6899
- const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
6900
- if (!hit) return;
6901
- if (Math.abs(hit.normal[1]) > SLAM_FLOOR_NORMAL2) return;
6902
- this.impact({ a, b, ca, cb, hit, apart: true, above: 0 });
6903
- }
6904
- /**
6905
- * Two boxes that hit each other (a book falling on the top edge of one standing, a board swinging into a row): they exchange the impulse of `knock` at the point they meet,
6906
- * so the one hit turns about that point as well as moving, and a sleeping one wakes. Only a hit faster than `BOX_IMPACT_SPEED`, and no deeper than a contact: pieces resting on each other, or
6907
- * made inside each other, are left to `separatePair`, which holds or slides them without a kick.
6908
- */
6909
- boxImpact(a, b, ca, cb, dt) {
6910
- const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
6911
- if (!hit) return;
6912
- const n = hit.normal;
6913
- const va = a.velocity;
6914
- const vb = b.velocity;
6915
- const closing = (va[0] - vb[0]) * n[0] + (va[1] - vb[1]) * n[1] + (va[2] - vb[2]) * n[2];
6916
- if (hit.depth > LIFT_DEPTH + 2 * Math.max(0, -closing) * dt) return;
6917
- this.impact({ a, b, ca, cb, hit, apart: false, above: BOX_IMPACT_SPEED });
6918
- }
6919
- /** The impulse two pieces that met exchange (`knock`), put apart first when `apart`, and only when they close faster than `above` (m/s). */
6920
- impact({ a, b, ca, cb, hit, apart: apart2, above }) {
6921
- const n = hit.normal;
6922
- const ra = [hit.point[0] - ca[0], hit.point[1] - ca[1], hit.point[2] - ca[2]];
6923
- const rb = [hit.point[0] - cb[0], hit.point[1] - cb[1], hit.point[2] - cb[2]];
6924
- const pointV = (body, r) => {
6925
- const v2 = body.velocity;
6926
- const w = body.angularVelocity;
6927
- 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]];
6928
- };
6929
- const relative = () => {
6930
- const va = pointV(a, ra);
6931
- const vb = pointV(b, rb);
6932
- return [va[0] - vb[0], va[1] - vb[1], va[2] - vb[2]];
6933
- };
6934
- const vn0 = relative();
6935
- const closing = vn0[0] * n[0] + vn0[1] * n[1] + vn0[2] * n[2];
6936
- if (closing >= -above) return;
6937
- const wake = (body) => {
6938
- body.sleeping = false;
6939
- body.still = 0;
6940
- body.calmAt = null;
6941
- };
6942
- if (closing < -WAKE_ABOVE_SPEED) {
6943
- if (a.sleeping) wake(a);
6944
- if (b.sleeping) wake(b);
6945
- }
6946
- const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
6947
- const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
6948
- if (ima + imb === 0) return;
6949
- const ea = [...this.m.makeRotationFromQuaternion(a.root.quaternion).elements];
6950
- const eb = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
6951
- const flat = Math.hypot(n[0], n[2]);
6952
- const out = flat > KNOCK_FLAT ? [n[0] / flat, 0, n[2] / flat] : n;
6953
- const depth = Math.max(0, hit.depth - SLOP) / (flat > KNOCK_FLAT ? flat : 1);
6954
- for (const [body, share, sign] of [[a, ima, 1], [b, imb, -1]]) {
6955
- if (share === 0 || !apart2) continue;
6956
- const move = depth * share * sign / (ima + imb);
6957
- body.root.position.x += out[0] * move;
6958
- body.root.position.y += out[1] * move;
6959
- body.root.position.z += out[2] * move;
6960
- }
6961
- if (closing >= 0) return;
6962
- const turning = (body, e, r, d) => {
6963
- 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]);
6964
- 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];
6965
- };
6966
- const resist = (d) => ima + imb + (ima > 0 ? turning(a, ea, ra, d) : 0) + (imb > 0 ? turning(b, eb, rb, d) : 0);
6967
- const give = (j2) => {
6968
- if (ima > 0) {
6969
- for (let i = 0; i < 3; i++) a.velocity[i] += j2[i] * ima;
6970
- 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]);
6971
- for (let i = 0; i < 3; i++) a.angularVelocity[i] += dw[i];
6972
- }
6973
- if (imb > 0) {
6974
- for (let i = 0; i < 3; i++) b.velocity[i] -= j2[i] * imb;
6975
- 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]);
6976
- for (let i = 0; i < 3; i++) b.angularVelocity[i] -= dw[i];
6977
- }
6978
- };
6979
- const bounce = apart2 && -closing > BOUNCE_MIN_SPEED ? pairRestitution(a.restitution, b.restitution) : 0;
6980
- const j = -(1 + bounce) * closing / resist(n);
6981
- give([n[0] * j, n[1] * j, n[2] * j]);
6982
- const v = relative();
6983
- const vn = v[0] * n[0] + v[1] * n[1] + v[2] * n[2];
6984
- const vt = [v[0] - vn * n[0], v[1] - vn * n[1], v[2] - vn * n[2]];
6985
- const slide = Math.hypot(vt[0], vt[1], vt[2]);
6986
- if (slide < 1e-6) return;
6987
- const t = [vt[0] / slide, vt[1] / slide, vt[2] / slide];
6988
- const jt = Math.min(slide / resist(t), pairFriction(a.friction, b.friction ?? null).dynamic * j);
6989
- give([-t[0] * jt, -t[1] * jt, -t[2] * jt]);
6990
- if (-closing >= SLAM_MIN_SPEED2 && this.time - Math.max(a.lastHit, b.lastHit) > 0.15) {
6991
- a.lastHit = b.lastHit = this.time;
6992
- this.o.onKnock?.(a, b, hit.point, -closing);
6993
- }
6994
- }
6995
6173
  /** Whether a part of `a` and a part of `b` (their boxes of the step, as world-aligned boxes around them) overlap. */
6996
6174
  partsOverlap(a, b) {
6997
6175
  for (const pa of this.surfacesOf.get(a) ?? []) {
@@ -7003,66 +6181,6 @@ var PieceSystem = class {
7003
6181
  }
7004
6182
  return false;
7005
6183
  }
7006
- /**
7007
- * Two boxes overlapping, put apart by their separating axes (`shapeContact`); a held or sleeping piece is not moved. The move (`SEPARATE_SHARE` of the overlap less `SLOP`) is
7008
- * summed into `pushes` and bounded per piece in `applyPushes`.
7009
- * - Side by side (a normal at least `KNOCK_FLAT` level): apart along the ground only, as in `knock`, split by their inverse masses; the speed they close in with stops.
7010
- * - One over the other as deep as a contact (`LIFT_DEPTH` and what the closing speed sank them this step): left alone when `resting` (stacked, on a top, leaning on a
7011
- * part: its support holds it); else only the upper rises, held up by the lower (`propped`), and its point in the lower stops sinking (`contactImpulse`): never a kick up.
7012
- * - One over the other deeper than that: they were made inside each other (chunks of one wall), or stand on one floor side by side. Lifting the upper out would give it
7013
- * m g d nothing supplied (a 3 m chunk climbing 5 m through a pile), so they slide apart along the ground by their least level overlaps (`boxesApartAlongGround`).
7014
- */
7015
- separatePair(a, b, ca, cb, dt, resting) {
7016
- const sa = this.contactShape(a, ca);
7017
- const sb = this.contactShape(b, cb);
7018
- const hit = shapeContact(sa, sb);
7019
- if (!hit) return;
7020
- const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
7021
- const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
7022
- if (ima + imb === 0) return;
7023
- if (Math.hypot(hit.normal[0], hit.normal[2]) > KNOCK_FLAT) {
7024
- this.slideApart(a, b, ima, imb, levelBoxesApart(sa, sb) ?? hit);
7025
- return;
7026
- }
7027
- const n = hit.normal;
7028
- const va = a.velocity;
7029
- const vb = b.velocity;
7030
- const contact = LIFT_DEPTH + Math.max(0, -((va[0] - vb[0]) * n[0] + (va[1] - vb[1]) * n[1] + (va[2] - vb[2]) * n[2])) * dt;
7031
- if (hit.depth > contact) {
7032
- const level = boxesApartAlongGround(sa, sb);
7033
- if (level) this.slideApart(a, b, ima, imb, level);
7034
- return;
7035
- }
7036
- if (resting) return;
7037
- const [upper, c, up] = n[1] > 0 ? [a, ca, n] : [b, cb, [-n[0], -n[1], -n[2]]];
7038
- if (upper.held || upper.sleeping) return;
7039
- const otherCentre = upper === a ? cb : ca;
7040
- if (c[1] < otherCentre[1] - SLOP) {
7041
- const level = boxesApartAlongGround(sa, sb);
7042
- if (level) this.slideApart(a, b, ima, imb, level);
7043
- return;
7044
- }
7045
- upper.propped = up;
7046
- const e = this.m.makeRotationFromQuaternion(upper.root.quaternion).elements;
7047
- const r = [hit.point[0] - c[0], hit.point[1] - c[1], hit.point[2] - c[2]];
7048
- upper.proppedAt = r;
7049
- const turn2 = this.mulInvInertiaWith(upper, e, r[1] * up[2] - r[2] * up[1], r[2] * up[0] - r[0] * up[2], r[0] * up[1] - r[1] * up[0]);
7050
- const angular = (turn2[1] * r[2] - turn2[2] * r[1]) * up[0] + (turn2[2] * r[0] - turn2[0] * r[2]) * up[1] + (turn2[0] * r[1] - turn2[1] * r[0]) * up[2];
7051
- const lift = SEPARATE_SHARE * Math.max(0, hit.depth - SLOP) / (1 + angular * upper.mass);
7052
- const push = this.pushes.get(upper) ?? [0, 0, 0];
7053
- for (let i = 0; i < 3; i++) push[i] = Math.abs(up[i] * lift) > Math.abs(push[i]) ? up[i] * lift : push[i];
7054
- this.pushes.set(upper, push);
7055
- this.turnAboutCentre(upper, c, [turn2[0] * lift * upper.mass, turn2[1] * lift * upper.mass, turn2[2] * lift * upper.mass]);
7056
- this.contactImpulse(upper, e, r, up, 0);
7057
- }
7058
- /** Turns a piece by the rotation vector `by` (rad) about its centre `c`, which stays where it is. */
7059
- turnAboutCentre(b, c, by) {
7060
- const angle = Math.hypot(by[0], by[1], by[2]);
7061
- if (angle < 1e-9) return;
7062
- b.root.quaternion.premultiply(this.dq.setFromAxisAngle(this.ax.set(by[0] / angle, by[1] / angle, by[2] / angle), angle)).normalize();
7063
- const off2 = this.ax.copy(b.centerV).applyQuaternion(b.root.quaternion);
7064
- b.root.position.set(c[0] - off2.x, c[1] - off2.y, c[2] - off2.z);
7065
- }
7066
6184
  /** Two boxes put apart along the ground by `hit` (its normal from `b` to `a`, laid level), split by their inverse masses; the speed they close in with along it stops. */
7067
6185
  slideApart(a, b, ima, imb, hit) {
7068
6186
  const n = hit.normal;
@@ -7084,12 +6202,6 @@ var PieceSystem = class {
7084
6202
  }
7085
6203
  }
7086
6204
  };
7087
- function solve3(k, rhs) {
7088
- const det = (a, b, c) => a[0] * (b[1] * c[2] - b[2] * c[1]) - b[0] * (a[1] * c[2] - a[2] * c[1]) + c[0] * (a[1] * b[2] - a[2] * b[1]);
7089
- const d = det(k[0], k[1], k[2]);
7090
- if (Math.abs(d) < 1e-12) return null;
7091
- return [det(rhs, k[1], k[2]) / d, det(k[0], rhs, k[2]) / d, det(k[0], k[1], rhs) / d];
7092
- }
7093
6205
 
7094
6206
  // src/rebar.ts
7095
6207
  var RebarCages = class {
@@ -7454,7 +6566,6 @@ var CRATER_DEPTH = 0.3;
7454
6566
  var CRATER_RADIUS = 2.5;
7455
6567
  var ORIGIN = [4e3, 0, 4e3];
7456
6568
  var warmed = /* @__PURE__ */ new WeakSet();
7457
- var OWN_SOLVER = {};
7458
6569
  var brickWall = (THREE) => {
7459
6570
  const parts = [];
7460
6571
  const geometries = [];
@@ -7484,7 +6595,7 @@ var brickWall = (THREE) => {
7484
6595
  merged.setIndex(index);
7485
6596
  return { parts, joints: inferJoints2(parts), mesh: new THREE.Mesh(merged, new THREE.MeshStandardMaterial()), ranges };
7486
6597
  };
7487
- var runScene = (THREE, makeRenderer, options) => {
6598
+ var runScene = (THREE, makeRenderer, engine, worldDesc) => {
7488
6599
  const scene2 = new THREE.Scene();
7489
6600
  const world = createDestruction();
7490
6601
  const vfx = createVfx({});
@@ -7494,12 +6605,12 @@ var runScene = (THREE, makeRenderer, options) => {
7494
6605
  let renderer;
7495
6606
  let bound;
7496
6607
  try {
7497
- engineWorld = options.engine?.createWorld(options.world ?? KIT_WORLD);
7498
- const loop = engineWorld ? createPhysicsLoop2(engineWorld) : void 0;
6608
+ engineWorld = engine.createWorld(worldDesc ?? KIT_WORLD);
6609
+ const loop = createPhysicsLoop2(engineWorld);
7499
6610
  const terrainGridAt = (xs, zs, out) => {
7500
6611
  for (let j = 0; j < zs.length; j++) for (let i = 0; i < xs.length; i++) out[j * xs.length + i] = terrainAt(xs[i], zs[j]);
7501
6612
  };
7502
- renderer = makeRenderer({ physics: loop ?? "own", scene: scene2, world, vfx, eventBudgetMs: Infinity, groundAt: terrainAt, terrainAt, terrainGridAt, jitWarmUp: false });
6613
+ renderer = makeRenderer({ physics: loop, scene: scene2, world, vfx, eventBudgetMs: Infinity, groundAt: terrainAt, terrainAt, terrainGridAt, jitWarmUp: false });
7503
6614
  const wall = brickWall(THREE);
7504
6615
  const root = new THREE.Group();
7505
6616
  root.position.set(...ORIGIN);
@@ -7526,7 +6637,7 @@ var blastAndRun = ({ world, loop, renderer, onCrater }) => {
7526
6637
  renderer.wakeRegion(point[0], point[2], CRATER_RADIUS);
7527
6638
  for (let frame = 0; frame < FRAMES; frame++) {
7528
6639
  world.update(FRAME_SECONDS);
7529
- loop?.step(FRAME_SECONDS);
6640
+ loop.step(FRAME_SECONDS);
7530
6641
  renderer.update(FRAME_SECONDS);
7531
6642
  const stats = renderer.stats();
7532
6643
  seen.pieces = Math.max(seen.pieces, stats.pieces);
@@ -7535,10 +6646,10 @@ var blastAndRun = ({ world, loop, renderer, onCrater }) => {
7535
6646
  return seen;
7536
6647
  };
7537
6648
  var warmUpDestructionJit = (THREE, makeRenderer, options = {}) => {
7538
- const key = options.engine ?? OWN_SOLVER;
7539
- if (warmed.has(key)) return void 0;
7540
- warmed.add(key);
7541
- return runScene(THREE, makeRenderer, options);
6649
+ const { engine } = options;
6650
+ if (!engine || warmed.has(engine)) return void 0;
6651
+ warmed.add(engine);
6652
+ return runScene(THREE, makeRenderer, engine, options.world);
7542
6653
  };
7543
6654
 
7544
6655
  // src/visual.ts
@@ -9531,7 +8642,7 @@ var Renderer = class {
9531
8642
  __publicField(this, "blastDebris");
9532
8643
  __publicField(this, "blastWakes");
9533
8644
  __publicField(this, "costMeter");
9534
- /** The step cost the debris policy reads this frame (`debrisCost`), null when there is none (the kit's own solver, or `fixed`). */
8645
+ /** The step cost the debris policy reads this frame (`debrisCost`), null when there is none (`fixed`). */
9535
8646
  __publicField(this, "cost", null);
9536
8647
  __publicField(this, "costFrame", 0);
9537
8648
  /** The pieces faded away (`faded`): their root shrinks, then leaves the scene. */
@@ -9542,6 +8653,9 @@ var Renderer = class {
9542
8653
  __publicField(this, "staticBoxes", /* @__PURE__ */ new Map());
9543
8654
  /** `staticSolids` per object, kept until `staticNow` measures the object again for a reason. */
9544
8655
  __publicField(this, "solidCache", /* @__PURE__ */ new Map());
8656
+ /** The fixed bodies a host module gives that are no bound object (`setHostSolids`), and the list last made of each: the same numbers keep the same list. */
8657
+ __publicField(this, "hostSolids", /* @__PURE__ */ new Map());
8658
+ __publicField(this, "hostSolidLists", /* @__PURE__ */ new Map());
9545
8659
  /** The objects whose last `staticSolids` list is not all of what a ray can hit: measured in part, or with a loose or hidden part left out. */
9546
8660
  __publicField(this, "unsureSolids", /* @__PURE__ */ new Set());
9547
8661
  /** The objects `staticNow` measured again for a reason (an event, a moved root, boxes that changed): their `staticSolids` are measured again too. */
@@ -9945,8 +9059,7 @@ var Renderer = class {
9945
9059
  }
9946
9060
  if (total.isEmpty()) return { half: [0.25, 0.25, 0.25], center: new this.THREE.Vector3() };
9947
9061
  const size = total.getSize(new this.THREE.Vector3());
9948
- const least = minPieceHalf(this.o.physics);
9949
- 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()) };
9062
+ 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()) };
9950
9063
  }
9951
9064
  /**
9952
9065
  * 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
@@ -10640,8 +9753,28 @@ var Renderer = class {
10640
9753
  } finally {
10641
9754
  this.measuredMeshes.clear();
10642
9755
  }
9756
+ for (const [id, source] of this.hostSolids) out.set(id, this.hostSolidsOf(id, source()));
10643
9757
  return out;
10644
9758
  }
9759
+ setHostSolids(id, source) {
9760
+ if (source) this.hostSolids.set(id, source);
9761
+ else {
9762
+ this.hostSolids.delete(id);
9763
+ this.hostSolidLists.delete(id);
9764
+ }
9765
+ }
9766
+ /** The module's boxes as static solids: measured again to the same numbers, the list is the one held (an engine finds it unchanged by identity). */
9767
+ hostSolidsOf(id, given) {
9768
+ const kept = this.hostSolidLists.get(id);
9769
+ if (kept && kept.given.length === given.length && given.every((s, i) => s === kept.given[i])) return kept.solids;
9770
+ const next = given.map((s, i) => ({ center: [s.center[0], s.center[1], s.center[2]], half: [s.half[0], s.half[1], s.half[2]], rotation: s.rotation ? [s.rotation[0], s.rotation[1], s.rotation[2], s.rotation[3]] : [0, 0, 0, 1], key: s.key ?? String(i) }));
9771
+ if (kept && sameSolids(kept.solids, next)) {
9772
+ this.hostSolidLists.set(id, { given, solids: kept.solids });
9773
+ return kept.solids;
9774
+ }
9775
+ this.hostSolidLists.set(id, { given, solids: next });
9776
+ return next;
9777
+ }
10645
9778
  /** `staticSolids` for each fixed object of `now`, into `out`. */
10646
9779
  measureEach(now, budget, out) {
10647
9780
  for (const id of now.keys()) {