@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.js
CHANGED
|
@@ -2756,11 +2756,22 @@ var sameShape = (a, b) => near(a.center, b.center) && near(a.half, b.half) && ne
|
|
|
2756
2756
|
var sameBox = (a, b) => a === b || sameShape(a, b) && sameFriction(a.friction, b.friction);
|
|
2757
2757
|
var sameSolids = (a, b) => a.length === b.length && a.every((s, i) => sameBox(s, b[i]));
|
|
2758
2758
|
var NONE = /* @__PURE__ */ new Set();
|
|
2759
|
+
var MESH_ORIGIN = [0, 0, 0];
|
|
2760
|
+
var MESH_ROTATION = [0, 0, 0, 1];
|
|
2759
2761
|
var partOfKey = (key) => {
|
|
2760
2762
|
const colon = key.lastIndexOf(":");
|
|
2761
2763
|
return colon < 0 || !/^\d+$/.test(key.slice(colon + 1)) ? key : key.slice(0, colon);
|
|
2762
2764
|
};
|
|
2763
2765
|
var hasSize = (s) => s.half[0] > 0 && s.half[1] > 0 && s.half[2] > 0;
|
|
2766
|
+
var orientedReach = (s) => {
|
|
2767
|
+
const [x, y, z, w] = s.rotation;
|
|
2768
|
+
const row = (a, b, c) => Math.abs(a) * s.half[0] + Math.abs(b) * s.half[1] + Math.abs(c) * s.half[2];
|
|
2769
|
+
return [
|
|
2770
|
+
row(1 - 2 * (y * y + z * z), 2 * (x * y - z * w), 2 * (x * z + y * w)),
|
|
2771
|
+
row(2 * (x * y + z * w), 1 - 2 * (x * x + z * z), 2 * (y * z - x * w)),
|
|
2772
|
+
row(2 * (x * z - y * w), 2 * (y * z + x * w), 1 - 2 * (x * x + y * y))
|
|
2773
|
+
];
|
|
2774
|
+
};
|
|
2764
2775
|
var shapeOf = (s) => s.mesh ? { kind: "trimesh", vertices: s.mesh.vertices, indices: s.mesh.indices } : { kind: "box", half: s.half };
|
|
2765
2776
|
var EngineStatics = class {
|
|
2766
2777
|
constructor(world, material) {
|
|
@@ -2775,6 +2786,8 @@ var EngineStatics = class {
|
|
|
2775
2786
|
__publicField(this, "ownerOf", /* @__PURE__ */ new Map());
|
|
2776
2787
|
/** The part of the object each held body draws (the key without its mesh number): what a blow on the body hits. */
|
|
2777
2788
|
__publicField(this, "partOf", /* @__PURE__ */ new Map());
|
|
2789
|
+
/** 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. */
|
|
2790
|
+
__publicField(this, "meshBounds", /* @__PURE__ */ new Map());
|
|
2778
2791
|
/** Changes whenever an answer of `covers` may have (an object came, went, or was fitted again). */
|
|
2779
2792
|
__publicField(this, "coverageRevision", 0);
|
|
2780
2793
|
__publicField(this, "unsure", NONE);
|
|
@@ -2803,6 +2816,7 @@ var EngineStatics = class {
|
|
|
2803
2816
|
if (now.has(id)) continue;
|
|
2804
2817
|
for (const h of f.held.values()) this.release(h, f.settled);
|
|
2805
2818
|
this.bodies.delete(id);
|
|
2819
|
+
this.meshBounds.delete(id);
|
|
2806
2820
|
this.coverageRevision++;
|
|
2807
2821
|
}
|
|
2808
2822
|
const budget = { triangles: TRIANGLES_PER_UPDATE };
|
|
@@ -2819,6 +2833,7 @@ var EngineStatics = class {
|
|
|
2819
2833
|
clear() {
|
|
2820
2834
|
for (const f of this.bodies.values()) for (const h of f.held.values()) this.drop(h.id);
|
|
2821
2835
|
this.bodies.clear();
|
|
2836
|
+
this.meshBounds.clear();
|
|
2822
2837
|
this.coverageRevision++;
|
|
2823
2838
|
this.changed.clear();
|
|
2824
2839
|
}
|
|
@@ -2875,6 +2890,22 @@ var EngineStatics = class {
|
|
|
2875
2890
|
}
|
|
2876
2891
|
f.waiting = waiting;
|
|
2877
2892
|
if (waiting === 0) f.settled = true;
|
|
2893
|
+
this.boundMeshes(f);
|
|
2894
|
+
}
|
|
2895
|
+
boundMeshes(f) {
|
|
2896
|
+
let bounds;
|
|
2897
|
+
for (const h of f.held.values()) {
|
|
2898
|
+
const s = h.solid;
|
|
2899
|
+
if (!s.mesh) continue;
|
|
2900
|
+
bounds ?? (bounds = new Float64Array([Infinity, Infinity, Infinity, -Infinity, -Infinity, -Infinity]));
|
|
2901
|
+
const reach = orientedReach(s);
|
|
2902
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
2903
|
+
bounds[axis] = Math.min(bounds[axis], s.center[axis] - reach[axis]);
|
|
2904
|
+
bounds[axis + 3] = Math.max(bounds[axis + 3], s.center[axis] + reach[axis]);
|
|
2905
|
+
}
|
|
2906
|
+
}
|
|
2907
|
+
if (bounds) this.meshBounds.set(f.objectId, bounds);
|
|
2908
|
+
else this.meshBounds.delete(f.objectId);
|
|
2878
2909
|
}
|
|
2879
2910
|
/** 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). */
|
|
2880
2911
|
reshape(h, solid) {
|
|
@@ -2883,7 +2914,8 @@ var EngineStatics = class {
|
|
|
2883
2914
|
if (!sameFriction(was.friction, solid.friction)) this.world.setMaterial(h.id, this.materialOf(solid));
|
|
2884
2915
|
this.changed.add(h.id);
|
|
2885
2916
|
if (sameShape(was, solid)) return;
|
|
2886
|
-
if (
|
|
2917
|
+
if (solid.mesh) this.world.setPose(h.id, MESH_ORIGIN, MESH_ROTATION, false);
|
|
2918
|
+
else this.world.setPose(h.id, solid.center, solid.rotation, false);
|
|
2887
2919
|
this.world.setShape(h.id, shapeOf(solid));
|
|
2888
2920
|
}
|
|
2889
2921
|
describe(solid) {
|
|
@@ -3531,6 +3563,26 @@ var GroundSchedule = class {
|
|
|
3531
3563
|
var RAY_MARGIN = 0.25;
|
|
3532
3564
|
var LONGEST_RAY = 2e3;
|
|
3533
3565
|
var FIXED_ONLY = { layers: ["static"] };
|
|
3566
|
+
var segmentMeetsBox = (origin, direction, length, box) => {
|
|
3567
|
+
let enter = 0;
|
|
3568
|
+
let leave = length;
|
|
3569
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
3570
|
+
const low = box[axis] - RAY_MARGIN;
|
|
3571
|
+
const high = box[axis + 3] + RAY_MARGIN;
|
|
3572
|
+
const step = direction[axis];
|
|
3573
|
+
const start = origin[axis];
|
|
3574
|
+
if (Math.abs(step) < 1e-12) {
|
|
3575
|
+
if (start < low || start > high) return false;
|
|
3576
|
+
continue;
|
|
3577
|
+
}
|
|
3578
|
+
const a = (low - start) / step;
|
|
3579
|
+
const b = (high - start) / step;
|
|
3580
|
+
enter = Math.max(enter, Math.min(a, b));
|
|
3581
|
+
leave = Math.min(leave, Math.max(a, b));
|
|
3582
|
+
if (enter > leave) return false;
|
|
3583
|
+
}
|
|
3584
|
+
return true;
|
|
3585
|
+
};
|
|
3534
3586
|
var createEngineRayBroadPhase = (world, statics) => {
|
|
3535
3587
|
const hits = [];
|
|
3536
3588
|
const inside = [];
|
|
@@ -3557,6 +3609,12 @@ var createEngineRayBroadPhase = (world, statics) => {
|
|
|
3557
3609
|
metList.push(owner);
|
|
3558
3610
|
}
|
|
3559
3611
|
}
|
|
3612
|
+
for (const [owner, bounds] of statics.meshBounds) {
|
|
3613
|
+
if (!met.has(owner) && segmentMeetsBox(origin, direction, maxDistance, bounds)) {
|
|
3614
|
+
met.add(owner);
|
|
3615
|
+
metList.push(owner);
|
|
3616
|
+
}
|
|
3617
|
+
}
|
|
3560
3618
|
const around = world.overlapSphere(from, RAY_MARGIN, inside, FIXED_ONLY);
|
|
3561
3619
|
for (let i = 0; i < around; i++) {
|
|
3562
3620
|
const owner = statics.ownerOf.get(inside[i]);
|
|
@@ -3636,7 +3694,6 @@ var MU_STATIC = 0.7;
|
|
|
3636
3694
|
var ROLLING_RESISTANCE = 0.015;
|
|
3637
3695
|
var MU_DYNAMIC = 0.6;
|
|
3638
3696
|
var GROUND = { frictionStatic: MU_STATIC, frictionDynamic: MU_DYNAMIC, restitution: 0, rolling: ROLLING_RESISTANCE };
|
|
3639
|
-
var groundTileKey = (x, z) => (Math.floor(x / TILE) + TILE_KEY_ORIGIN) * TILE_KEY + (Math.floor(z / TILE) + TILE_KEY_ORIGIN);
|
|
3640
3697
|
var tileRollingAt = (rollingAt, x, z) => rollingAt((Math.floor(x / TILE) + 0.5) * TILE, (Math.floor(z / TILE) + 0.5) * TILE);
|
|
3641
3698
|
var STILL_SPEED = 0.12;
|
|
3642
3699
|
var STILL_SPIN = 0.45;
|
|
@@ -4224,6 +4281,7 @@ var EnginePieces = class {
|
|
|
4224
4281
|
isSlipping: false
|
|
4225
4282
|
};
|
|
4226
4283
|
this.links.set(b, link);
|
|
4284
|
+
if (b.held) this.passThroughHolder(id, true);
|
|
4227
4285
|
this.byUser.set(user, b);
|
|
4228
4286
|
this.byBody.set(id, b);
|
|
4229
4287
|
this.bodyIds.add(id);
|
|
@@ -4234,14 +4292,16 @@ var EnginePieces = class {
|
|
|
4234
4292
|
bodyDesc(b, user) {
|
|
4235
4293
|
const c = this.centre(b);
|
|
4236
4294
|
const q = b.root.quaternion;
|
|
4295
|
+
const shape = this.shapeOf(b);
|
|
4237
4296
|
return {
|
|
4238
|
-
shape
|
|
4297
|
+
shape,
|
|
4239
4298
|
motion: b.held ? "kinematic" : "dynamic",
|
|
4240
4299
|
position: [c.x, c.y, c.z],
|
|
4241
4300
|
rotation: [q.x, q.y, q.z, q.w],
|
|
4242
4301
|
mass: b.mass,
|
|
4243
4302
|
inertia: this.inertiaOf(b),
|
|
4244
4303
|
centreAtOrigin: true,
|
|
4304
|
+
keepPartContacts: shape.kind === "compound" && shape.children.length > 1,
|
|
4245
4305
|
material: this.material(b),
|
|
4246
4306
|
layer: "debris",
|
|
4247
4307
|
// no air drag: the kit's pieces never had any (a round piece's rolling resistance is the adapter's, `rolling`)
|
|
@@ -4257,6 +4317,10 @@ var EnginePieces = class {
|
|
|
4257
4317
|
angularVelocity: [b.angularVelocity[0], b.angularVelocity[1], b.angularVelocity[2]]
|
|
4258
4318
|
};
|
|
4259
4319
|
}
|
|
4320
|
+
passThroughHolder(id, ignore) {
|
|
4321
|
+
const holder = this.o.heldIgnores?.();
|
|
4322
|
+
if (holder !== void 0) this.world.ignorePair(id, holder, ignore);
|
|
4323
|
+
}
|
|
4260
4324
|
/** 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). */
|
|
4261
4325
|
sync(b, dt, h) {
|
|
4262
4326
|
const c = this.centre(b);
|
|
@@ -4267,6 +4331,7 @@ var EnginePieces = class {
|
|
|
4267
4331
|
if (b.held !== link.held) {
|
|
4268
4332
|
link.held = b.held;
|
|
4269
4333
|
this.world.setMotion(link.id, b.held ? "kinematic" : "dynamic");
|
|
4334
|
+
this.passThroughHolder(link.id, b.held);
|
|
4270
4335
|
}
|
|
4271
4336
|
if (b.held) {
|
|
4272
4337
|
this.world.moveKinematic(link.id, [c.x, c.y, c.z], [q.x, q.y, q.z, q.w], h);
|
|
@@ -5048,9 +5113,8 @@ var PhysicsOptionError = class extends Error {
|
|
|
5048
5113
|
}
|
|
5049
5114
|
};
|
|
5050
5115
|
var resolvePhysics = (option) => {
|
|
5051
|
-
if (option === "own") return null;
|
|
5052
5116
|
if (option === void 0 || typeof option !== "object") {
|
|
5053
|
-
throw new PhysicsOptionError("the `physics` option is required: pass a PhysicsLoop
|
|
5117
|
+
throw new PhysicsOptionError("the `physics` option is required: pass a PhysicsLoop or a world from the host");
|
|
5054
5118
|
}
|
|
5055
5119
|
if (isPhysicsLoop(option)) return { loop: option, stepsItself: false };
|
|
5056
5120
|
if (typeof option.stepFixed !== "function") {
|
|
@@ -5100,44 +5164,20 @@ function isOverSupport(points, centre, tolerance) {
|
|
|
5100
5164
|
}
|
|
5101
5165
|
|
|
5102
5166
|
// src/pieces.ts
|
|
5103
|
-
var SLAM_MIN_SPEED2 = 2;
|
|
5104
|
-
var SLAM_BOUNCE2 = 0.2;
|
|
5105
5167
|
var BALANCE_TOLERANCE = 0.01;
|
|
5106
|
-
var SLAM_FLOOR_NORMAL2 = 0.7;
|
|
5107
|
-
var SUB_DT = 1 / 120;
|
|
5108
5168
|
var MU_STATIC2 = 0.7;
|
|
5109
5169
|
var MU_DYNAMIC2 = 0.6;
|
|
5110
|
-
var SLIP_SPEED = 0.02;
|
|
5111
5170
|
var ROLLING_RESISTANCE2 = 0.015;
|
|
5112
|
-
var ROUND_LYING = Math.SQRT1_2;
|
|
5113
5171
|
var MIN_PIECE_HALF = 1e-3;
|
|
5114
|
-
var OWN_MIN_PIECE_HALF = 0.01;
|
|
5115
|
-
var OWN_MIN_MEASURED_HALF = 0.02;
|
|
5116
|
-
var minPieceHalf = (physics) => physics === "own" ? OWN_MIN_MEASURED_HALF : MIN_PIECE_HALF;
|
|
5117
5172
|
var SLEEP_AFTER = 0.5;
|
|
5118
5173
|
var STILL_SPEED2 = 0.12;
|
|
5119
5174
|
var STILL_SPIN2 = 0.45;
|
|
5120
|
-
var CALM_DRIFT = 0.01;
|
|
5121
|
-
var CALM_TURN = 0.05;
|
|
5122
|
-
var CALM_SLEEP_AFTER = 1.5;
|
|
5123
|
-
var STEADY_TURN2 = 0.1;
|
|
5124
5175
|
var STEADY_SECONDS2 = 1.5;
|
|
5125
|
-
var HARD_LANDING2 = 3;
|
|
5126
5176
|
var SLOP = 2e-3;
|
|
5127
|
-
var NARROW2 = 0.3;
|
|
5128
|
-
var SUPPORT_MARGIN = 0.05;
|
|
5129
|
-
var TOPPLE2 = 3;
|
|
5130
|
-
var TIP_ACCELERATION = 30;
|
|
5131
|
-
var TIP_RISING = 0.02;
|
|
5132
|
-
var TIP_RISE_AFTER = 0.5;
|
|
5133
|
-
var TIP_LYING = 0.25;
|
|
5134
5177
|
var TIP_SECONDS = 3;
|
|
5135
5178
|
var STEP = 0.03;
|
|
5136
5179
|
var WAKE_SAMPLES = 4;
|
|
5137
5180
|
var WAKE_DROP = 0.05;
|
|
5138
|
-
var WARM_SLOTS = 8 + 12 * 7;
|
|
5139
|
-
var SOLVER_PASSES = 10;
|
|
5140
|
-
var SIDE_BOUNCE = 0.1;
|
|
5141
5181
|
var EDGE_POINTS = [-0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75];
|
|
5142
5182
|
var EDGE_SPACING = 0.05;
|
|
5143
5183
|
var MAX_EDGE_POINTS = 31;
|
|
@@ -5154,15 +5194,9 @@ var STEEP_MARGIN = 1.1;
|
|
|
5154
5194
|
var SLOPE_MIN_NORMAL = 0.5;
|
|
5155
5195
|
var LIES_FLAT = 0.985;
|
|
5156
5196
|
var KEEP_EVERY = 0.5;
|
|
5157
|
-
var WAKE_ABOVE_SPEED = 0.05;
|
|
5158
|
-
var WAKE_ABOVE_SPIN = 0.1;
|
|
5159
5197
|
var SURFACE_CELL = 0.5;
|
|
5160
5198
|
var SURFACE_CELLS_ACROSS = 24;
|
|
5161
5199
|
var cellKey = (ix, iz) => (ix + 32768) * 65536 + (iz + 32768);
|
|
5162
|
-
var MAX_COLLISION_STEPS = 8;
|
|
5163
|
-
var MIN_ROUND_RADIUS = 5e-3;
|
|
5164
|
-
var BOUNCE_MIN_SPEED = 0.1;
|
|
5165
|
-
var BOX_IMPACT_SPEED = 1.5;
|
|
5166
5200
|
var KNOCK_FLAT = 0.5;
|
|
5167
5201
|
var SEPARATE_SPEED = 1.5;
|
|
5168
5202
|
var LIFT_DEPTH = STEP;
|
|
@@ -5211,8 +5245,6 @@ var pairFriction = (a, b) => ({
|
|
|
5211
5245
|
// both lose energy in the contact: a log on grass is held by its bark and by the blades
|
|
5212
5246
|
rollingDamping: (a?.rollingDamping ?? PIECE_ROLLING_DAMPING) + (b?.rollingDamping ?? GROUND_ROLLING_DAMPING)
|
|
5213
5247
|
});
|
|
5214
|
-
var pairRestitution = (a, b) => Math.sqrt((a ?? SLAM_BOUNCE2) * (b ?? SLAM_BOUNCE2));
|
|
5215
|
-
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)));
|
|
5216
5248
|
var PieceSystem = class {
|
|
5217
5249
|
constructor(THREE, o) {
|
|
5218
5250
|
__publicField(this, "THREE", THREE);
|
|
@@ -5221,19 +5253,14 @@ var PieceSystem = class {
|
|
|
5221
5253
|
__publicField(this, "time", 0);
|
|
5222
5254
|
__publicField(this, "generations", 0);
|
|
5223
5255
|
__publicField(this, "p");
|
|
5224
|
-
__publicField(this, "q");
|
|
5225
5256
|
__publicField(this, "dq");
|
|
5226
|
-
__publicField(this, "nextQ");
|
|
5227
5257
|
__publicField(this, "m");
|
|
5228
|
-
__publicField(this, "hingeM");
|
|
5229
5258
|
__publicField(this, "ax");
|
|
5230
5259
|
__publicField(this, "wakeBox");
|
|
5231
5260
|
__publicField(this, "solids", []);
|
|
5232
|
-
/** The engine stepping the pieces (`PieceSystemOptions.physics`)
|
|
5261
|
+
/** The engine stepping the pieces (`PieceSystemOptions.physics`). */
|
|
5233
5262
|
__publicField(this, "engine");
|
|
5234
|
-
/** The
|
|
5235
|
-
__publicField(this, "groundWorker");
|
|
5236
|
-
/** The loop the system steps itself (a world was handed), the loop it only registers on (a loop was handed), or null on `'own'`. */
|
|
5263
|
+
/** The loop the system steps itself (a world was handed) or only registers on (a loop was handed). */
|
|
5237
5264
|
__publicField(this, "loop");
|
|
5238
5265
|
__publicField(this, "stepsItself");
|
|
5239
5266
|
/** The phases' way out of the loop, and whether this frame's `prepare` ran (it runs once, in the first fixed step). */
|
|
@@ -5283,12 +5310,8 @@ var PieceSystem = class {
|
|
|
5283
5310
|
__publicField(this, "supportNormal", UP);
|
|
5284
5311
|
__publicField(this, "supportFriction", null);
|
|
5285
5312
|
__publicField(this, "supportBody", null);
|
|
5286
|
-
/**
|
|
5287
|
-
__publicField(this, "
|
|
5288
|
-
/** The terrain's surface by ground tile (`groundTileKey`) this update, null for the default: read once per tile, not per contact per substep. */
|
|
5289
|
-
__publicField(this, "surfaceByTile", /* @__PURE__ */ new Map());
|
|
5290
|
-
/** 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. */
|
|
5291
|
-
__publicField(this, "isSettled", (body) => this.engine ? this.engine.isSettled(body) : body.sleeping && !body.held);
|
|
5313
|
+
/** A piece is asleep on both sides (the engine's too): nothing of it changes until it wakes. */
|
|
5314
|
+
__publicField(this, "isSettled", (body) => this.engine.isSettled(body));
|
|
5292
5315
|
__publicField(this, "measureAwake", (body) => {
|
|
5293
5316
|
const rec = this.measured(body);
|
|
5294
5317
|
const from = rec.from;
|
|
@@ -5312,18 +5335,15 @@ var PieceSystem = class {
|
|
|
5312
5335
|
if (rec.from !== from || rec.list.length !== count || rec.list[0] !== first) this.reshaped = true;
|
|
5313
5336
|
});
|
|
5314
5337
|
this.p = new THREE.Vector3();
|
|
5315
|
-
this.q = new THREE.Quaternion();
|
|
5316
5338
|
this.dq = new THREE.Quaternion();
|
|
5317
|
-
this.nextQ = new THREE.Quaternion();
|
|
5318
5339
|
this.m = new THREE.Matrix4();
|
|
5319
|
-
this.hingeM = new THREE.Matrix4();
|
|
5320
5340
|
this.ax = new THREE.Vector3();
|
|
5321
5341
|
this.wakeBox = new THREE.Box3();
|
|
5322
5342
|
const resolved = resolvePhysics(o.physics);
|
|
5323
|
-
this.loop = resolved
|
|
5324
|
-
this.stepsItself = resolved
|
|
5343
|
+
this.loop = resolved.loop;
|
|
5344
|
+
this.stepsItself = resolved.stepsItself;
|
|
5325
5345
|
this.trail = o.interpolate ? new PoseTrail() : null;
|
|
5326
|
-
this.engine =
|
|
5346
|
+
this.engine = new EnginePieces(THREE, resolved.loop.world, {
|
|
5327
5347
|
groundAt: o.terrainAt ?? o.groundAt,
|
|
5328
5348
|
...o.terrainRollingAt ? { groundRollingAt: o.terrainRollingAt } : {},
|
|
5329
5349
|
...o.terrainRollingDampingAt ? { groundRollingDampingAt: o.terrainRollingDampingAt } : {},
|
|
@@ -5340,15 +5360,15 @@ var PieceSystem = class {
|
|
|
5340
5360
|
onKnock: o.onKnock,
|
|
5341
5361
|
onHardLanding: o.onHardLanding,
|
|
5342
5362
|
...o.tilesUnderParts ? { tilesUnderParts: true } : {},
|
|
5363
|
+
...o.heldIgnores ? { heldIgnores: o.heldIgnores } : {},
|
|
5343
5364
|
...o.groundWorker ? { groundWorker: o.groundWorker } : {},
|
|
5344
5365
|
...o.groundWorkerFrames !== void 0 ? { groundWorkerFrames: o.groundWorkerFrames } : {}
|
|
5345
|
-
})
|
|
5346
|
-
this.
|
|
5347
|
-
if (resolved && !resolved.stepsItself) this.joinLoop(resolved.loop, this.engine);
|
|
5366
|
+
});
|
|
5367
|
+
if (!resolved.stepsItself) this.joinLoop(resolved.loop, this.engine);
|
|
5348
5368
|
}
|
|
5349
|
-
/** Contact blows on the fixed parts become `blunt` hits of `destruction` (`engine-impacts.ts`)
|
|
5369
|
+
/** Contact blows on the fixed parts become `blunt` hits of `destruction` (`engine-impacts.ts`). Returns the way to stop it. */
|
|
5350
5370
|
contactHits(destruction, options) {
|
|
5351
|
-
return
|
|
5371
|
+
return createEngineImpacts(this.loop, this.engine.statics, destruction, options);
|
|
5352
5372
|
}
|
|
5353
5373
|
/**
|
|
5354
5374
|
* The pieces a blast at `point` can reach: the bodies the engine's `blastQuery` finds within `radius` (the pieces among them), in the order of `list`, and the pieces that have no
|
|
@@ -5356,7 +5376,6 @@ var PieceSystem = class {
|
|
|
5356
5376
|
*/
|
|
5357
5377
|
piecesInBlast(blastQuery, point, radius) {
|
|
5358
5378
|
const engine = this.engine;
|
|
5359
|
-
if (!engine) return [...this.list];
|
|
5360
5379
|
const found = blastQuery.run({ origin: point, radius });
|
|
5361
5380
|
const reached = /* @__PURE__ */ new Set();
|
|
5362
5381
|
for (let i = 0; i < found.bodyCount; i++) {
|
|
@@ -5365,9 +5384,9 @@ var PieceSystem = class {
|
|
|
5365
5384
|
}
|
|
5366
5385
|
return this.list.filter((piece) => reached.has(piece) || !engine.hasBody(piece));
|
|
5367
5386
|
}
|
|
5368
|
-
/** The engine's fixed bodies as the broad phase of the world's rays
|
|
5387
|
+
/** The engine's fixed bodies as the broad phase of the world's rays. */
|
|
5369
5388
|
rayBroadPhase() {
|
|
5370
|
-
return this.engine
|
|
5389
|
+
return this.engine.rayBroadPhase();
|
|
5371
5390
|
}
|
|
5372
5391
|
/**
|
|
5373
5392
|
* The loop mode: the system's work goes into the loop's phases. `prepare` (sync, kinematic targets, the kit's rules that came before the step) runs in the first fixed step of
|
|
@@ -5417,8 +5436,7 @@ var PieceSystem = class {
|
|
|
5417
5436
|
dispose() {
|
|
5418
5437
|
this.trail?.clear();
|
|
5419
5438
|
for (const leave of this.leaveLoop.splice(0)) leave();
|
|
5420
|
-
|
|
5421
|
-
else this.groundWorker?.dispose();
|
|
5439
|
+
this.engine.dispose();
|
|
5422
5440
|
}
|
|
5423
5441
|
/**
|
|
5424
5442
|
* Pairs born overlapping (both made in the same update, or one made inside the other) deeper than a contact (`LIFT_DEPTH`: chunks of one wall) go apart along the ground by
|
|
@@ -5529,7 +5547,7 @@ var PieceSystem = class {
|
|
|
5529
5547
|
return existing;
|
|
5530
5548
|
}
|
|
5531
5549
|
const awake = this.awake;
|
|
5532
|
-
const least =
|
|
5550
|
+
const least = MIN_PIECE_HALF;
|
|
5533
5551
|
let asleep = false;
|
|
5534
5552
|
let inHand = false;
|
|
5535
5553
|
const body = {
|
|
@@ -5570,13 +5588,13 @@ var PieceSystem = class {
|
|
|
5570
5588
|
this.bornAt.set(body, this.time);
|
|
5571
5589
|
return body;
|
|
5572
5590
|
}
|
|
5573
|
-
/** 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.
|
|
5591
|
+
/** What the loop's last frame cost (ms) and how many bodies the world had awake: the pair the cost of a body is read from. */
|
|
5574
5592
|
stepCost() {
|
|
5575
|
-
return
|
|
5593
|
+
return { stepMs: this.loop.frame.stepMs, active: this.loop.world.activeCount };
|
|
5576
5594
|
}
|
|
5577
|
-
/** Bodies awake in the physics world
|
|
5595
|
+
/** Bodies awake in the physics world. */
|
|
5578
5596
|
activeBodies() {
|
|
5579
|
-
return this.loop
|
|
5597
|
+
return this.loop.world.activeCount;
|
|
5580
5598
|
}
|
|
5581
5599
|
/** Move the centre of mass (after a part broke off). */
|
|
5582
5600
|
setCenter(b, c) {
|
|
@@ -5596,12 +5614,12 @@ var PieceSystem = class {
|
|
|
5596
5614
|
}
|
|
5597
5615
|
if (gone) {
|
|
5598
5616
|
this.forgetSolids(gone);
|
|
5599
|
-
this.engine
|
|
5617
|
+
this.engine.remove(gone);
|
|
5600
5618
|
}
|
|
5601
5619
|
}
|
|
5602
5620
|
clear() {
|
|
5603
5621
|
this.trail?.clear();
|
|
5604
|
-
this.engine
|
|
5622
|
+
this.engine.clear();
|
|
5605
5623
|
this.list.length = 0;
|
|
5606
5624
|
this.awake.clear();
|
|
5607
5625
|
this.layoutDirty = true;
|
|
@@ -5715,29 +5733,13 @@ var PieceSystem = class {
|
|
|
5715
5733
|
}
|
|
5716
5734
|
return out;
|
|
5717
5735
|
}
|
|
5718
|
-
/**
|
|
5719
|
-
* 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.
|
|
5720
|
-
*/
|
|
5721
|
-
contactImpulse(b, e, r, n, bounce) {
|
|
5722
|
-
const v = b.velocity;
|
|
5723
|
-
const w = b.angularVelocity;
|
|
5724
|
-
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];
|
|
5725
|
-
if (vn >= 0) return;
|
|
5726
|
-
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]);
|
|
5727
|
-
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];
|
|
5728
|
-
const j = -(1 + bounce) * vn / (1 / b.mass + k);
|
|
5729
|
-
for (let i = 0; i < 3; i++) {
|
|
5730
|
-
v[i] += j * n[i] / b.mass;
|
|
5731
|
-
w[i] += a[i] * j;
|
|
5732
|
-
}
|
|
5733
|
-
}
|
|
5734
5736
|
/** A blast at (x, z) of `radius`: the ground within its reach is laid at the next update, before the pieces it makes need it (`EnginePieces.layAhead`). */
|
|
5735
5737
|
layGroundAt(x, z, radius) {
|
|
5736
|
-
this.engine
|
|
5738
|
+
this.engine.layAhead(x, z, radius);
|
|
5737
5739
|
}
|
|
5738
5740
|
/** What lies inside the disc looks at the ground again: it was dug or heaped under it. */
|
|
5739
5741
|
wakeRegion(x, z, radius) {
|
|
5740
|
-
this.engine
|
|
5742
|
+
this.engine.groundChanged(x, z, radius);
|
|
5741
5743
|
for (const b of this.list) {
|
|
5742
5744
|
if (!b.sleeping || b.held) continue;
|
|
5743
5745
|
if (Math.hypot(b.root.position.x - x, b.root.position.z - z) > radius && !this.lostGroundIn(b, x, z, radius)) continue;
|
|
@@ -5780,7 +5782,7 @@ var PieceSystem = class {
|
|
|
5780
5782
|
this.trail?.blend(alpha);
|
|
5781
5783
|
}
|
|
5782
5784
|
update(dt) {
|
|
5783
|
-
if (this.trail &&
|
|
5785
|
+
if (this.trail && this.stepsItself) {
|
|
5784
5786
|
this.beginTick();
|
|
5785
5787
|
this.advance(dt);
|
|
5786
5788
|
this.trail.commit();
|
|
@@ -5789,96 +5791,21 @@ var PieceSystem = class {
|
|
|
5789
5791
|
this.advance(dt);
|
|
5790
5792
|
}
|
|
5791
5793
|
advance(dt) {
|
|
5792
|
-
if (this.
|
|
5793
|
-
|
|
5794
|
-
this.time = this.loop.frame.time;
|
|
5795
|
-
this.keepFar();
|
|
5796
|
-
return;
|
|
5797
|
-
}
|
|
5798
|
-
this.time += dt;
|
|
5799
|
-
this.engine.wakePending(this.list, this.awake);
|
|
5800
|
-
this.measure();
|
|
5801
|
-
this.wakeUnsupported(true);
|
|
5802
|
-
if (dt > 0) {
|
|
5803
|
-
this.engine.prepare(this.list, dt, dt, this.apartAll(dt), this.awake);
|
|
5804
|
-
this.loop.step(dt);
|
|
5805
|
-
this.engine.finish(this.list, dt, this.time, this.awake);
|
|
5806
|
-
}
|
|
5807
|
-
this.measure();
|
|
5808
|
-
this.keepBudget();
|
|
5794
|
+
if (!this.stepsItself) {
|
|
5795
|
+
this.time = this.loop.frame.time;
|
|
5809
5796
|
this.keepFar();
|
|
5810
5797
|
return;
|
|
5811
5798
|
}
|
|
5812
|
-
|
|
5813
|
-
|
|
5814
|
-
this.time += dt;
|
|
5815
|
-
this.step(dt);
|
|
5816
|
-
return;
|
|
5817
|
-
}
|
|
5818
|
-
const start = this.time;
|
|
5819
|
-
this.time = start + dt;
|
|
5820
|
-
const far2 = (b) => !near2.has(b);
|
|
5821
|
-
this.step(dt, { moves: far2, meets: (a, b) => far2(a) && far2(b) });
|
|
5822
|
-
for (let i = 0; i < steps; i++) {
|
|
5823
|
-
this.time = start + dt * (i + 1) / steps;
|
|
5824
|
-
this.step(dt / steps, { moves: (b) => near2.has(b), meets: (a, b) => near2.has(a) || near2.has(b) });
|
|
5825
|
-
}
|
|
5826
|
-
}
|
|
5827
|
-
/**
|
|
5828
|
-
* 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
|
|
5829
|
-
* 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).
|
|
5830
|
-
* 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.
|
|
5831
|
-
*/
|
|
5832
|
-
collisionSteps(dt) {
|
|
5833
|
-
let need = 1;
|
|
5834
|
-
const near2 = /* @__PURE__ */ new Set();
|
|
5835
|
-
const p = this.p;
|
|
5836
|
-
const speedOf = (b) => b.held ? 0 : Math.hypot(b.velocity[0], b.velocity[1], b.velocity[2]);
|
|
5837
|
-
for (const b of this.list) {
|
|
5838
|
-
if (!b.round || b.held) continue;
|
|
5839
|
-
const radius = Math.max(MIN_ROUND_RADIUS, b.round.radius);
|
|
5840
|
-
const speed = b.sleeping ? 0 : speedOf(b);
|
|
5841
|
-
const c = this.centerOf(b, p);
|
|
5842
|
-
const cx = c.x, cy = c.y, cz = c.z;
|
|
5843
|
-
const rb = Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]);
|
|
5844
|
-
for (const o of this.list) {
|
|
5845
|
-
if (o === b || b.sleeping && o.sleeping) continue;
|
|
5846
|
-
const closing = speed + (o.sleeping ? 0 : speedOf(o));
|
|
5847
|
-
if (closing * dt < radius * 0.5) continue;
|
|
5848
|
-
const reach = closing * dt + rb + Math.hypot(o.halfExtents[0], o.halfExtents[1], o.halfExtents[2]);
|
|
5849
|
-
const oc = this.centerOf(o, p);
|
|
5850
|
-
if (Math.abs(oc.x - cx) > reach || Math.abs(oc.y - cy) > reach || Math.abs(oc.z - cz) > reach) continue;
|
|
5851
|
-
const pair = Math.ceil(closing * dt / radius);
|
|
5852
|
-
if (pair < 2) continue;
|
|
5853
|
-
need = Math.max(need, pair);
|
|
5854
|
-
near2.add(b);
|
|
5855
|
-
near2.add(o);
|
|
5856
|
-
}
|
|
5857
|
-
}
|
|
5858
|
-
return { steps: Math.max(1, Math.min(MAX_COLLISION_STEPS, need)), near: near2 };
|
|
5859
|
-
}
|
|
5860
|
-
/** 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. */
|
|
5861
|
-
step(dt, only) {
|
|
5862
|
-
this.surfaceByTile.clear();
|
|
5799
|
+
this.time += dt;
|
|
5800
|
+
this.engine.wakePending(this.list, this.awake);
|
|
5863
5801
|
this.measure();
|
|
5864
|
-
this.wakeUnsupported();
|
|
5865
|
-
|
|
5866
|
-
|
|
5867
|
-
|
|
5868
|
-
|
|
5869
|
-
b.sleeping = false;
|
|
5870
|
-
b.still = 0;
|
|
5871
|
-
b.calmAt = null;
|
|
5872
|
-
continue;
|
|
5873
|
-
}
|
|
5874
|
-
if (b.sleeping || only && !only.moves(b)) continue;
|
|
5875
|
-
this.integrate(b, dt);
|
|
5876
|
-
if (b.sleeping) continue;
|
|
5877
|
-
const v = b.velocity;
|
|
5878
|
-
const w = b.angularVelocity;
|
|
5879
|
-
if (Math.hypot(v[0], v[1], v[2]) > WAKE_ABOVE_SPEED || Math.hypot(w[0], w[1], w[2]) > WAKE_ABOVE_SPIN) this.wakeAbove(b);
|
|
5802
|
+
this.wakeUnsupported(true);
|
|
5803
|
+
if (dt > 0) {
|
|
5804
|
+
this.engine.prepare(this.list, dt, dt, this.apartAll(dt), this.awake);
|
|
5805
|
+
this.loop.step(dt);
|
|
5806
|
+
this.engine.finish(this.list, dt, this.time, this.awake);
|
|
5880
5807
|
}
|
|
5881
|
-
this.
|
|
5808
|
+
this.measure();
|
|
5882
5809
|
this.keepBudget();
|
|
5883
5810
|
this.keepFar();
|
|
5884
5811
|
}
|
|
@@ -6029,29 +5956,6 @@ var PieceSystem = class {
|
|
|
6029
5956
|
}
|
|
6030
5957
|
return top;
|
|
6031
5958
|
}
|
|
6032
|
-
/** 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. */
|
|
6033
|
-
wakeAbove(b) {
|
|
6034
|
-
const below = this.solidOf.get(b);
|
|
6035
|
-
if (!below) return;
|
|
6036
|
-
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]);
|
|
6037
|
-
const x0 = Math.floor((below.c[0] - below.r) / SURFACE_CELL);
|
|
6038
|
-
const x1 = Math.floor((below.c[0] + below.r) / SURFACE_CELL);
|
|
6039
|
-
const z0 = Math.floor((below.c[2] - below.r) / SURFACE_CELL);
|
|
6040
|
-
const z1 = Math.floor((below.c[2] + below.r) / SURFACE_CELL);
|
|
6041
|
-
const wide = x1 - x0 > SURFACE_CELLS_ACROSS || z1 - z0 > SURFACE_CELLS_ACROSS;
|
|
6042
|
-
const look = (part) => {
|
|
6043
|
-
const o = part.body;
|
|
6044
|
-
const s = this.solidOf.get(o);
|
|
6045
|
-
if (!s || !o.sleeping || o.held || o === b) return;
|
|
6046
|
-
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;
|
|
6047
|
-
if (boxTopAt(below, s.c[0], s.c[2]) === -Infinity && !this.restsOn(o, b, true)) return;
|
|
6048
|
-
o.sleeping = false;
|
|
6049
|
-
o.still = 0;
|
|
6050
|
-
o.calmAt = null;
|
|
6051
|
-
};
|
|
6052
|
-
if (wide) for (const s of this.solids) look(s);
|
|
6053
|
-
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);
|
|
6054
|
-
}
|
|
6055
5959
|
/**
|
|
6056
5960
|
* Whether `o` rests on a top of `below` (its parts, or its box): a corner of `o` (its contacts or its box) is over that top and within `STEP` of it. With `lowest`, only its
|
|
6057
5961
|
* lowest corners count (within `STEP` of its lowest): a can rolling by another is beside it, not on it; without, a board leaning on a seat by its high end rests on the seat.
|
|
@@ -6068,36 +5972,6 @@ var PieceSystem = class {
|
|
|
6068
5972
|
}
|
|
6069
5973
|
return false;
|
|
6070
5974
|
}
|
|
6071
|
-
/**
|
|
6072
|
-
* 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
|
|
6073
|
-
* (`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
|
|
6074
|
-
* per update, and one object per value, so a substep makes none.
|
|
6075
|
-
*/
|
|
6076
|
-
terrainSurface(x, y, z, raw) {
|
|
6077
|
-
const rollingAt = this.o.terrainRollingAt;
|
|
6078
|
-
if (!rollingAt || y !== raw || this.o.terrainAt && y !== this.o.terrainAt(x, z)) return null;
|
|
6079
|
-
const key = groundTileKey(x, z);
|
|
6080
|
-
let surface = this.surfaceByTile.get(key);
|
|
6081
|
-
if (surface === void 0) {
|
|
6082
|
-
surface = this.tileSurface(rollingAt, x, z);
|
|
6083
|
-
this.surfaceByTile.set(key, surface);
|
|
6084
|
-
}
|
|
6085
|
-
return surface;
|
|
6086
|
-
}
|
|
6087
|
-
/** The terrain's surface of the tile at (x, z) (`tileRollingAt`), or null when it says nothing (the default surface). */
|
|
6088
|
-
tileSurface(rollingAt, x, z) {
|
|
6089
|
-
const dampingAt = this.o.terrainRollingDampingAt;
|
|
6090
|
-
const rolling = tileRollingAt(rollingAt, x, z);
|
|
6091
|
-
const rollingDamping = dampingAt ? tileRollingAt(dampingAt, x, z) : void 0;
|
|
6092
|
-
if (rolling === void 0 && rollingDamping === void 0) return null;
|
|
6093
|
-
const id = `${rolling ?? ROLLING_RESISTANCE2}|${rollingDamping ?? GROUND_ROLLING_DAMPING}`;
|
|
6094
|
-
let surface = this.terrainSurfaces.get(id);
|
|
6095
|
-
if (!surface) {
|
|
6096
|
-
surface = { static: MU_STATIC2, dynamic: MU_DYNAMIC2, rolling: rolling ?? ROLLING_RESISTANCE2, rollingDamping: rollingDamping ?? GROUND_ROLLING_DAMPING };
|
|
6097
|
-
this.terrainSurfaces.set(id, surface);
|
|
6098
|
-
}
|
|
6099
|
-
return surface;
|
|
6100
|
-
}
|
|
6101
5975
|
/** Where a corner of `self` at `(x, y, z)` lands: the ground, the top of another piece or of something fixed under it. */
|
|
6102
5976
|
supportAt(self, x, y, z, step) {
|
|
6103
5977
|
let top = this.o.groundAt(x, z);
|
|
@@ -6119,391 +5993,6 @@ var PieceSystem = class {
|
|
|
6119
5993
|
this.supportBody = on?.body ?? null;
|
|
6120
5994
|
return Math.max(top, fixed);
|
|
6121
5995
|
}
|
|
6122
|
-
/** 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. */
|
|
6123
|
-
sweepOn(b, start, pos) {
|
|
6124
|
-
const travel = Math.hypot(pos[0] - start[0], pos[1] - start[1], pos[2] - start[2]);
|
|
6125
|
-
if (travel < 1e-3) return;
|
|
6126
|
-
const hit = this.o.sweep?.(b, start, pos);
|
|
6127
|
-
if (!hit || hit.normal[1] > SLAM_FLOOR_NORMAL2) return;
|
|
6128
|
-
const v = b.velocity;
|
|
6129
|
-
const n = hit.normal;
|
|
6130
|
-
const into = -(v[0] * n[0] + v[1] * n[1] + v[2] * n[2]);
|
|
6131
|
-
if (into <= 0) return;
|
|
6132
|
-
const k = Math.min(1, hit.distance / travel);
|
|
6133
|
-
const bounce = pairRestitution(b.restitution, hit.restitution);
|
|
6134
|
-
for (let i = 0; i < 3; i++) {
|
|
6135
|
-
pos[i] = start[i] + (pos[i] - start[i]) * k;
|
|
6136
|
-
v[i] += (1 + bounce) * into * n[i];
|
|
6137
|
-
}
|
|
6138
|
-
const w = b.angularVelocity;
|
|
6139
|
-
w[0] *= 0.5;
|
|
6140
|
-
w[1] *= 0.5;
|
|
6141
|
-
w[2] *= 0.5;
|
|
6142
|
-
if (into >= SLAM_MIN_SPEED2 && this.time - b.lastHit > 0.15) {
|
|
6143
|
-
b.lastHit = this.time;
|
|
6144
|
-
this.o.onSlam?.(b, hit, into);
|
|
6145
|
-
}
|
|
6146
|
-
}
|
|
6147
|
-
/**
|
|
6148
|
-
* 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
|
|
6149
|
-
* gravity gave the centre turns into a swing about the hinge.
|
|
6150
|
-
*/
|
|
6151
|
-
holdOnHinge(b, hinge, q) {
|
|
6152
|
-
const e = this.hingeM.makeRotationFromQuaternion(q).elements;
|
|
6153
|
-
const [lx, ly, lz] = hinge.local;
|
|
6154
|
-
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];
|
|
6155
|
-
const v = b.velocity;
|
|
6156
|
-
const w = b.angularVelocity;
|
|
6157
|
-
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]];
|
|
6158
|
-
const k = [0, 1, 2].map((i) => {
|
|
6159
|
-
const d = [i === 0 ? 1 : 0, i === 1 ? 1 : 0, i === 2 ? 1 : 0];
|
|
6160
|
-
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]);
|
|
6161
|
-
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]];
|
|
6162
|
-
});
|
|
6163
|
-
const j = solve3(k, [-point[0], -point[1], -point[2]]);
|
|
6164
|
-
if (!j) return;
|
|
6165
|
-
for (let i = 0; i < 3; i++) v[i] += j[i] / b.mass;
|
|
6166
|
-
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]);
|
|
6167
|
-
for (let i = 0; i < 3; i++) w[i] += dw[i];
|
|
6168
|
-
}
|
|
6169
|
-
/** Puts the hinge point back where it hangs (what the step's rounding moved it by), moving the centre. */
|
|
6170
|
-
keepOnHinge(hinge, pos, q) {
|
|
6171
|
-
const e = this.hingeM.makeRotationFromQuaternion(q).elements;
|
|
6172
|
-
const [lx, ly, lz] = hinge.local;
|
|
6173
|
-
pos[0] = hinge.point[0] - (e[0] * lx + e[4] * ly + e[8] * lz);
|
|
6174
|
-
pos[1] = hinge.point[1] - (e[1] * lx + e[5] * ly + e[9] * lz);
|
|
6175
|
-
pos[2] = hinge.point[2] - (e[2] * lx + e[6] * ly + e[10] * lz);
|
|
6176
|
-
}
|
|
6177
|
-
/** The screw tears out: past `HINGE_RELEASE` of turn, or after `HINGE_SECONDS`, the piece falls free. */
|
|
6178
|
-
wearHinge(b, hinge, q, dt) {
|
|
6179
|
-
hinge.seconds -= dt;
|
|
6180
|
-
const [x, y, z, w] = hinge.from;
|
|
6181
|
-
const turned = 2 * Math.acos(Math.min(1, Math.abs(q.x * x + q.y * y + q.z * z + q.w * w)));
|
|
6182
|
-
if (turned >= HINGE_RELEASE || hinge.seconds <= 0) b.hinge = void 0;
|
|
6183
|
-
}
|
|
6184
|
-
integrate(b, dt) {
|
|
6185
|
-
const c = this.centerOf(b, this.p);
|
|
6186
|
-
const pos = [c.x, c.y, c.z];
|
|
6187
|
-
const start = [c.x, c.y, c.z];
|
|
6188
|
-
const q = this.q.copy(b.root.quaternion);
|
|
6189
|
-
const v = b.velocity;
|
|
6190
|
-
const w = b.angularVelocity;
|
|
6191
|
-
const invM = 1 / b.mass;
|
|
6192
|
-
const steps = clamp(Math.ceil(dt / SUB_DT), 1, 8);
|
|
6193
|
-
const h = dt / steps;
|
|
6194
|
-
let landing = 0;
|
|
6195
|
-
let landPoint = [pos[0], pos[1], pos[2]];
|
|
6196
|
-
let contactedLast = false;
|
|
6197
|
-
const propped = b.propped;
|
|
6198
|
-
const proppedAt = b.proppedAt;
|
|
6199
|
-
b.propped = void 0;
|
|
6200
|
-
b.proppedAt = void 0;
|
|
6201
|
-
let touched = false;
|
|
6202
|
-
let unstable = false;
|
|
6203
|
-
const corners = this.samples(b, false);
|
|
6204
|
-
this.m.makeRotationFromQuaternion(q);
|
|
6205
|
-
let gLow = Infinity;
|
|
6206
|
-
let gHigh = -Infinity;
|
|
6207
|
-
for (const [sx, sy, sz] of corners) {
|
|
6208
|
-
const e0 = this.m.elements;
|
|
6209
|
-
const y = pos[1] + e0[1] * sx + e0[5] * sy + e0[9] * sz;
|
|
6210
|
-
const x = pos[0] + e0[0] * sx + e0[4] * sy + e0[8] * sz;
|
|
6211
|
-
const z = pos[2] + e0[2] * sx + e0[6] * sy + e0[10] * sz;
|
|
6212
|
-
const g = Math.max(this.o.groundAt(x, z), this.o.staticTopAt?.(x, z, y, STEP) ?? -Infinity);
|
|
6213
|
-
gLow = Math.min(gLow, g);
|
|
6214
|
-
gHigh = Math.max(gHigh, g);
|
|
6215
|
-
}
|
|
6216
|
-
let local = b.round ? this.roundSamples(b, q) : gHigh - gLow > STEP ? this.samples(b, true) : corners;
|
|
6217
|
-
let prev = local.map(() => [0, 0, 0]);
|
|
6218
|
-
for (let s = 0; s < steps; s++) {
|
|
6219
|
-
if (b.round) {
|
|
6220
|
-
const wl0 = Math.hypot(w[0], w[1], w[2]);
|
|
6221
|
-
const next = this.nextQ.copy(q);
|
|
6222
|
-
if (wl0 > 1e-6) next.premultiply(this.dq.setFromAxisAngle(this.ax.set(w[0] / wl0, w[1] / wl0, w[2] / wl0), wl0 * h)).normalize();
|
|
6223
|
-
local = this.roundSamples(b, next);
|
|
6224
|
-
if (prev.length !== local.length) prev = local.map(() => [0, 0, 0]);
|
|
6225
|
-
}
|
|
6226
|
-
this.m.makeRotationFromQuaternion(q);
|
|
6227
|
-
const e0 = this.m.elements;
|
|
6228
|
-
for (let i = 0; i < local.length; i++) {
|
|
6229
|
-
const [sx, sy, sz] = local[i];
|
|
6230
|
-
prev[i][0] = pos[0] + e0[0] * sx + e0[4] * sy + e0[8] * sz;
|
|
6231
|
-
prev[i][1] = pos[1] + e0[1] * sx + e0[5] * sy + e0[9] * sz;
|
|
6232
|
-
prev[i][2] = pos[2] + e0[2] * sx + e0[6] * sy + e0[10] * sz;
|
|
6233
|
-
}
|
|
6234
|
-
v[1] -= this.o.gravity * h;
|
|
6235
|
-
if (b.hinge) this.holdOnHinge(b, b.hinge, q);
|
|
6236
|
-
if (propped) {
|
|
6237
|
-
const into = v[0] * propped[0] + v[1] * propped[1] + v[2] * propped[2];
|
|
6238
|
-
if (into < 0) for (let k = 0; k < 3; k++) v[k] -= into * propped[k];
|
|
6239
|
-
}
|
|
6240
|
-
pos[0] += v[0] * h;
|
|
6241
|
-
pos[1] += v[1] * h;
|
|
6242
|
-
pos[2] += v[2] * h;
|
|
6243
|
-
const wl = Math.hypot(w[0], w[1], w[2]);
|
|
6244
|
-
if (wl > 1e-6) {
|
|
6245
|
-
this.dq.setFromAxisAngle(this.ax.set(w[0] / wl, w[1] / wl, w[2] / wl), wl * h);
|
|
6246
|
-
q.premultiply(this.dq).normalize();
|
|
6247
|
-
}
|
|
6248
|
-
if (b.hinge) this.keepOnHinge(b.hinge, pos, q);
|
|
6249
|
-
const damp = b.round ? 1 : Math.exp(-0.15 * h);
|
|
6250
|
-
w[0] *= damp;
|
|
6251
|
-
w[1] *= damp;
|
|
6252
|
-
w[2] *= damp;
|
|
6253
|
-
this.m.makeRotationFromQuaternion(q);
|
|
6254
|
-
const e = this.m.elements;
|
|
6255
|
-
const rs = [];
|
|
6256
|
-
const ids = [];
|
|
6257
|
-
const ns = [];
|
|
6258
|
-
const mus = [];
|
|
6259
|
-
let maxPen = 0;
|
|
6260
|
-
let penNormal = UP;
|
|
6261
|
-
const rise = STEP + Math.max(0, -v[1]) * h * 2;
|
|
6262
|
-
let side = null;
|
|
6263
|
-
let lowest = Infinity;
|
|
6264
|
-
for (let i = 0; i < local.length; i++) {
|
|
6265
|
-
const [sx, sy, sz] = local[i];
|
|
6266
|
-
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];
|
|
6267
|
-
const px = pos[0] + r[0];
|
|
6268
|
-
const pz = pos[2] + r[2];
|
|
6269
|
-
const cy = pos[1] + r[1];
|
|
6270
|
-
const was = prev[i];
|
|
6271
|
-
const raw = this.o.groundAt(px, pz);
|
|
6272
|
-
if (raw > cy - SLOP && was[1] < raw - STEP) {
|
|
6273
|
-
if (this.o.groundAt(was[0], was[2]) >= raw - STEP) {
|
|
6274
|
-
const floor = b.floor;
|
|
6275
|
-
if (floor !== void 0 && floor - cy > -SLOP && floor < raw - STEP) {
|
|
6276
|
-
rs.push(r);
|
|
6277
|
-
ids.push(i);
|
|
6278
|
-
ns.push(UP);
|
|
6279
|
-
mus.push(pairFriction(b.friction, b.round ? this.terrainSurface(px, floor, pz, floor) : null));
|
|
6280
|
-
maxPen = Math.max(maxPen, floor - cy);
|
|
6281
|
-
}
|
|
6282
|
-
continue;
|
|
6283
|
-
}
|
|
6284
|
-
const dx = px - was[0];
|
|
6285
|
-
const dz = pz - was[2];
|
|
6286
|
-
const len = Math.hypot(dx, dz);
|
|
6287
|
-
if (len > 1e-6) {
|
|
6288
|
-
let lo = 0;
|
|
6289
|
-
let hi = 1;
|
|
6290
|
-
for (let it = 0; it < 6; it++) {
|
|
6291
|
-
const mid = (lo + hi) / 2;
|
|
6292
|
-
if (this.o.groundAt(was[0] + dx * mid, was[2] + dz * mid) >= raw - STEP) hi = mid;
|
|
6293
|
-
else lo = mid;
|
|
6294
|
-
}
|
|
6295
|
-
const back = (1 - lo) * len + 1e-3;
|
|
6296
|
-
if (!side || back > side.back) side = { back, dir: [dx / len, dz / len], r };
|
|
6297
|
-
}
|
|
6298
|
-
continue;
|
|
6299
|
-
}
|
|
6300
|
-
this.supportNormal = UP;
|
|
6301
|
-
this.supportFriction = null;
|
|
6302
|
-
const gy = i < 8 || b.contacts ? this.supportAt(b, px, cy, pz, rise) : Math.max(raw, this.o.staticTopAt?.(px, pz, cy, rise) ?? -Infinity);
|
|
6303
|
-
const pen = gy - cy;
|
|
6304
|
-
if (pen > -SLOP) {
|
|
6305
|
-
rs.push(r);
|
|
6306
|
-
ids.push(i);
|
|
6307
|
-
ns.push(this.supportNormal);
|
|
6308
|
-
mus.push(pairFriction(b.friction, this.supportFriction ?? (b.round ? this.terrainSurface(px, gy, pz, raw) : null)));
|
|
6309
|
-
if (pen > maxPen) {
|
|
6310
|
-
maxPen = pen;
|
|
6311
|
-
penNormal = this.supportNormal;
|
|
6312
|
-
}
|
|
6313
|
-
lowest = Math.min(lowest, gy);
|
|
6314
|
-
}
|
|
6315
|
-
}
|
|
6316
|
-
if (lowest < Infinity) b.floor = lowest;
|
|
6317
|
-
if (b.round && ns.length > 0) {
|
|
6318
|
-
const sum = [0, 0, 0];
|
|
6319
|
-
for (const one of ns) for (let k = 0; k < 3; k++) sum[k] += one[k];
|
|
6320
|
-
const len = Math.hypot(sum[0], sum[1], sum[2]);
|
|
6321
|
-
b.surfaceNormal = len > 1e-6 ? [sum[0] / len, sum[1] / len, sum[2] / len] : UP;
|
|
6322
|
-
} else if (b.round && rs.length === 0) b.surfaceNormal = UP;
|
|
6323
|
-
if (side) {
|
|
6324
|
-
pos[0] -= side.dir[0] * side.back;
|
|
6325
|
-
pos[2] -= side.dir[1] * side.back;
|
|
6326
|
-
this.contactImpulse(b, e, side.r, [-side.dir[0], 0, -side.dir[1]], SIDE_BOUNCE);
|
|
6327
|
-
}
|
|
6328
|
-
contactedLast = rs.length > 0;
|
|
6329
|
-
if (contactedLast) touched = true;
|
|
6330
|
-
if (!contactedLast) continue;
|
|
6331
|
-
const out = Math.max(0, maxPen - SLOP * 0.5) * penNormal[1];
|
|
6332
|
-
pos[0] += penNormal[0] * out;
|
|
6333
|
-
pos[1] += penNormal[1] * out;
|
|
6334
|
-
pos[2] += penNormal[2] * out;
|
|
6335
|
-
let impact2 = 0;
|
|
6336
|
-
for (const r of rs) impact2 = Math.max(impact2, -(v[1] + w[2] * r[0] - w[0] * r[2]));
|
|
6337
|
-
const restitution = impact2 > 2 ? 0.25 : 0;
|
|
6338
|
-
if (impact2 > landing) {
|
|
6339
|
-
landing = impact2;
|
|
6340
|
-
let sx = 0;
|
|
6341
|
-
let sz = 0;
|
|
6342
|
-
for (const r of rs) {
|
|
6343
|
-
sx += r[0];
|
|
6344
|
-
sz += r[2];
|
|
6345
|
-
}
|
|
6346
|
-
const lx = pos[0] + sx / rs.length;
|
|
6347
|
-
const lz = pos[2] + sz / rs.length;
|
|
6348
|
-
landPoint = [lx, pos[1] - Math.max(...rs.map((r) => -r[1])), lz];
|
|
6349
|
-
}
|
|
6350
|
-
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]];
|
|
6351
|
-
const dot2 = (a, c2) => a[0] * c2[0] + a[1] * c2[1] + a[2] * c2[2];
|
|
6352
|
-
const goal = rs.map((r, k) => {
|
|
6353
|
-
const vn = dot2(pointV(r), ns[k]);
|
|
6354
|
-
return vn < 0 ? -restitution * vn : 0;
|
|
6355
|
-
});
|
|
6356
|
-
const slipping = rs.map((r, k) => {
|
|
6357
|
-
const pv = pointV(r);
|
|
6358
|
-
pv[1] += this.o.gravity * h;
|
|
6359
|
-
const vn = dot2(pv, ns[k]);
|
|
6360
|
-
return Math.hypot(pv[0] - vn * ns[k][0], pv[1] - vn * ns[k][1], pv[2] - vn * ns[k][2]) > SLIP_SPEED;
|
|
6361
|
-
});
|
|
6362
|
-
const vx0 = v[0];
|
|
6363
|
-
const vz0 = v[2];
|
|
6364
|
-
if (!b.warm || b.warm.length !== WARM_SLOTS * 4) b.warm = new Float64Array(WARM_SLOTS * 4);
|
|
6365
|
-
const warm = b.warm;
|
|
6366
|
-
const accN = ids.map((id) => id < WARM_SLOTS ? warm[id * 4] : 0);
|
|
6367
|
-
const accT = ids.map((id) => id < WARM_SLOTS ? [warm[id * 4 + 1], warm[id * 4 + 2], warm[id * 4 + 3]] : [0, 0, 0]);
|
|
6368
|
-
const push = (r, j) => {
|
|
6369
|
-
v[0] += j[0] * invM;
|
|
6370
|
-
v[1] += j[1] * invM;
|
|
6371
|
-
v[2] += j[2] * invM;
|
|
6372
|
-
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]);
|
|
6373
|
-
w[0] += dw[0];
|
|
6374
|
-
w[1] += dw[1];
|
|
6375
|
-
w[2] += dw[2];
|
|
6376
|
-
};
|
|
6377
|
-
const reach = (r, d) => {
|
|
6378
|
-
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]);
|
|
6379
|
-
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];
|
|
6380
|
-
};
|
|
6381
|
-
for (let k = 0; k < rs.length; k++) {
|
|
6382
|
-
const n = ns[k];
|
|
6383
|
-
const t = accT[k];
|
|
6384
|
-
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]]);
|
|
6385
|
-
}
|
|
6386
|
-
for (let it = 0; it < SOLVER_PASSES; it++) {
|
|
6387
|
-
for (let k = 0; k < rs.length; k++) {
|
|
6388
|
-
const r = rs[k];
|
|
6389
|
-
const n = ns[k];
|
|
6390
|
-
const vn = dot2(pointV(r), n);
|
|
6391
|
-
const total = Math.max(0, accN[k] + (goal[k] - vn) / reach(r, n));
|
|
6392
|
-
const j = total - accN[k];
|
|
6393
|
-
accN[k] = total;
|
|
6394
|
-
if (j !== 0) push(r, [n[0] * j, n[1] * j, n[2] * j]);
|
|
6395
|
-
const pv = pointV(r);
|
|
6396
|
-
const pn = dot2(pv, n);
|
|
6397
|
-
const vt = [pv[0] - pn * n[0], pv[1] - pn * n[1], pv[2] - pn * n[2]];
|
|
6398
|
-
const speed = Math.hypot(vt[0], vt[1], vt[2]);
|
|
6399
|
-
const f = [accT[k][0], accT[k][1], accT[k][2]];
|
|
6400
|
-
if (speed > 1e-6) {
|
|
6401
|
-
const d = [vt[0] / speed, vt[1] / speed, vt[2] / speed];
|
|
6402
|
-
const jt = speed / reach(r, d);
|
|
6403
|
-
f[0] -= d[0] * jt;
|
|
6404
|
-
f[1] -= d[1] * jt;
|
|
6405
|
-
f[2] -= d[2] * jt;
|
|
6406
|
-
} else if (f[0] === 0 && f[1] === 0 && f[2] === 0) continue;
|
|
6407
|
-
const most = (slipping[k] ? mus[k].dynamic : mus[k].static) * accN[k];
|
|
6408
|
-
const fl = Math.hypot(f[0], f[1], f[2]);
|
|
6409
|
-
if (fl > most) {
|
|
6410
|
-
f[0] *= most / fl;
|
|
6411
|
-
f[1] *= most / fl;
|
|
6412
|
-
f[2] *= most / fl;
|
|
6413
|
-
}
|
|
6414
|
-
const dj = [f[0] - accT[k][0], f[1] - accT[k][1], f[2] - accT[k][2]];
|
|
6415
|
-
accT[k] = f;
|
|
6416
|
-
if (dj[0] !== 0 || dj[1] !== 0 || dj[2] !== 0) push(r, dj);
|
|
6417
|
-
}
|
|
6418
|
-
}
|
|
6419
|
-
pos[0] += (v[0] - vx0) * h;
|
|
6420
|
-
pos[2] += (v[2] - vz0) * h;
|
|
6421
|
-
warm.fill(0);
|
|
6422
|
-
for (let k = 0; k < rs.length; k++) {
|
|
6423
|
-
const id = ids[k];
|
|
6424
|
-
if (id >= WARM_SLOTS) continue;
|
|
6425
|
-
warm[id * 4] = accN[k];
|
|
6426
|
-
warm[id * 4 + 1] = accT[k][0];
|
|
6427
|
-
warm[id * 4 + 2] = accT[k][1];
|
|
6428
|
-
warm[id * 4 + 3] = accT[k][2];
|
|
6429
|
-
}
|
|
6430
|
-
let x0 = Infinity, x1 = -Infinity, z0 = Infinity, z1 = -Infinity, low = 0;
|
|
6431
|
-
for (const r of rs) {
|
|
6432
|
-
x0 = Math.min(x0, r[0]);
|
|
6433
|
-
x1 = Math.max(x1, r[0]);
|
|
6434
|
-
z0 = Math.min(z0, r[2]);
|
|
6435
|
-
z1 = Math.max(z1, r[2]);
|
|
6436
|
-
low = Math.min(low, r[1]);
|
|
6437
|
-
}
|
|
6438
|
-
if (proppedAt) {
|
|
6439
|
-
x0 = Math.min(x0, proppedAt[0]);
|
|
6440
|
-
x1 = Math.max(x1, proppedAt[0]);
|
|
6441
|
-
z0 = Math.min(z0, proppedAt[2]);
|
|
6442
|
-
z1 = Math.max(z1, proppedAt[2]);
|
|
6443
|
-
}
|
|
6444
|
-
const height = -low;
|
|
6445
|
-
const wx = x1 - x0;
|
|
6446
|
-
const wz = z1 - z0;
|
|
6447
|
-
const pastX = x0 > -SUPPORT_MARGIN * wx || x1 < SUPPORT_MARGIN * wx;
|
|
6448
|
-
const pastZ = z0 > -SUPPORT_MARGIN * wz || z1 < SUPPORT_MARGIN * wz;
|
|
6449
|
-
unstable = !b.round && (Math.min(wx, wz) < NARROW2 * height || pastX || pastZ);
|
|
6450
|
-
if (b.tipping) {
|
|
6451
|
-
b.tipping.seconds -= h;
|
|
6452
|
-
const [hx, hy, hz] = b.halfExtents;
|
|
6453
|
-
const upHalf = Math.abs(e[1]) * hx + Math.abs(e[5]) * hy + Math.abs(e[9]) * hz;
|
|
6454
|
-
const rising = TIP_SECONDS - b.tipping.seconds > TIP_RISE_AFTER && v[1] > TIP_RISING;
|
|
6455
|
-
if (upHalf < TIP_LYING || b.tipping.seconds <= 0 || rising) b.tipping = null;
|
|
6456
|
-
else {
|
|
6457
|
-
w[0] += b.tipping.x * TIP_ACCELERATION * h;
|
|
6458
|
-
w[2] += b.tipping.z * TIP_ACCELERATION * h;
|
|
6459
|
-
unstable = true;
|
|
6460
|
-
}
|
|
6461
|
-
}
|
|
6462
|
-
if (unstable) {
|
|
6463
|
-
const dirX = x0 + x1 > 0 ? -1 : 1;
|
|
6464
|
-
const dirZ = z0 + z1 > 0 ? -1 : 1;
|
|
6465
|
-
const k = TOPPLE2 * Math.min(height, 1) * h;
|
|
6466
|
-
const overZ = pastX === pastZ ? wx <= wz : pastX;
|
|
6467
|
-
if (overZ) w[2] -= dirX * k;
|
|
6468
|
-
else w[0] += dirZ * k;
|
|
6469
|
-
}
|
|
6470
|
-
if (b.round) {
|
|
6471
|
-
let rolling = 0;
|
|
6472
|
-
let damping = 0;
|
|
6473
|
-
for (const mu of mus) {
|
|
6474
|
-
rolling = Math.max(rolling, mu.rolling ?? ROLLING_RESISTANCE2);
|
|
6475
|
-
damping = Math.max(damping, mu.rollingDamping ?? 0);
|
|
6476
|
-
}
|
|
6477
|
-
this.rollingResistance(b, q, w, h, { rolling, damping });
|
|
6478
|
-
} else {
|
|
6479
|
-
const rd = Math.exp((rs.length >= 3 ? -3 : -1.5) * h);
|
|
6480
|
-
w[0] *= rd;
|
|
6481
|
-
w[1] *= rd;
|
|
6482
|
-
w[2] *= rd;
|
|
6483
|
-
const fd = Math.exp(-0.3 * h);
|
|
6484
|
-
v[0] *= fd;
|
|
6485
|
-
v[2] *= fd;
|
|
6486
|
-
}
|
|
6487
|
-
}
|
|
6488
|
-
if (b.hinge) this.wearHinge(b, b.hinge, q, dt);
|
|
6489
|
-
if (this.o.sweep && !b.held) this.sweepOn(b, start, pos);
|
|
6490
|
-
b.root.quaternion.copy(q);
|
|
6491
|
-
const off2 = this.ax.copy(b.centerV).applyQuaternion(q);
|
|
6492
|
-
b.root.position.set(pos[0] - off2.x, pos[1] - off2.y, pos[2] - off2.z);
|
|
6493
|
-
b.root.updateMatrixWorld(true);
|
|
6494
|
-
b.onGround = contactedLast;
|
|
6495
|
-
if (landing > HARD_LANDING2 && this.time - b.lastHit > 0.15) {
|
|
6496
|
-
b.lastHit = this.time;
|
|
6497
|
-
this.o.onHardLanding(b, landPoint, landing);
|
|
6498
|
-
}
|
|
6499
|
-
const still = touched && !unstable && Math.hypot(v[0], v[1], v[2]) < STILL_SPEED2 && Math.hypot(w[0], w[1], w[2]) < STILL_SPIN2;
|
|
6500
|
-
if (still) b.still += dt;
|
|
6501
|
-
else b.still = 0;
|
|
6502
|
-
b.balancing = unstable;
|
|
6503
|
-
this.keepsTilt(b, q, dt);
|
|
6504
|
-
const held = (touched || propped !== void 0) && !b.tipping;
|
|
6505
|
-
if (!b.hinge && (b.still >= SLEEP_AFTER || this.staysInPlace(b, held, pos, q, dt)) && !this.onSteepSupport(b)) this.sleep(b);
|
|
6506
|
-
}
|
|
6507
5996
|
/**
|
|
6508
5997
|
* Whether `b` lies flat on a settled top (asleep) tilted past what the friction of the two holds (a laptop on a table top gone over at 42 degrees, held by 0.49 up to 26),
|
|
6509
5998
|
* with every corner that touches on it: it slides, it does not sleep there, and one asleep there wakes. Held by anything else (the floor, a flat top, a side it leans on, a
|
|
@@ -6537,15 +6026,6 @@ var PieceSystem = class {
|
|
|
6537
6026
|
b.velocity[0] = b.velocity[1] = b.velocity[2] = 0;
|
|
6538
6027
|
b.angularVelocity[0] = b.angularVelocity[1] = b.angularVelocity[2] = 0;
|
|
6539
6028
|
}
|
|
6540
|
-
/** 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. */
|
|
6541
|
-
keepsTilt(b, q, dt) {
|
|
6542
|
-
const at = b.steady;
|
|
6543
|
-
if (at && turnBetween(at.quaternion, q) <= STEADY_TURN2) {
|
|
6544
|
-
at.seconds += dt;
|
|
6545
|
-
return;
|
|
6546
|
-
}
|
|
6547
|
-
b.steady = { quaternion: [q.x, q.y, q.z, q.w], seconds: 0 };
|
|
6548
|
-
}
|
|
6549
6029
|
/**
|
|
6550
6030
|
* Over `maxAwake` pieces awake, the oldest that have come to rest sleep where they lie: on the ground, slower than `STILL_SPEED` and `STILL_SPIN`, not falling over (`tip`),
|
|
6551
6031
|
* and one held up at an angle (a board leaning on a seat) only once it kept its tilt for `STEADY_SECONDS`; that one sleeps then under the budget too, since nothing else puts
|
|
@@ -6656,118 +6136,6 @@ var PieceSystem = class {
|
|
|
6656
6136
|
const y = c.y + e[1] * sx + e[5] * sy + e[9] * sz;
|
|
6657
6137
|
return this.supportAt(b, c.x + e[0] * sx + e[4] * sy + e[8] * sz, y, c.z + e[2] * sx + e[6] * sy + e[10] * sz, STEP) >= y - SUPPORT_GAP2;
|
|
6658
6138
|
}
|
|
6659
|
-
/** Whether the piece has kept to the same spot on the ground for `CALM_SLEEP_AFTER` (see `CALM_DRIFT`). */
|
|
6660
|
-
staysInPlace(b, touched, center, q, dt) {
|
|
6661
|
-
const at = b.calmAt;
|
|
6662
|
-
const turned = at ? turnBetween(at.quaternion, q) : Infinity;
|
|
6663
|
-
if (!touched || !at || Math.hypot(center[0] - at.center[0], center[1] - at.center[1], center[2] - at.center[2]) > CALM_DRIFT || turned > CALM_TURN) {
|
|
6664
|
-
b.calmAt = touched ? { center: [center[0], center[1], center[2]], quaternion: [q.x, q.y, q.z, q.w] } : null;
|
|
6665
|
-
b.calm = 0;
|
|
6666
|
-
return false;
|
|
6667
|
-
}
|
|
6668
|
-
b.calm += dt;
|
|
6669
|
-
return b.calm >= CALM_SLEEP_AFTER;
|
|
6670
|
-
}
|
|
6671
|
-
/** Whether the centre `c` of `upper` is over the top of `lower`'s box (as it was at the start of the update). */
|
|
6672
|
-
onTopOf(upper, lower, c) {
|
|
6673
|
-
const s = this.solidOf.get(lower);
|
|
6674
|
-
if (!s) return false;
|
|
6675
|
-
const top = boxTopAt(s, c.x, c.z);
|
|
6676
|
-
return top > -Infinity && c.y >= top - 0.01;
|
|
6677
|
-
}
|
|
6678
|
-
/**
|
|
6679
|
-
* 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
|
|
6680
|
-
* 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.
|
|
6681
|
-
*/
|
|
6682
|
-
roundSamples(b, q) {
|
|
6683
|
-
const round = b.round;
|
|
6684
|
-
const rims = b.contacts ?? [];
|
|
6685
|
-
if (!round) return rims;
|
|
6686
|
-
const e = this.m.makeRotationFromQuaternion(q).elements;
|
|
6687
|
-
const n = b.surfaceNormal ?? UP;
|
|
6688
|
-
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])];
|
|
6689
|
-
if (round.kind === "sphere") return [...rims, [down[0] * round.radius, down[1] * round.radius, down[2] * round.radius]];
|
|
6690
|
-
const k = round.axis;
|
|
6691
|
-
const across = [down[0], down[1], down[2]];
|
|
6692
|
-
across[k] = 0;
|
|
6693
|
-
const len = Math.hypot(across[0], across[1], across[2]);
|
|
6694
|
-
if (len < 1e-3) return rims;
|
|
6695
|
-
const lowest = (at, radius) => {
|
|
6696
|
-
const p = [across[0] / len * radius, across[1] / len * radius, across[2] / len * radius];
|
|
6697
|
-
p[k] = at;
|
|
6698
|
-
return p;
|
|
6699
|
-
};
|
|
6700
|
-
const half = b.halfExtents[k];
|
|
6701
|
-
const low = (round.profile ?? [[-half, round.radius], [half, round.radius]]).map(([at, radius]) => lowest(at, radius));
|
|
6702
|
-
return len > ROUND_LYING ? low : [...rims, ...low];
|
|
6703
|
-
}
|
|
6704
|
-
/**
|
|
6705
|
-
* 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
|
|
6706
|
-
* 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
|
|
6707
|
-
* 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`,
|
|
6708
|
-
* the pair's) then takes exp(-d h) of what is left, as the grass or the sand drag more the faster it rolls.
|
|
6709
|
-
*/
|
|
6710
|
-
rollingResistance(b, q, w, h, { rolling, damping }) {
|
|
6711
|
-
const round = b.round;
|
|
6712
|
-
if (!round) return;
|
|
6713
|
-
const e = this.m.makeRotationFromQuaternion(q).elements;
|
|
6714
|
-
const cylinder = round.kind === "cylinder";
|
|
6715
|
-
const a = cylinder ? [e[round.axis * 4], e[round.axis * 4 + 1], e[round.axis * 4 + 2]] : [0, 0, 0];
|
|
6716
|
-
const along2 = cylinder ? w[0] * a[0] + w[1] * a[1] + w[2] * a[2] : 0;
|
|
6717
|
-
const roll = cylinder ? [a[0] * along2, a[1] * along2, a[2] * along2] : [w[0], w[1], w[2]];
|
|
6718
|
-
const rest = [w[0] - roll[0], w[1] - roll[1], w[2] - roll[2]];
|
|
6719
|
-
const spin = Math.hypot(roll[0], roll[1], roll[2]);
|
|
6720
|
-
const upright = b.surfaceNormal?.[1] ?? 1;
|
|
6721
|
-
const slow = spin > 0 ? Math.max(0, spin - rolling * this.o.gravity * upright * h / (round.inertia * round.radius)) / spin * Math.exp(-damping * h) : 0;
|
|
6722
|
-
const lying = cylinder && Math.abs(a[1]) < ROUND_LYING;
|
|
6723
|
-
const damp = lying ? 1 : Math.exp(-3 * h);
|
|
6724
|
-
for (let i = 0; i < 3; i++) w[i] = roll[i] * slow + rest[i] * damp;
|
|
6725
|
-
}
|
|
6726
|
-
/** Half the height of the box of a piece as it is turned now. */
|
|
6727
|
-
halfHeight(b) {
|
|
6728
|
-
const e = this.m.makeRotationFromQuaternion(b.root.quaternion).elements;
|
|
6729
|
-
return Math.abs(e[1]) * b.halfExtents[0] + Math.abs(e[5]) * b.halfExtents[1] + Math.abs(e[9]) * b.halfExtents[2];
|
|
6730
|
-
}
|
|
6731
|
-
/**
|
|
6732
|
-
* 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
|
|
6733
|
-
* 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`).
|
|
6734
|
-
*/
|
|
6735
|
-
collide(dt, meets) {
|
|
6736
|
-
const n = this.list.length;
|
|
6737
|
-
if (n < 2) return;
|
|
6738
|
-
const centers = this.list.map((b) => {
|
|
6739
|
-
const c = this.centerOf(b, this.p);
|
|
6740
|
-
return [c.x, c.y, c.z];
|
|
6741
|
-
});
|
|
6742
|
-
const radii = this.list.map((b) => Math.hypot(b.halfExtents[0], b.halfExtents[1], b.halfExtents[2]));
|
|
6743
|
-
const order = this.list.map((_, i) => i).sort((i, j) => centers[i][0] - radii[i] - (centers[j][0] - radii[j]));
|
|
6744
|
-
for (let oi = 0; oi < n; oi++) {
|
|
6745
|
-
const i = order[oi];
|
|
6746
|
-
const reach = centers[i][0] + radii[i];
|
|
6747
|
-
for (let oj = oi + 1; oj < n; oj++) {
|
|
6748
|
-
const j = order[oj];
|
|
6749
|
-
if (centers[j][0] - radii[j] > reach) break;
|
|
6750
|
-
const a = this.list[i];
|
|
6751
|
-
const b = this.list[j];
|
|
6752
|
-
if ((a.held || a.sleeping) && (b.held || b.sleeping)) continue;
|
|
6753
|
-
if (meets && !meets(a, b)) continue;
|
|
6754
|
-
const r = radii[i] + radii[j];
|
|
6755
|
-
const ca = centers[i];
|
|
6756
|
-
const cb = centers[j];
|
|
6757
|
-
if (Math.abs(ca[1] - cb[1]) > r || Math.abs(ca[2] - cb[2]) > r) continue;
|
|
6758
|
-
const stacked = !a.round && !b.round && Math.abs(ca[1] - cb[1]) >= (this.halfHeight(a) + this.halfHeight(b)) * 0.9;
|
|
6759
|
-
const onTop = this.onTopOf(b, a, new this.THREE.Vector3(...cb)) || this.onTopOf(a, b, new this.THREE.Vector3(...ca));
|
|
6760
|
-
if (a.round || b.round) {
|
|
6761
|
-
if (!stacked && !onTop) this.knock(a, b, ca, cb);
|
|
6762
|
-
continue;
|
|
6763
|
-
}
|
|
6764
|
-
if (!this.partsOverlap(a, b)) continue;
|
|
6765
|
-
this.boxImpact(a, b, ca, cb, dt);
|
|
6766
|
-
this.separatePair(a, b, ca, cb, dt, stacked || onTop || this.restsOn(a, b, false) || this.restsOn(b, a, false));
|
|
6767
|
-
}
|
|
6768
|
-
}
|
|
6769
|
-
this.applyPushes(dt);
|
|
6770
|
-
}
|
|
6771
6139
|
/**
|
|
6772
6140
|
* Moves each piece by what its pairs put it apart this step (`pushes`), at most `SEPARATE_SPEED` along the ground and as much up: a chunk in five others is not pushed five
|
|
6773
6141
|
* times as fast, and nothing is ever moved down (into what holds it). A piece still being put apart is moving, though its speed is nil: it does not fall asleep in the other.
|
|
@@ -6802,129 +6170,6 @@ var PieceSystem = class {
|
|
|
6802
6170
|
last.push[2] = push[2];
|
|
6803
6171
|
return last.share;
|
|
6804
6172
|
}
|
|
6805
|
-
/** 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. */
|
|
6806
|
-
contactShape(b, c) {
|
|
6807
|
-
const e = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
|
|
6808
|
-
const round = b.round;
|
|
6809
|
-
if (round) {
|
|
6810
|
-
if (round.kind === "sphere") return { kind: "capsule", from: c, to: c, radius: round.radius };
|
|
6811
|
-
const reach = Math.max(0, b.halfExtents[round.axis] - round.radius);
|
|
6812
|
-
const k = round.axis * 4;
|
|
6813
|
-
const d = [e[k] * reach, e[k + 1] * reach, e[k + 2] * reach];
|
|
6814
|
-
return { kind: "capsule", from: [c[0] - d[0], c[1] - d[1], c[2] - d[2]], to: [c[0] + d[0], c[1] + d[1], c[2] + d[2]], radius: round.radius };
|
|
6815
|
-
}
|
|
6816
|
-
const parts = b.partBoxes ?? [{ center: [0, 0, 0], half: b.halfExtents }];
|
|
6817
|
-
return {
|
|
6818
|
-
kind: "boxes",
|
|
6819
|
-
boxes: parts.map(squareBoxOf).map(({ center, half }) => ({
|
|
6820
|
-
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]],
|
|
6821
|
-
e,
|
|
6822
|
-
half
|
|
6823
|
-
}))
|
|
6824
|
-
};
|
|
6825
|
-
}
|
|
6826
|
-
/**
|
|
6827
|
-
* 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
|
|
6828
|
-
* 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
|
|
6829
|
-
* 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.
|
|
6830
|
-
*/
|
|
6831
|
-
knock(a, b, ca, cb) {
|
|
6832
|
-
const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
|
|
6833
|
-
if (!hit) return;
|
|
6834
|
-
if (Math.abs(hit.normal[1]) > SLAM_FLOOR_NORMAL2) return;
|
|
6835
|
-
this.impact({ a, b, ca, cb, hit, apart: true, above: 0 });
|
|
6836
|
-
}
|
|
6837
|
-
/**
|
|
6838
|
-
* 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,
|
|
6839
|
-
* 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
|
|
6840
|
-
* made inside each other, are left to `separatePair`, which holds or slides them without a kick.
|
|
6841
|
-
*/
|
|
6842
|
-
boxImpact(a, b, ca, cb, dt) {
|
|
6843
|
-
const hit = shapeContact(this.contactShape(a, ca), this.contactShape(b, cb));
|
|
6844
|
-
if (!hit) return;
|
|
6845
|
-
const n = hit.normal;
|
|
6846
|
-
const va = a.velocity;
|
|
6847
|
-
const vb = b.velocity;
|
|
6848
|
-
const closing = (va[0] - vb[0]) * n[0] + (va[1] - vb[1]) * n[1] + (va[2] - vb[2]) * n[2];
|
|
6849
|
-
if (hit.depth > LIFT_DEPTH + 2 * Math.max(0, -closing) * dt) return;
|
|
6850
|
-
this.impact({ a, b, ca, cb, hit, apart: false, above: BOX_IMPACT_SPEED });
|
|
6851
|
-
}
|
|
6852
|
-
/** The impulse two pieces that met exchange (`knock`), put apart first when `apart`, and only when they close faster than `above` (m/s). */
|
|
6853
|
-
impact({ a, b, ca, cb, hit, apart: apart2, above }) {
|
|
6854
|
-
const n = hit.normal;
|
|
6855
|
-
const ra = [hit.point[0] - ca[0], hit.point[1] - ca[1], hit.point[2] - ca[2]];
|
|
6856
|
-
const rb = [hit.point[0] - cb[0], hit.point[1] - cb[1], hit.point[2] - cb[2]];
|
|
6857
|
-
const pointV = (body, r) => {
|
|
6858
|
-
const v2 = body.velocity;
|
|
6859
|
-
const w = body.angularVelocity;
|
|
6860
|
-
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]];
|
|
6861
|
-
};
|
|
6862
|
-
const relative = () => {
|
|
6863
|
-
const va = pointV(a, ra);
|
|
6864
|
-
const vb = pointV(b, rb);
|
|
6865
|
-
return [va[0] - vb[0], va[1] - vb[1], va[2] - vb[2]];
|
|
6866
|
-
};
|
|
6867
|
-
const vn0 = relative();
|
|
6868
|
-
const closing = vn0[0] * n[0] + vn0[1] * n[1] + vn0[2] * n[2];
|
|
6869
|
-
if (closing >= -above) return;
|
|
6870
|
-
const wake = (body) => {
|
|
6871
|
-
body.sleeping = false;
|
|
6872
|
-
body.still = 0;
|
|
6873
|
-
body.calmAt = null;
|
|
6874
|
-
};
|
|
6875
|
-
if (closing < -WAKE_ABOVE_SPEED) {
|
|
6876
|
-
if (a.sleeping) wake(a);
|
|
6877
|
-
if (b.sleeping) wake(b);
|
|
6878
|
-
}
|
|
6879
|
-
const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
|
|
6880
|
-
const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
|
|
6881
|
-
if (ima + imb === 0) return;
|
|
6882
|
-
const ea = [...this.m.makeRotationFromQuaternion(a.root.quaternion).elements];
|
|
6883
|
-
const eb = [...this.m.makeRotationFromQuaternion(b.root.quaternion).elements];
|
|
6884
|
-
const flat = Math.hypot(n[0], n[2]);
|
|
6885
|
-
const out = flat > KNOCK_FLAT ? [n[0] / flat, 0, n[2] / flat] : n;
|
|
6886
|
-
const depth = Math.max(0, hit.depth - SLOP) / (flat > KNOCK_FLAT ? flat : 1);
|
|
6887
|
-
for (const [body, share, sign] of [[a, ima, 1], [b, imb, -1]]) {
|
|
6888
|
-
if (share === 0 || !apart2) continue;
|
|
6889
|
-
const move = depth * share * sign / (ima + imb);
|
|
6890
|
-
body.root.position.x += out[0] * move;
|
|
6891
|
-
body.root.position.y += out[1] * move;
|
|
6892
|
-
body.root.position.z += out[2] * move;
|
|
6893
|
-
}
|
|
6894
|
-
if (closing >= 0) return;
|
|
6895
|
-
const turning = (body, e, r, d) => {
|
|
6896
|
-
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]);
|
|
6897
|
-
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];
|
|
6898
|
-
};
|
|
6899
|
-
const resist = (d) => ima + imb + (ima > 0 ? turning(a, ea, ra, d) : 0) + (imb > 0 ? turning(b, eb, rb, d) : 0);
|
|
6900
|
-
const give = (j2) => {
|
|
6901
|
-
if (ima > 0) {
|
|
6902
|
-
for (let i = 0; i < 3; i++) a.velocity[i] += j2[i] * ima;
|
|
6903
|
-
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]);
|
|
6904
|
-
for (let i = 0; i < 3; i++) a.angularVelocity[i] += dw[i];
|
|
6905
|
-
}
|
|
6906
|
-
if (imb > 0) {
|
|
6907
|
-
for (let i = 0; i < 3; i++) b.velocity[i] -= j2[i] * imb;
|
|
6908
|
-
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]);
|
|
6909
|
-
for (let i = 0; i < 3; i++) b.angularVelocity[i] -= dw[i];
|
|
6910
|
-
}
|
|
6911
|
-
};
|
|
6912
|
-
const bounce = apart2 && -closing > BOUNCE_MIN_SPEED ? pairRestitution(a.restitution, b.restitution) : 0;
|
|
6913
|
-
const j = -(1 + bounce) * closing / resist(n);
|
|
6914
|
-
give([n[0] * j, n[1] * j, n[2] * j]);
|
|
6915
|
-
const v = relative();
|
|
6916
|
-
const vn = v[0] * n[0] + v[1] * n[1] + v[2] * n[2];
|
|
6917
|
-
const vt = [v[0] - vn * n[0], v[1] - vn * n[1], v[2] - vn * n[2]];
|
|
6918
|
-
const slide = Math.hypot(vt[0], vt[1], vt[2]);
|
|
6919
|
-
if (slide < 1e-6) return;
|
|
6920
|
-
const t = [vt[0] / slide, vt[1] / slide, vt[2] / slide];
|
|
6921
|
-
const jt = Math.min(slide / resist(t), pairFriction(a.friction, b.friction ?? null).dynamic * j);
|
|
6922
|
-
give([-t[0] * jt, -t[1] * jt, -t[2] * jt]);
|
|
6923
|
-
if (-closing >= SLAM_MIN_SPEED2 && this.time - Math.max(a.lastHit, b.lastHit) > 0.15) {
|
|
6924
|
-
a.lastHit = b.lastHit = this.time;
|
|
6925
|
-
this.o.onKnock?.(a, b, hit.point, -closing);
|
|
6926
|
-
}
|
|
6927
|
-
}
|
|
6928
6173
|
/** Whether a part of `a` and a part of `b` (their boxes of the step, as world-aligned boxes around them) overlap. */
|
|
6929
6174
|
partsOverlap(a, b) {
|
|
6930
6175
|
for (const pa of this.surfacesOf.get(a) ?? []) {
|
|
@@ -6936,66 +6181,6 @@ var PieceSystem = class {
|
|
|
6936
6181
|
}
|
|
6937
6182
|
return false;
|
|
6938
6183
|
}
|
|
6939
|
-
/**
|
|
6940
|
-
* 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
|
|
6941
|
-
* summed into `pushes` and bounded per piece in `applyPushes`.
|
|
6942
|
-
* - 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.
|
|
6943
|
-
* - 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
|
|
6944
|
-
* 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.
|
|
6945
|
-
* - 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
|
|
6946
|
-
* 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`).
|
|
6947
|
-
*/
|
|
6948
|
-
separatePair(a, b, ca, cb, dt, resting) {
|
|
6949
|
-
const sa = this.contactShape(a, ca);
|
|
6950
|
-
const sb = this.contactShape(b, cb);
|
|
6951
|
-
const hit = shapeContact(sa, sb);
|
|
6952
|
-
if (!hit) return;
|
|
6953
|
-
const ima = a.held || a.sleeping ? 0 : 1 / a.mass;
|
|
6954
|
-
const imb = b.held || b.sleeping ? 0 : 1 / b.mass;
|
|
6955
|
-
if (ima + imb === 0) return;
|
|
6956
|
-
if (Math.hypot(hit.normal[0], hit.normal[2]) > KNOCK_FLAT) {
|
|
6957
|
-
this.slideApart(a, b, ima, imb, levelBoxesApart(sa, sb) ?? hit);
|
|
6958
|
-
return;
|
|
6959
|
-
}
|
|
6960
|
-
const n = hit.normal;
|
|
6961
|
-
const va = a.velocity;
|
|
6962
|
-
const vb = b.velocity;
|
|
6963
|
-
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;
|
|
6964
|
-
if (hit.depth > contact) {
|
|
6965
|
-
const level = boxesApartAlongGround(sa, sb);
|
|
6966
|
-
if (level) this.slideApart(a, b, ima, imb, level);
|
|
6967
|
-
return;
|
|
6968
|
-
}
|
|
6969
|
-
if (resting) return;
|
|
6970
|
-
const [upper, c, up] = n[1] > 0 ? [a, ca, n] : [b, cb, [-n[0], -n[1], -n[2]]];
|
|
6971
|
-
if (upper.held || upper.sleeping) return;
|
|
6972
|
-
const otherCentre = upper === a ? cb : ca;
|
|
6973
|
-
if (c[1] < otherCentre[1] - SLOP) {
|
|
6974
|
-
const level = boxesApartAlongGround(sa, sb);
|
|
6975
|
-
if (level) this.slideApart(a, b, ima, imb, level);
|
|
6976
|
-
return;
|
|
6977
|
-
}
|
|
6978
|
-
upper.propped = up;
|
|
6979
|
-
const e = this.m.makeRotationFromQuaternion(upper.root.quaternion).elements;
|
|
6980
|
-
const r = [hit.point[0] - c[0], hit.point[1] - c[1], hit.point[2] - c[2]];
|
|
6981
|
-
upper.proppedAt = r;
|
|
6982
|
-
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]);
|
|
6983
|
-
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];
|
|
6984
|
-
const lift = SEPARATE_SHARE * Math.max(0, hit.depth - SLOP) / (1 + angular * upper.mass);
|
|
6985
|
-
const push = this.pushes.get(upper) ?? [0, 0, 0];
|
|
6986
|
-
for (let i = 0; i < 3; i++) push[i] = Math.abs(up[i] * lift) > Math.abs(push[i]) ? up[i] * lift : push[i];
|
|
6987
|
-
this.pushes.set(upper, push);
|
|
6988
|
-
this.turnAboutCentre(upper, c, [turn2[0] * lift * upper.mass, turn2[1] * lift * upper.mass, turn2[2] * lift * upper.mass]);
|
|
6989
|
-
this.contactImpulse(upper, e, r, up, 0);
|
|
6990
|
-
}
|
|
6991
|
-
/** Turns a piece by the rotation vector `by` (rad) about its centre `c`, which stays where it is. */
|
|
6992
|
-
turnAboutCentre(b, c, by) {
|
|
6993
|
-
const angle = Math.hypot(by[0], by[1], by[2]);
|
|
6994
|
-
if (angle < 1e-9) return;
|
|
6995
|
-
b.root.quaternion.premultiply(this.dq.setFromAxisAngle(this.ax.set(by[0] / angle, by[1] / angle, by[2] / angle), angle)).normalize();
|
|
6996
|
-
const off2 = this.ax.copy(b.centerV).applyQuaternion(b.root.quaternion);
|
|
6997
|
-
b.root.position.set(c[0] - off2.x, c[1] - off2.y, c[2] - off2.z);
|
|
6998
|
-
}
|
|
6999
6184
|
/** Two boxes put apart along the ground by `hit` (its normal from `b` to `a`, laid level), split by their inverse masses; the speed they close in with along it stops. */
|
|
7000
6185
|
slideApart(a, b, ima, imb, hit) {
|
|
7001
6186
|
const n = hit.normal;
|
|
@@ -7017,12 +6202,6 @@ var PieceSystem = class {
|
|
|
7017
6202
|
}
|
|
7018
6203
|
}
|
|
7019
6204
|
};
|
|
7020
|
-
function solve3(k, rhs) {
|
|
7021
|
-
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]);
|
|
7022
|
-
const d = det(k[0], k[1], k[2]);
|
|
7023
|
-
if (Math.abs(d) < 1e-12) return null;
|
|
7024
|
-
return [det(rhs, k[1], k[2]) / d, det(k[0], rhs, k[2]) / d, det(k[0], k[1], rhs) / d];
|
|
7025
|
-
}
|
|
7026
6205
|
|
|
7027
6206
|
// src/rebar.ts
|
|
7028
6207
|
var RebarCages = class {
|
|
@@ -7387,7 +6566,6 @@ var CRATER_DEPTH = 0.3;
|
|
|
7387
6566
|
var CRATER_RADIUS = 2.5;
|
|
7388
6567
|
var ORIGIN = [4e3, 0, 4e3];
|
|
7389
6568
|
var warmed = /* @__PURE__ */ new WeakSet();
|
|
7390
|
-
var OWN_SOLVER = {};
|
|
7391
6569
|
var brickWall = (THREE) => {
|
|
7392
6570
|
const parts = [];
|
|
7393
6571
|
const geometries = [];
|
|
@@ -7417,7 +6595,7 @@ var brickWall = (THREE) => {
|
|
|
7417
6595
|
merged.setIndex(index);
|
|
7418
6596
|
return { parts, joints: inferJoints2(parts), mesh: new THREE.Mesh(merged, new THREE.MeshStandardMaterial()), ranges };
|
|
7419
6597
|
};
|
|
7420
|
-
var runScene = (THREE, makeRenderer,
|
|
6598
|
+
var runScene = (THREE, makeRenderer, engine, worldDesc) => {
|
|
7421
6599
|
const scene2 = new THREE.Scene();
|
|
7422
6600
|
const world = createDestruction();
|
|
7423
6601
|
const vfx = createVfx({});
|
|
@@ -7427,12 +6605,12 @@ var runScene = (THREE, makeRenderer, options) => {
|
|
|
7427
6605
|
let renderer;
|
|
7428
6606
|
let bound;
|
|
7429
6607
|
try {
|
|
7430
|
-
engineWorld =
|
|
7431
|
-
const loop =
|
|
6608
|
+
engineWorld = engine.createWorld(worldDesc ?? KIT_WORLD);
|
|
6609
|
+
const loop = createPhysicsLoop2(engineWorld);
|
|
7432
6610
|
const terrainGridAt = (xs, zs, out) => {
|
|
7433
6611
|
for (let j = 0; j < zs.length; j++) for (let i = 0; i < xs.length; i++) out[j * xs.length + i] = terrainAt(xs[i], zs[j]);
|
|
7434
6612
|
};
|
|
7435
|
-
renderer = makeRenderer({ physics: loop
|
|
6613
|
+
renderer = makeRenderer({ physics: loop, scene: scene2, world, vfx, eventBudgetMs: Infinity, groundAt: terrainAt, terrainAt, terrainGridAt, jitWarmUp: false });
|
|
7436
6614
|
const wall = brickWall(THREE);
|
|
7437
6615
|
const root = new THREE.Group();
|
|
7438
6616
|
root.position.set(...ORIGIN);
|
|
@@ -7459,7 +6637,7 @@ var blastAndRun = ({ world, loop, renderer, onCrater }) => {
|
|
|
7459
6637
|
renderer.wakeRegion(point[0], point[2], CRATER_RADIUS);
|
|
7460
6638
|
for (let frame = 0; frame < FRAMES; frame++) {
|
|
7461
6639
|
world.update(FRAME_SECONDS);
|
|
7462
|
-
loop
|
|
6640
|
+
loop.step(FRAME_SECONDS);
|
|
7463
6641
|
renderer.update(FRAME_SECONDS);
|
|
7464
6642
|
const stats = renderer.stats();
|
|
7465
6643
|
seen.pieces = Math.max(seen.pieces, stats.pieces);
|
|
@@ -7468,10 +6646,10 @@ var blastAndRun = ({ world, loop, renderer, onCrater }) => {
|
|
|
7468
6646
|
return seen;
|
|
7469
6647
|
};
|
|
7470
6648
|
var warmUpDestructionJit = (THREE, makeRenderer, options = {}) => {
|
|
7471
|
-
const
|
|
7472
|
-
if (warmed.has(
|
|
7473
|
-
warmed.add(
|
|
7474
|
-
return runScene(THREE, makeRenderer, options);
|
|
6649
|
+
const { engine } = options;
|
|
6650
|
+
if (!engine || warmed.has(engine)) return void 0;
|
|
6651
|
+
warmed.add(engine);
|
|
6652
|
+
return runScene(THREE, makeRenderer, engine, options.world);
|
|
7475
6653
|
};
|
|
7476
6654
|
|
|
7477
6655
|
// src/visual.ts
|
|
@@ -9464,7 +8642,7 @@ var Renderer = class {
|
|
|
9464
8642
|
__publicField(this, "blastDebris");
|
|
9465
8643
|
__publicField(this, "blastWakes");
|
|
9466
8644
|
__publicField(this, "costMeter");
|
|
9467
|
-
/** The step cost the debris policy reads this frame (`debrisCost`), null when there is none (
|
|
8645
|
+
/** The step cost the debris policy reads this frame (`debrisCost`), null when there is none (`fixed`). */
|
|
9468
8646
|
__publicField(this, "cost", null);
|
|
9469
8647
|
__publicField(this, "costFrame", 0);
|
|
9470
8648
|
/** The pieces faded away (`faded`): their root shrinks, then leaves the scene. */
|
|
@@ -9475,6 +8653,9 @@ var Renderer = class {
|
|
|
9475
8653
|
__publicField(this, "staticBoxes", /* @__PURE__ */ new Map());
|
|
9476
8654
|
/** `staticSolids` per object, kept until `staticNow` measures the object again for a reason. */
|
|
9477
8655
|
__publicField(this, "solidCache", /* @__PURE__ */ new Map());
|
|
8656
|
+
/** The fixed bodies a host module gives that are no bound object (`setHostSolids`), and the list last made of each: the same numbers keep the same list. */
|
|
8657
|
+
__publicField(this, "hostSolids", /* @__PURE__ */ new Map());
|
|
8658
|
+
__publicField(this, "hostSolidLists", /* @__PURE__ */ new Map());
|
|
9478
8659
|
/** The objects whose last `staticSolids` list is not all of what a ray can hit: measured in part, or with a loose or hidden part left out. */
|
|
9479
8660
|
__publicField(this, "unsureSolids", /* @__PURE__ */ new Set());
|
|
9480
8661
|
/** The objects `staticNow` measured again for a reason (an event, a moved root, boxes that changed): their `staticSolids` are measured again too. */
|
|
@@ -9688,6 +8869,7 @@ var Renderer = class {
|
|
|
9688
8869
|
scene: o.scene,
|
|
9689
8870
|
maxAwake: o.maxAwakePieces ?? Infinity,
|
|
9690
8871
|
...o.tilesUnderParts ? { tilesUnderParts: true } : {},
|
|
8872
|
+
...o.heldIgnores ? { heldIgnores: o.heldIgnores } : {},
|
|
9691
8873
|
...o.groundWorker ? { groundWorker: o.groundWorker } : {},
|
|
9692
8874
|
...o.groundWorkerFrames !== void 0 ? { groundWorkerFrames: o.groundWorkerFrames } : {},
|
|
9693
8875
|
...this.blastDebris ? { keep: { maxLive: this.blastDebris.maxLivePieces, far: this.blastDebris.far, ...o.focus ? { focus: o.focus } : {}, onFade: (b) => this.fadePiece(b) } } : {},
|
|
@@ -9877,8 +9059,7 @@ var Renderer = class {
|
|
|
9877
9059
|
}
|
|
9878
9060
|
if (total.isEmpty()) return { half: [0.25, 0.25, 0.25], center: new this.THREE.Vector3() };
|
|
9879
9061
|
const size = total.getSize(new this.THREE.Vector3());
|
|
9880
|
-
|
|
9881
|
-
return { half: [Math.max(least, size.x / 2), Math.max(least, size.y / 2), Math.max(least, size.z / 2)], center: total.getCenter(new this.THREE.Vector3()) };
|
|
9062
|
+
return { half: [Math.max(MIN_PIECE_HALF, size.x / 2), Math.max(MIN_PIECE_HALF, size.y / 2), Math.max(MIN_PIECE_HALF, size.z / 2)], center: total.getCenter(new this.THREE.Vector3()) };
|
|
9882
9063
|
}
|
|
9883
9064
|
/**
|
|
9884
9065
|
* A hosted body's boxes (`hostedSolids`: its damage zones) cut to the box of what its root draws, in the root's frame: the zones reach past the drawn body (17 cm each
|
|
@@ -10572,8 +9753,28 @@ var Renderer = class {
|
|
|
10572
9753
|
} finally {
|
|
10573
9754
|
this.measuredMeshes.clear();
|
|
10574
9755
|
}
|
|
9756
|
+
for (const [id, source] of this.hostSolids) out.set(id, this.hostSolidsOf(id, source()));
|
|
10575
9757
|
return out;
|
|
10576
9758
|
}
|
|
9759
|
+
setHostSolids(id, source) {
|
|
9760
|
+
if (source) this.hostSolids.set(id, source);
|
|
9761
|
+
else {
|
|
9762
|
+
this.hostSolids.delete(id);
|
|
9763
|
+
this.hostSolidLists.delete(id);
|
|
9764
|
+
}
|
|
9765
|
+
}
|
|
9766
|
+
/** The module's boxes as static solids: measured again to the same numbers, the list is the one held (an engine finds it unchanged by identity). */
|
|
9767
|
+
hostSolidsOf(id, given) {
|
|
9768
|
+
const kept = this.hostSolidLists.get(id);
|
|
9769
|
+
if (kept && kept.given.length === given.length && given.every((s, i) => s === kept.given[i])) return kept.solids;
|
|
9770
|
+
const next = given.map((s, i) => ({ center: [s.center[0], s.center[1], s.center[2]], half: [s.half[0], s.half[1], s.half[2]], rotation: s.rotation ? [s.rotation[0], s.rotation[1], s.rotation[2], s.rotation[3]] : [0, 0, 0, 1], key: s.key ?? String(i) }));
|
|
9771
|
+
if (kept && sameSolids(kept.solids, next)) {
|
|
9772
|
+
this.hostSolidLists.set(id, { given, solids: kept.solids });
|
|
9773
|
+
return kept.solids;
|
|
9774
|
+
}
|
|
9775
|
+
this.hostSolidLists.set(id, { given, solids: next });
|
|
9776
|
+
return next;
|
|
9777
|
+
}
|
|
10577
9778
|
/** `staticSolids` for each fixed object of `now`, into `out`. */
|
|
10578
9779
|
measureEach(now, budget, out) {
|
|
10579
9780
|
for (const id of now.keys()) {
|