@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.
@@ -14427,7 +14427,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
14427
14427
  var ROLLING_RESISTANCE = 0.015;
14428
14428
  var MU_DYNAMIC = 0.6;
14429
14429
  var GROUND = { frictionStatic: MU_STATIC, frictionDynamic: MU_DYNAMIC, restitution: 0, rolling: ROLLING_RESISTANCE };
14430
- var groundTileKey = (x, z) => (Math.floor(x / TILE) + TILE_KEY_ORIGIN) * TILE_KEY + (Math.floor(z / TILE) + TILE_KEY_ORIGIN);
14431
14430
  var tileRollingAt = (rollingAt, x, z) => rollingAt((Math.floor(x / TILE) + 0.5) * TILE, (Math.floor(z / TILE) + 0.5) * TILE);
14432
14431
  var STILL_SPEED = 0.12;
14433
14432
  var STILL_SPIN = 0.45;
@@ -15846,9 +15845,8 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
15846
15845
  }
15847
15846
  };
15848
15847
  var resolvePhysics = (option) => {
15849
- if (option === "own") return null;
15850
15848
  if (option === void 0 || typeof option !== "object") {
15851
- throw new PhysicsOptionError("the `physics` option is required: pass a PhysicsLoop, a world from the host, or 'own' for the kit's own solver");
15849
+ throw new PhysicsOptionError("the `physics` option is required: pass a PhysicsLoop or a world from the host");
15852
15850
  }
15853
15851
  if (isPhysicsLoop(option)) return { loop: option, stepsItself: false };
15854
15852
  if (typeof option.stepFixed !== "function") {
@@ -15898,44 +15896,20 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
15898
15896
  }
15899
15897
 
15900
15898
  // src/pieces.ts
15901
- var SLAM_MIN_SPEED2 = 2;
15902
- var SLAM_BOUNCE2 = 0.2;
15903
15899
  var BALANCE_TOLERANCE = 0.01;
15904
- var SLAM_FLOOR_NORMAL2 = 0.7;
15905
- var SUB_DT = 1 / 120;
15906
15900
  var MU_STATIC2 = 0.7;
15907
15901
  var MU_DYNAMIC2 = 0.6;
15908
- var SLIP_SPEED = 0.02;
15909
15902
  var ROLLING_RESISTANCE2 = 0.015;
15910
- var ROUND_LYING = Math.SQRT1_2;
15911
15903
  var MIN_PIECE_HALF = 1e-3;
15912
- var OWN_MIN_PIECE_HALF = 0.01;
15913
- var OWN_MIN_MEASURED_HALF = 0.02;
15914
- var minPieceHalf = (physics) => physics === "own" ? OWN_MIN_MEASURED_HALF : MIN_PIECE_HALF;
15915
15904
  var SLEEP_AFTER = 0.5;
15916
15905
  var STILL_SPEED2 = 0.12;
15917
15906
  var STILL_SPIN2 = 0.45;
15918
- var CALM_DRIFT = 0.01;
15919
- var CALM_TURN = 0.05;
15920
- var CALM_SLEEP_AFTER = 1.5;
15921
- var STEADY_TURN2 = 0.1;
15922
15907
  var STEADY_SECONDS2 = 1.5;
15923
- var HARD_LANDING2 = 3;
15924
15908
  var SLOP = 2e-3;
15925
- var NARROW2 = 0.3;
15926
- var SUPPORT_MARGIN = 0.05;
15927
- var TOPPLE2 = 3;
15928
- var TIP_ACCELERATION = 30;
15929
- var TIP_RISING = 0.02;
15930
- var TIP_RISE_AFTER = 0.5;
15931
- var TIP_LYING = 0.25;
15932
15909
  var TIP_SECONDS = 3;
15933
15910
  var STEP = 0.03;
15934
15911
  var WAKE_SAMPLES = 4;
15935
15912
  var WAKE_DROP = 0.05;
15936
- var WARM_SLOTS = 8 + 12 * 7;
15937
- var SOLVER_PASSES = 10;
15938
- var SIDE_BOUNCE = 0.1;
15939
15913
  var EDGE_POINTS = [-0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75];
15940
15914
  var EDGE_SPACING = 0.05;
15941
15915
  var MAX_EDGE_POINTS = 31;
@@ -15952,15 +15926,9 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
15952
15926
  var SLOPE_MIN_NORMAL = 0.5;
15953
15927
  var LIES_FLAT = 0.985;
15954
15928
  var KEEP_EVERY = 0.5;
15955
- var WAKE_ABOVE_SPEED = 0.05;
15956
- var WAKE_ABOVE_SPIN = 0.1;
15957
15929
  var SURFACE_CELL = 0.5;
15958
15930
  var SURFACE_CELLS_ACROSS = 24;
15959
15931
  var cellKey = (ix, iz) => (ix + 32768) * 65536 + (iz + 32768);
15960
- var MAX_COLLISION_STEPS = 8;
15961
- var MIN_ROUND_RADIUS = 5e-3;
15962
- var BOUNCE_MIN_SPEED = 0.1;
15963
- var BOX_IMPACT_SPEED = 1.5;
15964
15932
  var KNOCK_FLAT = 0.5;
15965
15933
  var SEPARATE_SPEED = 1.5;
15966
15934
  var LIFT_DEPTH = STEP;
@@ -16009,8 +15977,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16009
15977
  // both lose energy in the contact: a log on grass is held by its bark and by the blades
16010
15978
  rollingDamping: (a?.rollingDamping ?? PIECE_ROLLING_DAMPING) + (b?.rollingDamping ?? GROUND_ROLLING_DAMPING)
16011
15979
  });
