@adatechnology/game-destruction-three 0.29.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +18 -11
- package/dist/index.global.js +139 -938
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +139 -938
- package/dist/index.js.map +1 -1
- package/package.json +5 -6
package/dist/index.global.js
CHANGED
|
@@ -13494,11 +13494,22 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
13494
13494
|
var sameBox = (a, b) => a === b || sameShape(a, b) && sameFriction(a.friction, b.friction);
|
|
13495
13495
|
var sameSolids = (a, b) => a.length === b.length && a.every((s, i) => sameBox(s, b[i]));
|
|
13496
13496
|
var NONE = /* @__PURE__ */ new Set();
|
|
13497
|
+
var MESH_ORIGIN = [0, 0, 0];
|
|
13498
|
+
var MESH_ROTATION = [0, 0, 0, 1];
|
|
13497
13499
|
var partOfKey = (key) => {
|
|
13498
13500
|
const colon = key.lastIndexOf(":");
|
|
13499
13501
|
return colon < 0 || !/^\d+$/.test(key.slice(colon + 1)) ? key : key.slice(0, colon);
|
|
13500
13502
|
};
|
|
13501
13503
|
var hasSize = (s) => s.half[0] > 0 && s.half[1] > 0 && s.half[2] > 0;
|
|
13504
|
+
var orientedReach = (s) => {
|
|
13505
|
+
const [x, y, z, w] = s.rotation;
|
|
13506
|
+
const row = (a, b, c) => Math.abs(a) * s.half[0] + Math.abs(b) * s.half[1] + Math.abs(c) * s.half[2];
|
|
13507
|
+
return [
|
|
13508
|
+
row(1 - 2 * (y * y + z * z), 2 * (x * y - z * w), 2 * (x * z + y * w)),
|
|
13509
|
+
row(2 * (x * y + z * w), 1 - 2 * (x * x + z * z), 2 * (y * z - x * w)),
|
|
13510
|
+
row(2 * (x * z - y * w), 2 * (y * z + x * w), 1 - 2 * (x * x + y * y))
|
|
13511
|
+
];
|
|
13512
|
+
};
|
|
13502
13513
|
var shapeOf = (s) => s.mesh ? { kind: "trimesh", vertices: s.mesh.vertices, indices: s.mesh.indices } : { kind: "box", half: s.half };
|
|
13503
13514
|
var EngineStatics = class {
|
|
13504
13515
|
constructor(world, material) {
|
|
@@ -13513,6 +13524,8 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
13513
13524
|
__publicField(this, "ownerOf", /* @__PURE__ */ new Map());
|
|
13514
13525
|
/** The part of the object each held body draws (the key without its mesh number): what a blow on the body hits. */
|
|
13515
13526
|
__publicField(this, "partOf", /* @__PURE__ */ new Map());
|
|
13527
|
+
/** The box around the meshes of each object that has any (min x, y, z then max x, y, z): the triangles are what the engine meets, but the world tests the volume the part is drawn in. */
|
|
13528
|
+
__publicField(this, "meshBounds", /* @__PURE__ */ new Map());
|
|
13516
13529
|
/** Changes whenever an answer of `covers` may have (an object came, went, or was fitted again). */
|
|
13517
13530
|
__publicField(this, "coverageRevision", 0);
|
|
13518
13531
|
__publicField(this, "unsure", NONE);
|
|
@@ -13541,6 +13554,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
13541
13554
|
if (now.has(id)) continue;
|
|
13542
13555
|
for (const h of f.held.values()) this.release(h, f.settled);
|
|
13543
13556
|
this.bodies.delete(id);
|
|
13557
|
+
this.meshBounds.delete(id);
|
|
13544
13558
|
this.coverageRevision++;
|
|
13545
13559
|
}
|
|
13546
13560
|
const budget = { triangles: TRIANGLES_PER_UPDATE };
|
|
@@ -13557,6 +13571,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
13557
13571
|
clear() {
|
|
13558
13572
|
for (const f of this.bodies.values()) for (const h of f.held.values()) this.drop(h.id);
|
|
13559
13573
|
this.bodies.clear();
|
|
13574
|
+
this.meshBounds.clear();
|
|
13560
13575
|
this.coverageRevision++;
|
|
13561
13576
|
this.changed.clear();
|
|
13562
13577
|
}
|
|
@@ -13613,6 +13628,22 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
13613
13628
|
}
|
|
13614
13629
|
f.waiting = waiting;
|
|
13615
13630
|
if (waiting === 0) f.settled = true;
|
|
13631
|
+
this.boundMeshes(f);
|
|
13632
|
+
}
|
|
13633
|
+
boundMeshes(f) {
|
|
13634
|
+
let bounds;
|
|
13635
|
+
for (const h of f.held.values()) {
|
|
13636
|
+
const s = h.solid;
|
|
13637
|
+
if (!s.mesh) continue;
|
|
13638
|
+
bounds ?? (bounds = new Float64Array([Infinity, Infinity, Infinity, -Infinity, -Infinity, -Infinity]));
|
|
13639
|
+
const reach2 = orientedReach(s);
|
|
13640
|
+
for (let axis2 = 0; axis2 < 3; axis2++) {
|
|
13641
|
+
bounds[axis2] = Math.min(bounds[axis2], s.center[axis2] - reach2[axis2]);
|
|
13642
|
+
bounds[axis2 + 3] = Math.max(bounds[axis2 + 3], s.center[axis2] + reach2[axis2]);
|
|
13643
|
+
}
|
|
13644
|
+
}
|
|
13645
|
+
if (bounds) this.meshBounds.set(f.objectId, bounds);
|
|
13646
|
+
else this.meshBounds.delete(f.objectId);
|
|
13616
13647
|
}
|
|
13617
13648
|
/** The held part takes the new solid: its shape where that changed, its surface where that did (only the friction: the same shape, no new one). */
|
|
13618
13649
|
reshape(h, solid) {
|
|
@@ -13621,7 +13652,8 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
13621
13652
|
if (!sameFriction(was.friction, solid.friction)) this.world.setMaterial(h.id, this.materialOf(solid));
|
|
13622
13653
|
this.changed.add(h.id);
|
|
13623
13654
|
if (sameShape(was, solid)) return;
|
|
13624
|
-
if (
|
|
13655
|
+
if (solid.mesh) this.world.setPose(h.id, MESH_ORIGIN, MESH_ROTATION, false);
|
|
13656
|
+
else this.world.setPose(h.id, solid.center, solid.rotation, false);
|
|
13625
13657
|
this.world.setShape(h.id, shapeOf(solid));
|
|
13626
13658
|
}
|
|
13627
13659
|
describe(solid) {
|
|
@@ -14264,6 +14296,26 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
14264
14296
|
var RAY_MARGIN = 0.25;
|
|
14265
14297
|
var LONGEST_RAY = 2e3;
|
|
14266
14298
|
var FIXED_ONLY = { layers: ["static"] };
|
|
14299
|
+
var segmentMeetsBox = (origin, direction, length, box) => {
|
|
14300
|
+
let enter = 0;
|
|
14301
|
+
let leave = length;
|
|
14302
|
+
for (let axis2 = 0; axis2 < 3; axis2++) {
|
|
14303
|
+
const low = box[axis2] - RAY_MARGIN;
|
|
14304
|
+
const high = box[axis2 + 3] + RAY_MARGIN;
|
|
14305
|
+
const step = direction[axis2];
|
|
14306
|
+
const start = origin[axis2];
|
|
14307
|
+
if (Math.abs(step) < 1e-12) {
|
|
14308
|
+
if (start < low || start > high) return false;
|
|
14309
|
+
continue;
|
|
14310
|
+
}
|
|
14311
|
+
const a = (low - start) / step;
|
|
14312
|
+
const b = (high - start) / step;
|
|
14313
|
+
enter = Math.max(enter, Math.min(a, b));
|
|
14314
|
+
leave = Math.min(leave, Math.max(a, b));
|
|
14315
|
+
if (enter > leave) return false;
|
|
14316
|
+
}
|
|
14317
|
+
return true;
|
|
14318
|
+
};
|
|
14267
14319
|
var createEngineRayBroadPhase = (world, statics) => {
|
|
14268
14320
|
const hits = [];
|
|
14269
14321
|
const inside = [];
|
|
@@ -14290,6 +14342,12 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
14290
14342
|
metList.push(owner);
|
|
14291
14343
|
}
|
|
14292
14344
|
}
|
|
14345
|
+
for (const [owner, bounds] of statics.meshBounds) {
|
|
14346
|
+
if (!met.has(owner) && segmentMeetsBox(origin, direction, maxDistance, bounds)) {
|
|
14347
|
+
met.add(owner);
|
|
14348
|
+
metList.push(owner);
|
|
14349
|
+
}
|
|
14350
|
+
}
|
|
14293
14351
|
const around = world.overlapSphere(from, RAY_MARGIN, inside, FIXED_ONLY);
|
|
14294
14352
|
for (let i = 0; i < around; i++) {
|
|
14295
14353
|
const owner = statics.ownerOf.get(inside[i]);
|
|
@@ -14369,7 +14427,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
14369
14427
|
var ROLLING_RESISTANCE = 0.015;
|
|
14370
14428
|
var MU_DYNAMIC = 0.6;
|
|
14371
14429
|
var GROUND = { frictionStatic: MU_STATIC, frictionDynamic: MU_DYNAMIC, restitution: 0, rolling: ROLLING_RESISTANCE };
|
|
14372
|
-
var groundTileKey = (x, z) => (Math.floor(x / TILE) + TILE_KEY_ORIGIN) * TILE_KEY + (Math.floor(z / TILE) + TILE_KEY_ORIGIN);
|
|
14373
14430
|
var tileRollingAt = (rollingAt, x, z) => rollingAt((Math.floor(x / TILE) + 0.5) * TILE, (Math.floor(z / TILE) + 0.5) * TILE);
|
|
14374
14431
|
var STILL_SPEED = 0.12;
|
|
14375
14432
|
var STILL_SPIN = 0.45;
|
|
@@ -14957,6 +15014,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
14957
15014
|
isSlipping: false
|
|
14958
15015
|
};
|
|
14959
15016
|
this.links.set(b, link);
|
|
15017
|
+
if (b.held) this.passThroughHolder(id, true);
|
|
14960
15018
|
this.byUser.set(user, b);
|
|
14961
15019
|
this.byBody.set(id, b);
|
|
14962
15020
|
this.bodyIds.add(id);
|
|
@@ -14967,14 +15025,16 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
14967
15025
|
bodyDesc(b, user) {
|
|
14968
15026
|
const c = this.centre(b);
|
|
14969
15027
|
const q = b.root.quaternion;
|
|
15028
|
+
const shape = this.shapeOf(b);
|
|
14970
15029
|
return {
|
|
14971
|
-
shape
|
|
15030
|
+
shape,
|
|
14972
15031
|
motion: b.held ? "kinematic" : "dynamic",
|
|
14973
15032
|
position: [c.x, c.y, c.z],
|
|
14974
15033
|
rotation: [q.x, q.y, q.z, q.w],
|
|
14975
15034
|
mass: b.mass,
|
|
14976
15035
|
inertia: this.inertiaOf(b),
|
|
14977
15036
|
centreAtOrigin: true,
|
|
15037
|
+
keepPartContacts: shape.kind === "compound" && shape.children.length > 1,
|
|
14978
15038
|
material: this.material(b),
|
|
14979
15039
|
layer: "debris",
|
|
14980
15040
|
// no air drag: the kit's pieces never had any (a round piece's rolling resistance is the adapter's, `rolling`)
|
|
@@ -14990,6 +15050,10 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
14990
15050
|
angularVelocity: [b.angularVelocity[0], b.angularVelocity[1], b.angularVelocity[2]]
|
|
14991
15051
|
};
|
|
14992
15052
|
}
|
|
15053
|
+
passThroughHolder(id, ignore) {
|
|
15054
|
+
const holder = this.o.heldIgnores?.();
|
|
15055
|
+
if (holder !== void 0) this.world.ignorePair(id, holder, ignore);
|
|
15056
|
+
}
|
|
14993
15057
|
/** Brings the body of a piece up to date with what changed on the piece since the last step (moved, pushed, picked up, put to sleep). */
|
|
14994
15058
|
sync(b, dt, h) {
|
|
14995
15059
|
const c = this.centre(b);
|
|
@@ -15000,6 +15064,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
15000
15064
|
if (b.held !== link.held) {
|
|
15001
15065
|
link.held = b.held;
|
|
15002
15066
|
this.world.setMotion(link.id, b.held ? "kinematic" : "dynamic");
|
|
15067
|
+
this.passThroughHolder(link.id, b.held);
|
|
15003
15068
|
}
|
|
15004
15069
|
if (b.held) {
|
|
15005
15070
|
this.world.moveKinematic(link.id, [c.x, c.y, c.z], [q.x, q.y, q.z, q.w], h);
|
|
@@ -15780,9 +15845,8 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
15780
15845
|
}
|
|
15781
15846
|
};
|
|
15782
15847
|
var resolvePhysics = (option) => {
|
|
15783
|
-
if (option === "own") return null;
|
|
15784
15848
|
if (option === void 0 || typeof option !== "object") {
|
|
15785
|
-
throw new PhysicsOptionError("the `physics` option is required: pass a PhysicsLoop
|
|
15849
|
+
throw new PhysicsOptionError("the `physics` option is required: pass a PhysicsLoop or a world from the host");
|
|
15786
15850
|
}
|
|
15787
15851
|
if (isPhysicsLoop(option)) return { loop: option, stepsItself: false };
|
|
15788
15852
|
if (typeof option.stepFixed !== "function") {
|
|
@@ -15832,44 +15896,20 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
15832
15896
|
}
|
|
15833
15897
|
|
|
15834
15898
|
// src/pieces.ts
|
|
15835
|
-
var SLAM_MIN_SPEED2 = 2;
|
|
15836
|
-
var SLAM_BOUNCE2 = 0.2;
|
|
15837
15899
|
var BALANCE_TOLERANCE = 0.01;
|
|
15838
|
-
var SLAM_FLOOR_NORMAL2 = 0.7;
|
|
15839
|
-
var SUB_DT = 1 / 120;
|
|
15840
15900
|
var MU_STATIC2 = 0.7;
|
|
15841
15901
|
var MU_DYNAMIC2 = 0.6;
|
|
15842
|
-
var SLIP_SPEED = 0.02;
|
|
15843
15902
|
var ROLLING_RESISTANCE2 = 0.015;
|
|
15844
|
-
var ROUND_LYING = Math.SQRT1_2;
|
|
15845
15903
|
var MIN_PIECE_HALF = 1e-3;
|
|
15846
|
-
var OWN_MIN_PIECE_HALF = 0.01;
|
|
15847
|
-
var OWN_MIN_MEASURED_HALF = 0.02;
|
|
15848
|
-
var minPieceHalf = (physics) => physics === "own" ? OWN_MIN_MEASURED_HALF : MIN_PIECE_HALF;
|
|
15849
15904
|
var SLEEP_AFTER = 0.5;
|
|
15850
15905
|
var STILL_SPEED2 = 0.12;
|
|
15851
15906
|
var STILL_SPIN2 = 0.45;
|
|
15852
|
-
var CALM_DRIFT = 0.01;
|
|
15853
|
-
var CALM_TURN = 0.05;
|
|
15854
|
-
var CALM_SLEEP_AFTER = 1.5;
|
|
15855
|
-
var STEADY_TURN2 = 0.1;
|
|
15856
15907
|
var STEADY_SECONDS2 = 1.5;
|
|
15857
|
-
var HARD_LANDING2 = 3;
|
|
15858
15908
|
var SLOP = 2e-3;
|
|
15859
|
-
var NARROW2 = 0.3;
|
|
15860
|
-
var SUPPORT_MARGIN = 0.05;
|
|
15861
|
-
var TOPPLE2 = 3;
|
|
15862
|
-
var TIP_ACCELERATION = 30;
|
|
15863
|
-
var TIP_RISING = 0.02;
|
|
15864
|
-
var TIP_RISE_AFTER = 0.5;
|
|
15865
|
-
var TIP_LYING = 0.25;
|
|
15866
15909
|
var TIP_SECONDS = 3;
|
|
15867
15910
|
var STEP = 0.03;
|
|
15868
15911
|
var WAKE_SAMPLES = 4;
|
|
15869
15912
|
var WAKE_DROP = 0.05;
|
|
15870
|
-
var WARM_SLOTS = 8 + 12 * 7;
|
|
15871
|
-
var SOLVER_PASSES = 10;
|
|
15872
|
-
var SIDE_BOUNCE = 0.1;
|
|
15873
15913
|
var EDGE_POINTS = [-0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75];
|
|
15874
15914
|
var EDGE_SPACING = 0.05;
|
|
15875
15915
|
var MAX_EDGE_POINTS = 31;
|
|
@@ -15886,15 +15926,9 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
15886
15926
|
var SLOPE_MIN_NORMAL = 0.5;
|
|
15887
15927
|
var LIES_FLAT = 0.985;
|
|
15888
15928
|
var KEEP_EVERY = 0.5;
|
|
15889
|
-
var WAKE_ABOVE_SPEED = 0.05;
|
|
15890
|
-
var WAKE_ABOVE_SPIN = 0.1;
|
|
15891
15929
|
var SURFACE_CELL = 0.5;
|
|
15892
15930
|
var SURFACE_CELLS_ACROSS = 24;
|
|
15893
15931
|
var cellKey = (ix, iz) => (ix + 32768) * 65536 + (iz + 32768);
|
|
15894
|
-
var MAX_COLLISION_STEPS = 8;
|
|
15895
|
-
var MIN_ROUND_RADIUS = 5e-3;
|
|
15896
|
-
var BOUNCE_MIN_SPEED = 0.1;
|
|
15897
|
-
var BOX_IMPACT_SPEED = 1.5;
|
|
15898
15932
|
var KNOCK_FLAT = 0.5;
|
|
15899
15933
|
var SEPARATE_SPEED = 1.5;
|
|
15900
15934
|
var LIFT_DEPTH = STEP;
|
|
@@ -15943,8 +15977,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
15943
15977
|
// both lose energy in the contact: a log on grass is held by its bark and by the blades
|
|
15944
15978
|
rollingDamping: (a?.rollingDamping ?? PIECE_ROLLING_DAMPING) + (b?.rollingDamping ?? GROUND_ROLLING_DAMPING)
|
|
15945
15979
|
});
|
|
15946
|
-
var pairRestitution = (a, b) => Math.sqrt((a ?? SLAM_BOUNCE2) * (b ?? SLAM_BOUNCE2));
|
|
15947
|
-
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)));
|
|
15948
15980
|
var PieceSystem = class {
|
|
15949
15981
|
constructor(THREE, o) {
|
|
15950
15982
|
__publicField(this, "THREE", THREE);
|
|
@@ -15953,19 +15985,14 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
15953
15985
|
__publicField(this, "time", 0);
|
|
15954
15986
|
__publicField(this, "generations", 0);
|
|
15955
15987
|
__publicField(this, "p");
|
|
15956
|
-
__publicField(this, "q");
|
|
15957
15988
|
__publicField(this, "dq");
|
|
15958
|
-
__publicField(this, "nextQ");
|
|
15959
15989
|
__publicField(this, "m");
|
|
15960
|
-
__publicField(this, "hingeM");
|
|
15961
15990
|
__publicField(this, "ax");
|
|
15962
15991
|
__publicField(this, "wakeBox");
|
|
15963
15992
|
__publicField(this, "solids", []);
|
|
15964
|
-
/** The engine stepping the pieces (`PieceSystemOptions.physics`)
|
|
15993
|
+
/** The engine stepping the pieces (`PieceSystemOptions.physics`). */
|
|
15965
15994
|
__publicField(this, "engine");
|
|
15966
|
-
/** The
|
|
15967
|
-
__publicField(this, "groundWorker");
|
|
15968
|
-
/** 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). */
|
|
15969
15996
|
__publicField(this, "loop");
|
|
15970
15997
|
__publicField(this, "stepsItself");
|
|
15971
15998
|
/** The phases' way out of the loop, and whether this frame's `prepare` ran (it runs once, in the first fixed step). */
|
|
@@ -16015,12 +16042,8 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16015
16042
|
__publicField(this, "supportNormal", UP16);
|
|
16016
16043
|
__publicField(this, "supportFriction", null);
|
|
16017
16044
|
__publicField(this, "supportBody", null);
|
|
16018
|
-
/**
|
|
16019
|
-
__publicField(this, "
|
|
16020
|
-
/** The terrain's surface by ground tile (`groundTileKey`) this update, null for the default: read once per tile, not per contact per substep. */
|
|
16021
|
-
__publicField(this, "surfaceByTile", /* @__PURE__ */ new Map());
|
|
16022
|
-
/** 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. */
|
|
16023
|
-
__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));
|
|
16024
16047
|
__publicField(this, "measureAwake", (body) => {
|
|
16025
16048
|
const rec = this.measured(body);
|
|
16026
16049
|
const from = rec.from;
|
|
@@ -16044,18 +16067,15 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16044
16067
|
if (rec.from !== from || rec.list.length !== count || rec.list[0] !== first) this.reshaped = true;
|
|
16045
16068
|
});
|
|
16046
16069
|
this.p = new THREE.Vector3();
|
|
16047
|
-
this.q = new THREE.Quaternion();
|
|
16048
16070
|
this.dq = new THREE.Quaternion();
|
|
16049
|
-
this.nextQ = new THREE.Quaternion();
|
|
16050
16071
|
this.m = new THREE.Matrix4();
|
|
16051
|
-
this.hingeM = new THREE.Matrix4();
|
|
16052
16072
|
this.ax = new THREE.Vector3();
|
|
16053
16073
|
this.wakeBox = new THREE.Box3();
|
|
16054
16074
|
const resolved = resolvePhysics(o.physics);
|
|
16055
|
-
this.loop = resolved
|
|
16056
|
-
this.stepsItself = resolved
|
|
16075
|
+
this.loop = resolved.loop;
|
|
16076
|
+
this.stepsItself = resolved.stepsItself;
|
|
16057
16077
|
this.trail = o.interpolate ? new PoseTrail() : null;
|
|
16058
|
-
this.engine =
|
|
16078
|
+
this.engine = new EnginePieces(THREE, resolved.loop.world, {
|
|
16059
16079
|
groundAt: o.terrainAt ?? o.groundAt,
|
|
16060
16080
|
...o.terrainRollingAt ? { groundRollingAt: o.terrainRollingAt } : {},
|
|
16061
16081
|
...o.terrainRollingDampingAt ? { groundRollingDampingAt: o.terrainRollingDampingAt } : {},
|
|
@@ -16072,15 +16092,15 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16072
16092
|
onKnock: o.onKnock,
|
|
16073
16093
|
onHardLanding: o.onHardLanding,
|
|
16074
16094
|
...o.tilesUnderParts ? { tilesUnderParts: true } : {},
|
|
16095
|
+
...o.heldIgnores ? { heldIgnores: o.heldIgnores } : {},
|
|
16075
16096
|
...o.groundWorker ? { groundWorker: o.groundWorker } : {},
|
|
16076
16097
|
...o.groundWorkerFrames !== void 0 ? { groundWorkerFrames: o.groundWorkerFrames } : {}
|
|
16077
|
-
})
|
|
16078
|
-
this.
|
|
16079
|
-
if (resolved && !resolved.stepsItself) this.joinLoop(resolved.loop, this.engine);
|
|
16098
|
+
});
|
|
16099
|
+
if (!resolved.stepsItself) this.joinLoop(resolved.loop, this.engine);
|
|
16080
16100
|
}
|
|
16081
|
-
/** Contact blows on the fixed parts become `blunt` hits of `destruction` (`engine-impacts.ts`)
|
|
16101
|
+
/** Contact blows on the fixed parts become `blunt` hits of `destruction` (`engine-impacts.ts`). Returns the way to stop it. */
|
|
16082
16102
|
contactHits(destruction, options) {
|
|
16083
|
-
return
|
|
16103
|
+
return createEngineImpacts(this.loop, this.engine.statics, destruction, options);
|
|
16084
16104
|
}
|
|
16085
16105
|
/**
|
|
16086
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
|
|
@@ -16088,7 +16108,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16088
16108
|
*/
|
|
16089
16109
|
piecesInBlast(blastQuery, point, radius) {
|
|
16090
16110
|
const engine = this.engine;
|
|
16091
|
-
if (!engine) return [...this.list];
|
|
16092
16111
|
const found = blastQuery.run({ origin: point, radius });
|
|
16093
16112
|
const reached = /* @__PURE__ */ new Set();
|
|
16094
16113
|
for (let i = 0; i < found.bodyCount; i++) {
|
|
@@ -16097,9 +16116,9 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16097
16116
|
}
|
|
16098
16117
|
return this.list.filter((piece) => reached.has(piece) || !engine.hasBody(piece));
|
|
16099
16118
|
}
|
|
16100
|
-
/** The engine's fixed bodies as the broad phase of the world's rays
|
|
16119
|
+
/** The engine's fixed bodies as the broad phase of the world's rays. */
|
|
16101
16120
|
rayBroadPhase() {
|
|
16102
|
-
return this.engine
|
|
16121
|
+
return this.engine.rayBroadPhase();
|
|
16103
16122
|
}
|
|
16104
16123
|
/**
|
|
16105
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
|
|
@@ -16149,8 +16168,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16149
16168
|
dispose() {
|
|
16150
16169
|
this.trail?.clear();
|
|
16151
16170
|
for (const leave of this.leaveLoop.splice(0)) leave();
|
|
16152
|
-
|
|
16153
|
-
else this.groundWorker?.dispose();
|
|
16171
|
+
this.engine.dispose();
|
|
16154
16172
|
}
|
|
16155
16173
|
/**
|
|
16156
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
|
|
@@ -16261,7 +16279,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16261
16279
|
return existing;
|
|
16262
16280
|
}
|
|
16263
16281
|
const awake = this.awake;
|
|
16264
|
-
const least =
|
|
16282
|
+
const least = MIN_PIECE_HALF;
|
|
16265
16283
|
let asleep = false;
|
|
16266
16284
|
let inHand = false;
|
|
16267
16285
|
const body = {
|
|
@@ -16302,13 +16320,13 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16302
16320
|
this.bornAt.set(body, this.time);
|
|
16303
16321
|
return body;
|
|
16304
16322
|
}
|
|
16305
|
-
/** 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.
|
|
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. */
|
|
16306
16324
|
stepCost() {
|
|
16307
|
-
return
|
|
16325
|
+
return { stepMs: this.loop.frame.stepMs, active: this.loop.world.activeCount };
|
|
16308
16326
|
}
|
|
16309
|
-
/** Bodies awake in the physics world
|
|
16327
|
+
/** Bodies awake in the physics world. */
|
|
16310
16328
|
activeBodies() {
|
|
16311
|
-
return this.loop
|
|
16329
|
+
return this.loop.world.activeCount;
|
|
16312
16330
|
}
|
|
16313
16331
|
/** Move the centre of mass (after a part broke off). */
|
|
16314
16332
|
setCenter(b, c) {
|
|
@@ -16328,12 +16346,12 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16328
16346
|
}
|
|
16329
16347
|
if (gone) {
|
|
16330
16348
|
this.forgetSolids(gone);
|
|
16331
|
-
this.engine
|
|
16349
|
+
this.engine.remove(gone);
|
|
16332
16350
|
}
|
|
16333
16351
|
}
|
|
16334
16352
|
clear() {
|
|
16335
16353
|
this.trail?.clear();
|
|
16336
|
-
this.engine
|
|
16354
|
+
this.engine.clear();
|
|
16337
16355
|
this.list.length = 0;
|
|
16338
16356
|
this.awake.clear();
|
|
16339
16357
|
this.layoutDirty = true;
|
|
@@ -16447,29 +16465,13 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16447
16465
|
}
|
|
16448
16466
|
return out;
|
|
16449
16467
|
}
|
|
16450
|
-
/**
|
|
16451
|
-
* 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.
|
|
16452
|
-
*/
|
|
16453
|
-
contactImpulse(b, e, r, n, bounce2) {
|
|
16454
|
-
const v = b.velocity;
|
|
16455
|
-
const w = b.angularVelocity;
|
|
16456
|
-
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];
|
|
16457
|
-
if (vn >= 0) return;
|
|
16458
|
-
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]);
|
|
16459
|
-
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];
|
|
16460
|
-
const j = -(1 + bounce2) * vn / (1 / b.mass + k);
|
|
16461
|
-
for (let i = 0; i < 3; i++) {
|
|
16462
|
-
v[i] += j * n[i] / b.mass;
|
|
16463
|
-
w[i] += a[i] * j;
|
|
16464
|
-
}
|
|
16465
|
-
}
|
|
16466
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`). */
|
|
16467
16469
|
layGroundAt(x, z, radius) {
|
|
16468
|
-
this.engine
|
|
16470
|
+
this.engine.layAhead(x, z, radius);
|
|
16469
16471
|
}
|
|
16470
16472
|
/** What lies inside the disc looks at the ground again: it was dug or heaped under it. */
|
|
16471
16473
|
wakeRegion(x, z, radius) {
|
|
16472
|
-
this.engine
|
|
16474
|
+
this.engine.groundChanged(x, z, radius);
|
|
16473
16475
|
for (const b of this.list) {
|
|
16474
16476
|
if (!b.sleeping || b.held) continue;
|
|
16475
16477
|
if (Math.hypot(b.root.position.x - x, b.root.position.z - z) > radius && !this.lostGroundIn(b, x, z, radius)) continue;
|
|
@@ -16512,7 +16514,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16512
16514
|
this.trail?.blend(alpha);
|
|
16513
16515
|
}
|
|
16514
16516
|
update(dt) {
|
|
16515
|
-
if (this.trail &&
|
|
16517
|
+
if (this.trail && this.stepsItself) {
|
|
16516
16518
|
this.beginTick();
|
|
16517
16519
|
this.advance(dt);
|
|
16518
16520
|
this.trail.commit();
|
|
@@ -16521,96 +16523,21 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16521
16523
|
this.advance(dt);
|
|
16522
16524
|
}
|
|
16523
16525
|
advance(dt) {
|
|
16524
|
-
if (this.
|
|
16525
|
-
|
|
16526
|
-
this.time = this.loop.frame.time;
|
|
16527
|
-
this.keepFar();
|
|
16528
|
-
return;
|
|
16529
|
-
}
|
|
16530
|
-
this.time += dt;
|
|
16531
|
-
this.engine.wakePending(this.list, this.awake);
|
|
16532
|
-
this.measure();
|
|
16533
|
-
this.wakeUnsupported(true);
|
|
16534
|
-
if (dt > 0) {
|
|
16535
|
-
this.engine.prepare(this.list, dt, dt, this.apartAll(dt), this.awake);
|
|
16536
|
-
this.loop.step(dt);
|
|
16537
|
-
this.engine.finish(this.list, dt, this.time, this.awake);
|
|
16538
|
-
}
|
|
16539
|
-
this.measure();
|
|
16540
|
-
this.keepBudget();
|
|
16526
|
+
if (!this.stepsItself) {
|
|
16527
|
+
this.time = this.loop.frame.time;
|
|
16541
16528
|
this.keepFar();
|
|
16542
16529
|
return;
|
|
16543
16530
|
}
|
|
16544
|
-
|
|
16545
|
-
|
|
16546
|
-
this.time += dt;
|
|
16547
|
-
this.step(dt);
|
|
16548
|
-
return;
|
|
16549
|
-
}
|
|
16550
|
-
const start = this.time;
|
|
16551
|
-
this.time = start + dt;
|
|
16552
|
-
const far2 = (b) => !near2.has(b);
|
|
16553
|
-
this.step(dt, { moves: far2, meets: (a, b) => far2(a) && far2(b) });
|
|
16554
|
-
for (let i = 0; i < steps; i++) {
|
|
16555
|
-
this.time = start + dt * (i + 1) / steps;
|
|
16556
|
-
this.step(dt / steps, { moves: (b) => near2.has(b), meets: (a, b) => near2.has(a) || near2.has(b) });
|
|
16557
|
-
}
|
|
16558
|
-
}
|
|
16559
|
-
/**
|
|
16560
|
-
* 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
|
|
16561
|
-
* 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).
|
|
16562
|
-
* 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.
|
|
16563
|
-
*/
|
|
16564
|
-
collisionSteps(dt) {
|
|
16565
|
-
let need = 1;
|
|
16566
|
-
const near2 = /* @__PURE__ */ new Set();
|
|
16567
|
-
const p = this.p;
|
|
16568
|
-
const speedOf = (b) => b.held ? 0 : Math.hypot(b.velocity[0], b.velocity[1], b.velocity[2]);
|
|
16569
|
-
for (const b of this.list) {
|
|
16570
|
-
if (!b.round || b.held) continue;
|
|
16571
|
-
const radius = Math.max(MIN_ROUND_RADIUS, b.round.radius);
|
|
16572
|
-
const speed = b.sleeping ? 0 : speedOf(b);
|
|
16573
|
-
const c = this.centerOf(b, p);
|
|
16574
|
-
const cx = c.x, cy = c.y, cz = c.z;
|
|
16575
|
-
const rb = Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]);
|
|
16576
|
-
for (const o of this.list) {
|
|
16577
|
-
if (o === b || b.sleeping && o.sleeping) continue;
|
|
16578
|
-
const closing = speed + (o.sleeping ? 0 : speedOf(o));
|
|
16579
|
-
if (closing * dt < radius * 0.5) continue;
|
|
16580
|
-
const reach2 = closing * dt + rb + Math.hypot(o.halfExtents[0], o.halfExtents[1], o.halfExtents[2]);
|
|
16581
|
-
const oc = this.centerOf(o, p);
|
|
16582
|
-
if (Math.abs(oc.x - cx) > reach2 || Math.abs(oc.y - cy) > reach2 || Math.abs(oc.z - cz) > reach2) continue;
|
|
16583
|
-
const pair = Math.ceil(closing * dt / radius);
|
|
16584
|
-
if (pair < 2) continue;
|
|
16585
|
-
need = Math.max(need, pair);
|
|
16586
|
-
near2.add(b);
|
|
16587
|
-
near2.add(o);
|
|
16588
|
-
}
|
|
16589
|
-
}
|
|
16590
|
-
return { steps: Math.max(1, Math.min(MAX_COLLISION_STEPS, need)), near: near2 };
|
|
16591
|
-
}
|
|
16592
|
-
/** 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. */
|
|
16593
|
-
step(dt, only) {
|
|
16594
|
-
this.surfaceByTile.clear();
|
|
16531
|
+
this.time += dt;
|
|
16532
|
+
this.engine.wakePending(this.list, this.awake);
|
|
16595
16533
|
this.measure();
|
|
16596
|
-
this.wakeUnsupported();
|
|
16597
|
-
|
|
16598
|
-
|
|
16599
|
-
|
|
16600
|
-
|
|
16601
|
-
b.sleeping = false;
|
|
16602
|
-
b.still = 0;
|
|
16603
|
-
b.calmAt = null;
|
|
16604
|
-
continue;
|
|
16605
|
-
}
|
|
16606
|
-
if (b.sleeping || only && !only.moves(b)) continue;
|
|
16607
|
-
this.integrate(b, dt);
|
|
16608
|
-
if (b.sleeping) continue;
|
|
16609
|
-
const v = b.velocity;
|
|
16610
|
-
const w = b.angularVelocity;
|
|
16611
|
-
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);
|
|
16612
16539
|
}
|
|
16613
|
-
this.
|
|
16540
|
+
this.measure();
|
|
16614
16541
|
this.keepBudget();
|
|
16615
16542
|
this.keepFar();
|
|
16616
16543
|
}
|
|
@@ -16761,29 +16688,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16761
16688
|
}
|
|
16762
16689
|
return top;
|
|
16763
16690
|
}
|
|
16764
|
-
/** 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. */
|
|
16765
|
-
wakeAbove(b) {
|
|
16766
|
-
const below = this.solidOf.get(b);
|
|
16767
|
-
if (!below) return;
|
|
16768
|
-
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]);
|
|
16769
|
-
const x0 = Math.floor((below.c[0] - below.r) / SURFACE_CELL);
|
|
16770
|
-
const x1 = Math.floor((below.c[0] + below.r) / SURFACE_CELL);
|
|
16771
|
-
const z0 = Math.floor((below.c[2] - below.r) / SURFACE_CELL);
|
|
16772
|
-
const z1 = Math.floor((below.c[2] + below.r) / SURFACE_CELL);
|
|
16773
|
-
const wide2 = x1 - x0 > SURFACE_CELLS_ACROSS || z1 - z0 > SURFACE_CELLS_ACROSS;
|
|
16774
|
-
const look = (part) => {
|
|
16775
|
-
const o = part.body;
|
|
16776
|
-
const s = this.solidOf.get(o);
|
|
16777
|
-
if (!s || !o.sleeping || o.held || o === b) return;
|
|
16778
|
-
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;
|
|
16779
|
-
if (boxTopAt(below, s.c[0], s.c[2]) === -Infinity && !this.restsOn(o, b, true)) return;
|
|
16780
|
-
o.sleeping = false;
|
|
16781
|
-
o.still = 0;
|
|
16782
|
-
o.calmAt = null;
|
|
16783
|
-
};
|
|
16784
|
-
if (wide2) for (const s of this.solids) look(s);
|
|
16785
|
-
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);
|
|
16786
|
-
}
|
|
16787
16691
|
/**
|
|
16788
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
|
|
16789
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.
|
|
@@ -16800,36 +16704,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16800
16704
|
}
|
|
16801
16705
|
return false;
|
|
16802
16706
|
}
|
|
16803
|
-
/**
|
|
16804
|
-
* 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
|
|
16805
|
-
* (`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
|
|
16806
|
-
* per update, and one object per value, so a substep makes none.
|
|
16807
|
-
*/
|
|
16808
|
-
terrainSurface(x, y, z, raw) {
|
|
16809
|
-
const rollingAt = this.o.terrainRollingAt;
|
|
16810
|
-
if (!rollingAt || y !== raw || this.o.terrainAt && y !== this.o.terrainAt(x, z)) return null;
|
|
16811
|
-
const key = groundTileKey(x, z);
|
|
16812
|
-
let surface = this.surfaceByTile.get(key);
|
|
16813
|
-
if (surface === void 0) {
|
|
16814
|
-
surface = this.tileSurface(rollingAt, x, z);
|
|
16815
|
-
this.surfaceByTile.set(key, surface);
|
|
16816
|
-
}
|
|
16817
|
-
return surface;
|
|
16818
|
-
}
|
|
16819
|
-
/** The terrain's surface of the tile at (x, z) (`tileRollingAt`), or null when it says nothing (the default surface). */
|
|
16820
|
-
tileSurface(rollingAt, x, z) {
|
|
16821
|
-
const dampingAt = this.o.terrainRollingDampingAt;
|
|
16822
|
-
const rolling = tileRollingAt(rollingAt, x, z);
|
|
16823
|
-
const rollingDamping = dampingAt ? tileRollingAt(dampingAt, x, z) : void 0;
|
|
16824
|
-
if (rolling === void 0 && rollingDamping === void 0) return null;
|
|
16825
|
-
const id = `${rolling ?? ROLLING_RESISTANCE2}|${rollingDamping ?? GROUND_ROLLING_DAMPING}`;
|
|
16826
|
-
let surface = this.terrainSurfaces.get(id);
|
|
16827
|
-
if (!surface) {
|
|
16828
|
-
surface = { static: MU_STATIC2, dynamic: MU_DYNAMIC2, rolling: rolling ?? ROLLING_RESISTANCE2, rollingDamping: rollingDamping ?? GROUND_ROLLING_DAMPING };
|
|
16829
|
-
this.terrainSurfaces.set(id, surface);
|
|
16830
|
-
}
|
|
16831
|
-
return surface;
|
|
16832
|
-
}
|
|
16833
16707
|
/** Where a corner of `self` at `(x, y, z)` lands: the ground, the top of another piece or of something fixed under it. */
|
|
16834
16708
|
supportAt(self, x, y, z, step) {
|
|
16835
16709
|
let top = this.o.groundAt(x, z);
|
|
@@ -16851,391 +16725,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
16851
16725
|
this.supportBody = on?.body ?? null;
|
|
16852
16726
|
return Math.max(top, fixed);
|
|
16853
16727
|
}
|
|
16854
|
-
/** 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. */
|
|
16855
|
-
sweepOn(b, start, pos) {
|
|
16856
|
-
const travel = Math.hypot(pos[0] - start[0], pos[1] - start[1], pos[2] - start[2]);
|
|
16857
|
-
if (travel < 1e-3) return;
|
|
16858
|
-
const hit = this.o.sweep?.(b, start, pos);
|
|
16859
|
-
if (!hit || hit.normal[1] > SLAM_FLOOR_NORMAL2) return;
|
|
16860
|
-
const v = b.velocity;
|
|
16861
|
-
const n = hit.normal;
|
|
16862
|
-
const into = -(v[0] * n[0] + v[1] * n[1] + v[2] * n[2]);
|
|
16863
|
-
if (into <= 0) return;
|
|
16864
|
-
const k = Math.min(1, hit.distance / travel);
|
|
16865
|
-
const bounce2 = pairRestitution(b.restitution, hit.restitution);
|
|
16866
|
-
for (let i = 0; i < 3; i++) {
|
|
16867
|
-
pos[i] = start[i] + (pos[i] - start[i]) * k;
|
|
16868
|
-
v[i] += (1 + bounce2) * into * n[i];
|
|
16869
|
-
}
|
|
16870
|
-
const w = b.angularVelocity;
|
|
16871
|
-
w[0] *= 0.5;
|
|
16872
|
-
w[1] *= 0.5;
|
|
16873
|
-
w[2] *= 0.5;
|
|
16874
|
-
if (into >= SLAM_MIN_SPEED2 && this.time - b.lastHit > 0.15) {
|
|
16875
|
-
b.lastHit = this.time;
|
|
16876
|
-
this.o.onSlam?.(b, hit, into);
|
|
16877
|
-
}
|
|
16878
|
-
}
|
|
16879
|
-
/**
|
|
16880
|
-
* 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
|
|
16881
|
-
* gravity gave the centre turns into a swing about the hinge.
|
|
16882
|
-
*/
|
|
16883
|
-
holdOnHinge(b, hinge, q) {
|
|
16884
|
-
const e = this.hingeM.makeRotationFromQuaternion(q).elements;
|
|
16885
|
-
const [lx, ly, lz] = hinge.local;
|
|
16886
|
-
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];
|
|
16887
|
-
const v = b.velocity;
|
|
16888
|
-
const w = b.angularVelocity;
|
|
16889
|
-
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]];
|
|
16890
|
-
const k = [0, 1, 2].map((i) => {
|
|
16891
|
-
const d = [i === 0 ? 1 : 0, i === 1 ? 1 : 0, i === 2 ? 1 : 0];
|
|
16892
|
-
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]);
|
|
16893
|
-
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]];
|
|
16894
|
-
});
|
|
16895
|
-
const j = solve3(k, [-point[0], -point[1], -point[2]]);
|
|
16896
|
-
if (!j) return;
|
|
16897
|
-
for (let i = 0; i < 3; i++) v[i] += j[i] / b.mass;
|
|
16898
|
-
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]);
|
|
16899
|
-
for (let i = 0; i < 3; i++) w[i] += dw[i];
|
|
16900
|
-
}
|
|
16901
|
-
/** Puts the hinge point back where it hangs (what the step's rounding moved it by), moving the centre. */
|
|
16902
|
-
keepOnHinge(hinge, pos, q) {
|
|
16903
|
-
const e = this.hingeM.makeRotationFromQuaternion(q).elements;
|
|
16904
|
-
const [lx, ly, lz] = hinge.local;
|
|
16905
|
-
pos[0] = hinge.point[0] - (e[0] * lx + e[4] * ly + e[8] * lz);
|
|
16906
|
-
pos[1] = hinge.point[1] - (e[1] * lx + e[5] * ly + e[9] * lz);
|
|
16907
|
-
pos[2] = hinge.point[2] - (e[2] * lx + e[6] * ly + e[10] * lz);
|
|
16908
|
-
}
|
|
16909
|
-
/** The screw tears out: past `HINGE_RELEASE` of turn, or after `HINGE_SECONDS`, the piece falls free. */
|
|
16910
|
-
wearHinge(b, hinge, q, dt) {
|
|
16911
|
-
hinge.seconds -= dt;
|
|
16912
|
-
const [x, y, z, w] = hinge.from;
|
|
16913
|
-
const turned = 2 * Math.acos(Math.min(1, Math.abs(q.x * x + q.y * y + q.z * z + q.w * w)));
|
|
16914
|
-
if (turned >= HINGE_RELEASE || hinge.seconds <= 0) b.hinge = void 0;
|
|
16915
|
-
}
|
|
16916
|
-
integrate(b, dt) {
|
|
16917
|
-
const c = this.centerOf(b, this.p);
|
|
16918
|
-
const pos = [c.x, c.y, c.z];
|
|
16919
|
-
const start = [c.x, c.y, c.z];
|
|
16920
|
-
const q = this.q.copy(b.root.quaternion);
|
|
16921
|
-
const v = b.velocity;
|
|
16922
|
-
const w = b.angularVelocity;
|
|
16923
|
-
const invM = 1 / b.mass;
|
|
16924
|
-
const steps = clamp6(Math.ceil(dt / SUB_DT), 1, 8);
|
|
16925
|
-
const h = dt / steps;
|
|
16926
|
-
let landing2 = 0;
|
|
16927
|
-
let landPoint = [pos[0], pos[1], pos[2]];
|
|
16928
|
-
let contactedLast = false;
|
|
16929
|
-
const propped = b.propped;
|
|
16930
|
-
const proppedAt = b.proppedAt;
|
|
16931
|
-
b.propped = void 0;
|
|
16932
|
-
b.proppedAt = void 0;
|
|
16933
|
-
let touched = false;
|
|
16934
|
-
let unstable = false;
|
|
16935
|
-
const corners = this.samples(b, false);
|
|
16936
|
-
this.m.makeRotationFromQuaternion(q);
|
|
16937
|
-
let gLow = Infinity;
|
|
16938
|
-
let gHigh = -Infinity;
|
|
16939
|
-
for (const [sx, sy, sz] of corners) {
|
|
16940
|
-
const e0 = this.m.elements;
|
|
16941
|
-
const y = pos[1] + e0[1] * sx + e0[5] * sy + e0[9] * sz;
|
|
16942
|
-
const x = pos[0] + e0[0] * sx + e0[4] * sy + e0[8] * sz;
|
|
16943
|
-
const z = pos[2] + e0[2] * sx + e0[6] * sy + e0[10] * sz;
|
|
16944
|
-
const g = Math.max(this.o.groundAt(x, z), this.o.staticTopAt?.(x, z, y, STEP) ?? -Infinity);
|
|
16945
|
-
gLow = Math.min(gLow, g);
|
|
16946
|
-
gHigh = Math.max(gHigh, g);
|
|
16947
|
-
}
|
|
16948
|
-
let local2 = b.round ? this.roundSamples(b, q) : gHigh - gLow > STEP ? this.samples(b, true) : corners;
|
|
16949
|
-
let prev = local2.map(() => [0, 0, 0]);
|
|
16950
|
-
for (let s = 0; s < steps; s++) {
|
|
16951
|
-
if (b.round) {
|
|
16952
|
-
const wl0 = Math.hypot(w[0], w[1], w[2]);
|
|
16953
|
-
const next = this.nextQ.copy(q);
|
|
16954
|
-
if (wl0 > 1e-6) next.premultiply(this.dq.setFromAxisAngle(this.ax.set(w[0] / wl0, w[1] / wl0, w[2] / wl0), wl0 * h)).normalize();
|
|
16955
|
-
local2 = this.roundSamples(b, next);
|
|
16956
|
-
if (prev.length !== local2.length) prev = local2.map(() => [0, 0, 0]);
|
|
16957
|
-
}
|
|
16958
|
-
this.m.makeRotationFromQuaternion(q);
|
|
16959
|
-
const e0 = this.m.elements;
|
|
16960
|
-
for (let i = 0; i < local2.length; i++) {
|
|
16961
|
-
const [sx, sy, sz] = local2[i];
|
|
16962
|
-
prev[i][0] = pos[0] + e0[0] * sx + e0[4] * sy + e0[8] * sz;
|
|
16963
|
-
prev[i][1] = pos[1] + e0[1] * sx + e0[5] * sy + e0[9] * sz;
|
|
16964
|
-
prev[i][2] = pos[2] + e0[2] * sx + e0[6] * sy + e0[10] * sz;
|
|
16965
|
-
}
|
|
16966
|
-
v[1] -= this.o.gravity * h;
|
|
16967
|
-
if (b.hinge) this.holdOnHinge(b, b.hinge, q);
|
|
16968
|
-
if (propped) {
|
|
16969
|
-
const into = v[0] * propped[0] + v[1] * propped[1] + v[2] * propped[2];
|
|
16970
|
-
if (into < 0) for (let k = 0; k < 3; k++) v[k] -= into * propped[k];
|
|
16971
|
-
}
|
|
16972
|
-
pos[0] += v[0] * h;
|
|
16973
|
-
pos[1] += v[1] * h;
|
|
16974
|
-
pos[2] += v[2] * h;
|
|
16975
|
-
const wl = Math.hypot(w[0], w[1], w[2]);
|
|
16976
|
-
if (wl > 1e-6) {
|
|
16977
|
-
this.dq.setFromAxisAngle(this.ax.set(w[0] / wl, w[1] / wl, w[2] / wl), wl * h);
|
|
16978
|
-
q.premultiply(this.dq).normalize();
|
|
16979
|
-
}
|
|
16980
|
-
if (b.hinge) this.keepOnHinge(b.hinge, pos, q);
|
|
16981
|
-
const damp = b.round ? 1 : Math.exp(-0.15 * h);
|
|
16982
|
-
w[0] *= damp;
|
|
16983
|
-
w[1] *= damp;
|
|
16984
|
-
w[2] *= damp;
|
|
16985
|
-
this.m.makeRotationFromQuaternion(q);
|
|
16986
|
-
const e = this.m.elements;
|
|
16987
|
-
const rs = [];
|
|
16988
|
-
const ids = [];
|
|
16989
|
-
const ns = [];
|
|
16990
|
-
const mus = [];
|
|
16991
|
-
let maxPen = 0;
|
|
16992
|
-
let penNormal = UP16;
|
|
16993
|
-
const rise = STEP + Math.max(0, -v[1]) * h * 2;
|
|
16994
|
-
let side = null;
|
|
16995
|
-
let lowest = Infinity;
|
|
16996
|
-
for (let i = 0; i < local2.length; i++) {
|
|
16997
|
-
const [sx, sy, sz] = local2[i];
|
|
16998
|
-
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];
|
|
16999
|
-
const px = pos[0] + r[0];
|
|
17000
|
-
const pz = pos[2] + r[2];
|
|
17001
|
-
const cy = pos[1] + r[1];
|
|
17002
|
-
const was = prev[i];
|
|
17003
|
-
const raw = this.o.groundAt(px, pz);
|
|
17004
|
-
if (raw > cy - SLOP && was[1] < raw - STEP) {
|
|
17005
|
-
if (this.o.groundAt(was[0], was[2]) >= raw - STEP) {
|
|
17006
|
-
const floor2 = b.floor;
|
|
17007
|
-
if (floor2 !== void 0 && floor2 - cy > -SLOP && floor2 < raw - STEP) {
|
|
17008
|
-
rs.push(r);
|
|
17009
|
-
ids.push(i);
|
|
17010
|
-
ns.push(UP16);
|
|
17011
|
-
mus.push(pairFriction(b.friction, b.round ? this.terrainSurface(px, floor2, pz, floor2) : null));
|
|
17012
|
-
maxPen = Math.max(maxPen, floor2 - cy);
|
|
17013
|
-
}
|
|
17014
|
-
continue;
|
|
17015
|
-
}
|
|
17016
|
-
const dx = px - was[0];
|
|
17017
|
-
const dz = pz - was[2];
|
|
17018
|
-
const len = Math.hypot(dx, dz);
|
|
17019
|
-
if (len > 1e-6) {
|
|
17020
|
-
let lo = 0;
|
|
17021
|
-
let hi = 1;
|
|
17022
|
-
for (let it = 0; it < 6; it++) {
|
|
17023
|
-
const mid = (lo + hi) / 2;
|
|
17024
|
-
if (this.o.groundAt(was[0] + dx * mid, was[2] + dz * mid) >= raw - STEP) hi = mid;
|
|
17025
|
-
else lo = mid;
|
|
17026
|
-
}
|
|
17027
|
-
const back = (1 - lo) * len + 1e-3;
|
|
17028
|
-
if (!side || back > side.back) side = { back, dir: [dx / len, dz / len], r };
|
|
17029
|
-
}
|
|
17030
|
-
continue;
|
|
17031
|
-
}
|
|
17032
|
-
this.supportNormal = UP16;
|
|
17033
|
-
this.supportFriction = null;
|
|
17034
|
-
const gy = i < 8 || b.contacts ? this.supportAt(b, px, cy, pz, rise) : Math.max(raw, this.o.staticTopAt?.(px, pz, cy, rise) ?? -Infinity);
|
|
17035
|
-
const pen = gy - cy;
|
|
17036
|
-
if (pen > -SLOP) {
|
|
17037
|
-
rs.push(r);
|
|
17038
|
-
ids.push(i);
|
|
17039
|
-
ns.push(this.supportNormal);
|
|
17040
|
-
mus.push(pairFriction(b.friction, this.supportFriction ?? (b.round ? this.terrainSurface(px, gy, pz, raw) : null)));
|
|
17041
|
-
if (pen > maxPen) {
|
|
17042
|
-
maxPen = pen;
|
|
17043
|
-
penNormal = this.supportNormal;
|
|
17044
|
-
}
|
|
17045
|
-
lowest = Math.min(lowest, gy);
|
|
17046
|
-
}
|
|
17047
|
-
}
|
|
17048
|
-
if (lowest < Infinity) b.floor = lowest;
|
|
17049
|
-
if (b.round && ns.length > 0) {
|
|
17050
|
-
const sum = [0, 0, 0];
|
|
17051
|
-
for (const one of ns) for (let k = 0; k < 3; k++) sum[k] += one[k];
|
|
17052
|
-
const len = Math.hypot(sum[0], sum[1], sum[2]);
|
|
17053
|
-
b.surfaceNormal = len > 1e-6 ? [sum[0] / len, sum[1] / len, sum[2] / len] : UP16;
|
|
17054
|
-
} else if (b.round && rs.length === 0) b.surfaceNormal = UP16;
|
|
17055
|
-
if (side) {
|
|
17056
|
-
pos[0] -= side.dir[0] * side.back;
|
|
17057
|
-
pos[2] -= side.dir[1] * side.back;
|
|
17058
|
-
this.contactImpulse(b, e, side.r, [-side.dir[0], 0, -side.dir[1]], SIDE_BOUNCE);
|
|
17059
|
-
}
|
|
17060
|
-
contactedLast = rs.length > 0;
|
|
17061
|
-
if (contactedLast) touched = true;
|
|
17062
|
-
if (!contactedLast) continue;
|
|
17063
|
-
const out = Math.max(0, maxPen - SLOP * 0.5) * penNormal[1];
|
|
17064
|
-
pos[0] += penNormal[0] * out;
|
|
17065
|
-
pos[1] += penNormal[1] * out;
|
|
17066
|
-
pos[2] += penNormal[2] * out;
|
|
17067
|
-
let impact3 = 0;
|
|
17068
|
-
for (const r of rs) impact3 = Math.max(impact3, -(v[1] + w[2] * r[0] - w[0] * r[2]));
|
|
17069
|
-
const restitution = impact3 > 2 ? 0.25 : 0;
|
|
17070
|
-
if (impact3 > landing2) {
|
|
17071
|
-
landing2 = impact3;
|
|
17072
|
-
let sx = 0;
|
|
17073
|
-
let sz = 0;
|
|
17074
|
-
for (const r of rs) {
|
|
17075
|
-
sx += r[0];
|
|
17076
|
-
sz += r[2];
|
|
17077
|
-
}
|
|
17078
|
-
const lx = pos[0] + sx / rs.length;
|
|
17079
|
-
const lz = pos[2] + sz / rs.length;
|
|
17080
|
-
landPoint = [lx, pos[1] - Math.max(...rs.map((r) => -r[1])), lz];
|
|
17081
|
-
}
|
|
17082
|
-
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]];
|
|
17083
|
-
const dot2 = (a, c2) => a[0] * c2[0] + a[1] * c2[1] + a[2] * c2[2];
|
|
17084
|
-
const goal = rs.map((r, k) => {
|
|
17085
|
-
const vn = dot2(pointV(r), ns[k]);
|
|
17086
|
-
return vn < 0 ? -restitution * vn : 0;
|
|
17087
|
-
});
|
|
17088
|
-
const slipping = rs.map((r, k) => {
|
|
17089
|
-
const pv = pointV(r);
|
|
17090
|
-
pv[1] += this.o.gravity * h;
|
|
17091
|
-
const vn = dot2(pv, ns[k]);
|
|
17092
|
-
return Math.hypot(pv[0] - vn * ns[k][0], pv[1] - vn * ns[k][1], pv[2] - vn * ns[k][2]) > SLIP_SPEED;
|
|
17093
|
-
});
|
|
17094
|
-
const vx0 = v[0];
|
|
17095
|
-
const vz0 = v[2];
|
|
17096
|
-
if (!b.warm || b.warm.length !== WARM_SLOTS * 4) b.warm = new Float64Array(WARM_SLOTS * 4);
|
|
17097
|
-
const warm = b.warm;
|
|
17098
|
-
const accN = ids.map((id) => id < WARM_SLOTS ? warm[id * 4] : 0);
|
|
17099
|
-
const accT = ids.map((id) => id < WARM_SLOTS ? [warm[id * 4 + 1], warm[id * 4 + 2], warm[id * 4 + 3]] : [0, 0, 0]);
|
|
17100
|
-
const push = (r, j) => {
|
|
17101
|
-
v[0] += j[0] * invM;
|
|
17102
|
-
v[1] += j[1] * invM;
|
|
17103
|
-
v[2] += j[2] * invM;
|
|
17104
|
-
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]);
|
|
17105
|
-
w[0] += dw[0];
|
|
17106
|
-
w[1] += dw[1];
|
|
17107
|
-
w[2] += dw[2];
|
|
17108
|
-
};
|
|
17109
|
-
const reach2 = (r, d) => {
|
|
17110
|
-
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]);
|
|
17111
|
-
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];
|
|
17112
|
-
};
|
|
17113
|
-
for (let k = 0; k < rs.length; k++) {
|
|
17114
|
-
const n = ns[k];
|
|
17115
|
-
const t = accT[k];
|
|
17116
|
-
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]]);
|
|
17117
|
-
}
|
|
17118
|
-
for (let it = 0; it < SOLVER_PASSES; it++) {
|
|
17119
|
-
for (let k = 0; k < rs.length; k++) {
|
|
17120
|
-
const r = rs[k];
|
|
17121
|
-
const n = ns[k];
|
|
17122
|
-
const vn = dot2(pointV(r), n);
|
|
17123
|
-
const total = Math.max(0, accN[k] + (goal[k] - vn) / reach2(r, n));
|
|
17124
|
-
const j = total - accN[k];
|
|
17125
|
-
accN[k] = total;
|
|
17126
|
-
if (j !== 0) push(r, [n[0] * j, n[1] * j, n[2] * j]);
|
|
17127
|
-
const pv = pointV(r);
|
|
17128
|
-
const pn = dot2(pv, n);
|
|
17129
|
-
const vt = [pv[0] - pn * n[0], pv[1] - pn * n[1], pv[2] - pn * n[2]];
|
|
17130
|
-
const speed = Math.hypot(vt[0], vt[1], vt[2]);
|
|
17131
|
-
const f = [accT[k][0], accT[k][1], accT[k][2]];
|
|
17132
|
-
if (speed > 1e-6) {
|
|
17133
|
-
const d = [vt[0] / speed, vt[1] / speed, vt[2] / speed];
|
|
17134
|
-
const jt = speed / reach2(r, d);
|
|
17135
|
-
f[0] -= d[0] * jt;
|
|
17136
|
-
f[1] -= d[1] * jt;
|
|
17137
|
-
f[2] -= d[2] * jt;
|
|
17138
|
-
} else if (f[0] === 0 && f[1] === 0 && f[2] === 0) continue;
|
|
17139
|
-
const most = (slipping[k] ? mus[k].dynamic : mus[k].static) * accN[k];
|
|
17140
|
-
const fl = Math.hypot(f[0], f[1], f[2]);
|
|
17141
|
-
if (fl > most) {
|
|
17142
|
-
f[0] *= most / fl;
|
|
17143
|
-
f[1] *= most / fl;
|
|
17144
|
-
f[2] *= most / fl;
|
|
17145
|
-
}
|
|
17146
|
-
const dj = [f[0] - accT[k][0], f[1] - accT[k][1], f[2] - accT[k][2]];
|
|
17147
|
-
accT[k] = f;
|
|
17148
|
-
if (dj[0] !== 0 || dj[1] !== 0 || dj[2] !== 0) push(r, dj);
|
|
17149
|
-
}
|
|
17150
|
-
}
|
|
17151
|
-
pos[0] += (v[0] - vx0) * h;
|
|
17152
|
-
pos[2] += (v[2] - vz0) * h;
|
|
17153
|
-
warm.fill(0);
|
|
17154
|
-
for (let k = 0; k < rs.length; k++) {
|
|
17155
|
-
const id = ids[k];
|
|
17156
|
-
if (id >= WARM_SLOTS) continue;
|
|
17157
|
-
warm[id * 4] = accN[k];
|
|
17158
|
-
warm[id * 4 + 1] = accT[k][0];
|
|
17159
|
-
warm[id * 4 + 2] = accT[k][1];
|
|
17160
|
-
warm[id * 4 + 3] = accT[k][2];
|
|
17161
|
-
}
|
|
17162
|
-
let x0 = Infinity, x1 = -Infinity, z0 = Infinity, z1 = -Infinity, low = 0;
|
|
17163
|
-
for (const r of rs) {
|
|
17164
|
-
x0 = Math.min(x0, r[0]);
|
|
17165
|
-
x1 = Math.max(x1, r[0]);
|
|
17166
|
-
z0 = Math.min(z0, r[2]);
|
|
17167
|
-
z1 = Math.max(z1, r[2]);
|
|
17168
|
-
low = Math.min(low, r[1]);
|
|
17169
|
-
}
|
|
17170
|
-
if (proppedAt) {
|
|
17171
|
-
x0 = Math.min(x0, proppedAt[0]);
|
|
17172
|
-
x1 = Math.max(x1, proppedAt[0]);
|
|
17173
|
-
z0 = Math.min(z0, proppedAt[2]);
|
|
17174
|
-
z1 = Math.max(z1, proppedAt[2]);
|
|
17175
|
-
}
|
|
17176
|
-
const height = -low;
|
|
17177
|
-
const wx = x1 - x0;
|
|
17178
|
-
const wz = z1 - z0;
|
|
17179
|
-
const pastX = x0 > -SUPPORT_MARGIN * wx || x1 < SUPPORT_MARGIN * wx;
|
|
17180
|
-
const pastZ = z0 > -SUPPORT_MARGIN * wz || z1 < SUPPORT_MARGIN * wz;
|
|
17181
|
-
unstable = !b.round && (Math.min(wx, wz) < NARROW2 * height || pastX || pastZ);
|
|
17182
|
-
if (b.tipping) {
|
|
17183
|
-
b.tipping.seconds -= h;
|
|
17184
|
-
const [hx, hy, hz2] = b.halfExtents;
|
|
17185
|
-
const upHalf = Math.abs(e[1]) * hx + Math.abs(e[5]) * hy + Math.abs(e[9]) * hz2;
|
|
17186
|
-
const rising = TIP_SECONDS - b.tipping.seconds > TIP_RISE_AFTER && v[1] > TIP_RISING;
|
|
17187
|
-
if (upHalf < TIP_LYING || b.tipping.seconds <= 0 || rising) b.tipping = null;
|
|
17188
|
-
else {
|
|
17189
|
-
w[0] += b.tipping.x * TIP_ACCELERATION * h;
|
|
17190
|
-
w[2] += b.tipping.z * TIP_ACCELERATION * h;
|
|
17191
|
-
unstable = true;
|
|
17192
|
-
}
|
|
17193
|
-
}
|
|
17194
|
-
if (unstable) {
|
|
17195
|
-
const dirX = x0 + x1 > 0 ? -1 : 1;
|
|
17196
|
-
const dirZ = z0 + z1 > 0 ? -1 : 1;
|
|
17197
|
-
const k = TOPPLE2 * Math.min(height, 1) * h;
|
|
17198
|
-
const overZ = pastX === pastZ ? wx <= wz : pastX;
|
|
17199
|
-
if (overZ) w[2] -= dirX * k;
|
|
17200
|
-
else w[0] += dirZ * k;
|
|
17201
|
-
}
|
|
17202
|
-
if (b.round) {
|
|
17203
|
-
let rolling = 0;
|
|
17204
|
-
let damping = 0;
|
|
17205
|
-
for (const mu of mus) {
|
|
17206
|
-
rolling = Math.max(rolling, mu.rolling ?? ROLLING_RESISTANCE2);
|
|
17207
|
-
damping = Math.max(damping, mu.rollingDamping ?? 0);
|
|
17208
|
-
}
|
|
17209
|
-
this.rollingResistance(b, q, w, h, { rolling, damping });
|
|
17210
|
-
} else {
|
|
17211
|
-
const rd = Math.exp((rs.length >= 3 ? -3 : -1.5) * h);
|
|
17212
|
-
w[0] *= rd;
|
|
17213
|
-
w[1] *= rd;
|
|
17214
|
-
w[2] *= rd;
|
|
17215
|
-
const fd = Math.exp(-0.3 * h);
|
|
17216
|
-
v[0] *= fd;
|
|
17217
|
-
v[2] *= fd;
|
|
17218
|
-
}
|
|
17219
|
-
}
|
|
17220
|
-
if (b.hinge) this.wearHinge(b, b.hinge, q, dt);
|
|
17221
|
-
if (this.o.sweep && !b.held) this.sweepOn(b, start, pos);
|
|
17222
|
-
b.root.quaternion.copy(q);
|
|
17223
|
-
const off2 = this.ax.copy(b.centerV).applyQuaternion(q);
|
|
17224
|
-
b.root.position.set(pos[0] - off2.x, pos[1] - off2.y, pos[2] - off2.z);
|
|
17225
|
-
b.root.updateMatrixWorld(true);
|
|
17226
|
-
b.onGround = contactedLast;
|
|
17227
|
-
if (landing2 > HARD_LANDING2 && this.time - b.lastHit > 0.15) {
|
|
17228
|
-
b.lastHit = this.time;
|
|
17229
|
-
this.o.onHardLanding(b, landPoint, landing2);
|
|
17230
|
-
}
|
|
17231
|
-
const still = touched && !unstable && Math.hypot(v[0], v[1], v[2]) < STILL_SPEED2 && Math.hypot(w[0], w[1], w[2]) < STILL_SPIN2;
|
|
17232
|
-
if (still) b.still += dt;
|
|
17233
|
-
else b.still = 0;
|
|
17234
|
-
b.balancing = unstable;
|
|
17235
|
-
this.keepsTilt(b, q, dt);
|
|
17236
|
-
const held = (touched || propped !== void 0) && !b.tipping;
|
|
17237
|
-
if (!b.hinge && (b.still >= SLEEP_AFTER || this.staysInPlace(b, held, pos, q, dt)) && !this.onSteepSupport(b)) this.sleep(b);
|
|
17238
|
-
}
|
|
17239
16728
|
/**
|
|
17240
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),
|
|
17241
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
|
|
@@ -17269,15 +16758,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
17269
16758
|
b.velocity[0] = b.velocity[1] = b.velocity[2] = 0;
|
|
17270
16759
|
b.angularVelocity[0] = b.angularVelocity[1] = b.angularVelocity[2] = 0;
|
|
17271
16760
|
}
|
|
17272
|
-
/** 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. */
|
|
17273
|
-
keepsTilt(b, q, dt) {
|
|
17274
|
-
const at3 = b.steady;
|
|
17275
|
-
if (at3 && turnBetween(at3.quaternion, q) <= STEADY_TURN2) {
|
|
17276
|
-
at3.seconds += dt;
|
|
17277
|
-
return;
|
|
17278
|
-
}
|
|
17279
|
-
b.steady = { quaternion: [q.x, q.y, q.z, q.w], seconds: 0 };
|
|
17280
|
-
}
|
|
17281
16761
|
/**
|
|
17282
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`),
|
|
17283
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
|
|
@@ -17388,118 +16868,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
17388
16868
|
const y = c.y + e[1] * sx + e[5] * sy + e[9] * sz;
|
|
17389
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;
|
|
17390
16870
|
}
|
|
17391
|
-
/** Whether the piece has kept to the same spot on the ground for `CALM_SLEEP_AFTER` (see `CALM_DRIFT`). */
|
|
17392
|
-
staysInPlace(b, touched, center, q, dt) {
|
|
17393
|
-
const at3 = b.calmAt;
|
|
17394
|
-
const turned = at3 ? turnBetween(at3.quaternion, q) : Infinity;
|
|
17395
|
-
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) {
|
|
17396
|
-
b.calmAt = touched ? { center: [center[0], center[1], center[2]], quaternion: [q.x, q.y, q.z, q.w] } : null;
|
|
17397
|
-
b.calm = 0;
|
|
17398
|
-
return false;
|
|
17399
|
-
}
|
|
17400
|
-
b.calm += dt;
|
|
17401
|
-
return b.calm >= CALM_SLEEP_AFTER;
|
|
17402
|
-
}
|
|
17403
|
-
/** Whether the centre `c` of `upper` is over the top of `lower`'s box (as it was at the start of the update). */
|
|
17404
|
-
onTopOf(upper, lower, c) {
|
|
17405
|
-
const s = this.solidOf.get(lower);
|
|
17406
|
-
if (!s) return false;
|
|
17407
|
-
const top = boxTopAt(s, c.x, c.z);
|
|
17408
|
-
return top > -Infinity && c.y >= top - 0.01;
|
|
17409
|
-
}
|
|
17410
|
-
/**
|
|
17411
|
-
* 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
|
|
17412
|
-
* 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.
|
|
17413
|
-
*/
|
|
17414
|
-
roundSamples(b, q) {
|
|
17415
|
-
const round2 = b.round;
|
|
17416
|
-
const rims = b.contacts ?? [];
|
|
17417
|
-
if (!round2) return rims;
|
|
17418
|
-
const e = this.m.makeRotationFromQuaternion(q).elements;
|
|
17419
|
-
const n = b.surfaceNormal ?? UP16;
|
|
17420
|
-
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])];
|
|
17421
|
-
if (round2.kind === "sphere") return [...rims, [down[0] * round2.radius, down[1] * round2.radius, down[2] * round2.radius]];
|
|
17422
|
-
const k = round2.axis;
|
|
17423
|
-
const across = [down[0], down[1], down[2]];
|
|
17424
|
-
across[k] = 0;
|
|
17425
|
-
const len = Math.hypot(across[0], across[1], across[2]);
|
|
17426
|
-
if (len < 1e-3) return rims;
|
|
17427
|
-
const lowest = (at3, radius) => {
|
|
17428
|
-
const p = [across[0] / len * radius, across[1] / len * radius, across[2] / len * radius];
|
|
17429
|
-
p[k] = at3;
|
|
17430
|
-
return p;
|
|
17431
|
-
};
|
|
17432
|
-
const half = b.halfExtents[k];
|
|
17433
|
-
const low = (round2.profile ?? [[-half, round2.radius], [half, round2.radius]]).map(([at3, radius]) => lowest(at3, radius));
|
|
17434
|
-
return len > ROUND_LYING ? low : [...rims, ...low];
|
|
17435
|
-
}
|
|
17436
|
-
/**
|
|
17437
|
-
* 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
|
|
17438
|
-
* 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
|
|
17439
|
-
* 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`,
|
|
17440
|
-
* the pair's) then takes exp(-d h) of what is left, as the grass or the sand drag more the faster it rolls.
|
|
17441
|
-
*/
|
|
17442
|
-
rollingResistance(b, q, w, h, { rolling, damping }) {
|
|
17443
|
-
const round2 = b.round;
|
|
17444
|
-
if (!round2) return;
|
|
17445
|
-
const e = this.m.makeRotationFromQuaternion(q).elements;
|
|
17446
|
-
const cylinder = round2.kind === "cylinder";
|
|
17447
|
-
const a = cylinder ? [e[round2.axis * 4], e[round2.axis * 4 + 1], e[round2.axis * 4 + 2]] : [0, 0, 0];
|
|
17448
|
-
const along2 = cylinder ? w[0] * a[0] + w[1] * a[1] + w[2] * a[2] : 0;
|
|
17449
|
-
const roll = cylinder ? [a[0] * along2, a[1] * along2, a[2] * along2] : [w[0], w[1], w[2]];
|
|
17450
|
-
const rest = [w[0] - roll[0], w[1] - roll[1], w[2] - roll[2]];
|
|
17451
|
-
const spin = Math.hypot(roll[0], roll[1], roll[2]);
|
|
17452
|
-
const upright = b.surfaceNormal?.[1] ?? 1;
|
|
17453
|
-
const slow = spin > 0 ? Math.max(0, spin - rolling * this.o.gravity * upright * h / (round2.inertia * round2.radius)) / spin * Math.exp(-damping * h) : 0;
|
|
17454
|
-
const lying = cylinder && Math.abs(a[1]) < ROUND_LYING;
|
|
17455
|
-
const damp = lying ? 1 : Math.exp(-3 * h);
|
|
17456
|
-
for (let i = 0; i < 3; i++) w[i] = roll[i] * slow + rest[i] * damp;
|
|
17457
|
-
}
|
|
17458
|
-
/** Half the height of the box of a piece as it is turned now. */
|
|
17459
|
-
halfHeight(b) {
|
|
17460
|
-
const e = this.m.makeRotationFromQuaternion(b.root.quaternion).elements;
|
|
17461
|
-
return Math.abs(e[1]) * b.halfExtents[0] + Math.abs(e[5]) * b.halfExtents[1] + Math.abs(e[9]) * b.halfExtents[2];
|
|
17462
|
-
}
|
|
17463
|
-
/**
|
|
17464
|
-
* 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
|
|
17465
|
-
* 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`).
|
|
17466
|
-
*/
|
|
17467
|
-
collide(dt, meets) {
|
|
17468
|
-
const n = this.list.length;
|
|
17469
|
-
if (n < 2) return;
|
|
17470
|
-
const centers = this.list.map((b) => {
|
|
17471
|
-
const c = this.centerOf(b, this.p);
|
|
17472
|
-
return [c.x, c.y, c.z];
|
|
17473
|
-
});
|
|
17474
|
-
const radii = this.list.map((b) => Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]));
|
|
17475
|
-
const order2 = this.list.map((_, i) => i).sort((i, j) => centers[i][0] - radii[i] - (centers[j][0] - radii[j]));
|
|
17476
|
-
for (let oi = 0; oi < n; oi++) {
|
|
17477
|
-
const i = order2[oi];
|
|
17478
|
-
const reach2 = centers[i][0] + radii[i];
|
|
17479
|
-
for (let oj = oi + 1; oj < n; oj++) {
|
|
17480
|
-
const j = order2[oj];
|
|
17481
|
-
if (centers[j][0] - radii[j] > reach2) break;
|
|
17482
|
-
const a = this.list[i];
|
|
17483
|
-
const b = this.list[j];
|
|
17484
|
-
if ((a.held || a.sleeping) && (b.held || b.sleeping)) continue;
|
|
17485
|
-
if (meets && !meets(a, b)) continue;
|
|
17486
|
-
const r = radii[i] + radii[j];
|
|
17487
|
-
const ca = centers[i];
|
|
17488
|
-
const cb = centers[j];
|
|
17489
|
-
if (Math.abs(ca[1] - cb[1]) > r || Math.abs(ca[2] - cb[2]) > r) continue;
|
|
17490
|
-
const stacked = !a.round && !b.round && Math.abs(ca[1] - cb[1]) >= (this.halfHeight(a) + this.halfHeight(b)) * 0.9;
|
|
17491
|
-
const onTop = this.onTopOf(b, a, new this.THREE.Vector3(...cb)) || this.onTopOf(a, b, new this.THREE.Vector3(...ca));
|
|
17492
|
-
if (a.round || b.round) {
|
|
17493
|
-
if (!stacked && !onTop) this.knock(a, b, ca, cb);
|
|
17494
|
-
continue;
|
|
17495
|
-
}
|
|
17496
|
-
if (!this.partsOverlap(a, b)) continue;
|
|
17497
|
-
this.boxImpact(a, b, ca, cb, dt);
|
|
17498
|
-
this.separatePair(a, b, ca, cb, dt, stacked || onTop || this.restsOn(a, b, false) || this.restsOn(b, a, false));
|
|
17499
|
-
}
|
|
17500
|
-
}
|
|
17501
|
-
this.applyPushes(dt);
|
|
17502
|
-
}
|
|
17503
16871
|
/**
|
|
17504
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
|
|
17505
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.
|
|
@@ -17534,129 +16902,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
17534
16902
|
last.push[2] = push[2];
|
|
17535
16903
|
return last.share;
|
|
17536
16904
|
}
|
|
17537
|
-
/** 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. */
|
|
17538
|
-
contactShape(b, c) {
|
|
17539
|
-
const e = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
|
|
17540
|
-
const round2 = b.round;
|
|
17541
|
-
if (round2) {
|
|
17542
|
-
if (round2.kind === "sphere") return { kind: "capsule", from: c, to: c, radius: round2.radius };
|
|
17543
|
-
const reach2 = Math.max(0, b.halfExtents[round2.axis] - round2.radius);
|
|
17544
|
-
const k = round2.axis * 4;
|
|
17545
|
-
const d = [e[k] * reach2, e[k + 1] * reach2, e[k + 2] * reach2];
|
|
17546
|
-
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 };
|
|
17547
|
-
}
|
|
17548
|
-
const parts = b.partBoxes ?? [{ center: [0, 0, 0], half: b.halfExtents }];
|
|
17549
|
-
return {
|
|
17550
|
-
kind: "boxes",
|
|
17551
|
-
boxes: parts.map(squareBoxOf).map(({ center, half }) => ({
|
|
17552
|
-
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]],
|
|
17553
|
-
e,
|
|
17554
|
-
half
|
|
17555
|
-
}))
|
|
17556
|
-
};
|
|
17557
|
-
}
|
|
17558
|
-
/**
|
|
17559
|
-
* 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
|
|
17560
|
-
* 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
|
|
17561
|
-
* 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.
|
|
17562
|
-
*/
|
|
17563
|
-
knock(a, b, ca, cb) {
|
|
17564
|
-
const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
|
|
17565
|
-
if (!hit) return;
|
|
17566
|
-
if (Math.abs(hit.normal[1]) > SLAM_FLOOR_NORMAL2) return;
|
|
17567
|
-
this.impact({ a, b, ca, cb, hit, apart: true, above: 0 });
|
|
17568
|
-
}
|
|
17569
|
-
/**
|
|
17570
|
-
* 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,
|
|
17571
|
-
* 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
|
|
17572
|
-
* made inside each other, are left to `separatePair`, which holds or slides them without a kick.
|
|
17573
|
-
*/
|
|
17574
|
-
boxImpact(a, b, ca, cb, dt) {
|
|
17575
|
-
const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
|
|
17576
|
-
if (!hit) return;
|
|
17577
|
-
const n = hit.normal;
|
|
17578
|
-
const va = a.velocity;
|
|
17579
|
-
const vb = b.velocity;
|
|
17580
|
-
const closing = (va[0] - vb[0]) * n[0] + (va[1] - vb[1]) * n[1] + (va[2] - vb[2]) * n[2];
|
|
17581
|
-
if (hit.depth > LIFT_DEPTH + 2 * Math.max(0, -closing) * dt) return;
|
|
17582
|
-
this.impact({ a, b, ca, cb, hit, apart: false, above: BOX_IMPACT_SPEED });
|
|
17583
|
-
}
|
|
17584
|
-
/** The impulse two pieces that met exchange (`knock`), put apart first when `apart`, and only when they close faster than `above` (m/s). */
|
|
17585
|
-
impact({ a, b, ca, cb, hit, apart: apart2, above: above2 }) {
|
|
17586
|
-
const n = hit.normal;
|
|
17587
|
-
const ra = [hit.point[0] - ca[0], hit.point[1] - ca[1], hit.point[2] - ca[2]];
|
|
17588
|
-
const rb = [hit.point[0] - cb[0], hit.point[1] - cb[1], hit.point[2] - cb[2]];
|
|
17589
|
-
const pointV = (body, r) => {
|
|
17590
|
-
const v2 = body.velocity;
|
|
17591
|
-
const w = body.angularVelocity;
|
|
17592
|
-
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]];
|
|
17593
|
-
};
|
|
17594
|
-
const relative = () => {
|
|
17595
|
-
const va = pointV(a, ra);
|
|
17596
|
-
const vb = pointV(b, rb);
|
|
17597
|
-
return [va[0] - vb[0], va[1] - vb[1], va[2] - vb[2]];
|
|
17598
|
-
};
|
|
17599
|
-
const vn0 = relative();
|
|
17600
|
-
const closing = vn0[0] * n[0] + vn0[1] * n[1] + vn0[2] * n[2];
|
|
17601
|
-
if (closing >= -above2) return;
|
|
17602
|
-
const wake = (body) => {
|
|
17603
|
-
body.sleeping = false;
|
|
17604
|
-
body.still = 0;
|
|
17605
|
-
body.calmAt = null;
|
|
17606
|
-
};
|
|
17607
|
-
if (closing < -WAKE_ABOVE_SPEED) {
|
|
17608
|
-
if (a.sleeping) wake(a);
|
|
17609
|
-
if (b.sleeping) wake(b);
|
|
17610
|
-
}
|
|
17611
|
-
const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
|
|
17612
|
-
const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
|
|
17613
|
-
if (ima + imb === 0) return;
|
|
17614
|
-
const ea = [...this.m.makeRotationFromQuaternion(a.root.quaternion).elements];
|
|
17615
|
-
const eb = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
|
|
17616
|
-
const flat = Math.hypot(n[0], n[2]);
|
|
17617
|
-
const out = flat > KNOCK_FLAT ? [n[0] / flat, 0, n[2] / flat] : n;
|
|
17618
|
-
const depth = Math.max(0, hit.depth - SLOP) / (flat > KNOCK_FLAT ? flat : 1);
|
|
17619
|
-
for (const [body, share, sign] of [[a, ima, 1], [b, imb, -1]]) {
|
|
17620
|
-
if (share === 0 || !apart2) continue;
|
|
17621
|
-
const move = depth * share * sign / (ima + imb);
|
|
17622
|
-
body.root.position.x += out[0] * move;
|
|
17623
|
-
body.root.position.y += out[1] * move;
|
|
17624
|
-
body.root.position.z += out[2] * move;
|
|
17625
|
-
}
|
|
17626
|
-
if (closing >= 0) return;
|
|
17627
|
-
const turning = (body, e, r, d) => {
|
|
17628
|
-
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]);
|
|
17629
|
-
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];
|
|
17630
|
-
};
|
|
17631
|
-
const resist = (d) => ima + imb + (ima > 0 ? turning(a, ea, ra, d) : 0) + (imb > 0 ? turning(b, eb, rb, d) : 0);
|
|
17632
|
-
const give = (j2) => {
|
|
17633
|
-
if (ima > 0) {
|
|
17634
|
-
for (let i = 0; i < 3; i++) a.velocity[i] += j2[i] * ima;
|
|
17635
|
-
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]);
|
|
17636
|
-
for (let i = 0; i < 3; i++) a.angularVelocity[i] += dw[i];
|
|
17637
|
-
}
|
|
17638
|
-
if (imb > 0) {
|
|
17639
|
-
for (let i = 0; i < 3; i++) b.velocity[i] -= j2[i] * imb;
|
|
17640
|
-
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]);
|
|
17641
|
-
for (let i = 0; i < 3; i++) b.angularVelocity[i] -= dw[i];
|
|
17642
|
-
}
|
|
17643
|
-
};
|
|
17644
|
-
const bounce2 = apart2 && -closing > BOUNCE_MIN_SPEED ? pairRestitution(a.restitution, b.restitution) : 0;
|
|
17645
|
-
const j = -(1 + bounce2) * closing / resist(n);
|
|
17646
|
-
give([n[0] * j, n[1] * j, n[2] * j]);
|
|
17647
|
-
const v = relative();
|
|
17648
|
-
const vn = v[0] * n[0] + v[1] * n[1] + v[2] * n[2];
|
|
17649
|
-
const vt = [v[0] - vn * n[0], v[1] - vn * n[1], v[2] - vn * n[2]];
|
|
17650
|
-
const slide = Math.hypot(vt[0], vt[1], vt[2]);
|
|
17651
|
-
if (slide < 1e-6) return;
|
|
17652
|
-
const t = [vt[0] / slide, vt[1] / slide, vt[2] / slide];
|
|
17653
|
-
const jt = Math.min(slide / resist(t), pairFriction(a.friction, b.friction ?? null).dynamic * j);
|
|
17654
|
-
give([-t[0] * jt, -t[1] * jt, -t[2] * jt]);
|
|
17655
|
-
if (-closing >= SLAM_MIN_SPEED2 && this.time - Math.max(a.lastHit, b.lastHit) > 0.15) {
|
|
17656
|
-
a.lastHit = b.lastHit = this.time;
|
|
17657
|
-
this.o.onKnock?.(a, b, hit.point, -closing);
|
|
17658
|
-
}
|
|
17659
|
-
}
|
|
17660
16905
|
/** Whether a part of `a` and a part of `b` (their boxes of the step, as world-aligned boxes around them) overlap. */
|
|
17661
16906
|
partsOverlap(a, b) {
|
|
17662
16907
|
for (const pa of this.surfacesOf.get(a) ?? []) {
|
|
@@ -17668,66 +16913,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
17668
16913
|
}
|
|
17669
16914
|
return false;
|
|
17670
16915
|
}
|
|
17671
|
-
/**
|
|
17672
|
-
* 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
|
|
17673
|
-
* summed into `pushes` and bounded per piece in `applyPushes`.
|
|
17674
|
-
* - 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.
|
|
17675
|
-
* - 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
|
|
17676
|
-
* 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.
|
|
17677
|
-
* - 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
|
|
17678
|
-
* 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`).
|
|
17679
|
-
*/
|
|
17680
|
-
separatePair(a, b, ca, cb, dt, resting) {
|
|
17681
|
-
const sa = this.contactShape(a, ca);
|
|
17682
|
-
const sb = this.contactShape(b, cb);
|
|
17683
|
-
const hit = shapeContact(sa, sb);
|
|
17684
|
-
if (!hit) return;
|
|
17685
|
-
const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
|
|
17686
|
-
const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
|
|
17687
|
-
if (ima + imb === 0) return;
|
|
17688
|
-
if (Math.hypot(hit.normal[0], hit.normal[2]) > KNOCK_FLAT) {
|
|
17689
|
-
this.slideApart(a, b, ima, imb, levelBoxesApart(sa, sb) ?? hit);
|
|
17690
|
-
return;
|
|
17691
|
-
}
|
|
17692
|
-
const n = hit.normal;
|
|
17693
|
-
const va = a.velocity;
|
|
17694
|
-
const vb = b.velocity;
|
|
17695
|
-
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;
|
|
17696
|
-
if (hit.depth > contact) {
|
|
17697
|
-
const level = boxesApartAlongGround(sa, sb);
|
|
17698
|
-
if (level) this.slideApart(a, b, ima, imb, level);
|
|
17699
|
-
return;
|
|
17700
|
-
}
|
|
17701
|
-
if (resting) return;
|
|
17702
|
-
const [upper, c, up] = n[1] > 0 ? [a, ca, n] : [b, cb, [-n[0], -n[1], -n[2]]];
|
|
17703
|
-
if (upper.held || upper.sleeping) return;
|
|
17704
|
-
const otherCentre = upper === a ? cb : ca;
|
|
17705
|
-
if (c[1] < otherCentre[1] - SLOP) {
|
|
17706
|
-
const level = boxesApartAlongGround(sa, sb);
|
|
17707
|
-
if (level) this.slideApart(a, b, ima, imb, level);
|
|
17708
|
-
return;
|
|
17709
|
-
}
|
|
17710
|
-
upper.propped = up;
|
|
17711
|
-
const e = this.m.makeRotationFromQuaternion(upper.root.quaternion).elements;
|
|
17712
|
-
const r = [hit.point[0] - c[0], hit.point[1] - c[1], hit.point[2] - c[2]];
|
|
17713
|
-
upper.proppedAt = r;
|
|
17714
|
-
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]);
|
|
17715
|
-
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];
|
|
17716
|
-
const lift = SEPARATE_SHARE * Math.max(0, hit.depth - SLOP) / (1 + angular * upper.mass);
|
|
17717
|
-
const push = this.pushes.get(upper) ?? [0, 0, 0];
|
|
17718
|
-
for (let i = 0; i < 3; i++) push[i] = Math.abs(up[i] * lift) > Math.abs(push[i]) ? up[i] * lift : push[i];
|
|
17719
|
-
this.pushes.set(upper, push);
|
|
17720
|
-
this.turnAboutCentre(upper, c, [turn2[0] * lift * upper.mass, turn2[1] * lift * upper.mass, turn2[2] * lift * upper.mass]);
|
|
17721
|
-
this.contactImpulse(upper, e, r, up, 0);
|
|
17722
|
-
}
|
|
17723
|
-
/** Turns a piece by the rotation vector `by` (rad) about its centre `c`, which stays where it is. */
|
|
17724
|
-
turnAboutCentre(b, c, by) {
|
|
17725
|
-
const angle = Math.hypot(by[0], by[1], by[2]);
|
|
17726
|
-
if (angle < 1e-9) return;
|
|
17727
|
-
b.root.quaternion.premultiply(this.dq.setFromAxisAngle(this.ax.set(by[0] / angle, by[1] / angle, by[2] / angle), angle)).normalize();
|
|
17728
|
-
const off2 = this.ax.copy(b.centerV).applyQuaternion(b.root.quaternion);
|
|
17729
|
-
b.root.position.set(c[0] - off2.x, c[1] - off2.y, c[2] - off2.z);
|
|
17730
|
-
}
|
|
17731
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. */
|
|
17732
16917
|
slideApart(a, b, ima, imb, hit) {
|
|
17733
16918
|
const n = hit.normal;
|
|
@@ -17749,12 +16934,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
17749
16934
|
}
|
|
17750
16935
|
}
|
|
17751
16936
|
};
|
|
17752
|
-
function solve3(k, rhs) {
|
|
17753
|
-
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]);
|
|
17754
|
-
const d = det(k[0], k[1], k[2]);
|
|
17755
|
-
if (Math.abs(d) < 1e-12) return null;
|
|
17756
|
-
return [det(rhs, k[1], k[2]) / d, det(k[0], rhs, k[2]) / d, det(k[0], k[1], rhs) / d];
|
|
17757
|
-
}
|
|
17758
16937
|
|
|
17759
16938
|
// src/rebar.ts
|
|
17760
16939
|
var RebarCages = class {
|
|
@@ -27372,7 +26551,6 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
27372
26551
|
var CRATER_RADIUS = 2.5;
|
|
27373
26552
|
var ORIGIN7 = [4e3, 0, 4e3];
|
|
27374
26553
|
var warmed = /* @__PURE__ */ new WeakSet();
|
|
27375
|
-
var OWN_SOLVER = {};
|
|
27376
26554
|
var brickWall = (THREE) => {
|
|
27377
26555
|
const parts = [];
|
|
27378
26556
|
const geometries = [];
|
|
@@ -27402,7 +26580,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
27402
26580
|
merged.setIndex(index);
|
|
27403
26581
|
return { parts, joints: inferJoints(parts), mesh: new THREE.Mesh(merged, new THREE.MeshStandardMaterial()), ranges };
|
|
27404
26582
|
};
|
|
27405
|
-
var runScene = (THREE, makeRenderer,
|
|
26583
|
+
var runScene = (THREE, makeRenderer, engine, worldDesc) => {
|
|
27406
26584
|
const scene2 = new THREE.Scene();
|
|
27407
26585
|
const world = createDestruction();
|
|
27408
26586
|
const vfx = createVfx({});
|
|
@@ -27412,12 +26590,12 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
27412
26590
|
let renderer;
|
|
27413
26591
|
let bound;
|
|
27414
26592
|
try {
|
|
27415
|
-
engineWorld =
|
|
27416
|
-
const loop =
|
|
26593
|
+
engineWorld = engine.createWorld(worldDesc ?? KIT_WORLD);
|
|
26594
|
+
const loop = createPhysicsLoop(engineWorld);
|
|
27417
26595
|
const terrainGridAt = (xs, zs, out) => {
|
|
27418
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]);
|
|
27419
26597
|
};
|
|
27420
|
-
renderer = makeRenderer({ physics: loop
|
|
26598
|
+
renderer = makeRenderer({ physics: loop, scene: scene2, world, vfx, eventBudgetMs: Infinity, groundAt: terrainAt, terrainAt, terrainGridAt, jitWarmUp: false });
|
|
27421
26599
|
const wall = brickWall(THREE);
|
|
27422
26600
|
const root = new THREE.Group();
|
|
27423
26601
|
root.position.set(...ORIGIN7);
|
|
@@ -27444,7 +26622,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
27444
26622
|
renderer.wakeRegion(point[0], point[2], CRATER_RADIUS);
|
|
27445
26623
|
for (let frame2 = 0; frame2 < FRAMES; frame2++) {
|
|
27446
26624
|
world.update(FRAME_SECONDS);
|
|
27447
|
-
loop
|
|
26625
|
+
loop.step(FRAME_SECONDS);
|
|
27448
26626
|
renderer.update(FRAME_SECONDS);
|
|
27449
26627
|
const stats = renderer.stats();
|
|
27450
26628
|
seen.pieces = Math.max(seen.pieces, stats.pieces);
|
|
@@ -27453,10 +26631,10 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
27453
26631
|
return seen;
|
|
27454
26632
|
};
|
|
27455
26633
|
var warmUpDestructionJit = (THREE, makeRenderer, options = {}) => {
|
|
27456
|
-
const
|
|
27457
|
-
if (warmed.has(
|
|
27458
|
-
warmed.add(
|
|
27459
|
-
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);
|
|
27460
26638
|
};
|
|
27461
26639
|
|
|
27462
26640
|
// src/bend.ts
|
|
@@ -29443,7 +28621,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
29443
28621
|
__publicField(this, "blastDebris");
|
|
29444
28622
|
__publicField(this, "blastWakes");
|
|
29445
28623
|
__publicField(this, "costMeter");
|
|
29446
|
-
/** The step cost the debris policy reads this frame (`debrisCost`), null when there is none (
|
|
28624
|
+
/** The step cost the debris policy reads this frame (`debrisCost`), null when there is none (`fixed`). */
|
|
29447
28625
|
__publicField(this, "cost", null);
|
|
29448
28626
|
__publicField(this, "costFrame", 0);
|
|
29449
28627
|
/** The pieces faded away (`faded`): their root shrinks, then leaves the scene. */
|
|
@@ -29454,6 +28632,9 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
29454
28632
|
__publicField(this, "staticBoxes", /* @__PURE__ */ new Map());
|
|
29455
28633
|
/** `staticSolids` per object, kept until `staticNow` measures the object again for a reason. */
|
|
29456
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());
|
|
29457
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. */
|
|
29458
28639
|
__publicField(this, "unsureSolids", /* @__PURE__ */ new Set());
|
|
29459
28640
|
/** The objects `staticNow` measured again for a reason (an event, a moved root, boxes that changed): their `staticSolids` are measured again too. */
|
|
@@ -29667,6 +28848,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
29667
28848
|
scene: o.scene,
|
|
29668
28849
|
maxAwake: o.maxAwakePieces ?? Infinity,
|
|
29669
28850
|
...o.tilesUnderParts ? { tilesUnderParts: true } : {},
|
|
28851
|
+
...o.heldIgnores ? { heldIgnores: o.heldIgnores } : {},
|
|
29670
28852
|
...o.groundWorker ? { groundWorker: o.groundWorker } : {},
|
|
29671
28853
|
...o.groundWorkerFrames !== void 0 ? { groundWorkerFrames: o.groundWorkerFrames } : {},
|
|
29672
28854
|
...this.blastDebris ? { keep: { maxLive: this.blastDebris.maxLivePieces, far: this.blastDebris.far, ...o.focus ? { focus: o.focus } : {}, onFade: (b) => this.fadePiece(b) } } : {},
|
|
@@ -29856,8 +29038,7 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
29856
29038
|
}
|
|
29857
29039
|
if (total.isEmpty()) return { half: [0.25, 0.25, 0.25], center: new this.THREE.Vector3() };
|
|
29858
29040
|
const size = total.getSize(new this.THREE.Vector3());
|
|
29859
|
-
|
|
29860
|
-
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()) };
|
|
29861
29042
|
}
|
|
29862
29043
|
/**
|
|
29863
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
|
|
@@ -30551,8 +29732,28 @@ vec3 c=mix(uDeep,uFoam,dome>.5?.8:.65+.35*vY);float alpha=body*vA;if(alpha<.004)
|
|
|
30551
29732
|
} finally {
|
|
30552
29733
|
this.measuredMeshes.clear();
|
|
30553
29734
|
}
|
|
29735
|
+
for (const [id, source] of this.hostSolids) out.set(id, this.hostSolidsOf(id, source()));
|
|
30554
29736
|
return out;
|
|
30555
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
|
+
}
|
|
30556
29757
|
/** `staticSolids` for each fixed object of `now`, into `out`. */
|
|
30557
29758
|
measureEach(now, budget, out) {
|
|
30558
29759
|
for (const id of now.keys()) {
|