@adatechnology/game-destruction-three 1.2.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +50 -1
- package/dist/index.global.js +617 -114
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +587 -88
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -269,6 +269,78 @@ function worldBoxOf(local, matrix, axis, from, to) {
|
|
|
269
269
|
return box;
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
+
// src/fragment-support.ts
|
|
273
|
+
var SUPPORT_SHARE = 1e-4;
|
|
274
|
+
var createSupport = () => ({ count: 0, x: 0, y: 0, z: 0, edgeX: 0, edgeZ: 0 });
|
|
275
|
+
function writeCorners(params) {
|
|
276
|
+
const { e, half, corners } = params;
|
|
277
|
+
for (let i = 0; i < 8; i++) {
|
|
278
|
+
const sx = i & 1 ? half.x : -half.x;
|
|
279
|
+
const sy = i & 2 ? half.y : -half.y;
|
|
280
|
+
const sz = i & 4 ? half.z : -half.z;
|
|
281
|
+
corners[3 * i] = e[0] * sx + e[4] * sy + e[8] * sz;
|
|
282
|
+
corners[3 * i + 1] = e[1] * sx + e[5] * sy + e[9] * sz;
|
|
283
|
+
corners[3 * i + 2] = e[2] * sx + e[6] * sy + e[10] * sz;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
function findSupport(params) {
|
|
287
|
+
const { corners, support } = params;
|
|
288
|
+
let low = Infinity;
|
|
289
|
+
for (let i = 0; i < 8; i++) low = Math.min(low, corners[3 * i + 1]);
|
|
290
|
+
const level = low + SUPPORT_SHARE * params.reach;
|
|
291
|
+
let count = 0, x = 0, y = 0, z = 0, first = -1, edgeX = 0, edgeZ = 0;
|
|
292
|
+
for (let i = 0; i < 8; i++) {
|
|
293
|
+
if (corners[3 * i + 1] > level) continue;
|
|
294
|
+
count++;
|
|
295
|
+
x += corners[3 * i];
|
|
296
|
+
y += corners[3 * i + 1];
|
|
297
|
+
z += corners[3 * i + 2];
|
|
298
|
+
if (first < 0) first = i;
|
|
299
|
+
else if (count === 2) {
|
|
300
|
+
edgeX = corners[3 * i] - corners[3 * first];
|
|
301
|
+
edgeZ = corners[3 * i + 2] - corners[3 * first + 2];
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const along2 = Math.hypot(edgeX, edgeZ);
|
|
305
|
+
support.count = count;
|
|
306
|
+
support.x = x / count;
|
|
307
|
+
support.y = y / count;
|
|
308
|
+
support.z = z / count;
|
|
309
|
+
support.edgeX = along2 > 0 ? edgeX / along2 : 0;
|
|
310
|
+
support.edgeZ = along2 > 0 ? edgeZ / along2 : 0;
|
|
311
|
+
}
|
|
312
|
+
function inertiaAbout(params) {
|
|
313
|
+
const { e, half, x, y, z } = params;
|
|
314
|
+
const ax = x * e[0] + y * e[1] + z * e[2];
|
|
315
|
+
const ay = x * e[4] + y * e[5] + z * e[6];
|
|
316
|
+
const az = x * e[8] + y * e[9] + z * e[10];
|
|
317
|
+
const xx = half.x * half.x, yy = half.y * half.y, zz = half.z * half.z;
|
|
318
|
+
return (ax * ax * (yy + zz) + ay * ay * (xx + zz) + az * az * (xx + yy)) / 3;
|
|
319
|
+
}
|
|
320
|
+
function applyInverseInertia(params) {
|
|
321
|
+
const { e, half, v } = params;
|
|
322
|
+
const xx = half.x * half.x, yy = half.y * half.y, zz = half.z * half.z;
|
|
323
|
+
const ax = 3 * (v.x * e[0] + v.y * e[1] + v.z * e[2]) / (yy + zz);
|
|
324
|
+
const ay = 3 * (v.x * e[4] + v.y * e[5] + v.z * e[6]) / (xx + zz);
|
|
325
|
+
const az = 3 * (v.x * e[8] + v.y * e[9] + v.z * e[10]) / (xx + yy);
|
|
326
|
+
v.x = ax * e[0] + ay * e[4] + az * e[8];
|
|
327
|
+
v.y = ax * e[1] + ay * e[5] + az * e[9];
|
|
328
|
+
v.z = ax * e[2] + ay * e[6] + az * e[10];
|
|
329
|
+
}
|
|
330
|
+
function turnToLanding(params) {
|
|
331
|
+
const { corners, pivot, axisX, axisZ } = params;
|
|
332
|
+
let first = Infinity;
|
|
333
|
+
for (let i = 0; i < 8; i++) {
|
|
334
|
+
const px = corners[3 * i] - pivot.x;
|
|
335
|
+
const py = corners[3 * i + 1] - pivot.y;
|
|
336
|
+
const pz = corners[3 * i + 2] - pivot.z;
|
|
337
|
+
if (py <= SUPPORT_SHARE * params.reach) continue;
|
|
338
|
+
const qy = axisZ * px - axisX * pz;
|
|
339
|
+
first = Math.min(first, Math.atan2(qy, py) + Math.PI / 2);
|
|
340
|
+
}
|
|
341
|
+
return first;
|
|
342
|
+
}
|
|
343
|
+
|
|
272
344
|
// src/fragment-instances.ts
|
|
273
345
|
import { kitOriginOf, markKitOrigin } from "@adatechnology/game-contracts";
|
|
274
346
|
import { BATCHED_LAYER } from "@adatechnology/game-render-three";
|
|
@@ -766,6 +838,67 @@ function slerpInto(out, a, ai, b, bi, t) {
|
|
|
766
838
|
out.set(x / length, y / length, z / length, w / length);
|
|
767
839
|
}
|
|
768
840
|
|
|
841
|
+
// src/support-hull.ts
|
|
842
|
+
var cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
843
|
+
function planHull(points) {
|
|
844
|
+
const sorted = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
845
|
+
if (sorted.length < 3) return sorted;
|
|
846
|
+
const lower = [];
|
|
847
|
+
for (const p of sorted) {
|
|
848
|
+
while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) lower.pop();
|
|
849
|
+
lower.push(p);
|
|
850
|
+
}
|
|
851
|
+
const upper = [];
|
|
852
|
+
for (let i = sorted.length - 1; i >= 0; i--) {
|
|
853
|
+
const p = sorted[i];
|
|
854
|
+
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) upper.pop();
|
|
855
|
+
upper.push(p);
|
|
856
|
+
}
|
|
857
|
+
return [...lower.slice(0, -1), ...upper.slice(0, -1)];
|
|
858
|
+
}
|
|
859
|
+
function toSegment(p, a, b) {
|
|
860
|
+
const dx = b[0] - a[0];
|
|
861
|
+
const dz = b[1] - a[1];
|
|
862
|
+
const length = dx * dx + dz * dz;
|
|
863
|
+
const t = length > 0 ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dz) / length)) : 0;
|
|
864
|
+
return Math.hypot(p[0] - a[0] - t * dx, p[1] - a[1] - t * dz);
|
|
865
|
+
}
|
|
866
|
+
function isOverSupport(points, centre, tolerance) {
|
|
867
|
+
const hull = planHull(points);
|
|
868
|
+
if (hull.length === 0) return false;
|
|
869
|
+
if (hull.length === 1) return Math.hypot(centre[0] - hull[0][0], centre[1] - hull[0][1]) <= tolerance;
|
|
870
|
+
let inside = hull.length >= 3;
|
|
871
|
+
let nearest = Infinity;
|
|
872
|
+
for (let i = 0; i < hull.length; i++) {
|
|
873
|
+
const a = hull[i];
|
|
874
|
+
const b = hull[(i + 1) % hull.length];
|
|
875
|
+
if (hull.length >= 3 && cross(a, b, centre) < 0) inside = false;
|
|
876
|
+
nearest = Math.min(nearest, toSegment(centre, a, b));
|
|
877
|
+
}
|
|
878
|
+
return inside || nearest <= tolerance;
|
|
879
|
+
}
|
|
880
|
+
function clipConvex(subject, window) {
|
|
881
|
+
let kept = [...subject];
|
|
882
|
+
for (let e = 0; e < window.length && kept.length > 0; e++) {
|
|
883
|
+
const a = window[e];
|
|
884
|
+
const b = window[(e + 1) % window.length];
|
|
885
|
+
const next = [];
|
|
886
|
+
for (let i = 0; i < kept.length; i++) {
|
|
887
|
+
const p = kept[i];
|
|
888
|
+
const q = kept[(i + 1) % kept.length];
|
|
889
|
+
const sp = cross(a, b, p);
|
|
890
|
+
const sq = cross(a, b, q);
|
|
891
|
+
if (sp >= 0) next.push(p);
|
|
892
|
+
if (sp >= 0 !== sq >= 0) {
|
|
893
|
+
const t = sp / (sp - sq);
|
|
894
|
+
next.push([p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t]);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
kept = next;
|
|
898
|
+
}
|
|
899
|
+
return kept;
|
|
900
|
+
}
|
|
901
|
+
|
|
769
902
|
// src/fragments.ts
|
|
770
903
|
function rotationOf(q) {
|
|
771
904
|
const { x, y, z, w } = q;
|
|
@@ -788,6 +921,20 @@ function shadowOf(e, half) {
|
|
|
788
921
|
return [e[0] * half.x, e[2] * half.x, e[4] * half.y, e[6] * half.y, e[8] * half.z, e[10] * half.z];
|
|
789
922
|
}
|
|
790
923
|
var reachAlong = (shadow, nx, nz) => Math.abs(nx * shadow[0] + nz * shadow[1]) + Math.abs(nx * shadow[2] + nz * shadow[3]) + Math.abs(nx * shadow[4] + nz * shadow[5]);
|
|
924
|
+
function shadowPolygon(shadow, cx, cz) {
|
|
925
|
+
const corners = [];
|
|
926
|
+
for (let k = 0; k < 8; k++) {
|
|
927
|
+
const a = k & 1 ? 1 : -1, b = k & 2 ? 1 : -1, c = k & 4 ? 1 : -1;
|
|
928
|
+
corners.push([cx + a * shadow[0] + b * shadow[2] + c * shadow[4], cz + a * shadow[1] + b * shadow[3] + c * shadow[5]]);
|
|
929
|
+
}
|
|
930
|
+
return planHull(corners);
|
|
931
|
+
}
|
|
932
|
+
function contactsOf(mine, under, x, z) {
|
|
933
|
+
const own = shadowPolygon(mine, 0, 0);
|
|
934
|
+
const points = [];
|
|
935
|
+
for (const box of under) points.push(...clipConvex(own, shadowPolygon(box.shadow, box.x - x, box.z - z)));
|
|
936
|
+
return points;
|
|
937
|
+
}
|
|
791
938
|
function shadowsOverlap(first, second, dx, dz) {
|
|
792
939
|
for (const shadow of [first, second]) {
|
|
793
940
|
for (let g = 0; g < 6; g += 2) {
|
|
@@ -838,8 +985,18 @@ var SWEEP_MIN_SPEED = 1;
|
|
|
838
985
|
var WALL_RESTITUTION = 0.25;
|
|
839
986
|
var WALL_GLIDE = 0.7;
|
|
840
987
|
var TUMBLE_REACH = 0.1;
|
|
988
|
+
var GROUND_RESTITUTION = 0.25;
|
|
989
|
+
var GROUND_GLIDE = 0.7;
|
|
990
|
+
var GROUND_SPIN_KEPT = 0.55;
|
|
991
|
+
var BOUNCE_LIFT_SHARE = 0.05;
|
|
992
|
+
var ROUGHNESS_SHARE = 0.05;
|
|
993
|
+
var SETTLE_FALL_HALVES = 2;
|
|
994
|
+
var NUDGE_ENERGY_SHARE = 0.1;
|
|
995
|
+
var DEBRIS_BOUNCE_SPEED = 0.6;
|
|
996
|
+
var DEBRIS_LAY_RATE = 7;
|
|
841
997
|
var LAID_GAP = 5e-3;
|
|
842
998
|
var STAND_RATIO = 0.6;
|
|
999
|
+
var FADE_SECONDS = 0.6;
|
|
843
1000
|
var SETTLE_SECONDS = 0.4;
|
|
844
1001
|
var isSlow = (f) => f.vel.lengthSq() < 0.012 && f.ang.lengthSq() < 0.2;
|
|
845
1002
|
var isBig = (size) => {
|
|
@@ -870,6 +1027,11 @@ var FragmentSystem = class {
|
|
|
870
1027
|
__publicField(this, "v");
|
|
871
1028
|
__publicField(this, "w");
|
|
872
1029
|
__publicField(this, "rng", createRng(FRAGMENT_SEED));
|
|
1030
|
+
/** The corners of the fragment being stepped (`writeCorners`), what of them it rests on, its halves at its fade, and its centre over that support: kept between calls. */
|
|
1031
|
+
__publicField(this, "corners", new Float64Array(24));
|
|
1032
|
+
__publicField(this, "support", createSupport());
|
|
1033
|
+
__publicField(this, "halves", { x: 0, y: 0, z: 0 });
|
|
1034
|
+
__publicField(this, "lever", { x: 0, y: 0, z: 0 });
|
|
873
1035
|
/** The fragments in flight drawn by instances, a draw per shared geometry and material (`FragmentInstances`). */
|
|
874
1036
|
__publicField(this, "instances");
|
|
875
1037
|
/** The instanced probe of the last `warmMeshes` (a new one each call, as the other probes are new meshes), freed by the next call or by `dispose`. */
|
|
@@ -1171,6 +1333,13 @@ var FragmentSystem = class {
|
|
|
1171
1333
|
continue;
|
|
1172
1334
|
}
|
|
1173
1335
|
if (this.clock > f.deadline) {
|
|
1336
|
+
const held = this.heldUntil(f);
|
|
1337
|
+
if (held > f.deadline) {
|
|
1338
|
+
f.deadline = held;
|
|
1339
|
+
if (held < next) next = held;
|
|
1340
|
+
this.mortal[kept++] = f;
|
|
1341
|
+
continue;
|
|
1342
|
+
}
|
|
1174
1343
|
f.isMortal = false;
|
|
1175
1344
|
f.dying = true;
|
|
1176
1345
|
f.mesh.matrixAutoUpdate = true;
|
|
@@ -1183,6 +1352,26 @@ var FragmentSystem = class {
|
|
|
1183
1352
|
this.mortal.length = kept;
|
|
1184
1353
|
this.nextDeadline = next;
|
|
1185
1354
|
}
|
|
1355
|
+
/**
|
|
1356
|
+
* When `f` may start to fade: once every chip lying on it (light and small, its bottom on f's top, their floor areas sharing ground) has shrunk away; never while one that
|
|
1357
|
+
* never fades lies on it, a settling time from now while one still moves on it, and at `f.deadline` when nothing lies on it.
|
|
1358
|
+
*/
|
|
1359
|
+
heldUntil(f) {
|
|
1360
|
+
const box = f.box ?? (f.box = restingBox(f));
|
|
1361
|
+
let until = f.deadline;
|
|
1362
|
+
for (const other of this.list) {
|
|
1363
|
+
if (other === f || other.rigid || other.body) continue;
|
|
1364
|
+
const on = other.box ?? (other.box = restingBox(other));
|
|
1365
|
+
const e = rotationOf(other.mesh.quaternion);
|
|
1366
|
+
const bottom = other.mesh.position.y - (Math.abs(e[1]) * other.half.x + Math.abs(e[5]) * other.half.y + Math.abs(e[9]) * other.half.z);
|
|
1367
|
+
if (bottom <= f.mesh.position.y || bottom > box.top + SUPPORT_GAP) continue;
|
|
1368
|
+
if (Math.abs(on.x - box.x) >= on.reachX + box.reachX || Math.abs(on.z - box.z) >= on.reachZ + box.reachZ) continue;
|
|
1369
|
+
if (!shadowsOverlap(box.shadow, on.shadow, on.x - box.x, on.z - box.z)) continue;
|
|
1370
|
+
const gone = other.dying ? this.clock + other.fade * FADE_SECONDS : other.asleep ? other.deadline + FADE_SECONDS : this.clock + SETTLE_SECONDS;
|
|
1371
|
+
until = Math.max(until, gone);
|
|
1372
|
+
}
|
|
1373
|
+
return until;
|
|
1374
|
+
}
|
|
1186
1375
|
/** What lies inside the disc looks at the ground again: it was dug or heaped under it. */
|
|
1187
1376
|
wakeRegion(x, z, radius) {
|
|
1188
1377
|
for (const f of this.list) {
|
|
@@ -1201,7 +1390,7 @@ var FragmentSystem = class {
|
|
|
1201
1390
|
const f = this.active[i];
|
|
1202
1391
|
if (f.dying) {
|
|
1203
1392
|
f.mesh.matrixAutoUpdate = true;
|
|
1204
|
-
f.fade -= dt /
|
|
1393
|
+
f.fade -= dt / FADE_SECONDS;
|
|
1205
1394
|
if (f.fade <= 0) {
|
|
1206
1395
|
this.instances.remove(f.mesh);
|
|
1207
1396
|
this.o.scene.remove(f.mesh);
|
|
@@ -1350,36 +1539,22 @@ var FragmentSystem = class {
|
|
|
1350
1539
|
if (mesh.position.y - ext < gy) {
|
|
1351
1540
|
contact = true;
|
|
1352
1541
|
mesh.position.y = gy + ext;
|
|
1353
|
-
if (
|
|
1354
|
-
|
|
1355
|
-
f.vel.x *= 0.7;
|
|
1356
|
-
f.vel.z *= 0.7;
|
|
1357
|
-
f.ang.multiplyScalar(0.55);
|
|
1358
|
-
const tumble = Math.min(1, TUMBLE_REACH / Math.max(h.x, h.y, h.z));
|
|
1359
|
-
f.ang.x += (this.rng() - 0.5) * 2 * tumble;
|
|
1360
|
-
f.ang.z += (this.rng() - 0.5) * 2 * tumble;
|
|
1542
|
+
if (pile > floor) {
|
|
1543
|
+
turning = this.restOnDebris(f, { g, dt, gy, sc, tip });
|
|
1361
1544
|
} else {
|
|
1362
|
-
f.
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1545
|
+
writeCorners({ e, half: this.scaledHalf(f, sc), corners: this.corners });
|
|
1546
|
+
findSupport({ corners: this.corners, reach: Math.max(h.x, h.y, h.z) * sc, support: this.support });
|
|
1547
|
+
if (!f.contact && this.bounces(f, { g, dt, sc })) {
|
|
1548
|
+
this.bounce(f, sc);
|
|
1549
|
+
} else {
|
|
1550
|
+
f.vel.y = 0;
|
|
1551
|
+
turning = this.restOn(f, { g, dt, sc });
|
|
1552
|
+
}
|
|
1368
1553
|
}
|
|
1369
1554
|
} else {
|
|
1370
1555
|
f.ang.multiplyScalar(Math.exp(-0.15 * dt));
|
|
1371
1556
|
if (f.air > 0) this.flutter(f, dt);
|
|
1372
1557
|
}
|
|
1373
|
-
if (contact && tip) {
|
|
1374
|
-
f.vel.x += tip.x * TIP_SPEED * dt * 10;
|
|
1375
|
-
f.vel.z += tip.z * TIP_SPEED * dt * 10;
|
|
1376
|
-
f.ang.x += tip.z * TIP_TURN * dt * 10;
|
|
1377
|
-
f.ang.z -= tip.x * TIP_TURN * dt * 10;
|
|
1378
|
-
} else if (contact && f.vel.lengthSq() < 4) {
|
|
1379
|
-
turning = this.settle(f, dt);
|
|
1380
|
-
this.m.makeRotationFromQuaternion(mesh.quaternion);
|
|
1381
|
-
mesh.position.y = gy + (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
|
|
1382
|
-
}
|
|
1383
1558
|
f.contact = contact;
|
|
1384
1559
|
if (contact && !turning && isSlow(f)) {
|
|
1385
1560
|
f.still += dt;
|
|
@@ -1432,7 +1607,8 @@ var FragmentSystem = class {
|
|
|
1432
1607
|
for (const other of this.list) {
|
|
1433
1608
|
if (other === f || other.dying) continue;
|
|
1434
1609
|
const box = other.box ?? (other.box = restingBox(other));
|
|
1435
|
-
|
|
1610
|
+
const beneath = !other.asleep && other.mesh.position.y < bottom;
|
|
1611
|
+
if (box.top > bottom + PILE_STEP && !beneath || box.top < top - PILE_STEP) continue;
|
|
1436
1612
|
if (Math.abs(box.x - p.x) >= box.reachX + reachX || Math.abs(box.z - p.z) >= box.reachZ + reachZ) continue;
|
|
1437
1613
|
if (!shadowsOverlap(mine, box.shadow, box.x - p.x, box.z - p.z)) continue;
|
|
1438
1614
|
if (box.top > top + PILE_STEP) under.length = 0;
|
|
@@ -1441,13 +1617,16 @@ var FragmentSystem = class {
|
|
|
1441
1617
|
}
|
|
1442
1618
|
this.offEdge = null;
|
|
1443
1619
|
if (under.length > 0 && !under.some((box) => box.top >= top - PILE_STEP && shadowsOverlap(POINT, box.shadow, box.x - p.x, box.z - p.z))) {
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
x
|
|
1447
|
-
|
|
1620
|
+
const contacts = contactsOf(mine, under, p.x, p.z);
|
|
1621
|
+
if (!isOverSupport(contacts, [0, 0], 0)) {
|
|
1622
|
+
let x = 0, z = 0;
|
|
1623
|
+
for (const [cx, cz] of contacts) {
|
|
1624
|
+
x -= cx;
|
|
1625
|
+
z -= cz;
|
|
1626
|
+
}
|
|
1627
|
+
const d = Math.hypot(x, z);
|
|
1628
|
+
this.offEdge = d > 1e-6 ? { x: x / d, z: z / d } : { x: 1, z: 0 };
|
|
1448
1629
|
}
|
|
1449
|
-
const d = Math.hypot(x, z);
|
|
1450
|
-
this.offEdge = d > 1e-6 ? { x: x / d, z: z / d } : { x: 1, z: 0 };
|
|
1451
1630
|
}
|
|
1452
1631
|
return top;
|
|
1453
1632
|
}
|
|
@@ -1459,8 +1638,41 @@ var FragmentSystem = class {
|
|
|
1459
1638
|
f.ang.x += (this.rng() - 0.5) * SHEET_SWAY * 4 * dt;
|
|
1460
1639
|
f.ang.z += (this.rng() - 0.5) * SHEET_SWAY * 4 * dt;
|
|
1461
1640
|
}
|
|
1462
|
-
/**
|
|
1463
|
-
|
|
1641
|
+
/**
|
|
1642
|
+
* On other debris, by the pile's rules (`DEBRIS_BOUNCE_SPEED`): a bounce with a tumble by the speed of its end, Coulomb friction, its spin damped, off the edge of what holds
|
|
1643
|
+
* it when its centre is past it (`tip`), else laid flat (`layOnDebris`). True while it is still being laid flat.
|
|
1644
|
+
*/
|
|
1645
|
+
restOnDebris(f, params) {
|
|
1646
|
+
const { g, dt, gy, sc, tip } = params;
|
|
1647
|
+
const h = f.half;
|
|
1648
|
+
if (f.vel.y < -DEBRIS_BOUNCE_SPEED) {
|
|
1649
|
+
f.vel.y = -f.vel.y * GROUND_RESTITUTION;
|
|
1650
|
+
f.vel.x *= GROUND_GLIDE;
|
|
1651
|
+
f.vel.z *= GROUND_GLIDE;
|
|
1652
|
+
f.ang.multiplyScalar(GROUND_SPIN_KEPT);
|
|
1653
|
+
const tumble = Math.min(1, TUMBLE_REACH / Math.max(h.x, h.y, h.z));
|
|
1654
|
+
f.ang.x += (this.rng() - 0.5) * 2 * tumble;
|
|
1655
|
+
f.ang.z += (this.rng() - 0.5) * 2 * tumble;
|
|
1656
|
+
} else {
|
|
1657
|
+
f.vel.y = 0;
|
|
1658
|
+
this.slide(f, g, dt);
|
|
1659
|
+
f.ang.multiplyScalar(Math.exp(-6 * dt));
|
|
1660
|
+
}
|
|
1661
|
+
if (tip) {
|
|
1662
|
+
f.vel.x += tip.x * TIP_SPEED * dt * 10;
|
|
1663
|
+
f.vel.z += tip.z * TIP_SPEED * dt * 10;
|
|
1664
|
+
f.ang.x += tip.z * TIP_TURN * dt * 10;
|
|
1665
|
+
f.ang.z -= tip.x * TIP_TURN * dt * 10;
|
|
1666
|
+
return false;
|
|
1667
|
+
}
|
|
1668
|
+
if (f.vel.lengthSq() >= 4) return false;
|
|
1669
|
+
const turning = this.layOnDebris(f, dt);
|
|
1670
|
+
const e = this.m.makeRotationFromQuaternion(f.mesh.quaternion).elements;
|
|
1671
|
+
f.mesh.position.y = gy + (Math.abs(e[1]) * h.x + Math.abs(e[5]) * h.y + Math.abs(e[9]) * h.z) * sc;
|
|
1672
|
+
return turning;
|
|
1673
|
+
}
|
|
1674
|
+
/** Tip the fragment lying on other debris towards lying on its flattest face. True while its end is still more than `LAID_GAP` from lying. */
|
|
1675
|
+
layOnDebris(f, dt) {
|
|
1464
1676
|
const mesh = f.mesh;
|
|
1465
1677
|
this.m.makeRotationFromQuaternion(mesh.quaternion);
|
|
1466
1678
|
const e = this.m.elements;
|
|
@@ -1480,9 +1692,204 @@ var FragmentSystem = class {
|
|
|
1480
1692
|
this.w.set(0, 1, 0);
|
|
1481
1693
|
this.dq.setFromUnitVectors(this.v, this.w);
|
|
1482
1694
|
this.q.copy(this.dq).multiply(mesh.quaternion);
|
|
1483
|
-
mesh.quaternion.slerp(this.q,
|
|
1695
|
+
mesh.quaternion.slerp(this.q, Math.min(1, dt * DEBRIS_LAY_RATE));
|
|
1484
1696
|
return Math.sqrt(Math.max(0, 1 - this.v.y * this.v.y)) * Math.max(half[0], half[1], half[2]) > LAID_GAP;
|
|
1485
1697
|
}
|
|
1698
|
+
/** The fragment's halves (m) at its fade, into `halves`. */
|
|
1699
|
+
scaledHalf(f, sc) {
|
|
1700
|
+
this.halves.x = f.half.x * sc;
|
|
1701
|
+
this.halves.y = f.half.y * sc;
|
|
1702
|
+
this.halves.z = f.half.z * sc;
|
|
1703
|
+
return this.halves;
|
|
1704
|
+
}
|
|
1705
|
+
/** The speed (m/s, up) of the point it rests on (`support`): its centre's, and its spin's about the centre there. */
|
|
1706
|
+
supportSpeed(f) {
|
|
1707
|
+
const s = this.support;
|
|
1708
|
+
return f.vel.y + f.ang.z * s.x - f.ang.x * s.z;
|
|
1709
|
+
}
|
|
1710
|
+
/**
|
|
1711
|
+
* It hits the floor hard enough to bounce: the point it comes down on goes into it faster than the rebound that lifts it `BOUNCE_LIFT_SHARE` of its thinnest half, and than one
|
|
1712
|
+
* step of gravity gives what lies on it.
|
|
1713
|
+
*/
|
|
1714
|
+
bounces(f, params) {
|
|
1715
|
+
const { g, dt, sc } = params;
|
|
1716
|
+
const thinnest = Math.min(f.half.x, f.half.y, f.half.z) * sc;
|
|
1717
|
+
const least = Math.max(2 * g * dt, Math.sqrt(2 * g * BOUNCE_LIFT_SHARE * thinnest) / GROUND_RESTITUTION);
|
|
1718
|
+
return this.supportSpeed(f) < -least;
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* The hit on the floor as an impulse at the point it comes down on (`support`): j = (1 + e) v / (1 + n · ((I⁻¹ (r × n)) × r)) per unit of mass, which sends the centre up
|
|
1722
|
+
* and turns it by its own inertia about that point. A box landing flat on a face is not turned; one coming down on a corner tumbles as much as its size and the hit give,
|
|
1723
|
+
* so a board metres long is turned as a long body is, not as a chip.
|
|
1724
|
+
*/
|
|
1725
|
+
bounce(f, sc) {
|
|
1726
|
+
const s = this.support;
|
|
1727
|
+
const into = this.supportSpeed(f);
|
|
1728
|
+
const rough = ROUGHNESS_SHARE * Math.max(f.half.x, f.half.y, f.half.z) * sc;
|
|
1729
|
+
s.x += (this.rng() - 0.5) * 2 * rough;
|
|
1730
|
+
s.z += (this.rng() - 0.5) * 2 * rough;
|
|
1731
|
+
const turn2 = this.w.set(-s.z, 0, s.x);
|
|
1732
|
+
applyInverseInertia({ e: this.m.elements, half: this.scaledHalf(f, sc), v: turn2 });
|
|
1733
|
+
const share = 1 + (turn2.z * s.x - turn2.x * s.z);
|
|
1734
|
+
const impulse = -(1 + GROUND_RESTITUTION) * into / share;
|
|
1735
|
+
f.vel.y += impulse;
|
|
1736
|
+
f.ang.addScaledVector(turn2, impulse);
|
|
1737
|
+
f.vel.x *= GROUND_GLIDE;
|
|
1738
|
+
f.vel.z *= GROUND_GLIDE;
|
|
1739
|
+
f.ang.multiplyScalar(GROUND_SPIN_KEPT);
|
|
1740
|
+
}
|
|
1741
|
+
/**
|
|
1742
|
+
* Coulomb friction on the floor: the sliding speed falls by mu g each second, so a piece leaving at v slides v^2 / (2 mu g), as a body of the piece system does (a snapped
|
|
1743
|
+
* shovel handle thrown at 7.5 m/s comes to rest 5.7 m away, near the blade, not a metre from where it broke).
|
|
1744
|
+
*/
|
|
1745
|
+
slide(f, g, dt) {
|
|
1746
|
+
const sliding = Math.hypot(f.vel.x, f.vel.z);
|
|
1747
|
+
const kept = sliding > 0 ? Math.max(0, sliding - DEBRIS_FRICTION * g * dt) / sliding : 0;
|
|
1748
|
+
f.vel.x *= kept;
|
|
1749
|
+
f.vel.z *= kept;
|
|
1750
|
+
}
|
|
1751
|
+
/**
|
|
1752
|
+
* On the floor and not bouncing: friction acts on its foot (the point it rests on), which slides or holds while the piece turns over it; it turns only as what it rests on lets
|
|
1753
|
+
* it (not at all on a face, only about the edge on an edge), its spin about the vertical falls by the friction under it, and gravity tips it over its foot (`settle`). True
|
|
1754
|
+
* while it is still turning over.
|
|
1755
|
+
*/
|
|
1756
|
+
restOn(f, params) {
|
|
1757
|
+
const { g, dt, sc } = params;
|
|
1758
|
+
const s = this.support;
|
|
1759
|
+
const ang = f.ang;
|
|
1760
|
+
const dx = -s.x, dy = -s.y, dz = -s.z;
|
|
1761
|
+
f.vel.x -= ang.y * dz - ang.z * dy;
|
|
1762
|
+
f.vel.z -= ang.x * dy - ang.y * dx;
|
|
1763
|
+
this.slide(f, g, dt);
|
|
1764
|
+
const footX = f.vel.x;
|
|
1765
|
+
const footZ = f.vel.z;
|
|
1766
|
+
const half = this.scaledHalf(f, sc);
|
|
1767
|
+
const gyration = Math.sqrt(inertiaAbout({ e: this.m.elements, half, x: 0, y: 1, z: 0 }));
|
|
1768
|
+
if (gyration > 0) ang.y = Math.sign(ang.y) * Math.max(0, Math.abs(ang.y) - DEBRIS_FRICTION * g * dt / gyration);
|
|
1769
|
+
if (s.count >= 3) {
|
|
1770
|
+
ang.x = 0;
|
|
1771
|
+
ang.z = 0;
|
|
1772
|
+
} else if (s.count === 2) {
|
|
1773
|
+
const along2 = ang.x * s.edgeX + ang.z * s.edgeZ;
|
|
1774
|
+
ang.x = along2 * s.edgeX;
|
|
1775
|
+
ang.z = along2 * s.edgeZ;
|
|
1776
|
+
}
|
|
1777
|
+
const lever = this.lever;
|
|
1778
|
+
lever.x = dx;
|
|
1779
|
+
lever.y = dy;
|
|
1780
|
+
lever.z = dz;
|
|
1781
|
+
const slow = footX * footX + footZ * footZ < 2 * g * SETTLE_FALL_HALVES * Math.max(half.x, half.y, half.z);
|
|
1782
|
+
const turning = slow && this.settle(f, g, dt);
|
|
1783
|
+
f.vel.x = footX + (ang.y * lever.z - ang.z * lever.y);
|
|
1784
|
+
f.vel.y = ang.z * lever.x - ang.x * lever.z;
|
|
1785
|
+
f.vel.z = footZ + (ang.x * lever.y - ang.y * lever.x);
|
|
1786
|
+
return turning;
|
|
1787
|
+
}
|
|
1788
|
+
/**
|
|
1789
|
+
* Gravity tips it over what it rests on (`support`), as a rigid body tips over its foot: the turn m g × the lever from the foot to the centre (`lever`), over its inertia about
|
|
1790
|
+
* the foot (its own, more the lever squared), so the time to lie down goes as √(L / g), never at a fixed rate. On a face it does not turn; balanced on a face too narrow for
|
|
1791
|
+
* it, or on an edge, it is let over (`nudge`, which moves `lever` to that edge). Its turn this step stops where the face it falls onto reaches the floor, and that face then
|
|
1792
|
+
* holds it. True while it still turns.
|
|
1793
|
+
*/
|
|
1794
|
+
settle(f, g, dt) {
|
|
1795
|
+
const s = this.support;
|
|
1796
|
+
const e = this.m.elements;
|
|
1797
|
+
const half = this.halves;
|
|
1798
|
+
const reach = Math.max(half.x, half.y, half.z);
|
|
1799
|
+
const lever = this.lever;
|
|
1800
|
+
let torqueX = g * lever.z;
|
|
1801
|
+
let torqueZ = -g * lever.x;
|
|
1802
|
+
if (s.count >= 3) {
|
|
1803
|
+
torqueX = 0;
|
|
1804
|
+
torqueZ = 0;
|
|
1805
|
+
} else if (s.count === 2) {
|
|
1806
|
+
const along2 = torqueX * s.edgeX + torqueZ * s.edgeZ;
|
|
1807
|
+
torqueX = along2 * s.edgeX;
|
|
1808
|
+
torqueZ = along2 * s.edgeZ;
|
|
1809
|
+
}
|
|
1810
|
+
const torque = Math.hypot(torqueX, torqueZ);
|
|
1811
|
+
const ang = f.ang;
|
|
1812
|
+
if (torque <= g * SUPPORT_SHARE * reach && ang.x === 0 && ang.z === 0) {
|
|
1813
|
+
if (s.count >= 3 && !this.isUnsteady(f)) return false;
|
|
1814
|
+
if (!this.nudge(f, g)) return false;
|
|
1815
|
+
} else if (torque > 0) {
|
|
1816
|
+
const axisX = torqueX / torque;
|
|
1817
|
+
const axisZ = torqueZ / torque;
|
|
1818
|
+
const along2 = lever.x * axisX + lever.z * axisZ;
|
|
1819
|
+
const inertia = inertiaAbout({ e, half, x: axisX, y: 0, z: axisZ }) + lever.x * lever.x + lever.y * lever.y + lever.z * lever.z - along2 * along2;
|
|
1820
|
+
ang.x += axisX * torque / inertia * dt;
|
|
1821
|
+
ang.z += axisZ * torque / inertia * dt;
|
|
1822
|
+
}
|
|
1823
|
+
const spin = Math.hypot(ang.x, ang.z);
|
|
1824
|
+
if (spin === 0) return false;
|
|
1825
|
+
const landing = turnToLanding({ corners: this.corners, pivot: { x: -lever.x, y: -lever.y, z: -lever.z }, axisX: ang.x / spin, axisZ: ang.z / spin, reach });
|
|
1826
|
+
if (spin * dt > landing) {
|
|
1827
|
+
ang.x *= landing / (spin * dt);
|
|
1828
|
+
ang.z *= landing / (spin * dt);
|
|
1829
|
+
}
|
|
1830
|
+
return true;
|
|
1831
|
+
}
|
|
1832
|
+
/**
|
|
1833
|
+
* It stands on a face it would not stay on: a sheet on its edge or a board on its end (the face narrower than `STAND_RATIO` of its height), or a big piece on any face but
|
|
1834
|
+
* its flattest.
|
|
1835
|
+
*/
|
|
1836
|
+
isUnsteady(f) {
|
|
1837
|
+
const e = this.m.elements;
|
|
1838
|
+
const up = [Math.abs(e[1]), Math.abs(e[5]), Math.abs(e[9])];
|
|
1839
|
+
const half = [f.half.x, f.half.y, f.half.z];
|
|
1840
|
+
let small = 0;
|
|
1841
|
+
let standing = 0;
|
|
1842
|
+
for (let i = 1; i < 3; i++) {
|
|
1843
|
+
if (half[i] < half[small]) small = i;
|
|
1844
|
+
if (up[i] > up[standing]) standing = i;
|
|
1845
|
+
}
|
|
1846
|
+
if (f.body) return half[standing] > half[small] * (1 + SUPPORT_SHARE);
|
|
1847
|
+
const across = Math.min(...half.filter((_, i) => i !== standing));
|
|
1848
|
+
return across < STAND_RATIO * half[standing];
|
|
1849
|
+
}
|
|
1850
|
+
/**
|
|
1851
|
+
* Let over the edge towards lying on its flattest face, from balance: the spin about that edge that lifts its centre over it with `NUDGE_ENERGY_SHARE` to spare (½ I ω² =
|
|
1852
|
+
* m g Δh), so it falls from there by its own weight, as a piece a breath tipped over. The edge becomes the lever (`lever`). False when there is no way down to that face.
|
|
1853
|
+
*/
|
|
1854
|
+
nudge(f, g) {
|
|
1855
|
+
const e = this.m.elements;
|
|
1856
|
+
const half = this.halves;
|
|
1857
|
+
const tolerance = SUPPORT_SHARE * Math.max(half.x, half.y, half.z);
|
|
1858
|
+
let small = 0;
|
|
1859
|
+
if (half.y < half.x) small = 1;
|
|
1860
|
+
if (half.z < (small === 1 ? half.y : half.x)) small = 2;
|
|
1861
|
+
const sign = e[small * 4 + 1] >= 0 ? 1 : -1;
|
|
1862
|
+
const axisX = -e[small * 4 + 2] * sign;
|
|
1863
|
+
const axisZ = e[small * 4] * sign;
|
|
1864
|
+
const length = Math.hypot(axisX, axisZ);
|
|
1865
|
+
if (length < SUPPORT_SHARE) return false;
|
|
1866
|
+
const kx = axisX / length;
|
|
1867
|
+
const kz = axisZ / length;
|
|
1868
|
+
const c = this.corners;
|
|
1869
|
+
const floor = this.support.y + tolerance;
|
|
1870
|
+
let far2 = -Infinity;
|
|
1871
|
+
for (let i = 0; i < 8; i++) if (c[3 * i + 1] <= floor) far2 = Math.max(far2, kx * c[3 * i + 2] - kz * c[3 * i]);
|
|
1872
|
+
let count = 0, x = 0, y = 0, z = 0;
|
|
1873
|
+
for (let i = 0; i < 8; i++) {
|
|
1874
|
+
if (c[3 * i + 1] > floor || kx * c[3 * i + 2] - kz * c[3 * i] < far2 - tolerance) continue;
|
|
1875
|
+
count++;
|
|
1876
|
+
x += c[3 * i];
|
|
1877
|
+
y += c[3 * i + 1];
|
|
1878
|
+
z += c[3 * i + 2];
|
|
1879
|
+
}
|
|
1880
|
+
const lever = this.lever;
|
|
1881
|
+
lever.x = -x / count;
|
|
1882
|
+
lever.y = -y / count;
|
|
1883
|
+
lever.z = -z / count;
|
|
1884
|
+
const along2 = lever.x * kx + lever.z * kz;
|
|
1885
|
+
const square = lever.x * lever.x + lever.y * lever.y + lever.z * lever.z - along2 * along2;
|
|
1886
|
+
const rise = Math.max(Math.sqrt(square) - lever.y, tolerance);
|
|
1887
|
+
const inertia = inertiaAbout({ e, half, x: kx, y: 0, z: kz }) + square;
|
|
1888
|
+
const spinOfInertia = Math.sqrt(2 * g * rise * (1 + NUDGE_ENERGY_SHARE) / inertia);
|
|
1889
|
+
f.ang.x = kx * spinOfInertia;
|
|
1890
|
+
f.ang.z = kz * spinOfInertia;
|
|
1891
|
+
return true;
|
|
1892
|
+
}
|
|
1486
1893
|
/** Remove the fragments of one object (after `restore`). */
|
|
1487
1894
|
removeHome(home) {
|
|
1488
1895
|
for (let i = this.list.length - 1; i >= 0; i--) {
|
|
@@ -3452,6 +3859,97 @@ var createEngineImpacts = (loop, statics, destruction, options = {}, isDebris) =
|
|
|
3452
3859
|
// src/engine-pieces.ts
|
|
3453
3860
|
import { samePatch } from "@adatechnology/game-terrain";
|
|
3454
3861
|
|
|
3862
|
+
// src/kit-energy.ts
|
|
3863
|
+
var KIT_RULE = {
|
|
3864
|
+
/** A piece that lost a leg and is balanced on the line between the ones left is started down over it (`renderer.ts`). */
|
|
3865
|
+
GO_OVER_IF_BALANCED: "goOverIfBalanced",
|
|
3866
|
+
/** What stood on ground that went (a hole under a foot) is started down over the gap (`renderer.ts`). */
|
|
3867
|
+
CHECK_UNDERMINED: "checkUndermined",
|
|
3868
|
+
/** Furniture made a piece balanced on an edge of its feet is started over it (`EDGE_NUDGE`, `renderer.ts`). */
|
|
3869
|
+
LOOSEN_IF_FOOTLESS: "loosenIfFootless",
|
|
3870
|
+
/** A piece of the engine balanced on a narrow support is turned about its edge (`engine-pieces.ts`). */
|
|
3871
|
+
TOPPLE: "topple"
|
|
3872
|
+
};
|
|
3873
|
+
var RULES = Object.values(KIT_RULE);
|
|
3874
|
+
var GRAVITY = 9.81;
|
|
3875
|
+
var KIT_ENERGY_RESOLUTION = 1e-3;
|
|
3876
|
+
var REST_HEIGHT = KIT_ENERGY_RESOLUTION;
|
|
3877
|
+
var kineticEnergyOf = (body) => {
|
|
3878
|
+
const [vx, vy, vz] = body.velocity;
|
|
3879
|
+
const [wx, wy, wz] = body.angularVelocity;
|
|
3880
|
+
const { x, y, z, w } = body.root.quaternion;
|
|
3881
|
+
const lx = (1 - 2 * (y * y + z * z)) * wx + 2 * (x * y + z * w) * wy + 2 * (x * z - y * w) * wz;
|
|
3882
|
+
const ly = 2 * (x * y - z * w) * wx + (1 - 2 * (x * x + z * z)) * wy + 2 * (y * z + x * w) * wz;
|
|
3883
|
+
const lz = 2 * (x * z + y * w) * wx + 2 * (y * z - x * w) * wy + (1 - 2 * (x * x + y * y)) * wz;
|
|
3884
|
+
const [hx, hy, hz] = body.halfExtents;
|
|
3885
|
+
const m = body.mass;
|
|
3886
|
+
let ix = m * (hy * hy + hz * hz) / 3;
|
|
3887
|
+
let iy = m * (hx * hx + hz * hz) / 3;
|
|
3888
|
+
let iz = m * (hx * hx + hy * hy) / 3;
|
|
3889
|
+
const round = body.round;
|
|
3890
|
+
if (round) {
|
|
3891
|
+
const about = round.inertia * m * round.radius * round.radius;
|
|
3892
|
+
if (round.kind === "sphere") ix = iy = iz = about;
|
|
3893
|
+
else if (round.axis === 0) ix = about;
|
|
3894
|
+
else if (round.axis === 1) iy = about;
|
|
3895
|
+
else iz = about;
|
|
3896
|
+
}
|
|
3897
|
+
return 0.5 * m * (vx * vx + vy * vy + vz * vz) + 0.5 * (ix * lx * lx + iy * ly * ly + iz * lz * lz);
|
|
3898
|
+
};
|
|
3899
|
+
var fallEnergyOf = (body) => {
|
|
3900
|
+
const { x, y, z, w } = body.root.quaternion;
|
|
3901
|
+
const [hx, hy, hz] = body.halfExtents;
|
|
3902
|
+
const [cx, cy, cz] = body.massCentre ?? [0, 0, 0];
|
|
3903
|
+
const rx = 2 * (x * y + z * w);
|
|
3904
|
+
const ry = 1 - 2 * (x * x + z * z);
|
|
3905
|
+
const rz = 2 * (y * z - x * w);
|
|
3906
|
+
const over2 = hx * Math.abs(rx) + hy * Math.abs(ry) + hz * Math.abs(rz) + rx * cx + ry * cy + rz * cz;
|
|
3907
|
+
const lowest = Math.min(hx - Math.abs(cx), hy - Math.abs(cy), hz - Math.abs(cz));
|
|
3908
|
+
return body.mass * GRAVITY * Math.max(0, over2 - lowest);
|
|
3909
|
+
};
|
|
3910
|
+
var KitEnergyLedger = class {
|
|
3911
|
+
constructor(onOver) {
|
|
3912
|
+
__publicField(this, "onOver", onOver);
|
|
3913
|
+
__publicField(this, "before", 0);
|
|
3914
|
+
__publicField(this, "tallies", new Map(RULES.map((rule) => [rule, { pushes: 0, overs: 0, worstShare: 0, worst: null }])));
|
|
3915
|
+
__publicField(this, "episodes", /* @__PURE__ */ new WeakMap());
|
|
3916
|
+
}
|
|
3917
|
+
begin(body) {
|
|
3918
|
+
this.before = kineticEnergyOf(body);
|
|
3919
|
+
if (this.before > body.mass * GRAVITY * REST_HEIGHT) return;
|
|
3920
|
+
const episode = this.episodes.get(body);
|
|
3921
|
+
if (!episode) {
|
|
3922
|
+
this.episodes.set(body, { given: new Float64Array(RULES.length), over: new Uint8Array(RULES.length), fall: fallEnergyOf(body) });
|
|
3923
|
+
return;
|
|
3924
|
+
}
|
|
3925
|
+
episode.given.fill(0);
|
|
3926
|
+
episode.over.fill(0);
|
|
3927
|
+
episode.fall = fallEnergyOf(body);
|
|
3928
|
+
}
|
|
3929
|
+
end(rule, body) {
|
|
3930
|
+
const tally = this.tallies.get(rule);
|
|
3931
|
+
tally.pushes++;
|
|
3932
|
+
const episode = this.episodes.get(body);
|
|
3933
|
+
const open = episode ?? { given: new Float64Array(RULES.length), over: new Uint8Array(RULES.length), fall: fallEnergyOf(body) };
|
|
3934
|
+
if (!episode) this.episodes.set(body, open);
|
|
3935
|
+
const index = RULES.indexOf(rule);
|
|
3936
|
+
const given = open.given[index] + kineticEnergyOf(body) - this.before;
|
|
3937
|
+
open.given[index] = given;
|
|
3938
|
+
const share = given <= 0 ? 0 : given / (open.fall + body.mass * GRAVITY * KIT_ENERGY_RESOLUTION);
|
|
3939
|
+
if (share > tally.worstShare) {
|
|
3940
|
+
tally.worstShare = share;
|
|
3941
|
+
tally.worst = { rule, objectId: body.objectId, given, fall: open.fall };
|
|
3942
|
+
}
|
|
3943
|
+
if (share <= 1 || open.over[index]) return;
|
|
3944
|
+
open.over[index] = 1;
|
|
3945
|
+
tally.overs++;
|
|
3946
|
+
this.onOver?.({ rule, objectId: body.objectId, given, fall: open.fall });
|
|
3947
|
+
}
|
|
3948
|
+
tally() {
|
|
3949
|
+
return RULES.map((rule) => ({ rule, ...this.tallies.get(rule) }));
|
|
3950
|
+
}
|
|
3951
|
+
};
|
|
3952
|
+
|
|
3455
3953
|
// src/engine-ground-heights.ts
|
|
3456
3954
|
var changedHeights = (before, after, size) => {
|
|
3457
3955
|
let minX = size;
|
|
@@ -3847,6 +4345,10 @@ var TILE_KEY = 65536;
|
|
|
3847
4345
|
var TILE_KEY_ORIGIN = 32768;
|
|
3848
4346
|
var MU_STATIC = 0.7;
|
|
3849
4347
|
var ROLLING_RESISTANCE = 0.015;
|
|
4348
|
+
var roundPartsRadius = (parts) => {
|
|
4349
|
+
if (!parts || parts.length === 0 || !parts.every((part) => part.round && !part.hull)) return void 0;
|
|
4350
|
+
return Math.max(...parts.map((part) => (part.half[0] + part.half[2]) / 2));
|
|
4351
|
+
};
|
|
3850
4352
|
var MU_DYNAMIC = 0.6;
|
|
3851
4353
|
var GROUND = { frictionStatic: MU_STATIC, frictionDynamic: MU_DYNAMIC, restitution: 0, rolling: ROLLING_RESISTANCE };
|
|
3852
4354
|
var tileRollingAt = (rollingAt, x, z) => rollingAt((Math.floor(x / TILE) + 0.5) * TILE, (Math.floor(z / TILE) + 0.5) * TILE);
|
|
@@ -4395,9 +4897,13 @@ var EnginePieces = class {
|
|
|
4395
4897
|
out[round.axis] = about;
|
|
4396
4898
|
return out;
|
|
4397
4899
|
}
|
|
4398
|
-
/**
|
|
4900
|
+
/**
|
|
4901
|
+
* A round piece rolls to a stop by its surface's rolling resistance (the adapter's rule; the engine has none). Round is its shape: the `round` of the piece, or parts
|
|
4902
|
+
* that are all cylinders (a valve knob cut off a shower), which roll on the widest of them; without it such a knob rolled 9 m and was still rolling 8 s later.
|
|
4903
|
+
*/
|
|
4399
4904
|
rollingOf(b) {
|
|
4400
|
-
|
|
4905
|
+
const radius = b.round?.radius ?? roundPartsRadius(b.partBoxes);
|
|
4906
|
+
return radius === void 0 ? void 0 : { coefficient: b.friction?.rolling ?? ROLLING_RESISTANCE, radius, damping: b.friction?.rollingDamping ?? PIECE_ROLLING_DAMPING };
|
|
4401
4907
|
}
|
|
4402
4908
|
material(b) {
|
|
4403
4909
|
return {
|
|
@@ -4776,20 +5282,25 @@ var EnginePieces = class {
|
|
|
4776
5282
|
if (wx <= wz) {
|
|
4777
5283
|
const edge = dirX > 0 ? x1 : x0;
|
|
4778
5284
|
const dw = edge * dirX > 0 ? -dirX * this.topplePush(edge, height, isColumn && Math.abs(w[2]) < PUSH_SPIN, this.turningOf(b, 2)) * dt : 0;
|
|
5285
|
+
if (dw === 0) return;
|
|
4779
5286
|
const rx = -edge;
|
|
4780
5287
|
const ry = -low;
|
|
5288
|
+
this.o.kitEnergy?.begin(b);
|
|
4781
5289
|
w[2] += dw;
|
|
4782
5290
|
v[0] += -dw * ry;
|
|
4783
5291
|
v[1] += dw * rx;
|
|
4784
5292
|
} else {
|
|
4785
5293
|
const edge = dirZ > 0 ? z1 : z0;
|
|
4786
5294
|
const dw = edge * dirZ > 0 ? dirZ * this.topplePush(edge, height, isColumn && Math.abs(w[0]) < PUSH_SPIN, this.turningOf(b, 0)) * dt : 0;
|
|
5295
|
+
if (dw === 0) return;
|
|
4787
5296
|
const rz = -edge;
|
|
4788
5297
|
const ry = -low;
|
|
5298
|
+
this.o.kitEnergy?.begin(b);
|
|
4789
5299
|
w[0] += dw;
|
|
4790
5300
|
v[1] += -dw * rz;
|
|
4791
5301
|
v[2] += dw * ry;
|
|
4792
5302
|
}
|
|
5303
|
+
this.o.kitEnergy?.end(KIT_RULE.TOPPLE, b);
|
|
4793
5304
|
}
|
|
4794
5305
|
/** Propped pieces lose most of their friction until they move or sleep (the push only presses them into the prop: an energy-free way to end a wedge). */
|
|
4795
5306
|
slip(b, link, isSlipping) {
|
|
@@ -5354,46 +5865,6 @@ var resolvePhysics = (option) => {
|
|
|
5354
5865
|
return { loop: createPhysicsLoop(option), stepsItself: true };
|
|
5355
5866
|
};
|
|
5356
5867
|
|
|
5357
|
-
// src/support-hull.ts
|
|
5358
|
-
var cross = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
5359
|
-
function planHull(points) {
|
|
5360
|
-
const sorted = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
5361
|
-
if (sorted.length < 3) return sorted;
|
|
5362
|
-
const lower = [];
|
|
5363
|
-
for (const p of sorted) {
|
|
5364
|
-
while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0) lower.pop();
|
|
5365
|
-
lower.push(p);
|
|
5366
|
-
}
|
|
5367
|
-
const upper = [];
|
|
5368
|
-
for (let i = sorted.length - 1; i >= 0; i--) {
|
|
5369
|
-
const p = sorted[i];
|
|
5370
|
-
while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0) upper.pop();
|
|
5371
|
-
upper.push(p);
|
|
5372
|
-
}
|
|
5373
|
-
return [...lower.slice(0, -1), ...upper.slice(0, -1)];
|
|
5374
|
-
}
|
|
5375
|
-
function toSegment(p, a, b) {
|
|
5376
|
-
const dx = b[0] - a[0];
|
|
5377
|
-
const dz = b[1] - a[1];
|
|
5378
|
-
const length = dx * dx + dz * dz;
|
|
5379
|
-
const t = length > 0 ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dz) / length)) : 0;
|
|
5380
|
-
return Math.hypot(p[0] - a[0] - t * dx, p[1] - a[1] - t * dz);
|
|
5381
|
-
}
|
|
5382
|
-
function isOverSupport(points, centre, tolerance) {
|
|
5383
|
-
const hull = planHull(points);
|
|
5384
|
-
if (hull.length === 0) return false;
|
|
5385
|
-
if (hull.length === 1) return Math.hypot(centre[0] - hull[0][0], centre[1] - hull[0][1]) <= tolerance;
|
|
5386
|
-
let inside = hull.length >= 3;
|
|
5387
|
-
let nearest = Infinity;
|
|
5388
|
-
for (let i = 0; i < hull.length; i++) {
|
|
5389
|
-
const a = hull[i];
|
|
5390
|
-
const b = hull[(i + 1) % hull.length];
|
|
5391
|
-
if (hull.length >= 3 && cross(a, b, centre) < 0) inside = false;
|
|
5392
|
-
nearest = Math.min(nearest, toSegment(centre, a, b));
|
|
5393
|
-
}
|
|
5394
|
-
return inside || nearest <= tolerance;
|
|
5395
|
-
}
|
|
5396
|
-
|
|
5397
5868
|
// src/pieces.ts
|
|
5398
5869
|
var BALANCE_TOLERANCE = 0.01;
|
|
5399
5870
|
var MU_STATIC2 = 0.7;
|
|
@@ -5541,6 +6012,8 @@ var PieceSystem = class {
|
|
|
5541
6012
|
__publicField(this, "supportNormal", UP);
|
|
5542
6013
|
__publicField(this, "supportFriction", null);
|
|
5543
6014
|
__publicField(this, "supportBody", null);
|
|
6015
|
+
/** What each rule of the kit gave the pieces it pushed (`ruleImpulse`, and the engine's own `topple`). */
|
|
6016
|
+
__publicField(this, "kitEnergy");
|
|
5544
6017
|
/** A piece is asleep on both sides (the engine's too): nothing of it changes until it wakes. */
|
|
5545
6018
|
__publicField(this, "isSettled", (body) => this.engine.isSettled(body));
|
|
5546
6019
|
__publicField(this, "measureAwake", (body) => {
|
|
@@ -5574,7 +6047,9 @@ var PieceSystem = class {
|
|
|
5574
6047
|
this.loop = resolved.loop;
|
|
5575
6048
|
this.stepsItself = resolved.stepsItself;
|
|
5576
6049
|
this.trail = o.interpolate ? new PoseTrail() : null;
|
|
6050
|
+
this.kitEnergy = new KitEnergyLedger(o.onKitEnergy);
|
|
5577
6051
|
this.engine = new EnginePieces(THREE, resolved.loop.world, {
|
|
6052
|
+
kitEnergy: this.kitEnergy,
|
|
5578
6053
|
groundAt: o.terrainAt ?? o.groundAt,
|
|
5579
6054
|
...o.terrainRollingAt ? { groundRollingAt: o.terrainRollingAt } : {},
|
|
5580
6055
|
...o.terrainRollingDampingAt ? { groundRollingDampingAt: o.terrainRollingDampingAt } : {},
|
|
@@ -5916,6 +6391,20 @@ var PieceSystem = class {
|
|
|
5916
6391
|
b.still = 0;
|
|
5917
6392
|
b.calmAt = null;
|
|
5918
6393
|
}
|
|
6394
|
+
/** `impulse` given by a rule of the kit on its own, measured by its name (`kitEnergy`). */
|
|
6395
|
+
ruleImpulse(rule, b, impulse, point) {
|
|
6396
|
+
this.kitEnergy.begin(b);
|
|
6397
|
+
this.impulse(b, impulse, point);
|
|
6398
|
+
this.kitEnergy.end(rule, b);
|
|
6399
|
+
}
|
|
6400
|
+
/** Spin (rad/s, world axes x and z) added by a rule of the kit on its own, measured by its name (`kitEnergy`). */
|
|
6401
|
+
ruleSpin(rule, b, x, z) {
|
|
6402
|
+
this.kitEnergy.begin(b);
|
|
6403
|
+
b.angularVelocity[0] += x;
|
|
6404
|
+
b.angularVelocity[2] += z;
|
|
6405
|
+
b.sleeping = false;
|
|
6406
|
+
this.kitEnergy.end(rule, b);
|
|
6407
|
+
}
|
|
5919
6408
|
/** I^-1 * (x, y, z) in world space for a box with the body's half extents and mass (needs `this.m` to hold the body's rotation). */
|
|
5920
6409
|
mulInvInertia(b, x, y, z) {
|
|
5921
6410
|
this.m.makeRotationFromQuaternion(b.root.quaternion);
|
|
@@ -8835,7 +9324,8 @@ var UNDERMINED_RIM_RISE = 0.1;
|
|
|
8835
9324
|
var UNDERMINED_STAND = 0.4;
|
|
8836
9325
|
var UNDERMINED_KEEP = 0.6;
|
|
8837
9326
|
var SOLIDS_MAX = 64;
|
|
8838
|
-
var
|
|
9327
|
+
var UNDERMINED_FROUDE = 0.13;
|
|
9328
|
+
var GRAVITY2 = 9.81;
|
|
8839
9329
|
var DEBRIS_ID = "debris#";
|
|
8840
9330
|
var UNDERMINED_STILL = 0.05;
|
|
8841
9331
|
var SOLID_WELD = 1e-4;
|
|
@@ -9212,6 +9702,7 @@ var Renderer = class {
|
|
|
9212
9702
|
...o.groundWorkerFrames !== void 0 ? { groundWorkerFrames: o.groundWorkerFrames } : {},
|
|
9213
9703
|
...this.blastDebris ? { keep: { maxLive: this.blastDebris.maxLivePieces, far: this.blastDebris.far, ...o.focus ? { focus: o.focus } : {}, onFade: (b) => this.fadePiece(b) } } : {},
|
|
9214
9704
|
onHardLanding: (p, point, speed) => this.hardLanding(p, point, speed),
|
|
9705
|
+
...o.onKitEnergy ? { onKitEnergy: o.onKitEnergy } : {},
|
|
9215
9706
|
sweep: (p, from, to) => this.sweepPiece(p, from, to),
|
|
9216
9707
|
onSlam: (p, hit, speed) => this.slam(p, hit, speed),
|
|
9217
9708
|
// two pieces knocked together: each sounds as its material does (a can on a can, a bottle on a table's leg)
|
|
@@ -9663,6 +10154,10 @@ var Renderer = class {
|
|
|
9663
10154
|
const piece = this.pieceSys.get(objectId);
|
|
9664
10155
|
return piece && !piece.debris ? piece : null;
|
|
9665
10156
|
}
|
|
10157
|
+
/** What each rule of the kit that moves a piece by itself (`KIT_RULE`) gave the pieces it pushed, against their own fall (see `RendererOptions.onKitEnergy`). */
|
|
10158
|
+
kitEnergy() {
|
|
10159
|
+
return this.pieceSys.kitEnergy.tally();
|
|
10160
|
+
}
|
|
9666
10161
|
impulse(objectId, impulse, point) {
|
|
9667
10162
|
const p = this.pieceSys.get(objectId);
|
|
9668
10163
|
if (p) this.pieceSys.impulse(p, impulse, point);
|
|
@@ -10690,13 +11185,13 @@ var Renderer = class {
|
|
|
10690
11185
|
}
|
|
10691
11186
|
/**
|
|
10692
11187
|
* Plays the effect the event map names for a destruction event (`data/event-effects.json` of `game-vfx`, or `RendererOptions.eventEffects`) at a point.
|
|
10693
|
-
* `play` gives the family of the material (the map's `{material}`) and the colours of its debris and dust
|
|
10694
|
-
* leave out the burst it replaces.
|
|
11188
|
+
* `play` gives the family of the material (the map's `{material}`) and the colours of its debris and dust, `normal` the face it happened on (world). Answers
|
|
11189
|
+
* whether one played, so the caller can leave out the burst it replaces.
|
|
10695
11190
|
*/
|
|
10696
|
-
playEvent(event, point, play) {
|
|
11191
|
+
playEvent(event, point, play, normal) {
|
|
10697
11192
|
const id = effectForEvent(play && event.material === void 0 ? { ...event, material: play.material } : event, this.o.eventEffects ?? EVENT_EFFECTS_MAP);
|
|
10698
11193
|
if (!id) return false;
|
|
10699
|
-
return this.vfx.play(id, point, { seed: hashNumbers(point[0], point[1], point[2], this.time), ...play ? { values: play.values } : {} }) !== null;
|
|
11194
|
+
return this.vfx.play(id, point, { seed: hashNumbers(point[0], point[1], point[2], this.time), ...play ? { values: play.values } : {}, ...normal ? { normal } : {} }) !== null;
|
|
10700
11195
|
}
|
|
10701
11196
|
/** The family and colours of a part's material, for the effects of the event map. */
|
|
10702
11197
|
partPlay(objectId, partId) {
|
|
@@ -10716,7 +11211,7 @@ var Renderer = class {
|
|
|
10716
11211
|
const core = def?.visual?.innerColor;
|
|
10717
11212
|
const look = debrisLook(def, "spall", { partColor: this.partColor(pv, def) ?? void 0 });
|
|
10718
11213
|
if (core !== void 0 || look.impact === void 0) this.vfx.burst(look.debris, ev.point, [normal.x, normal.y, normal.z], 5, 2.5, { color: look.color });
|
|
10719
|
-
else this.playEvent({ kind: "spall", material: look.impact }, ev.point);
|
|
11214
|
+
else this.playEvent({ kind: "spall", material: look.impact }, ev.point, void 0, [normal.x, normal.y, normal.z]);
|
|
10720
11215
|
if (core !== void 0) this.trickleFrom(ev.point, [normal.x, normal.y, normal.z], core, mv);
|
|
10721
11216
|
if (mv.mesh.userData.drawnSpalls?.includes(ev.part)) return;
|
|
10722
11217
|
this.visuals.addSpall(mv, wp, normal, Math.sqrt(ev.area / Math.PI), core ?? fallbackColor(def));
|
|
@@ -11340,9 +11835,7 @@ var Renderer = class {
|
|
|
11340
11835
|
this.setContacts(b, b.piece ?? piece, left);
|
|
11341
11836
|
const loose = b.piece;
|
|
11342
11837
|
if (away && loose) {
|
|
11343
|
-
|
|
11344
|
-
loose.angularVelocity[2] -= away.x * EDGE_NUDGE;
|
|
11345
|
-
loose.sleeping = false;
|
|
11838
|
+
this.pieceSys.ruleSpin(KIT_RULE.LOOSEN_IF_FOOTLESS, loose, away.z * EDGE_NUDGE, -away.x * EDGE_NUDGE);
|
|
11346
11839
|
} else this.tipIfTopHeavy(b);
|
|
11347
11840
|
this.o.onLoosened?.(objectId, piece, "apart");
|
|
11348
11841
|
}
|
|
@@ -11438,7 +11931,7 @@ var Renderer = class {
|
|
|
11438
11931
|
body.sleeping = false;
|
|
11439
11932
|
body.still = 0;
|
|
11440
11933
|
body.calmAt = null;
|
|
11441
|
-
if (held.length > 0 && length > 1e-6) this.pieceSys.
|
|
11934
|
+
if (held.length > 0 && length > 1e-6) this.pieceSys.ruleImpulse(KIT_RULE.CHECK_UNDERMINED, body, [0, -body.mass * startDownSpeed(centre[1] - floor), 0], [centre[0] + gx, floor, centre[2] + gz]);
|
|
11442
11935
|
if (!was) this.o.onLoosened?.(b.id, body, "ground");
|
|
11443
11936
|
}
|
|
11444
11937
|
}
|
|
@@ -11455,7 +11948,8 @@ var Renderer = class {
|
|
|
11455
11948
|
if (this.standsWith(held, null, centre, feet.floor)) return;
|
|
11456
11949
|
const inside = insideBy(convexHull(held), [centre[0], centre[2]]);
|
|
11457
11950
|
const reach = this.reachAlong(body, inside.out.x, inside.out.z);
|
|
11458
|
-
this.pieceSys.
|
|
11951
|
+
const mid = this.pieceSys.centerOf(body, new this.THREE.Vector3());
|
|
11952
|
+
this.pieceSys.ruleImpulse(KIT_RULE.GO_OVER_IF_BALANCED, body, [0, -body.mass * startDownSpeed(centre[1] - feet.floor), 0], [mid.x + inside.out.x * reach, feet.floor, mid.z + inside.out.z * reach]);
|
|
11459
11953
|
}
|
|
11460
11954
|
/** How far (m) the box of a piece reaches from its middle along the level direction (x, z), turned as it is: the support of its box that way. */
|
|
11461
11955
|
reachAlong(body, x, z) {
|
|
@@ -12520,6 +13014,9 @@ function convexHull(points) {
|
|
|
12520
13014
|
};
|
|
12521
13015
|
return [...half(sorted), ...half([...sorted].reverse())];
|
|
12522
13016
|
}
|
|
13017
|
+
function startDownSpeed(height) {
|
|
13018
|
+
return UNDERMINED_FROUDE * Math.sqrt(GRAVITY2 * height);
|
|
13019
|
+
}
|
|
12523
13020
|
function centreOfBoxes(boxes) {
|
|
12524
13021
|
const sum = [0, 0, 0];
|
|
12525
13022
|
let total = 0;
|
|
@@ -13527,6 +14024,8 @@ export {
|
|
|
13527
14024
|
IMPACT_FAMILY_TAGS,
|
|
13528
14025
|
IMPACT_SOUNDS,
|
|
13529
14026
|
IMPACT_SOUND_KEYS,
|
|
14027
|
+
KIT_ENERGY_RESOLUTION,
|
|
14028
|
+
KIT_RULE,
|
|
13530
14029
|
MIN_PIECE_MASS_KG,
|
|
13531
14030
|
PhysicsOptionError,
|
|
13532
14031
|
ROCKET_MOTOR_LOOP,
|