16012
- var pairRestitution = (a, b) => Math.sqrt((a ?? SLAM_BOUNCE2) * (b ?? SLAM_BOUNCE2));
16013
- 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)));
16014
15980
  var PieceSystem = class {
16015
15981
  constructor(THREE, o) {
16016
15982
  __publicField(this, "THREE", THREE);
@@ -16019,19 +15985,14 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16019
15985
  __publicField(this, "time", 0);
16020
15986
  __publicField(this, "generations", 0);
16021
15987
  __publicField(this, "p");
16022
- __publicField(this, "q");
16023
15988
  __publicField(this, "dq");
16024
- __publicField(this, "nextQ");
16025
15989
  __publicField(this, "m");
16026
- __publicField(this, "hingeM");
16027
15990
  __publicField(this, "ax");
16028
15991
  __publicField(this, "wakeBox");
16029
15992
  __publicField(this, "solids", []);
16030
- /** The engine stepping the pieces (`PieceSystemOptions.physics`), or null for the kit's own solver. */
15993
+ /** The engine stepping the pieces (`PieceSystemOptions.physics`). */
16031
15994
  __publicField(this, "engine");
16032
- /** 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). */
16033
- __publicField(this, "groundWorker");
16034
- /** The loop the system steps itself (a world was handed), the loop it only registers on (a loop was handed), or null on `'own'`. */
15995
+ /** The loop the system steps itself (a world was handed) or only registers on (a loop was handed). */
16035
15996
  __publicField(this, "loop");
16036
15997
  __publicField(this, "stepsItself");
16037
15998
  /** The phases' way out of the loop, and whether this frame's `prepare` ran (it runs once, in the first fixed step). */
@@ -16081,12 +16042,8 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16081
16042
  __publicField(this, "supportNormal", UP16);
16082
16043
  __publicField(this, "supportFriction", null);
16083
16044
  __publicField(this, "supportBody", null);
16084
- /** The terrain's surfaces by rolling resistance and damping (`terrainSurface`), one object each. */
16085
- __publicField(this, "terrainSurfaces", /* @__PURE__ */ new Map());
16086
- /** The terrain's surface by ground tile (`groundTileKey`) this update, null for the default: read once per tile, not per contact per substep. */
16087
- __publicField(this, "surfaceByTile", /* @__PURE__ */ new Map());
16088
- /** 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. */
16089
- __publicField(this, "isSettled", (body) => this.engine ? this.engine.isSettled(body) : body.sleeping && !body.held);
16045
+ /** A piece is asleep on both sides (the engine's too): nothing of it changes until it wakes. */
16046
+ __publicField(this, "isSettled", (body) => this.engine.isSettled(body));
16090
16047
  __publicField(this, "measureAwake", (body) => {
16091
16048
  const rec = this.measured(body);
16092
16049
  const from = rec.from;
@@ -16110,18 +16067,15 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16110
16067
  if (rec.from !== from || rec.list.length !== count || rec.list[0] !== first) this.reshaped = true;
16111
16068
  });
16112
16069
  this.p = new THREE.Vector3();
16113
- this.q = new THREE.Quaternion();
16114
16070
  this.dq = new THREE.Quaternion();
16115
- this.nextQ = new THREE.Quaternion();
16116
16071
  this.m = new THREE.Matrix4();
16117
- this.hingeM = new THREE.Matrix4();
16118
16072
  this.ax = new THREE.Vector3();
16119
16073
  this.wakeBox = new THREE.Box3();
16120
16074
  const resolved = resolvePhysics(o.physics);
16121
- this.loop = resolved?.loop ?? null;
16122
- this.stepsItself = resolved?.stepsItself ?? false;
16075
+ this.loop = resolved.loop;
16076
+ this.stepsItself = resolved.stepsItself;
16123
16077
  this.trail = o.interpolate ? new PoseTrail() : null;
16124
- this.engine = resolved ? new EnginePieces(THREE, resolved.loop.world, {
16078
+ this.engine = new EnginePieces(THREE, resolved.loop.world, {
16125
16079
  groundAt: o.terrainAt ?? o.groundAt,
16126
16080
  ...o.terrainRollingAt ? { groundRollingAt: o.terrainRollingAt } : {},
16127
16081
  ...o.terrainRollingDampingAt ? { groundRollingDampingAt: o.terrainRollingDampingAt } : {},
@@ -16141,13 +16095,12 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16141
16095
  ...o.heldIgnores ? { heldIgnores: o.heldIgnores } : {},
16142
16096
  ...o.groundWorker ? { groundWorker: o.groundWorker } : {},
16143
16097
  ...o.groundWorkerFrames !== void 0 ? { groundWorkerFrames: o.groundWorkerFrames } : {}
16144
- }) : null;
16145
- this.groundWorker = o.groundWorker ?? null;
16146
- if (resolved && !resolved.stepsItself) this.joinLoop(resolved.loop, this.engine);
16098
+ });
16099
+ if (!resolved.stepsItself) this.joinLoop(resolved.loop, this.engine);
16147
16100
  }
16148
- /** 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. */
16101
+ /** Contact blows on the fixed parts become `blunt` hits of `destruction` (`engine-impacts.ts`). Returns the way to stop it. */
16149
16102
  contactHits(destruction, options) {
16150
- return this.engine && this.loop ? createEngineImpacts(this.loop, this.engine.statics, destruction, options) : null;
16103
+ return createEngineImpacts(this.loop, this.engine.statics, destruction, options);
16151
16104
  }
16152
16105
  /**
16153
16106
  * 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
@@ -16155,7 +16108,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16155
16108
  */
16156
16109
  piecesInBlast(blastQuery, point, radius) {
16157
16110
  const engine = this.engine;
16158
- if (!engine) return [...this.list];
16159
16111
  const found = blastQuery.run({ origin: point, radius });
16160
16112
  const reached = /* @__PURE__ */ new Set();
16161
16113
  for (let i = 0; i < found.bodyCount; i++) {
@@ -16164,9 +16116,9 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16164
16116
  }
16165
16117
  return this.list.filter((piece) => reached.has(piece) || !engine.hasBody(piece));
16166
16118
  }
16167
- /** The engine's fixed bodies as the broad phase of the world's rays, or null when the pieces are not in the engine. */
16119
+ /** The engine's fixed bodies as the broad phase of the world's rays. */
16168
16120
  rayBroadPhase() {
16169
- return this.engine?.rayBroadPhase() ?? null;
16121
+ return this.engine.rayBroadPhase();
16170
16122
  }
16171
16123
  /**
16172
16124
  * 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
@@ -16216,8 +16168,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16216
16168
  dispose() {
16217
16169
  this.trail?.clear();
16218
16170
  for (const leave of this.leaveLoop.splice(0)) leave();
16219
- if (this.engine) this.engine.dispose();
16220
- else this.groundWorker?.dispose();
16171
+ this.engine.dispose();
16221
16172
  }
16222
16173
  /**
16223
16174
  * 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
@@ -16328,7 +16279,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16328
16279
  return existing;
16329
16280
  }
16330
16281
  const awake = this.awake;
16331
- const least = this.engine ? MIN_PIECE_HALF : OWN_MIN_PIECE_HALF;
16282
+ const least = MIN_PIECE_HALF;
16332
16283
  let asleep = false;
16333
16284
  let inHand = false;
16334
16285
  const body = {
@@ -16369,13 +16320,13 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16369
16320
  this.bornAt.set(body, this.time);
16370
16321
  return body;
16371
16322
  }
16372
- /** 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. */
16323
+ /** 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. */
16373
16324
  stepCost() {
16374
- return this.loop ? { stepMs: this.loop.frame.stepMs, active: this.loop.world.activeCount } : null;
16325
+ return { stepMs: this.loop.frame.stepMs, active: this.loop.world.activeCount };
16375
16326
  }
16376
- /** Bodies awake in the physics world, or the pieces not asleep on the kit's own solver. */
16327
+ /** Bodies awake in the physics world. */
16377
16328
  activeBodies() {
16378
- return this.loop ? this.loop.world.activeCount : this.list.filter((b) => !b.sleeping).length;
16329
+ return this.loop.world.activeCount;
16379
16330
  }
16380
16331
  /** Move the centre of mass (after a part broke off). */
16381
16332
  setCenter(b, c) {
@@ -16395,12 +16346,12 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16395
16346
  }
16396
16347
  if (gone) {
16397
16348
  this.forgetSolids(gone);
16398
- this.engine?.remove(gone);
16349
+ this.engine.remove(gone);
16399
16350
  }
16400
16351
  }
16401
16352
  clear() {
16402
16353
  this.trail?.clear();
16403
- this.engine?.clear();
16354
+ this.engine.clear();
16404
16355
  this.list.length = 0;
16405
16356
  this.awake.clear();
16406
16357
  this.layoutDirty = true;
@@ -16514,29 +16465,13 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16514
16465
  }
16515
16466
  return out;
16516
16467
  }
16517
- /**
16518
- * 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.
16519
- */
16520
- contactImpulse(b, e, r, n, bounce2) {
16521
- const v = b.velocity;
16522
- const w = b.angularVelocity;
16523
- 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];
16524
- if (vn >= 0) return;
16525
- 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]);
16526
- 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];
16527
- const j = -(1 + bounce2) * vn / (1 / b.mass + k);
16528
- for (let i = 0; i < 3; i++) {
16529
- v[i] += j * n[i] / b.mass;
16530
- w[i] += a[i] * j;
16531
- }
16532
- }
16533
16468
  /** 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`). */
16534
16469
  layGroundAt(x, z, radius) {
16535
- this.engine?.layAhead(x, z, radius);
16470
+ this.engine.layAhead(x, z, radius);
16536
16471
  }
16537
16472
  /** What lies inside the disc looks at the ground again: it was dug or heaped under it. */
16538
16473
  wakeRegion(x, z, radius) {
16539
- this.engine?.groundChanged(x, z, radius);
16474
+ this.engine.groundChanged(x, z, radius);
16540
16475
  for (const b of this.list) {
16541
16476
  if (!b.sleeping || b.held) continue;
16542
16477
  if (Math.hypot(b.root.position.x - x, b.root.position.z - z) > radius && !this.lostGroundIn(b, x, z, radius)) continue;
@@ -16579,7 +16514,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16579
16514
  this.trail?.blend(alpha);
16580
16515
  }
16581
16516
  update(dt) {
16582
- if (this.trail && !(this.engine && this.loop && !this.stepsItself)) {
16517
+ if (this.trail && this.stepsItself) {
16583
16518
  this.beginTick();
16584
16519
  this.advance(dt);
16585
16520
  this.trail.commit();
@@ -16588,96 +16523,21 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16588
16523
  this.advance(dt);
16589
16524
  }
16590
16525
  advance(dt) {
16591
- if (this.engine && this.loop) {
16592
- if (!this.stepsItself) {
16593
- this.time = this.loop.frame.time;
16594
- this.keepFar();
16595
- return;
16596
- }
16597
- this.time += dt;
16598
- this.engine.wakePending(this.list, this.awake);
16599
- this.measure();
16600
- this.wakeUnsupported(true);
16601
- if (dt > 0) {
16602
- this.engine.prepare(this.list, dt, dt, this.apartAll(dt), this.awake);
16603
- this.loop.step(dt);
16604
- this.engine.finish(this.list, dt, this.time, this.awake);
16605
- }
16606
- this.measure();
16607
- this.keepBudget();
16526
+ if (!this.stepsItself) {
16527
+ this.time = this.loop.frame.time;
16608
16528
  this.keepFar();
16609
16529
  return;
16610
16530
  }
16611
- const { steps, near: near2 } = this.collisionSteps(dt);
16612
- if (steps === 1) {
16613
- this.time += dt;
16614
- this.step(dt);
16615
- return;
16616
- }
16617
- const start = this.time;
16618
- this.time = start + dt;
16619
- const far2 = (b) => !near2.has(b);
16620
- this.step(dt, { moves: far2, meets: (a, b) => far2(a) && far2(b) });
16621
- for (let i = 0; i < steps; i++) {
16622
- this.time = start + dt * (i + 1) / steps;
16623
- this.step(dt / steps, { moves: (b) => near2.has(b), meets: (a, b) => near2.has(a) || near2.has(b) });
16624
- }
16625
- }
16626
- /**
16627
- * 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
16628
- * 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).
16629
- * 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.
16630
- */
16631
- collisionSteps(dt) {
16632
- let need = 1;
16633
- const near2 = /* @__PURE__ */ new Set();
16634
- const p = this.p;
16635
- const speedOf = (b) => b.held ? 0 : Math.hypot(b.velocity[0], b.velocity[1], b.velocity[2]);
16636
- for (const b of this.list) {
16637
- if (!b.round || b.held) continue;
16638
- const radius = Math.max(MIN_ROUND_RADIUS, b.round.radius);
16639
- const speed = b.sleeping ? 0 : speedOf(b);
16640
- const c = this.centerOf(b, p);
16641
- const cx = c.x, cy = c.y, cz = c.z;
16642
- const rb = Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]);
16643
- for (const o of this.list) {
16644
- if (o === b || b.sleeping && o.sleeping) continue;
16645
- const closing = speed + (o.sleeping ? 0 : speedOf(o));
16646
- if (closing * dt < radius * 0.5) continue;
16647
- const reach2 = closing * dt + rb + Math.hypot(o.halfExtents[0], o.halfExtents[1], o.halfExtents[2]);
16648
- const oc = this.centerOf(o, p);
16649
- if (Math.abs(oc.x - cx) > reach2 || Math.abs(oc.y - cy) > reach2 || Math.abs(oc.z - cz) > reach2) continue;
16650
- const pair = Math.ceil(closing * dt / radius);
16651
- if (pair < 2) continue;
16652
- need = Math.max(need, pair);
16653
- near2.add(b);
16654
- near2.add(o);
16655
- }
16656
- }
16657
- return { steps: Math.max(1, Math.min(MAX_COLLISION_STEPS, need)), near: near2 };
16658
- }
16659
- /** 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. */
16660
- step(dt, only) {
16661
- this.surfaceByTile.clear();
16531
+ this.time += dt;
16532
+ this.engine.wakePending(this.list, this.awake);
16662
16533
  this.measure();
16663
- this.wakeUnsupported();
16664
- for (const b of this.list) {
16665
- if (b.held) {
16666
- b.velocity = [0, 0, 0];
16667
- b.angularVelocity = [0, 0, 0];
16668
- b.sleeping = false;
16669
- b.still = 0;
16670
- b.calmAt = null;
16671
- continue;
16672
- }
16673
- if (b.sleeping || only && !only.moves(b)) continue;
16674
- this.integrate(b, dt);
16675
- if (b.sleeping) continue;
16676
- const v = b.velocity;
16677
- const w = b.angularVelocity;
16678
- 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);
16534
+ this.wakeUnsupported(true);
16535
+ if (dt > 0) {
16536
+ this.engine.prepare(this.list, dt, dt, this.apartAll(dt), this.awake);
16537
+ this.loop.step(dt);
16538
+ this.engine.finish(this.list, dt, this.time, this.awake);
16679
16539
  }
16680
- this.collide(dt, only?.meets);
16540
+ this.measure();
16681
16541
  this.keepBudget();
16682
16542
  this.keepFar();
16683
16543
  }
@@ -16828,29 +16688,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16828
16688
  }
16829
16689
  return top;
16830
16690
  }
16831
- /** 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. */
16832
- wakeAbove(b) {
16833
- const below = this.solidOf.get(b);
16834
- if (!below) return;
16835
- 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]);
16836
- const x0 = Math.floor((below.c[0] - below.r) / SURFACE_CELL);
16837
- const x1 = Math.floor((below.c[0] + below.r) / SURFACE_CELL);
16838
- const z0 = Math.floor((below.c[2] - below.r) / SURFACE_CELL);
16839
- const z1 = Math.floor((below.c[2] + below.r) / SURFACE_CELL);
16840
- const wide2 = x1 - x0 > SURFACE_CELLS_ACROSS || z1 - z0 > SURFACE_CELLS_ACROSS;
16841
- const look = (part) => {
16842
- const o = part.body;
16843
- const s = this.solidOf.get(o);
16844
- if (!s || !o.sleeping || o.held || o === b) return;
16845
- 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;
16846
- if (boxTopAt(below, s.c[0], s.c[2]) === -Infinity && !this.restsOn(o, b, true)) return;
16847
- o.sleeping = false;
16848
- o.still = 0;
16849
- o.calmAt = null;
16850
- };
16851
- if (wide2) for (const s of this.solids) look(s);
16852
- 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);
16853
- }
16854
16691
  /**
16855
16692
  * 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
16856
16693
  * 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.
@@ -16867,36 +16704,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16867
16704
  }
16868
16705
  return false;
16869
16706
  }
16870
- /**
16871
- * 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
16872
- * (`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
16873
- * per update, and one object per value, so a substep makes none.
16874
- */
16875
- terrainSurface(x, y, z, raw) {
16876
- const rollingAt = this.o.terrainRollingAt;
16877
- if (!rollingAt || y !== raw || this.o.terrainAt && y !== this.o.terrainAt(x, z)) return null;
16878
- const key = groundTileKey(x, z);
16879
- let surface = this.surfaceByTile.get(key);
16880
- if (surface === void 0) {
16881
- surface = this.tileSurface(rollingAt, x, z);
16882
- this.surfaceByTile.set(key, surface);
16883
- }
16884
- return surface;
16885
- }
16886
- /** The terrain's surface of the tile at (x, z) (`tileRollingAt`), or null when it says nothing (the default surface). */
16887
- tileSurface(rollingAt, x, z) {
16888
- const dampingAt = this.o.terrainRollingDampingAt;
16889
- const rolling = tileRollingAt(rollingAt, x, z);
16890
- const rollingDamping = dampingAt ? tileRollingAt(dampingAt, x, z) : void 0;
16891
- if (rolling === void 0 && rollingDamping === void 0) return null;
16892
- const id = `${rolling ?? ROLLING_RESISTANCE2}|${rollingDamping ?? GROUND_ROLLING_DAMPING}`;
16893
- let surface = this.terrainSurfaces.get(id);
16894
- if (!surface) {
16895
- surface = { static: MU_STATIC2, dynamic: MU_DYNAMIC2, rolling: rolling ?? ROLLING_RESISTANCE2, rollingDamping: rollingDamping ?? GROUND_ROLLING_DAMPING };
16896
- this.terrainSurfaces.set(id, surface);
16897
- }
16898
- return surface;
16899
- }
16900
16707
  /** Where a corner of `self` at `(x, y, z)` lands: the ground, the top of another piece or of something fixed under it. */
16901
16708
  supportAt(self, x, y, z, step) {
16902
16709
  let top = this.o.groundAt(x, z);
@@ -16918,391 +16725,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
16918
16725
  this.supportBody = on?.body ?? null;
16919
16726
  return Math.max(top, fixed);
16920
16727
  }
16921
- /** 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. */
16922
- sweepOn(b, start, pos) {
16923
- const travel = Math.hypot(pos[0] - start[0], pos[1] - start[1], pos[2] - start[2]);
16924
- if (travel < 1e-3) return;
16925
- const hit = this.o.sweep?.(b, start, pos);
16926
- if (!hit || hit.normal[1] > SLAM_FLOOR_NORMAL2) return;
16927
- const v = b.velocity;
16928
- const n = hit.normal;
16929
- const into = -(v[0] * n[0] + v[1] * n[1] + v[2] * n[2]);
16930
- if (into <= 0) return;
16931
- const k = Math.min(1, hit.distance / travel);
16932
- const bounce2 = pairRestitution(b.restitution, hit.restitution);
16933
- for (let i = 0; i < 3; i++) {
16934
- pos[i] = start[i] + (pos[i] - start[i]) * k;
16935
- v[i] += (1 + bounce2) * into * n[i];
16936
- }
16937
- const w = b.angularVelocity;
16938
- w[0] *= 0.5;
16939
- w[1] *= 0.5;
16940
- w[2] *= 0.5;
16941
- if (into >= SLAM_MIN_SPEED2 && this.time - b.lastHit > 0.15) {
16942
- b.lastHit = this.time;
16943
- this.o.onSlam?.(b, hit, into);
16944
- }
16945
- }
16946
- /**
16947
- * 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
16948
- * gravity gave the centre turns into a swing about the hinge.
16949
- */
16950
- holdOnHinge(b, hinge, q) {
16951
- const e = this.hingeM.makeRotationFromQuaternion(q).elements;
16952
- const [lx, ly, lz] = hinge.local;
16953
- 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];
16954
- const v = b.velocity;
16955
- const w = b.angularVelocity;
16956
- 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]];
16957
- const k = [0, 1, 2].map((i) => {
16958
- const d = [i === 0 ? 1 : 0, i === 1 ? 1 : 0, i === 2 ? 1 : 0];
16959
- 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]);
16960
- 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]];
16961
- });
16962
- const j = solve3(k, [-point[0], -point[1], -point[2]]);
16963
- if (!j) return;
16964
- for (let i = 0; i < 3; i++) v[i] += j[i] / b.mass;
16965
- 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]);
16966
- for (let i = 0; i < 3; i++) w[i] += dw[i];
16967
- }
16968
- /** Puts the hinge point back where it hangs (what the step's rounding moved it by), moving the centre. */
16969
- keepOnHinge(hinge, pos, q) {
16970
- const e = this.hingeM.makeRotationFromQuaternion(q).elements;
16971
- const [lx, ly, lz] = hinge.local;
16972
- pos[0] = hinge.point[0] - (e[0] * lx + e[4] * ly + e[8] * lz);
16973
- pos[1] = hinge.point[1] - (e[1] * lx + e[5] * ly + e[9] * lz);
16974
- pos[2] = hinge.point[2] - (e[2] * lx + e[6] * ly + e[10] * lz);
16975
- }
16976
- /** The screw tears out: past `HINGE_RELEASE` of turn, or after `HINGE_SECONDS`, the piece falls free. */
16977
- wearHinge(b, hinge, q, dt) {
16978
- hinge.seconds -= dt;
16979
- const [x, y, z, w] = hinge.from;
16980
- const turned = 2 * Math.acos(Math.min(1, Math.abs(q.x * x + q.y * y + q.z * z + q.w * w)));
16981
- if (turned >= HINGE_RELEASE || hinge.seconds <= 0) b.hinge = void 0;
16982
- }
16983
- integrate(b, dt) {
16984
- const c = this.centerOf(b, this.p);
16985
- const pos = [c.x, c.y, c.z];
16986
- const start = [c.x, c.y, c.z];
16987
- const q = this.q.copy(b.root.quaternion);
16988
- const v = b.velocity;
16989
- const w = b.angularVelocity;
16990
- const invM = 1 / b.mass;
16991
- const steps = clamp6(Math.ceil(dt / SUB_DT), 1, 8);
16992
- const h = dt / steps;
16993
- let landing2 = 0;
16994
- let landPoint = [pos[0], pos[1], pos[2]];
16995
- let contactedLast = false;
16996
- const propped = b.propped;
16997
- const proppedAt = b.proppedAt;
16998
- b.propped = void 0;
16999
- b.proppedAt = void 0;
17000
- let touched = false;
17001
- let unstable = false;
17002
- const corners = this.samples(b, false);
17003
- this.m.makeRotationFromQuaternion(q);
17004
- let gLow = Infinity;
17005
- let gHigh = -Infinity;
17006
- for (const [sx, sy, sz] of corners) {
17007
- const e0 = this.m.elements;
17008
- const y = pos[1] + e0[1] * sx + e0[5] * sy + e0[9] * sz;
17009
- const x = pos[0] + e0[0] * sx + e0[4] * sy + e0[8] * sz;
17010
- const z = pos[2] + e0[2] * sx + e0[6] * sy + e0[10] * sz;
17011
- const g = Math.max(this.o.groundAt(x, z), this.o.staticTopAt?.(x, z, y, STEP) ?? -Infinity);
17012
- gLow = Math.min(gLow, g);
17013
- gHigh = Math.max(gHigh, g);
17014
- }
17015
- let local2 = b.round ? this.roundSamples(b, q) : gHigh - gLow > STEP ? this.samples(b, true) : corners;
17016
- let prev = local2.map(() => [0, 0, 0]);
17017
- for (let s = 0; s < steps; s++) {
17018
- if (b.round) {
17019
- const wl0 = Math.hypot(w[0], w[1], w[2]);
17020
- const next = this.nextQ.copy(q);
17021
- if (wl0 > 1e-6) next.premultiply(this.dq.setFromAxisAngle(this.ax.set(w[0] / wl0, w[1] / wl0, w[2] / wl0), wl0 * h)).normalize();
17022
- local2 = this.roundSamples(b, next);
17023
- if (prev.length !== local2.length) prev = local2.map(() => [0, 0, 0]);
17024
- }
17025
- this.m.makeRotationFromQuaternion(q);
17026
- const e0 = this.m.elements;
17027
- for (let i = 0; i < local2.length; i++) {
17028
- const [sx, sy, sz] = local2[i];
17029
- prev[i][0] = pos[0] + e0[0] * sx + e0[4] * sy + e0[8] * sz;
17030
- prev[i][1] = pos[1] + e0[1] * sx + e0[5] * sy + e0[9] * sz;
17031
- prev[i][2] = pos[2] + e0[2] * sx + e0[6] * sy + e0[10] * sz;
17032
- }
17033
- v[1] -= this.o.gravity * h;
17034
- if (b.hinge) this.holdOnHinge(b, b.hinge, q);
17035
- if (propped) {
17036
- const into = v[0] * propped[0] + v[1] * propped[1] + v[2] * propped[2];
17037
- if (into < 0) for (let k = 0; k < 3; k++) v[k] -= into * propped[k];
17038
- }
17039
- pos[0] += v[0] * h;
17040
- pos[1] += v[1] * h;
17041
- pos[2] += v[2] * h;
17042
- const wl = Math.hypot(w[0], w[1], w[2]);
17043
- if (wl > 1e-6) {
17044
- this.dq.setFromAxisAngle(this.ax.set(w[0] / wl, w[1] / wl, w[2] / wl), wl * h);
17045
- q.premultiply(this.dq).normalize();
17046
- }
17047
- if (b.hinge) this.keepOnHinge(b.hinge, pos, q);
17048
- const damp = b.round ? 1 : Math.exp(-0.15 * h);
17049
- w[0] *= damp;
17050
- w[1] *= damp;
17051
- w[2] *= damp;
17052
- this.m.makeRotationFromQuaternion(q);
17053
- const e = this.m.elements;
17054
- const rs = [];
17055
- const ids = [];
17056
- const ns = [];
17057
- const mus = [];
17058
- let maxPen = 0;
17059
- let penNormal = UP16;
17060
- const rise = STEP + Math.max(0, -v[1]) * h * 2;
17061
- let side = null;
17062
- let lowest = Infinity;
17063
- for (let i = 0; i < local2.length; i++) {
17064
- const [sx, sy, sz] = local2[i];
17065
- 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];
17066
- const px = pos[0] + r[0];
17067
- const pz = pos[2] + r[2];
17068
- const cy = pos[1] + r[1];
17069
- const was = prev[i];
17070
- const raw = this.o.groundAt(px, pz);
17071
- if (raw > cy - SLOP && was[1] < raw - STEP) {
17072
- if (this.o.groundAt(was[0], was[2]) >= raw - STEP) {
17073
- const floor2 = b.floor;
17074
- if (floor2 !== void 0 && floor2 - cy > -SLOP && floor2 < raw - STEP) {
17075
- rs.push(r);
17076
- ids.push(i);
17077
- ns.push(UP16);
17078
- mus.push(pairFriction(b.friction, b.round ? this.terrainSurface(px, floor2, pz, floor2) : null));
17079
- maxPen = Math.max(maxPen, floor2 - cy);
17080
- }
17081
- continue;
17082
- }
17083
- const dx = px - was[0];
17084
- const dz = pz - was[2];
17085
- const len = Math.hypot(dx, dz);
17086
- if (len > 1e-6) {
17087
- let lo = 0;
17088
- let hi = 1;
17089
- for (let it = 0; it < 6; it++) {
17090
- const mid = (lo + hi) / 2;
17091
- if (this.o.groundAt(was[0] + dx * mid, was[2] + dz * mid) >= raw - STEP) hi = mid;
17092
- else lo = mid;
17093
- }
17094
- const back = (1 - lo) * len + 1e-3;
17095
- if (!side || back > side.back) side = { back, dir: [dx / len, dz / len], r };
17096
- }
17097
- continue;
17098
- }
17099
- this.supportNormal = UP16;
17100
- this.supportFriction = null;
17101
- const gy = i < 8 || b.contacts ? this.supportAt(b, px, cy, pz, rise) : Math.max(raw, this.o.staticTopAt?.(px, pz, cy, rise) ?? -Infinity);
17102
- const pen = gy - cy;
17103
- if (pen > -SLOP) {
17104
- rs.push(r);
17105
- ids.push(i);
17106
- ns.push(this.supportNormal);
17107
- mus.push(pairFriction(b.friction, this.supportFriction ?? (b.round ? this.terrainSurface(px, gy, pz, raw) : null)));
17108
- if (pen > maxPen) {
17109
- maxPen = pen;
17110
- penNormal = this.supportNormal;
17111
- }
17112
- lowest = Math.min(lowest, gy);
17113
- }
17114
- }
17115
- if (lowest < Infinity) b.floor = lowest;
17116
- if (b.round && ns.length > 0) {
17117
- const sum = [0, 0, 0];
17118
- for (const one of ns) for (let k = 0; k < 3; k++) sum[k] += one[k];
17119
- const len = Math.hypot(sum[0], sum[1], sum[2]);
17120
- b.surfaceNormal = len > 1e-6 ? [sum[0] / len, sum[1] / len, sum[2] / len] : UP16;
17121
- } else if (b.round && rs.length === 0) b.surfaceNormal = UP16;
17122
- if (side) {
17123
- pos[0] -= side.dir[0] * side.back;
17124
- pos[2] -= side.dir[1] * side.back;
17125
- this.contactImpulse(b, e, side.r, [-side.dir[0], 0, -side.dir[1]], SIDE_BOUNCE);
17126
- }
17127
- contactedLast = rs.length > 0;
17128
- if (contactedLast) touched = true;
17129
- if (!contactedLast) continue;
17130
- const out = Math.max(0, maxPen - SLOP * 0.5) * penNormal[1];
17131
- pos[0] += penNormal[0] * out;
17132
- pos[1] += penNormal[1] * out;
17133
- pos[2] += penNormal[2] * out;
17134
- let impact3 = 0;
17135
- for (const r of rs) impact3 = Math.max(impact3, -(v[1] + w[2] * r[0] - w[0] * r[2]));
17136
- const restitution = impact3 > 2 ? 0.25 : 0;
17137
- if (impact3 > landing2) {
17138
- landing2 = impact3;
17139
- let sx = 0;
17140
- let sz = 0;
17141
- for (const r of rs) {
17142
- sx += r[0];
17143
- sz += r[2];
17144
- }
17145
- const lx = pos[0] + sx / rs.length;
17146
- const lz = pos[2] + sz / rs.length;
17147
- landPoint = [lx, pos[1] - Math.max(...rs.map((r) => -r[1])), lz];
17148
- }
17149
- 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]];
17150
- const dot2 = (a, c2) => a[0] * c2[0] + a[1] * c2[1] + a[2] * c2[2];
17151
- const goal = rs.map((r, k) => {
17152
- const vn = dot2(pointV(r), ns[k]);
17153
- return vn < 0 ? -restitution * vn : 0;
17154
- });
17155
- const slipping = rs.map((r, k) => {
17156
- const pv = pointV(r);
17157
- pv[1] += this.o.gravity * h;
17158
- const vn = dot2(pv, ns[k]);
17159
- return Math.hypot(pv[0] - vn * ns[k][0], pv[1] - vn * ns[k][1], pv[2] - vn * ns[k][2]) > SLIP_SPEED;
17160
- });
17161
- const vx0 = v[0];
17162
- const vz0 = v[2];
17163
- if (!b.warm || b.warm.length !== WARM_SLOTS * 4) b.warm = new Float64Array(WARM_SLOTS * 4);
17164
- const warm = b.warm;
17165
- const accN = ids.map((id) => id < WARM_SLOTS ? warm[id * 4] : 0);
17166
- const accT = ids.map((id) => id < WARM_SLOTS ? [warm[id * 4 + 1], warm[id * 4 + 2], warm[id * 4 + 3]] : [0, 0, 0]);
17167
- const push = (r, j) => {
17168
- v[0] += j[0] * invM;
17169
- v[1] += j[1] * invM;
17170
- v[2] += j[2] * invM;
17171
- 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]);
17172
- w[0] += dw[0];
17173
- w[1] += dw[1];
17174
- w[2] += dw[2];
17175
- };
17176
- const reach2 = (r, d) => {
17177
- 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]);
17178
- 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];
17179
- };
17180
- for (let k = 0; k < rs.length; k++) {
17181
- const n = ns[k];
17182
- const t = accT[k];
17183
- 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]]);
17184
- }
17185
- for (let it = 0; it < SOLVER_PASSES; it++) {
17186
- for (let k = 0; k < rs.length; k++) {
17187
- const r = rs[k];
17188
- const n = ns[k];
17189
- const vn = dot2(pointV(r), n);
17190
- const total = Math.max(0, accN[k] + (goal[k] - vn) / reach2(r, n));
17191
- const j = total - accN[k];
17192
- accN[k] = total;
17193
- if (j !== 0) push(r, [n[0] * j, n[1] * j, n[2] * j]);
17194
- const pv = pointV(r);
17195
- const pn = dot2(pv, n);
17196
- const vt = [pv[0] - pn * n[0], pv[1] - pn * n[1], pv[2] - pn * n[2]];
17197
- const speed = Math.hypot(vt[0], vt[1], vt[2]);
17198
- const f = [accT[k][0], accT[k][1], accT[k][2]];
17199
- if (speed > 1e-6) {
17200
- const d = [vt[0] / speed, vt[1] / speed, vt[2] / speed];
17201
- const jt = speed / reach2(r, d);
17202
- f[0] -= d[0] * jt;
17203
- f[1] -= d[1] * jt;
17204
- f[2] -= d[2] * jt;
17205
- } else if (f[0] === 0 && f[1] === 0 && f[2] === 0) continue;
17206
- const most = (slipping[k] ? mus[k].dynamic : mus[k].static) * accN[k];
17207
- const fl = Math.hypot(f[0], f[1], f[2]);
17208
- if (fl > most) {
17209
- f[0] *= most / fl;
17210
- f[1] *= most / fl;
17211
- f[2] *= most / fl;
17212
- }
17213
- const dj = [f[0] - accT[k][0], f[1] - accT[k][1], f[2] - accT[k][2]];
17214
- accT[k] = f;
17215
- if (dj[0] !== 0 || dj[1] !== 0 || dj[2] !== 0) push(r, dj);
17216
- }
17217
- }
17218
- pos[0] += (v[0] - vx0) * h;
17219
- pos[2] += (v[2] - vz0) * h;
17220
- warm.fill(0);
17221
- for (let k = 0; k < rs.length; k++) {
17222
- const id = ids[k];
17223
- if (id >= WARM_SLOTS) continue;
17224
- warm[id * 4] = accN[k];
17225
- warm[id * 4 + 1] = accT[k][0];
17226
- warm[id * 4 + 2] = accT[k][1];
17227
- warm[id * 4 + 3] = accT[k][2];
17228
- }
17229
- let x0 = Infinity, x1 = -Infinity, z0 = Infinity, z1 = -Infinity, low = 0;
17230
- for (const r of rs) {
17231
- x0 = Math.min(x0, r[0]);
17232
- x1 = Math.max(x1, r[0]);
17233
- z0 = Math.min(z0, r[2]);
17234
- z1 = Math.max(z1, r[2]);
17235
- low = Math.min(low, r[1]);
17236
- }
17237
- if (proppedAt) {
17238
- x0 = Math.min(x0, proppedAt[0]);
17239
- x1 = Math.max(x1, proppedAt[0]);
17240
- z0 = Math.min(z0, proppedAt[2]);
17241
- z1 = Math.max(z1, proppedAt[2]);
17242
- }
17243
- const height = -low;
17244
- const wx = x1 - x0;
17245
- const wz = z1 - z0;
17246
- const pastX = x0 > -SUPPORT_MARGIN * wx || x1 < SUPPORT_MARGIN * wx;
17247
- const pastZ = z0 > -SUPPORT_MARGIN * wz || z1 < SUPPORT_MARGIN * wz;
17248
- unstable = !b.round && (Math.min(wx, wz) < NARROW2 * height || pastX || pastZ);
17249
- if (b.tipping) {
17250
- b.tipping.seconds -= h;
17251
- const [hx, hy, hz2] = b.halfExtents;
17252
- const upHalf = Math.abs(e[1]) * hx + Math.abs(e[5]) * hy + Math.abs(e[9]) * hz2;
17253
- const rising = TIP_SECONDS - b.tipping.seconds > TIP_RISE_AFTER && v[1] > TIP_RISING;
17254
- if (upHalf < TIP_LYING || b.tipping.seconds <= 0 || rising) b.tipping = null;
17255
- else {
17256
- w[0] += b.tipping.x * TIP_ACCELERATION * h;
17257
- w[2] += b.tipping.z * TIP_ACCELERATION * h;
17258
- unstable = true;
17259
- }
17260
- }
17261
- if (unstable) {
17262
- const dirX = x0 + x1 > 0 ? -1 : 1;
17263
- const dirZ = z0 + z1 > 0 ? -1 : 1;
17264
- const k = TOPPLE2 * Math.min(height, 1) * h;
17265
- const overZ = pastX === pastZ ? wx <= wz : pastX;
17266
- if (overZ) w[2] -= dirX * k;
17267
- else w[0] += dirZ * k;
17268
- }
17269
- if (b.round) {
17270
- let rolling = 0;
17271
- let damping = 0;
17272
- for (const mu of mus) {
17273
- rolling = Math.max(rolling, mu.rolling ?? ROLLING_RESISTANCE2);
17274
- damping = Math.max(damping, mu.rollingDamping ?? 0);
17275
- }
17276
- this.rollingResistance(b, q, w, h, { rolling, damping });
17277
- } else {
17278
- const rd = Math.exp((rs.length >= 3 ? -3 : -1.5) * h);
17279
- w[0] *= rd;
17280
- w[1] *= rd;
17281
- w[2] *= rd;
17282
- const fd = Math.exp(-0.3 * h);
17283
- v[0] *= fd;
17284
- v[2] *= fd;
17285
- }
17286
- }
17287
- if (b.hinge) this.wearHinge(b, b.hinge, q, dt);
17288
- if (this.o.sweep && !b.held) this.sweepOn(b, start, pos);
17289
- b.root.quaternion.copy(q);
17290
- const off2 = this.ax.copy(b.centerV).applyQuaternion(q);
17291
- b.root.position.set(pos[0] - off2.x, pos[1] - off2.y, pos[2] - off2.z);
17292
- b.root.updateMatrixWorld(true);
17293
- b.onGround = contactedLast;
17294
- if (landing2 > HARD_LANDING2 && this.time - b.lastHit > 0.15) {
17295
- b.lastHit = this.time;
17296
- this.o.onHardLanding(b, landPoint, landing2);
17297
- }
17298
- const still = touched && !unstable && Math.hypot(v[0], v[1], v[2]) < STILL_SPEED2 && Math.hypot(w[0], w[1], w[2]) < STILL_SPIN2;
17299
- if (still) b.still += dt;
17300
- else b.still = 0;
17301
- b.balancing = unstable;
17302
- this.keepsTilt(b, q, dt);
17303
- const held = (touched || propped !== void 0) && !b.tipping;
17304
- if (!b.hinge && (b.still >= SLEEP_AFTER || this.staysInPlace(b, held, pos, q, dt)) && !this.onSteepSupport(b)) this.sleep(b);
17305
- }
17306
16728
  /**
17307
16729
  * 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),
17308
16730
  * 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
@@ -17336,15 +16758,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
17336
16758
  b.velocity[0] = b.velocity[1] = b.velocity[2] = 0;
17337
16759
  b.angularVelocity[0] = b.angularVelocity[1] = b.angularVelocity[2] = 0;
17338
16760
  }
17339
- /** 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. */
17340
- keepsTilt(b, q, dt) {
17341
- const at3 = b.steady;
17342
- if (at3 && turnBetween(at3.quaternion, q) <= STEADY_TURN2) {
17343
- at3.seconds += dt;
17344
- return;
17345
- }
17346
- b.steady = { quaternion: [q.x, q.y, q.z, q.w], seconds: 0 };
17347
- }
17348
16761
  /**
17349
16762
  * 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`),
17350
16763
  * 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
@@ -17455,118 +16868,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
17455
16868
  const y = c.y + e[1] * sx + e[5] * sy + e[9] * sz;
17456
16869
  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;
17457
16870
  }
17458
- /** Whether the piece has kept to the same spot on the ground for `CALM_SLEEP_AFTER` (see `CALM_DRIFT`). */
17459
- staysInPlace(b, touched, center, q, dt) {
17460
- const at3 = b.calmAt;
17461
- const turned = at3 ? turnBetween(at3.quaternion, q) : Infinity;
17462
- if (!touched || !at3 || Math.hypot(center[0] - at3.center[0], center[1] - at3.center[1], center[2] - at3.center[2]) > CALM_DRIFT || turned > CALM_TURN) {
17463
- b.calmAt = touched ? { center: [center[0], center[1], center[2]], quaternion: [q.x, q.y, q.z, q.w] } : null;
17464
- b.calm = 0;
17465
- return false;
17466
- }
17467
- b.calm += dt;
17468
- return b.calm >= CALM_SLEEP_AFTER;
17469
- }
17470
- /** Whether the centre `c` of `upper` is over the top of `lower`'s box (as it was at the start of the update). */
17471
- onTopOf(upper, lower, c) {
17472
- const s = this.solidOf.get(lower);
17473
- if (!s) return false;
17474
- const top = boxTopAt(s, c.x, c.z);
17475
- return top > -Infinity && c.y >= top - 0.01;
17476
- }
17477
- /**
17478
- * 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
17479
- * 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.
17480
- */
17481
- roundSamples(b, q) {
17482
- const round2 = b.round;
17483
- const rims = b.contacts ?? [];
17484
- if (!round2) return rims;
17485
- const e = this.m.makeRotationFromQuaternion(q).elements;
17486
- const n = b.surfaceNormal ?? UP16;
17487
- 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])];
17488
- if (round2.kind === "sphere") return [...rims, [down[0] * round2.radius, down[1] * round2.radius, down[2] * round2.radius]];
17489
- const k = round2.axis;
17490
- const across = [down[0], down[1], down[2]];
17491
- across[k] = 0;
17492
- const len = Math.hypot(across[0], across[1], across[2]);
17493
- if (len < 1e-3) return rims;
17494
- const lowest = (at3, radius) => {
17495
- const p = [across[0] / len * radius, across[1] / len * radius, across[2] / len * radius];
17496
- p[k] = at3;
17497
- return p;
17498
- };
17499
- const half = b.halfExtents[k];
17500
- const low = (round2.profile ?? [[-half, round2.radius], [half, round2.radius]]).map(([at3, radius]) => lowest(at3, radius));
17501
- return len > ROUND_LYING ? low : [...rims, ...low];
17502
- }
17503
- /**
17504
- * 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
17505
- * 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
17506
- * 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`,
17507
- * the pair's) then takes exp(-d h) of what is left, as the grass or the sand drag more the faster it rolls.
17508
- */
17509
- rollingResistance(b, q, w, h, { rolling, damping }) {
17510
- const round2 = b.round;
17511
- if (!round2) return;
17512
- const e = this.m.makeRotationFromQuaternion(q).elements;
17513
- const cylinder = round2.kind === "cylinder";
17514
- const a = cylinder ? [e[round2.axis * 4], e[round2.axis * 4 + 1], e[round2.axis * 4 + 2]] : [0, 0, 0];
17515
- const along2 = cylinder ? w[0] * a[0] + w[1] * a[1] + w[2] * a[2] : 0;
17516
- const roll = cylinder ? [a[0] * along2, a[1] * along2, a[2] * along2] : [w[0], w[1], w[2]];
17517
- const rest = [w[0] - roll[0], w[1] - roll[1], w[2] - roll[2]];
17518
- const spin = Math.hypot(roll[0], roll[1], roll[2]);
17519
- const upright = b.surfaceNormal?.[1] ?? 1;
17520
- const slow = spin > 0 ? Math.max(0, spin - rolling * this.o.gravity * upright * h / (round2.inertia * round2.radius)) / spin * Math.exp(-damping * h) : 0;
17521
- const lying = cylinder && Math.abs(a[1]) < ROUND_LYING;
17522
- const damp = lying ? 1 : Math.exp(-3 * h);
17523
- for (let i = 0; i < 3; i++) w[i] = roll[i] * slow + rest[i] * damp;
17524
- }
17525
- /** Half the height of the box of a piece as it is turned now. */
17526
- halfHeight(b) {
17527
- const e = this.m.makeRotationFromQuaternion(b.root.quaternion).elements;
17528
- return Math.abs(e[1]) * b.halfExtents[0] + Math.abs(e[5]) * b.halfExtents[1] + Math.abs(e[9]) * b.halfExtents[2];
17529
- }
17530
- /**
17531
- * 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
17532
- * 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`).
17533
- */
17534
- collide(dt, meets) {
17535
- const n = this.list.length;
17536
- if (n < 2) return;
17537
- const centers = this.list.map((b) => {
17538
- const c = this.centerOf(b, this.p);
17539
- return [c.x, c.y, c.z];
17540
- });
17541
- const radii = this.list.map((b) => Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]));
17542
- const order2 = this.list.map((_, i) => i).sort((i, j) => centers[i][0] - radii[i] - (centers[j][0] - radii[j]));
17543
- for (let oi = 0; oi < n; oi++) {
17544
- const i = order2[oi];
17545
- const reach2 = centers[i][0] + radii[i];
17546
- for (let oj = oi + 1; oj < n; oj++) {
17547
- const j = order2[oj];
17548
- if (centers[j][0] - radii[j] > reach2) break;
17549
- const a = this.list[i];
17550
- const b = this.list[j];
17551
- if ((a.held || a.sleeping) && (b.held || b.sleeping)) continue;
17552
- if (meets && !meets(a, b)) continue;
17553
- const r = radii[i] + radii[j];
17554
- const ca = centers[i];
17555
- const cb = centers[j];
17556
- if (Math.abs(ca[1] - cb[1]) > r || Math.abs(ca[2] - cb[2]) > r) continue;
17557
- const stacked = !a.round && !b.round && Math.abs(ca[1] - cb[1]) >= (this.halfHeight(a) + this.halfHeight(b)) * 0.9;
17558
- const onTop = this.onTopOf(b, a, new this.THREE.Vector3(...cb)) || this.onTopOf(a, b, new this.THREE.Vector3(...ca));
17559
- if (a.round || b.round) {
17560
- if (!stacked && !onTop) this.knock(a, b, ca, cb);
17561
- continue;
17562
- }
17563
- if (!this.partsOverlap(a, b)) continue;
17564
- this.boxImpact(a, b, ca, cb, dt);
17565
- this.separatePair(a, b, ca, cb, dt, stacked || onTop || this.restsOn(a, b, false) || this.restsOn(b, a, false));
17566
- }
17567
- }
17568
- this.applyPushes(dt);
17569
- }
17570
16871
  /**
17571
16872
  * 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
17572
16873
  * 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.
@@ -17601,129 +16902,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
17601
16902
  last.push[2] = push[2];
17602
16903
  return last.share;
17603
16904
  }
17604
- /** 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. */
17605
- contactShape(b, c) {
17606
- const e = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
17607
- const round2 = b.round;
17608
- if (round2) {
17609
- if (round2.kind === "sphere") return { kind: "capsule", from: c, to: c, radius: round2.radius };
17610
- const reach2 = Math.max(0, b.halfExtents[round2.axis] - round2.radius);
17611
- const k = round2.axis * 4;
17612
- const d = [e[k] * reach2, e[k + 1] * reach2, e[k + 2] * reach2];
17613
- 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: round2.radius };
17614
- }
17615
- const parts = b.partBoxes ?? [{ center: [0, 0, 0], half: b.halfExtents }];
17616
- return {
17617
- kind: "boxes",
17618
- boxes: parts.map(squareBoxOf).map(({ center, half }) => ({
17619
- 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]],
17620
- e,
17621
- half
17622
- }))
17623
- };
17624
- }
17625
- /**
17626
- * 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
17627
- * 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
17628
- * 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.
17629
- */
17630
- knock(a, b, ca, cb) {
17631
- const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
17632
- if (!hit) return;
17633
- if (Math.abs(hit.normal[1]) > SLAM_FLOOR_NORMAL2) return;
17634
- this.impact({ a, b, ca, cb, hit, apart: true, above: 0 });
17635
- }
17636
- /**
17637
- * 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,
17638
- * 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
17639
- * made inside each other, are left to `separatePair`, which holds or slides them without a kick.
17640
- */
17641
- boxImpact(a, b, ca, cb, dt) {
17642
- const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
17643
- if (!hit) return;
17644
- const n = hit.normal;
17645
- const va = a.velocity;
17646
- const vb = b.velocity;
17647
- const closing = (va[0] - vb[0]) * n[0] + (va[1] - vb[1]) * n[1] + (va[2] - vb[2]) * n[2];
17648
- if (hit.depth > LIFT_DEPTH + 2 * Math.max(0, -closing) * dt) return;
17649
- this.impact({ a, b, ca, cb, hit, apart: false, above: BOX_IMPACT_SPEED });
17650
- }
17651
- /** The impulse two pieces that met exchange (`knock`), put apart first when `apart`, and only when they close faster than `above` (m/s). */
17652
- impact({ a, b, ca, cb, hit, apart: apart2, above: above2 }) {
17653
- const n = hit.normal;
17654
- const ra = [hit.point[0] - ca[0], hit.point[1] - ca[1], hit.point[2] - ca[2]];
17655
- const rb = [hit.point[0] - cb[0], hit.point[1] - cb[1], hit.point[2] - cb[2]];
17656
- const pointV = (body, r) => {
17657
- const v2 = body.velocity;
17658
- const w = body.angularVelocity;
17659
- 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]];
17660
- };
17661
- const relative = () => {
17662
- const va = pointV(a, ra);
17663
- const vb = pointV(b, rb);
17664
- return [va[0] - vb[0], va[1] - vb[1], va[2] - vb[2]];
17665
- };
17666
- const vn0 = relative();
17667
- const closing = vn0[0] * n[0] + vn0[1] * n[1] + vn0[2] * n[2];
17668
- if (closing >= -above2) return;
17669
- const wake = (body) => {
17670
- body.sleeping = false;
17671
- body.still = 0;
17672
- body.calmAt = null;
17673
- };
17674
- if (closing < -WAKE_ABOVE_SPEED) {
17675
- if (a.sleeping) wake(a);
17676
- if (b.sleeping) wake(b);
17677
- }
17678
- const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
17679
- const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
17680
- if (ima + imb === 0) return;
17681
- const ea = [...this.m.makeRotationFromQuaternion(a.root.quaternion).elements];
17682
- const eb = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
17683
- const flat = Math.hypot(n[0], n[2]);
17684
- const out = flat > KNOCK_FLAT ? [n[0] / flat, 0, n[2] / flat] : n;
17685
- const depth = Math.max(0, hit.depth - SLOP) / (flat > KNOCK_FLAT ? flat : 1);
17686
- for (const [body, share, sign] of [[a, ima, 1], [b, imb, -1]]) {
17687
- if (share === 0 || !apart2) continue;
17688
- const move = depth * share * sign / (ima + imb);
17689
- body.root.position.x += out[0] * move;
17690
- body.root.position.y += out[1] * move;
17691
- body.root.position.z += out[2] * move;
17692
- }
17693
- if (closing >= 0) return;
17694
- const turning = (body, e, r, d) => {
17695
- 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]);
17696
- 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];
17697
- };
17698
- const resist = (d) => ima + imb + (ima > 0 ? turning(a, ea, ra, d) : 0) + (imb > 0 ? turning(b, eb, rb, d) : 0);
17699
- const give = (j2) => {
17700
- if (ima > 0) {
17701
- for (let i = 0; i < 3; i++) a.velocity[i] += j2[i] * ima;
17702
- 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]);
17703
- for (let i = 0; i < 3; i++) a.angularVelocity[i] += dw[i];
17704
- }
17705
- if (imb > 0) {
17706
- for (let i = 0; i < 3; i++) b.velocity[i] -= j2[i] * imb;
17707
- 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]);
17708
- for (let i = 0; i < 3; i++) b.angularVelocity[i] -= dw[i];
17709
- }
17710
- };
17711
- const bounce2 = apart2 && -closing > BOUNCE_MIN_SPEED ? pairRestitution(a.restitution, b.restitution) : 0;
17712
- const j = -(1 + bounce2) * closing / resist(n);
17713
- give([n[0] * j, n[1] * j, n[2] * j]);
17714
- const v = relative();
17715
- const vn = v[0] * n[0] + v[1] * n[1] + v[2] * n[2];
17716
- const vt = [v[0] - vn * n[0], v[1] - vn * n[1], v[2] - vn * n[2]];
17717
- const slide = Math.hypot(vt[0], vt[1], vt[2]);
17718
- if (slide < 1e-6) return;
17719
- const t = [vt[0] / slide, vt[1] / slide, vt[2] / slide];
17720
- const jt = Math.min(slide / resist(t), pairFriction(a.friction, b.friction ?? null).dynamic * j);
17721
- give([-t[0] * jt, -t[1] * jt, -t[2] * jt]);
17722
- if (-closing >= SLAM_MIN_SPEED2 && this.time - Math.max(a.lastHit, b.lastHit) > 0.15) {
17723
- a.lastHit = b.lastHit = this.time;
17724
- this.o.onKnock?.(a, b, hit.point, -closing);
17725
- }
17726
- }
17727
16905
  /** Whether a part of `a` and a part of `b` (their boxes of the step, as world-aligned boxes around them) overlap. */
17728
16906
  partsOverlap(a, b) {
17729
16907
  for (const pa of this.surfacesOf.get(a) ?? []) {
@@ -17735,66 +16913,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
17735
16913
  }
17736
16914
  return false;
17737
16915
  }
17738
- /**
17739
- * 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
17740
- * summed into `pushes` and bounded per piece in `applyPushes`.
17741
- * - 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.
17742
- * - 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
17743
- * 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.
17744
- * - 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
17745
- * 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`).
17746
- */
17747
- separatePair(a, b, ca, cb, dt, resting) {
17748
- const sa = this.contactShape(a, ca);
17749
- const sb = this.contactShape(b, cb);
17750
- const hit = shapeContact(sa, sb);
17751
- if (!hit) return;
17752
- const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
17753
- const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
17754
- if (ima + imb === 0) return;
17755
- if (Math.hypot(hit.normal[0], hit.normal[2]) > KNOCK_FLAT) {
17756
- this.slideApart(a, b, ima, imb, levelBoxesApart(sa, sb) ?? hit);
17757
- return;
17758
- }
17759
- const n = hit.normal;
17760
- const va = a.velocity;
17761
- const vb = b.velocity;
17762
- 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;
17763
- if (hit.depth > contact) {
17764
- const level = boxesApartAlongGround(sa, sb);
17765
- if (level) this.slideApart(a, b, ima, imb, level);
17766
- return;
17767
- }
17768
- if (resting) return;
17769
- const [upper, c, up] = n[1] > 0 ? [a, ca, n] : [b, cb, [-n[0], -n[1], -n[2]]];
17770
- if (upper.held || upper.sleeping) return;
17771
- const otherCentre = upper === a ? cb : ca;
17772
- if (c[1] < otherCentre[1] - SLOP) {
17773
- const level = boxesApartAlongGround(sa, sb);
17774
- if (level) this.slideApart(a, b, ima, imb, level);
17775
- return;
17776
- }
17777
- upper.propped = up;
17778
- const e = this.m.makeRotationFromQuaternion(upper.root.quaternion).elements;
17779
- const r = [hit.point[0] - c[0], hit.point[1] - c[1], hit.point[2] - c[2]];
17780
- upper.proppedAt = r;
17781
- 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]);
17782
- 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];
17783
- const lift = SEPARATE_SHARE * Math.max(0, hit.depth - SLOP) / (1 + angular * upper.mass);
17784
- const push = this.pushes.get(upper) ?? [0, 0, 0];
17785
- for (let i = 0; i < 3; i++) push[i] = Math.abs(up[i] * lift) > Math.abs(push[i]) ? up[i] * lift : push[i];
17786
- this.pushes.set(upper, push);
17787
- this.turnAboutCentre(upper, c, [turn2[0] * lift * upper.mass, turn2[1] * lift * upper.mass, turn2[2] * lift * upper.mass]);
17788
- this.contactImpulse(upper, e, r, up, 0);
17789
- }
17790
- /** Turns a piece by the rotation vector `by` (rad) about its centre `c`, which stays where it is. */
17791
- turnAboutCentre(b, c, by) {
17792
- const angle = Math.hypot(by[0], by[1], by[2]);
17793
- if (angle < 1e-9) return;
17794
- b.root.quaternion.premultiply(this.dq.setFromAxisAngle(this.ax.set(by[0] / angle, by[1] / angle, by[2] / angle), angle)).normalize();
17795
- const off2 = this.ax.copy(b.centerV).applyQuaternion(b.root.quaternion);
17796
- b.root.position.set(c[0] - off2.x, c[1] - off2.y, c[2] - off2.z);
17797
- }
17798
16916
  /** 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. */
17799
16917
  slideApart(a, b, ima, imb, hit) {
17800
16918
  const n = hit.normal;
@@ -17816,12 +16934,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
17816
16934
  }
17817
16935
  }
17818
16936
  };
17819
- function solve3(k, rhs) {
17820
- 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]);
17821
- const d = det(k[0], k[1], k[2]);
17822
- if (Math.abs(d) < 1e-12) return null;
17823
- return [det(rhs, k[1], k[2]) / d, det(k[0], rhs, k[2]) / d, det(k[0], k[1], rhs) / d];
17824
- }
17825
16937
 
17826
16938
  // src/rebar.ts
17827
16939
  var RebarCages = class {
@@ -27439,7 +26551,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
27439
26551
  var CRATER_RADIUS = 2.5;
27440
26552
  var ORIGIN7 = [4e3, 0, 4e3];
27441
26553
  var warmed = /* @__PURE__ */ new WeakSet();
27442
- var OWN_SOLVER = {};
27443
26554
  var brickWall = (THREE) => {
27444
26555
  const parts = [];
27445
26556
  const geometries = [];
@@ -27469,7 +26580,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
27469
26580
  merged.setIndex(index);
27470
26581
  return { parts, joints: inferJoints(parts), mesh: new THREE.Mesh(merged, new THREE.MeshStandardMaterial()), ranges };
27471
26582
  };
27472
- var runScene = (THREE, makeRenderer, options) => {
26583
+ var runScene = (THREE, makeRenderer, engine, worldDesc) => {
27473
26584
  const scene2 = new THREE.Scene();
27474
26585
  const world = createDestruction();
27475
26586
  const vfx = createVfx({});
@@ -27479,12 +26590,12 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
27479
26590
  let renderer;
27480
26591
  let bound;
27481
26592
  try {
27482
- engineWorld = options.engine?.createWorld(options.world ?? KIT_WORLD);
27483
- const loop = engineWorld ? createPhysicsLoop(engineWorld) : void 0;
26593
+ engineWorld = engine.createWorld(worldDesc ?? KIT_WORLD);
26594
+ const loop = createPhysicsLoop(engineWorld);
27484
26595
  const terrainGridAt = (xs, zs, out) => {
27485
26596
  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]);
27486
26597
  };
27487
- renderer = makeRenderer({ physics: loop ?? "own", scene: scene2, world, vfx, eventBudgetMs: Infinity, groundAt: terrainAt, terrainAt, terrainGridAt, jitWarmUp: false });
26598
+ renderer = makeRenderer({ physics: loop, scene: scene2, world, vfx, eventBudgetMs: Infinity, groundAt: terrainAt, terrainAt, terrainGridAt, jitWarmUp: false });
27488
26599
  const wall = brickWall(THREE);
27489
26600
  const root = new THREE.Group();
27490
26601
  root.position.set(...ORIGIN7);
@@ -27511,7 +26622,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
27511
26622
  renderer.wakeRegion(point[0], point[2], CRATER_RADIUS);
27512
26623
  for (let frame2 = 0; frame2 < FRAMES; frame2++) {
27513
26624
  world.update(FRAME_SECONDS);
27514
- loop?.step(FRAME_SECONDS);
26625
+ loop.step(FRAME_SECONDS);
27515
26626
  renderer.update(FRAME_SECONDS);
27516
26627
  const stats = renderer.stats();
27517
26628
  seen.pieces = Math.max(seen.pieces, stats.pieces);
@@ -27520,10 +26631,10 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
27520
26631
  return seen;
27521
26632
  };
27522
26633
  var warmUpDestructionJit = (THREE, makeRenderer, options = {}) => {
27523
- const key = options.engine ?? OWN_SOLVER;
27524
- if (warmed.has(key)) return void 0;
27525
- warmed.add(key);
27526
- return runScene(THREE, makeRenderer, options);
26634
+ const { engine } = options;
26635
+ if (!engine || warmed.has(engine)) return void 0;
26636
+ warmed.add(engine);
26637
+ return runScene(THREE, makeRenderer, engine, options.world);
27527
26638
  };
27528
26639
 
27529
26640
  // src/bend.ts
@@ -29510,7 +28621,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
29510
28621
  __publicField(this, "blastDebris");
29511
28622
  __publicField(this, "blastWakes");
29512
28623
  __publicField(this, "costMeter");
29513
- /** The step cost the debris policy reads this frame (`debrisCost`), null when there is none (the kit's own solver, or `fixed`). */
28624
+ /** The step cost the debris policy reads this frame (`debrisCost`), null when there is none (`fixed`). */
29514
28625
  __publicField(this, "cost", null);
29515
28626
  __publicField(this, "costFrame", 0);
29516
28627
  /** The pieces faded away (`faded`): their root shrinks, then leaves the scene. */
@@ -29521,6 +28632,9 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
29521
28632
  __publicField(this, "staticBoxes", /* @__PURE__ */ new Map());
29522
28633
  /** `staticSolids` per object, kept until `staticNow` measures the object again for a reason. */
29523
28634
  __publicField(this, "solidCache", /* @__PURE__ */ new Map());
28635
+ /** 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. */
28636
+ __publicField(this, "hostSolids", /* @__PURE__ */ new Map());
28637
+ __publicField(this, "hostSolidLists", /* @__PURE__ */ new Map());
29524
28638
  /** 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. */
29525
28639
  __publicField(this, "unsureSolids", /* @__PURE__ */ new Set());
29526
28640
  /** The objects `staticNow` measured again for a reason (an event, a moved root, boxes that changed): their `staticSolids` are measured again too. */
@@ -29924,8 +29038,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
29924
29038
  }
29925
29039
  if (total.isEmpty()) return { half: [0.25, 0.25, 0.25], center: new this.THREE.Vector3() };
29926
29040
  const size = total.getSize(new this.THREE.Vector3());
29927
- const least = minPieceHalf(this.o.physics);
29928
- 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()) };
29041
+ 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()) };
29929
29042
  }
29930
29043
  /**
29931
29044
  * 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
@@ -30619,8 +29732,28 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
30619
29732
  } finally {
30620
29733
  this.measuredMeshes.clear();
30621
29734
  }
29735
+ for (const [id, source] of this.hostSolids) out.set(id, this.hostSolidsOf(id, source()));
30622
29736
  return out;
30623
29737
  }
29738
+ setHostSolids(id, source) {
29739
+ if (source) this.hostSolids.set(id, source);
29740
+ else {
29741
+ this.hostSolids.delete(id);
29742
+ this.hostSolidLists.delete(id);
29743
+ }
29744
+ }
29745
+ /** 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). */
29746
+ hostSolidsOf(id, given) {
29747
+ const kept = this.hostSolidLists.get(id);
29748
+ if (kept && kept.given.length === given.length && given.every((s, i) => s === kept.given[i])) return kept.solids;
29749
+ 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) }));
29750
+ if (kept && sameSolids(kept.solids, next)) {
29751
+ this.hostSolidLists.set(id, { given, solids: kept.solids });
29752
+ return kept.solids;
29753
+ }
29754
+ this.hostSolidLists.set(id, { given, solids: next });
29755
+ return next;
29756
+ }
30624
29757
  /** `staticSolids` for each fixed object of `now`, into `out`. */
30625
29758
  measureEach(now, budget, out) {
30626
29759
  for (const id of now.keys()) {