@adatechnology/game-vehicles-three 0.11.1 → 0.12.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.
@@ -427,6 +427,42 @@ var GameVehiclesThree = (() => {
427
427
  };
428
428
 
429
429
  // ../game-vehicles-fleet/dist/index.js
430
+ var motorcycles = [
431
+ "bicycle",
432
+ "scooter",
433
+ "commuter_motorcycle",
434
+ "trail_bike",
435
+ "adventure_bike",
436
+ "highway_police_bike",
437
+ "quad_bike",
438
+ "step_through_scooter",
439
+ "naked_bike",
440
+ "sport_bike",
441
+ "cruiser",
442
+ "delivery_bike"
443
+ ];
444
+ var cars = [
445
+ "compact_hatch",
446
+ "taxi",
447
+ "entry_hatch",
448
+ "electric_hatch",
449
+ "midsize_sedan",
450
+ "police_sedan",
451
+ "highway_police_sedan",
452
+ "beetle",
453
+ "boxy_hatch",
454
+ "family_hatch",
455
+ "rideshare_hatch",
456
+ "turbo_hatch",
457
+ "fastback_sedan",
458
+ "classic_six_sedan",
459
+ "v8_coupe"
460
+ ];
461
+ var suvs = ["compact_suv", "hybrid_suv", "midsize_suv", "large_suv"];
462
+ var pickups = ["compact_pickup", "crossover_pickup", "midsize_pickup", "monster_truck"];
463
+ var vans = ["cargo_van", "ambulance"];
464
+ var heavy = ["box_truck", "tanker_truck", "school_bus", "city_bus", "armored_truck"];
465
+ var agricultural = ["tractor"];
430
466
  var military = [
431
467
  "light_4x4",
432
468
  "troop_truck",
@@ -437,6 +473,20 @@ var GameVehiclesThree = (() => {
437
473
  "light_attack_turboprop",
438
474
  "attack_helicopter"
439
475
  ];
476
+ var water = ["river_skiff", "speedboat", "fishing_boat", "jet_ski", "sailboat", "rescue_dinghy", "patrol_boat"];
477
+ var air = [
478
+ "single_engine_plane",
479
+ "crop_duster",
480
+ "piston_helicopter",
481
+ "rescue_helicopter",
482
+ "tandem_trainer",
483
+ "regional_turboprop",
484
+ "amphibian_turboprop",
485
+ "light_jet",
486
+ "trainer_helicopter",
487
+ "turbine_helicopter",
488
+ "police_helicopter"
489
+ ];
440
490
  var fleetGroupIds = [
441
491
  "motorcycles",
442
492
  "cars",
@@ -449,6 +499,18 @@ var GameVehiclesThree = (() => {
449
499
  "water",
450
500
  "air"
451
501
  ];
502
+ var fleetGroups = {
503
+ motorcycles,
504
+ cars,
505
+ suvs,
506
+ pickups,
507
+ vans,
508
+ heavy,
509
+ agricultural,
510
+ military,
511
+ water,
512
+ air
513
+ };
452
514
  var legacyVehicleSeeds = {
453
515
  compact_hatch: "hatch",
454
516
  taxi: "taxi",
@@ -28214,6 +28276,9 @@ var GameVehiclesThree = (() => {
28214
28276
  }
28215
28277
  return palette[0][0];
28216
28278
  };
28279
+ var fleetGroupOf = (id) => Object.entries(fleetGroups).find(([, ids]) => ids.includes(id))?.[0];
28280
+ var HULL_CEILING = { motorcycles: 64, cars: 256, suvs: 256, pickups: 256, vans: 300, heavy: 550, agricultural: 150, military: 500, air: 500, water: 450 };
28281
+ var fleetHullCeilingOf = (id) => HULL_CEILING[fleetGroupOf(id) ?? ""];
28217
28282
 
28218
28283
  // src/lite.ts
28219
28284
  var LITE_CLASSES = ["paint", "trim", "metal", "glass", "light", "plate"];
@@ -37107,9 +37172,9 @@ var GameVehiclesThree = (() => {
37107
37172
  opts = opts || {};
37108
37173
  dir = dir.clone().normalize();
37109
37174
  const m = c.meta;
37110
- const air = m.kind === "plane" || m.kind === "heli";
37111
- const Wref = air ? m.fuselageWidth || 1.3 : m.width || 1.7;
37112
- const R0 = (0.55 + 0.45 * force) * (air ? 1.25 : 1);
37175
+ const air2 = m.kind === "plane" || m.kind === "heli";
37176
+ const Wref = air2 ? m.fuselageWidth || 1.3 : m.width || 1.7;
37177
+ const R0 = (0.55 + 0.45 * force) * (air2 ? 1.25 : 1);
37113
37178
  const maxD = Math.min(0.18 * Wref, 0.26, R0 * 0.32, opts.maxDepth || 9) * (1 - 0.85 * clamp(m.stiff || 0, 0, 1));
37114
37179
  const H = {
37115
37180
  p,
@@ -37262,7 +37327,7 @@ var GameVehiclesThree = (() => {
37262
37327
  }
37263
37328
  const surf = findSurface(c, p, dir);
37264
37329
  if (surf) {
37265
- if (kit.config.impactStain && !opts.noStain && force > 0.45 && !air && m.kind !== "boat") {
37330
+ if (kit.config.impactStain && !opts.noStain && force > 0.45 && !air2 && m.kind !== "boat") {
37266
37331
  const sp = surf.p.clone().addScaledVector(surf.n, 2e-3);
37267
37332
  markDecal(
37268
37333
  kit.splat(c, sp, surf.n, 0.08 + force * 0.04, {
@@ -38705,17 +38770,17 @@ var GameVehiclesThree = (() => {
38705
38770
  return { lo, hi };
38706
38771
  }
38707
38772
  function regionCands(c, z, cands) {
38708
- if (!cands.some(heavy)) return cands;
38773
+ if (!cands.some(heavy2)) return cands;
38709
38774
  const R = decalRegion(c, z);
38710
38775
  const Mw = c.vehicle.matrixWorld;
38711
38776
  return cands.map(function(o) {
38712
- if (!heavy(o)) return o;
38777
+ if (!heavy2(o)) return o;
38713
38778
  if (!o.geometry.boundingSphere) o.geometry.computeBoundingSphere();
38714
38779
  if (!o.geometry.boundingBox) o.geometry.computeBoundingBox();
38715
38780
  return proxyIn(o, Mw, R);
38716
38781
  });
38717
38782
  }
38718
- function heavy(o) {
38783
+ function heavy2(o) {
38719
38784
  const g = o.geometry;
38720
38785
  const pos = g && g.attributes && g.attributes.position;
38721
38786
  return o.raycast === THREE.Mesh.prototype.raycast && !o.isSkinnedMesh && !Array.isArray(o.material) && pos && !pos.isInterleavedBufferAttribute && pos.itemSize === 3 && !g.groups.length && !(g.morphAttributes && g.morphAttributes.position) && (g.index ? g.index.count : pos.count) / 3 >= PROXY_MIN_TRIANGLES;
@@ -50215,8 +50280,8 @@ var GameVehiclesThree = (() => {
50215
50280
  state.charge = clamp3(state.charge + net * dt / 3600 / capacity, 0, 1);
50216
50281
  if (before >= crankCharge && state.charge < crankCharge && !running) events.push({ type: "battery_flat" });
50217
50282
  const work = running ? clamp3(0.15 + 0.85 * throttle * (driver.state.rpm / 6500), 0, 1) : 0;
50218
- const air = cooling.stopped + (cooling.fast - cooling.stopped) * clamp3(Math.abs(driver.state.u) / 27.8, 0, 1);
50219
- const cool = air * clamp3(radiator, 0.05, 1) * (running ? 1 + (controls.climate.on ? 0.15 : 0) : 1);
50283
+ const air2 = cooling.stopped + (cooling.fast - cooling.stopped) * clamp3(Math.abs(driver.state.u) / 27.8, 0, 1);
50284
+ const cool = air2 * clamp3(radiator, 0.05, 1) * (running ? 1 + (controls.climate.on ? 0.15 : 0) : 1);
50220
50285
  const opened = state.coolantC >= thermostat ? 1 : 0.15;
50221
50286
  state.coolantC += (heat * work - cool * opened * (state.coolantC - ambient)) * dt;
50222
50287
  state.coolantC = coolantC ?? Math.max(ambient, state.coolantC);
@@ -50335,14 +50400,14 @@ var GameVehiclesThree = (() => {
50335
50400
  const outside = options.outsideHumidity ?? 0.6;
50336
50401
  const breath = options.breath ?? 4e-3;
50337
50402
  const wet = options.wet ?? 2e-3;
50338
- const air = options.air ?? { shut: 2e-3, window: 0.05, climate: 0.01, demist: 0.06 };
50403
+ const air2 = options.air ?? { shut: 2e-3, window: 0.05, climate: 0.01, demist: 0.06 };
50339
50404
  const coldSpan = options.coldSpan ?? 15;
50340
50405
  const rates = options.rates ?? { build: 0.05, clear: 0.03, demist: 0.25 };
50341
50406
  const state = { humidity: outside, fog: 0 };
50342
50407
  return {
50343
50408
  state,
50344
50409
  step({ dt, occupants, outsideC, cabinC, rain = 0, speed = 0, windows = 0, climate, engineWarm = 1 }) {
50345
- const exchange = air.shut + air.window * clamp4(windows, 0, 1) + (climate.on ? air.climate : 0) + (climate.demist ? air.demist : 0);
50410
+ const exchange = air2.shut + air2.window * clamp4(windows, 0, 1) + (climate.on ? air2.climate : 0) + (climate.demist ? air2.demist : 0);
50346
50411
  const source = breath * Math.max(0, occupants) + wet * clamp4(rain, 0, 1) * (occupants > 0 ? 1 : 0);
50347
50412
  const towards = climate.on ? outside * 0.4 : outside;
50348
50413
  state.humidity = clamp4(state.humidity + (source - exchange * (state.humidity - towards)) * dt, 0, 1);
@@ -50916,6 +50981,33 @@ var GameVehiclesThree = (() => {
50916
50981
  var vehicleColliders = (THREE, vehicle, frame) => createVehicleColliders(THREE, vehicle, frame).pieces();
50917
50982
  var vehicleHulls = (THREE, vehicle, frame) => createVehicleColliders(THREE, vehicle, frame, { hulls: true }).hulls();
50918
50983
 
50984
+ // src/hull-plan-cache.ts
50985
+ var createHullPlanCache = () => {
50986
+ const shared2 = /* @__PURE__ */ new Map();
50987
+ const flights = /* @__PURE__ */ new Map();
50988
+ let planned = 0, reused = 0;
50989
+ let isDisposed = false;
50990
+ return {
50991
+ get: (key) => shared2.get(key),
50992
+ set: (key, value) => {
50993
+ if (!isDisposed && !shared2.has(key)) shared2.set(key, value);
50994
+ },
50995
+ flight: (key) => flights.get(key),
50996
+ setFlight: (key, flight) => void (isDisposed || flights.set(key, flight)),
50997
+ clearFlight: (key) => void flights.delete(key),
50998
+ noteReused: () => void reused++,
50999
+ noteDivided: () => void planned++,
51000
+ stats: () => ({ models: shared2.size, planned, reused }),
51001
+ dispose: () => {
51002
+ isDisposed = true;
51003
+ shared2.clear();
51004
+ flights.clear();
51005
+ planned = 0;
51006
+ reused = 0;
51007
+ }
51008
+ };
51009
+ };
51010
+
50919
51011
  // src/vehicle-lod.ts
50920
51012
  var VEHICLE_LOD_DISTANCE = 14;
50921
51013
  var HIT_BATCH_VERTICES = 16384;
@@ -51173,6 +51265,578 @@ var GameVehiclesThree = (() => {
51173
51265
  };
51174
51266
  }
51175
51267
 
51268
+ // ../game-physics/dist/index.js
51269
+ var CONTACT_READ_STRIDE = 7;
51270
+ var KIT_WORLD = {
51271
+ fixedStep: 1 / 120,
51272
+ velocityIterations: 32,
51273
+ positionIterations: 2,
51274
+ sleepAfter: 0.5,
51275
+ sleepSpeed: 0.08,
51276
+ penetrationSlop: 2e-3,
51277
+ maxPushOut: 2e-3,
51278
+ slipSpeed: 0.02,
51279
+ contactReportSpeed: 0.5,
51280
+ maxContacts: 4096,
51281
+ maxBodies: 8192
51282
+ };
51283
+ var POSE = new Float64Array(7);
51284
+ var VELOCITY = new Float64Array(6);
51285
+ var POSE2 = new Float64Array(7);
51286
+ var GOLDEN_ANGLE = Math.PI * (1 + Math.sqrt(5));
51287
+ var isPhysicsServices = (value) => {
51288
+ if (typeof value !== "object" || value === null) return false;
51289
+ const candidate = value;
51290
+ return typeof candidate.corpses === "function" && typeof candidate.setInterest === "function" && typeof candidate.blast === "object" && typeof candidate.loop === "object" && typeof candidate.world === "object";
51291
+ };
51292
+
51293
+ // src/crash-parts-clamp.ts
51294
+ var PASSES = 8;
51295
+ var holdWithin = (old, next, slop) => {
51296
+ const faces = convexFaces(old);
51297
+ if (!faces) return next;
51298
+ const middle = [0, 1, 2].map((k) => {
51299
+ let sum = 0;
51300
+ for (let i = k; i < old.length; i += 3) sum += old[i];
51301
+ return sum / (old.length / 3);
51302
+ });
51303
+ const planes = faces.map(([a, b, c]) => {
51304
+ const ux = old[b * 3] - old[a * 3], uy = old[b * 3 + 1] - old[a * 3 + 1], uz = old[b * 3 + 2] - old[a * 3 + 2];
51305
+ const wx = old[c * 3] - old[a * 3], wy = old[c * 3 + 1] - old[a * 3 + 1], wz = old[c * 3 + 2] - old[a * 3 + 2];
51306
+ const nx = uy * wz - uz * wy, ny = uz * wx - ux * wz, nz = ux * wy - uy * wx;
51307
+ const length = Math.hypot(nx, ny, nz) || 1;
51308
+ const towardMiddle = nx * (middle[0] - old[a * 3]) + ny * (middle[1] - old[a * 3 + 1]) + nz * (middle[2] - old[a * 3 + 2]);
51309
+ const sign2 = (towardMiddle > 0 ? -1 : 1) / length;
51310
+ const normal = [nx * sign2, ny * sign2, nz * sign2];
51311
+ return { normal, offset: normal[0] * old[a * 3] + normal[1] * old[a * 3 + 1] + normal[2] * old[a * 3 + 2] + slop };
51312
+ });
51313
+ let out;
51314
+ for (let i = 0; i < next.length; i += 3) {
51315
+ const p = [next[i], next[i + 1], next[i + 2]];
51316
+ let moved = false;
51317
+ for (let pass = 0; pass < PASSES; pass++) {
51318
+ let clean = true;
51319
+ for (const { normal, offset } of planes) {
51320
+ const past = normal[0] * p[0] + normal[1] * p[1] + normal[2] * p[2] - offset;
51321
+ if (past <= 0) continue;
51322
+ p[0] = p[0] - normal[0] * past;
51323
+ p[1] = p[1] - normal[1] * past;
51324
+ p[2] = p[2] - normal[2] * past;
51325
+ moved = true;
51326
+ clean = false;
51327
+ }
51328
+ if (clean) break;
51329
+ }
51330
+ if (!moved) continue;
51331
+ out ?? (out = Float32Array.from(next));
51332
+ out[i] = p[0];
51333
+ out[i + 1] = p[1];
51334
+ out[i + 2] = p[2];
51335
+ }
51336
+ return out ?? next;
51337
+ };
51338
+
51339
+ // src/crash-parts-chassis.ts
51340
+ var HULL_SLOP = 2e-3;
51341
+ var convexOf = (points) => ({ kind: "convex", points });
51342
+ var createChassisHulls = (colliders, partIndexOf) => {
51343
+ let children = [];
51344
+ const shape = () => {
51345
+ const hulls = colliders.hulls();
51346
+ if (hulls.length === 0) throw new Error("crash parts: the car has no hulls yet (its plan is still in the worker: wait for `isHullPending()`)");
51347
+ children = hulls.map((hull, handle) => ({ handle, key: hull.key, partId: hull.id, partIndex: partIndexOf.get(hull.id) ?? -1, applied: hull.points, wanted: hull, isHeld: false }));
51348
+ return { kind: "compound", mutable: true, children: children.map((child) => ({ shape: convexOf(child.applied), position: [0, 0, 0], tag: child.partIndex })) };
51349
+ };
51350
+ const release = (world, chassis, partId) => {
51351
+ const mine = children.filter((child) => child.partId === partId);
51352
+ for (const child of mine) world.setChildShape(chassis, child.handle, null);
51353
+ children = children.filter((child) => child.partId !== partId);
51354
+ return mine.map((child) => child.applied);
51355
+ };
51356
+ const settle = (world, chassis, isTouching) => {
51357
+ const now = new Map(colliders.hulls().map((hull) => [hull.key, hull]));
51358
+ let remade = 0;
51359
+ let held = 0;
51360
+ const next = [];
51361
+ for (const child of children) {
51362
+ const hull = now.get(child.key);
51363
+ now.delete(child.key);
51364
+ if (!hull) {
51365
+ world.setChildShape(chassis, child.handle, null);
51366
+ continue;
51367
+ }
51368
+ next.push(child);
51369
+ if (hull === child.wanted && !child.isHeld) continue;
51370
+ const points = isTouching(child.applied) ? holdWithin(child.applied, hull.points, HULL_SLOP) : hull.points;
51371
+ child.wanted = hull;
51372
+ child.isHeld = points !== hull.points;
51373
+ if (child.isHeld) held++;
51374
+ if (points === child.applied) continue;
51375
+ world.setChildShape(chassis, child.handle, convexOf(points));
51376
+ child.applied = points;
51377
+ remade++;
51378
+ }
51379
+ children = next;
51380
+ return { remade, held, unplaced: now.size };
51381
+ };
51382
+ return { shape, release, settle, count: () => children.length, hasWork: () => colliders.isHullPending() || children.some((child) => child.isHeld) };
51383
+ };
51384
+
51385
+ // src/crash-parts-math.ts
51386
+ var MIN_INERTIA_PER_MASS = 1e-3;
51387
+ var rotateBy = (q, v) => {
51388
+ const [x, y, z, w] = q;
51389
+ const tx = 2 * (y * v[2] - z * v[1]);
51390
+ const ty = 2 * (z * v[0] - x * v[2]);
51391
+ const tz = 2 * (x * v[1] - y * v[0]);
51392
+ return [v[0] + w * tx + y * tz - z * ty, v[1] + w * ty + z * tx - x * tz, v[2] + w * tz + x * ty - y * tx];
51393
+ };
51394
+ var multiplyQuat = (a, b) => [
51395
+ a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
51396
+ a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
51397
+ a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
51398
+ a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]
51399
+ ];
51400
+ var boxInertia = (mass, size) => [
51401
+ mass / 12 * (size[1] * size[1] + size[2] * size[2]),
51402
+ mass / 12 * (size[0] * size[0] + size[2] * size[2]),
51403
+ mass / 12 * (size[0] * size[0] + size[1] * size[1])
51404
+ ];
51405
+ var lever = (mass, d, k) => mass * (d[0] * d[0] + d[1] * d[1] + d[2] * d[2] - d[k] * d[k]);
51406
+ var withoutPart = (total, part) => {
51407
+ const mass = total.mass - part.mass;
51408
+ if (!(mass > 0)) throw new Error(`crash parts: a part of ${part.mass} kg cannot leave a body of ${total.mass} kg`);
51409
+ const centre = [0, 1, 2].map((k) => (total.mass * total.centre[k] - part.mass * part.centre[k]) / mass);
51410
+ const partOffset = [part.centre[0] - total.centre[0], part.centre[1] - total.centre[1], part.centre[2] - total.centre[2]];
51411
+ const shift = [centre[0] - total.centre[0], centre[1] - total.centre[1], centre[2] - total.centre[2]];
51412
+ const floor = mass * MIN_INERTIA_PER_MASS;
51413
+ const inertia = [0, 1, 2].map((k) => Math.max(floor, total.inertia[k] - (part.inertia[k] + lever(part.mass, partOffset, k)) - lever(mass, shift, k)));
51414
+ return { mass, centre, inertia };
51415
+ };
51416
+ var bodyMassOf = (parts) => {
51417
+ const mass = parts.reduce((sum, part) => sum + part.mass, 0);
51418
+ const centre = [0, 1, 2].map((k) => parts.reduce((sum, part) => sum + part.mass * part.box.center[k], 0) / mass);
51419
+ const inertia = [0, 1, 2].map((k) => parts.reduce((sum, part) => {
51420
+ const offset = [part.box.center[0] - centre[0], part.box.center[1] - centre[1], part.box.center[2] - centre[2]];
51421
+ return sum + boxInertia(part.mass, part.box.size)[k] + lever(part.mass, offset, k);
51422
+ }, 0));
51423
+ return { mass, centre, inertia };
51424
+ };
51425
+
51426
+ // src/crash-parts-bodies.ts
51427
+ var CLEAR_CHECK_STEPS = 30;
51428
+ var PUSH_OUT_PASSES = 3;
51429
+ var createPartBodies = (THREE, world, frame) => {
51430
+ const bodies = /* @__PURE__ */ new Map();
51431
+ const pose = new Float64Array(7);
51432
+ const speed = new Float64Array(6);
51433
+ const clearOut = [];
51434
+ const push = new Float64Array(3);
51435
+ const scratch2 = { box: new THREE.Box3(), corner: new THREE.Vector3(), position: new THREE.Vector3(), quaternion: new THREE.Quaternion(), parent: new THREE.Quaternion() };
51436
+ let steps = 0;
51437
+ let pushed = 0;
51438
+ const extentOf = (holder) => {
51439
+ holder.updateWorldMatrix(true, true);
51440
+ frame.updateWorldMatrix(true, false);
51441
+ const { box: box2, corner } = scratch2;
51442
+ box2.setFromObject(holder);
51443
+ const low = [Infinity, Infinity, Infinity];
51444
+ const high = [-Infinity, -Infinity, -Infinity];
51445
+ for (let k = 0; k < 8; k++) {
51446
+ corner.set(k & 1 ? box2.max.x : box2.min.x, k & 2 ? box2.max.y : box2.min.y, k & 4 ? box2.max.z : box2.min.z);
51447
+ frame.worldToLocal(corner);
51448
+ low[0] = Math.min(low[0], corner.x);
51449
+ low[1] = Math.min(low[1], corner.y);
51450
+ low[2] = Math.min(low[2], corner.z);
51451
+ high[0] = Math.max(high[0], corner.x);
51452
+ high[1] = Math.max(high[1], corner.y);
51453
+ high[2] = Math.max(high[2], corner.z);
51454
+ }
51455
+ return { centre: [(low[0] + high[0]) / 2, (low[1] + high[1]) / 2, (low[2] + high[2]) / 2], size: [high[0] - low[0], high[1] - low[1], high[2] - low[2]] };
51456
+ };
51457
+ const holderTurn = (holder) => {
51458
+ holder.getWorldQuaternion(scratch2.quaternion);
51459
+ frame.getWorldQuaternion(scratch2.parent);
51460
+ const turn = scratch2.parent.clone().invert().multiply(scratch2.quaternion);
51461
+ return [turn.x, turn.y, turn.z, turn.w];
51462
+ };
51463
+ const follower = (holder, offset) => ({ holder, offset, turn: holderTurn(holder) });
51464
+ const pushOutOfTheFixedWorld = (body) => {
51465
+ for (let pass = 0; pass < PUSH_OUT_PASSES; pass++) {
51466
+ const depth = world.penetration(body.id, push);
51467
+ if (depth <= 0) return;
51468
+ world.readPose(body.id, pose, 0);
51469
+ world.setPose(body.id, [pose[0] + push[0] * depth, pose[1] + push[1] * depth, pose[2] + push[2] * depth], [pose[3], pose[4], pose[5], pose[6]], true);
51470
+ pushed++;
51471
+ }
51472
+ };
51473
+ const create = ({ partIndex, mass, hulls, extent, holder, chassis, chassisMass }) => {
51474
+ world.readPose(chassis, pose, 0);
51475
+ world.readVelocity(chassis, speed, 0);
51476
+ const rotation = [pose[3], pose[4], pose[5], pose[6]];
51477
+ const lever2 = rotateBy(rotation, [extent.centre[0] - chassisMass.centre[0], extent.centre[1] - chassisMass.centre[1], extent.centre[2] - chassisMass.centre[2]]);
51478
+ const around = [speed[3], speed[4], speed[5]];
51479
+ const linear = [speed[0] + around[1] * lever2[2] - around[2] * lever2[1], speed[1] + around[2] * lever2[0] - around[0] * lever2[2], speed[2] + around[0] * lever2[1] - around[1] * lever2[0]];
51480
+ const origin = rotateBy(rotation, extent.centre);
51481
+ const shape = {
51482
+ kind: "compound",
51483
+ children: hulls.map((points) => ({ shape: { kind: "convex", points: shifted(points, extent.centre) }, position: [0, 0, 0], tag: partIndex }))
51484
+ };
51485
+ const id = world.createBody({
51486
+ shape,
51487
+ motion: "dynamic",
51488
+ mass,
51489
+ inertia: boxInertia(mass, extent.size),
51490
+ centreAtOrigin: true,
51491
+ continuous: true,
51492
+ layer: "debris",
51493
+ user: partIndex,
51494
+ position: [pose[0] + origin[0], pose[1] + origin[1], pose[2] + origin[2]],
51495
+ rotation,
51496
+ linearVelocity: linear,
51497
+ angularVelocity: around
51498
+ });
51499
+ const body = { id, partIndex, shape, extent, followers: [follower(holder, [0, 0, 0])], isIgnoring: true };
51500
+ world.ignorePair(id, chassis, true);
51501
+ pushOutOfTheFixedWorld(body);
51502
+ bodies.set(partIndex, body);
51503
+ return body;
51504
+ };
51505
+ const follow = (body, holder, extent) => {
51506
+ const base = body.extent.centre;
51507
+ body.followers.push(follower(holder, [extent.centre[0] - base[0], extent.centre[1] - base[1], extent.centre[2] - base[2]]));
51508
+ };
51509
+ const place = (body) => {
51510
+ world.readPose(body.id, pose, 0);
51511
+ const rotation = [pose[3], pose[4], pose[5], pose[6]];
51512
+ for (const { holder, offset, turn } of body.followers) {
51513
+ const at = rotateBy(rotation, offset);
51514
+ scratch2.position.set(pose[0] + at[0], pose[1] + at[1], pose[2] + at[2]);
51515
+ const turned = multiplyQuat(rotation, turn);
51516
+ const parent = holder.parent;
51517
+ if (parent) parent.worldToLocal(scratch2.position);
51518
+ holder.position.copy(scratch2.position);
51519
+ scratch2.quaternion.set(turned[0], turned[1], turned[2], turned[3]);
51520
+ if (parent) {
51521
+ parent.getWorldQuaternion(scratch2.parent);
51522
+ scratch2.quaternion.premultiply(scratch2.parent.invert());
51523
+ }
51524
+ holder.quaternion.copy(scratch2.quaternion);
51525
+ holder.updateMatrix();
51526
+ }
51527
+ };
51528
+ const release = (body, chassis) => {
51529
+ world.readPose(body.id, pose, 0);
51530
+ world.overlapShape({ kind: "box", half: [body.extent.size[0] / 2, body.extent.size[1] / 2, body.extent.size[2] / 2] }, [pose[0], pose[1], pose[2]], [pose[3], pose[4], pose[5], pose[6]], clearOut, { layers: ["vehicle"] });
51531
+ if (clearOut.includes(chassis)) return;
51532
+ world.ignorePair(body.id, chassis, false);
51533
+ body.isIgnoring = false;
51534
+ };
51535
+ return {
51536
+ extentOf,
51537
+ create,
51538
+ follow,
51539
+ bodies,
51540
+ pushedOut: () => pushed,
51541
+ step: (chassis) => {
51542
+ steps++;
51543
+ const check = steps % CLEAR_CHECK_STEPS === 0;
51544
+ for (const body of bodies.values()) {
51545
+ place(body);
51546
+ if (check && body.isIgnoring) release(body, chassis);
51547
+ }
51548
+ },
51549
+ dispose: () => {
51550
+ for (const body of bodies.values()) if (world.has(body.id)) world.removeBody(body.id);
51551
+ bodies.clear();
51552
+ }
51553
+ };
51554
+ };
51555
+ var shifted = (points, by) => {
51556
+ const out = new Float32Array(points.length);
51557
+ for (let i = 0; i < out.length; i += 3) {
51558
+ out[i] = points[i] - by[0];
51559
+ out[i + 1] = points[i + 1] - by[1];
51560
+ out[i + 2] = points[i + 2] - by[2];
51561
+ }
51562
+ return out;
51563
+ };
51564
+
51565
+ // src/crash-parts.ts
51566
+ var CONTACT_POINTS = 64;
51567
+ var SETTLE_STEPS = 30;
51568
+ var SETTLED_SPEED = 0.05;
51569
+ var TOUCH_MARGIN = 0.05;
51570
+ var createCrashParts = (options) => {
51571
+ const { THREE, world, vehicle, frame, colliders } = options;
51572
+ const parts = vehicle.model.parts;
51573
+ const partIndexOf = new Map(parts.map((part, index) => [part.id, index]));
51574
+ const chassisHulls = createChassisHulls(colliders, partIndexOf);
51575
+ const bodies = createPartBodies(THREE, world, frame);
51576
+ const pose = new Float64Array(7);
51577
+ const speed = new Float64Array(6);
51578
+ const touches = new Float32Array(CONTACT_READ_STRIDE * CONTACT_POINTS);
51579
+ const strongest = /* @__PURE__ */ new Map();
51580
+ let chassis;
51581
+ let current = options.mass;
51582
+ let calm = 0;
51583
+ let cooldown = 0;
51584
+ let isDirty = false;
51585
+ let released = 0;
51586
+ let settles = 0;
51587
+ let held = 0;
51588
+ let remade = 0;
51589
+ let unplaced = 0;
51590
+ let blows = 0;
51591
+ let events = 0;
51592
+ const hitParts = /* @__PURE__ */ new Set();
51593
+ const numbers = /* @__PURE__ */ new Map();
51594
+ const carried = (partIndex) => bodies.bodies.get(partIndex);
51595
+ const takePart = (part, partIndex, holder) => {
51596
+ const body = carried(partIndex);
51597
+ const extent = bodies.extentOf(holder);
51598
+ if (body) return bodies.follow(body, holder, extent);
51599
+ const hulls = chassisHulls.release(world, chassis, part.id);
51600
+ const solid = hulls.length > 0 ? hulls : [boxPoints(extent.size, extent.centre)];
51601
+ bodies.create({ partIndex, mass: part.mass, hulls: solid, extent, holder, chassis, chassisMass: current });
51602
+ const own = { mass: part.mass, centre: extent.centre, inertia: boxInertia(part.mass, extent.size) };
51603
+ numbers.set(part.id, own);
51604
+ current = withoutPart(current, own);
51605
+ world.setMassProperties(chassis, { mass: current.mass, inertia: current.inertia, centreOfMass: current.centre });
51606
+ released++;
51607
+ options.onPartLost?.(part);
51608
+ };
51609
+ const isTouching = (points) => {
51610
+ if (!chassis) return false;
51611
+ const count2 = world.readContacts(chassis, touches);
51612
+ if (count2 === 0) return false;
51613
+ world.readPose(chassis, pose, 0);
51614
+ let lowX = Infinity, lowY = Infinity, lowZ = Infinity;
51615
+ let highX = -Infinity, highY = -Infinity, highZ = -Infinity;
51616
+ for (let i = 0; i < points.length; i += 3) {
51617
+ const x = points[i], y = points[i + 1], z = points[i + 2];
51618
+ if (x < lowX) lowX = x;
51619
+ if (x > highX) highX = x;
51620
+ if (y < lowY) lowY = y;
51621
+ if (y > highY) highY = y;
51622
+ if (z < lowZ) lowZ = z;
51623
+ if (z > highZ) highZ = z;
51624
+ }
51625
+ const qx = -pose[3], qy = -pose[4], qz = -pose[5], qw = pose[6];
51626
+ for (let c = 0; c < count2; c++) {
51627
+ const at = c * CONTACT_READ_STRIDE;
51628
+ const vx = touches[at] - pose[0], vy = touches[at + 1] - pose[1], vz = touches[at + 2] - pose[2];
51629
+ const tx = 2 * (qy * vz - qz * vy);
51630
+ const ty = 2 * (qz * vx - qx * vz);
51631
+ const tz = 2 * (qx * vy - qy * vx);
51632
+ const x = vx + qw * tx + qy * tz - qz * ty;
51633
+ const y = vy + qw * ty + qz * tx - qx * tz;
51634
+ const z = vz + qw * tz + qx * ty - qy * tx;
51635
+ if (x >= lowX - TOUCH_MARGIN && x <= highX + TOUCH_MARGIN && y >= lowY - TOUCH_MARGIN && y <= highY + TOUCH_MARGIN && z >= lowZ - TOUCH_MARGIN && z <= highZ + TOUCH_MARGIN) return true;
51636
+ }
51637
+ return false;
51638
+ };
51639
+ const feedDamage = () => {
51640
+ const rows = world.contacts;
51641
+ strongest.clear();
51642
+ for (let i = 0; i < rows.count; i++) {
51643
+ const mine = rows.a[i] === chassis ? 0 : rows.b[i] === chassis ? 1 : -1;
51644
+ if (mine < 0) continue;
51645
+ const tag = mine === 0 ? rows.tagA[i] : rows.tagB[i];
51646
+ if (tag < 0 || tag >= parts.length) continue;
51647
+ const at = strongest.get(tag);
51648
+ if (at === void 0 || rows.approach[i] > rows.approach[at]) strongest.set(tag, i);
51649
+ }
51650
+ for (const [tag, i] of strongest) {
51651
+ const mine = rows.a[i] === chassis;
51652
+ const other = mine ? rows.b[i] : rows.a[i];
51653
+ const otherMass = world.has(other) ? world.massOf(other) : 0;
51654
+ const sign2 = mine ? 1 : -1;
51655
+ const part = parts[tag];
51656
+ const result = crashVehicle(vehicle, {
51657
+ point: [rows.point[i * 3], rows.point[i * 3 + 1], rows.point[i * 3 + 2]],
51658
+ direction: [sign2 * rows.normal[i * 3], sign2 * rows.normal[i * 3 + 1], sign2 * rows.normal[i * 3 + 2]],
51659
+ speed: rows.approach[i],
51660
+ massKg: current.mass,
51661
+ otherMassKg: otherMass > 0 ? otherMass : Infinity,
51662
+ contactWidth: Math.max(0.2, part.box.size[2]),
51663
+ source: part.id
51664
+ });
51665
+ blows++;
51666
+ events += result.events.length;
51667
+ hitParts.add(part.id);
51668
+ if (result.events.length > 0) {
51669
+ isDirty = true;
51670
+ cooldown = 0;
51671
+ }
51672
+ }
51673
+ };
51674
+ const settle = () => {
51675
+ world.readVelocity(chassis, speed, 0);
51676
+ const still = Math.hypot(speed[0], speed[1], speed[2]) < SETTLED_SPEED && Math.hypot(speed[3], speed[4], speed[5]) < SETTLED_SPEED;
51677
+ calm = still ? calm + 1 : 0;
51678
+ if (calm < SETTLE_STEPS || !(isDirty || chassisHulls.hasWork())) return;
51679
+ if (cooldown > 0) {
51680
+ cooldown--;
51681
+ return;
51682
+ }
51683
+ const done = chassisHulls.settle(world, chassis, isTouching);
51684
+ settles++;
51685
+ held += done.held;
51686
+ remade += done.remade;
51687
+ unplaced += done.unplaced;
51688
+ isDirty = done.held > 0 || colliders.isHullPending();
51689
+ cooldown = done.held > 0 && !colliders.isHullPending() ? SETTLE_STEPS : 0;
51690
+ };
51691
+ vehicle.setPartSink((part, holder) => {
51692
+ const partIndex = partIndexOf.get(part.id);
51693
+ if (chassis === void 0 || partIndex === void 0) return false;
51694
+ takePart(part, partIndex, holder);
51695
+ return true;
51696
+ });
51697
+ return {
51698
+ shape: () => chassisHulls.shape(),
51699
+ bind: (body) => {
51700
+ chassis = body;
51701
+ world.watchContacts(body, true);
51702
+ world.setMassProperties(body, { mass: current.mass, inertia: current.inertia, centreOfMass: current.centre });
51703
+ },
51704
+ step: () => {
51705
+ if (chassis === void 0) return;
51706
+ feedDamage();
51707
+ bodies.step(chassis);
51708
+ settle();
51709
+ },
51710
+ mass: () => current,
51711
+ partNumbers: () => numbers,
51712
+ partBodies: () => new Map([...bodies.bodies].map(([index, body]) => [parts[index].id, body.id])),
51713
+ stats: () => ({ released, pushedOut: bodies.pushedOut(), settles, remade, held, unplaced, blows, events, hitParts, children: chassisHulls.count() }),
51714
+ dispose: () => {
51715
+ vehicle.setPartSink(null);
51716
+ bodies.dispose();
51717
+ if (chassis !== void 0 && world.has(chassis)) world.watchContacts(chassis, false);
51718
+ }
51719
+ };
51720
+ };
51721
+ var boxPoints = (size, centre) => {
51722
+ const out = new Float32Array(24);
51723
+ for (let k = 0; k < 8; k++) {
51724
+ out[k * 3] = centre[0] + (k & 1 ? size[0] : -size[0]) / 2;
51725
+ out[k * 3 + 1] = centre[1] + (k & 2 ? size[1] : -size[1]) / 2;
51726
+ out[k * 3 + 2] = centre[2] + (k & 4 ? size[2] : -size[2]) / 2;
51727
+ }
51728
+ return out;
51729
+ };
51730
+
51731
+ // src/test-area/physical-car.ts
51732
+ var DEFAULT_HULL_CEILING = 550;
51733
+ var HALF_TURN = Math.SQRT1_2;
51734
+ var wheelChildOf = (size, center) => {
51735
+ const axis = size[0] <= size[1] && size[0] <= size[2] ? 0 : size[2] <= size[1] ? 2 : 1;
51736
+ const radius = Math.max(...size.filter((_, side2) => side2 !== axis)) / 2;
51737
+ const shape = { kind: "cylinder", halfHeight: size[axis] / 2, radius };
51738
+ if (axis === 1) return { shape, position: center };
51739
+ return { shape, position: center, rotation: axis === 0 ? [0, 0, HALF_TURN, HALF_TURN] : [HALF_TURN, 0, 0, HALF_TURN] };
51740
+ };
51741
+ var physicsServicesOf = (host) => host && isPhysicsServices(host.physics) ? host.physics : void 0;
51742
+ var physicalCarColliders = (THREE, vehicle, root, options) => createVehicleColliders(THREE, vehicle, root, {
51743
+ budget: options.budget,
51744
+ hulls: true,
51745
+ maxHulls: options.hullCeilingOf?.(options.fleetKey) ?? DEFAULT_HULL_CEILING,
51746
+ planCache: options.planCache,
51747
+ ...options.workers ? { workers: options.workers } : {}
51748
+ });
51749
+ var createPhysicalCar = ({ THREE, services, vehicle, root, colliders }) => {
51750
+ const { world } = services;
51751
+ const pose = new Float64Array(7);
51752
+ const matrix = new THREE.Matrix4();
51753
+ const parentInverse = new THREE.Matrix4();
51754
+ const position = new THREE.Vector3();
51755
+ const rotation = new THREE.Quaternion();
51756
+ const unusedScale = new THREE.Vector3();
51757
+ const wheelHandles = /* @__PURE__ */ new Map();
51758
+ let crash;
51759
+ let chassis;
51760
+ const withWheels = (hulls) => {
51761
+ if (hulls.kind !== "compound") return hulls;
51762
+ const tyres = vehicle.model.parts.filter((part) => part.role === "tyre");
51763
+ tyres.forEach((part, index) => wheelHandles.set(part.id, hulls.children.length + index));
51764
+ const inverse = new THREE.Matrix4().copy(root.matrixWorld).invert();
51765
+ const drawn = new THREE.Box3();
51766
+ const piece = new THREE.Box3();
51767
+ const centre = new THREE.Vector3();
51768
+ const size = new THREE.Vector3();
51769
+ const children = tyres.map((part) => {
51770
+ drawn.makeEmpty();
51771
+ for (const object2 of vehicle.objectsOf(part.id)) drawn.union(piece.setFromObject(object2).applyMatrix4(inverse));
51772
+ if (drawn.isEmpty()) return wheelChildOf(part.box.size, part.box.center);
51773
+ return wheelChildOf(drawn.getSize(size).toArray(), drawn.getCenter(centre).toArray());
51774
+ });
51775
+ return { ...hulls, children: [...hulls.children, ...children] };
51776
+ };
51777
+ const build = () => {
51778
+ if (!root.parent) return;
51779
+ colliders.pieces();
51780
+ if (colliders.isPending()) return;
51781
+ colliders.hulls();
51782
+ if (colliders.isHullPending()) return;
51783
+ root.updateWorldMatrix(true, false);
51784
+ root.matrixWorld.decompose(position, rotation, unusedScale);
51785
+ const mass = bodyMassOf(vehicle.model.parts);
51786
+ crash = createCrashParts({
51787
+ THREE,
51788
+ world,
51789
+ vehicle,
51790
+ frame: root,
51791
+ colliders,
51792
+ mass,
51793
+ onPartLost: (part) => {
51794
+ const handle = wheelHandles.get(part.id);
51795
+ if (handle !== void 0 && chassis !== void 0) world.setChildShape(chassis, handle, null);
51796
+ }
51797
+ });
51798
+ chassis = world.createBody({
51799
+ shape: withWheels(crash.shape()),
51800
+ motion: "dynamic",
51801
+ mass: mass.mass,
51802
+ inertia: mass.inertia,
51803
+ layer: "vehicle",
51804
+ user: 0,
51805
+ position: [position.x, position.y, position.z],
51806
+ rotation: [rotation.x, rotation.y, rotation.z, rotation.w]
51807
+ });
51808
+ crash.bind(chassis);
51809
+ };
51810
+ const follow = () => {
51811
+ world.readPose(chassis, pose, 0);
51812
+ matrix.compose(position.set(pose[0], pose[1], pose[2]), rotation.set(pose[3], pose[4], pose[5], pose[6]), unusedScale.set(1, 1, 1));
51813
+ root.parent.updateWorldMatrix(true, false);
51814
+ matrix.premultiply(parentInverse.copy(root.parent.matrixWorld).invert());
51815
+ matrix.decompose(root.position, root.quaternion, unusedScale);
51816
+ root.updateMatrixWorld(true);
51817
+ crash.step();
51818
+ };
51819
+ const stopPhase = services.loop.add({
51820
+ afterSteps: (_dt, frame) => {
51821
+ if (!chassis) return build();
51822
+ if (frame.steps > 0 && root.parent) follow();
51823
+ }
51824
+ }, "contacts");
51825
+ return {
51826
+ colliders: () => [],
51827
+ isBuilt: () => chassis !== void 0,
51828
+ chassis: () => chassis,
51829
+ parts: () => crash,
51830
+ dispose: () => {
51831
+ stopPhase();
51832
+ crash?.dispose();
51833
+ if (chassis !== void 0 && world.has(chassis)) world.removeBody(chassis);
51834
+ crash = void 0;
51835
+ chassis = void 0;
51836
+ }
51837
+ };
51838
+ };
51839
+
51176
51840
  // src/test-area/text.ts
51177
51841
  var scenarioIds = [
51178
51842
  "vehicles_9mm_windshield",
@@ -51600,7 +52264,7 @@ var GameVehiclesThree = (() => {
51600
52264
  var ZONE_COLOURS = { door: 5627221, hood: 14540083, lid: 14522675, bumper: 14505437, glass: 4500223, light: 16777215, wheel: 16742195 };
51601
52265
  var BODY_HEIGHT = 1.7;
51602
52266
  function createVehiclesTestModule(options = {}) {
51603
- const fleet = options.fleet ?? { colorFromSeed, fleetDims, installFleet, paintPalettes, paletteOf };
52267
+ const fleet = options.fleet ?? { colorFromSeed, fleetDims, hullCeilingOf: fleetHullCeilingOf, installFleet, paintPalettes, paletteOf };
51604
52268
  const locale = options.locale ?? "pt-BR";
51605
52269
  const text = TEXT[locale] ?? TEXT["pt-BR"];
51606
52270
  const keys = options.vehicles ?? DEFAULT_TEST_VEHICLES;
@@ -51614,6 +52278,8 @@ var GameVehiclesThree = (() => {
51614
52278
  const cabins = createCabinSmoke();
51615
52279
  const rainCovers = createVehicleRainCovers();
51616
52280
  let host = null;
52281
+ const physicalCars = /* @__PURE__ */ new Set();
52282
+ const planCache = createHullPlanCache();
51617
52283
  let three = null;
51618
52284
  let counter = 0;
51619
52285
  let helpersShown = "";
@@ -51684,9 +52350,25 @@ var GameVehiclesThree = (() => {
51684
52350
  const kit = r.userData.kit;
51685
52351
  const d = createVehicleDestructible(kit, r.userData.vehicle, { id, frame: root, name: text.items[itemKey] ?? key, creaseQueue, puddles: false });
51686
52352
  tapEffects(d);
51687
- const colliders = createVehicleColliders(kit.THREE, d, root, { budget: COLLIDER_BUDGET });
51688
- d.colliders = colliders.pieces;
51689
- d.collidersPending = colliders.isPending;
52353
+ const services = physicsServicesOf(host);
52354
+ if (services) {
52355
+ const colliders = physicalCarColliders(kit.THREE, d, root, { budget: COLLIDER_BUDGET, fleetKey: key, hullCeilingOf: fleet.hullCeilingOf, planCache, workers: host?.workers });
52356
+ const car = createPhysicalCar({ THREE: kit.THREE, services, vehicle: d, root, colliders });
52357
+ const disposeVehicle = d.dispose?.bind(d);
52358
+ d.dispose = () => {
52359
+ physicalCars.delete(car);
52360
+ car.dispose();
52361
+ disposeVehicle?.();
52362
+ };
52363
+ physicalCars.add(car);
52364
+ r.userData.physicalCar = car;
52365
+ d.colliders = car.colliders;
52366
+ d.collidersPending = () => false;
52367
+ } else {
52368
+ const colliders = createVehicleColliders(kit.THREE, d, root, { budget: COLLIDER_BUDGET });
52369
+ d.colliders = colliders.pieces;
52370
+ d.collidersPending = colliders.isPending;
52371
+ }
51690
52372
  const setPartPose = d.setPartPose?.bind(d);
51691
52373
  {
51692
52374
  const rests = new Map(d.describe().parts.map((part) => [part.id, part.rotation ?? [0, 0, 0]]));
@@ -52131,6 +52813,8 @@ var GameVehiclesThree = (() => {
52131
52813
  stopBlocker?.();
52132
52814
  stopCrease?.();
52133
52815
  stopLodBatch?.();
52816
+ for (const car of physicalCars) car.dispose();
52817
+ physicalCars.clear();
52134
52818
  cabins.clear(effects());
52135
52819
  rainCovers.clear();
52136
52820
  clearHelpers();
@@ -55157,482 +55841,6 @@ var GameVehiclesThree = (() => {
55157
55841
  function loadProvingModule(room, options) {
55158
55842
  return room.loadModule(createProvingModule(options));
55159
55843
  }
55160
-
55161
- // ../game-physics/dist/index.js
55162
- var CONTACT_READ_STRIDE = 7;
55163
- var KIT_WORLD = {
55164
- fixedStep: 1 / 120,
55165
- velocityIterations: 32,
55166
- positionIterations: 2,
55167
- sleepAfter: 0.5,
55168
- sleepSpeed: 0.08,
55169
- penetrationSlop: 2e-3,
55170
- maxPushOut: 2e-3,
55171
- slipSpeed: 0.02,
55172
- contactReportSpeed: 0.5,
55173
- maxContacts: 4096,
55174
- maxBodies: 8192
55175
- };
55176
- var POSE = new Float64Array(7);
55177
- var VELOCITY = new Float64Array(6);
55178
- var POSE2 = new Float64Array(7);
55179
- var GOLDEN_ANGLE = Math.PI * (1 + Math.sqrt(5));
55180
-
55181
- // src/crash-parts-clamp.ts
55182
- var PASSES = 8;
55183
- var holdWithin = (old, next, slop) => {
55184
- const faces = convexFaces(old);
55185
- if (!faces) return next;
55186
- const middle = [0, 1, 2].map((k) => {
55187
- let sum = 0;
55188
- for (let i = k; i < old.length; i += 3) sum += old[i];
55189
- return sum / (old.length / 3);
55190
- });
55191
- const planes = faces.map(([a, b, c]) => {
55192
- const ux = old[b * 3] - old[a * 3], uy = old[b * 3 + 1] - old[a * 3 + 1], uz = old[b * 3 + 2] - old[a * 3 + 2];
55193
- const wx = old[c * 3] - old[a * 3], wy = old[c * 3 + 1] - old[a * 3 + 1], wz = old[c * 3 + 2] - old[a * 3 + 2];
55194
- const nx = uy * wz - uz * wy, ny = uz * wx - ux * wz, nz = ux * wy - uy * wx;
55195
- const length = Math.hypot(nx, ny, nz) || 1;
55196
- const towardMiddle = nx * (middle[0] - old[a * 3]) + ny * (middle[1] - old[a * 3 + 1]) + nz * (middle[2] - old[a * 3 + 2]);
55197
- const sign2 = (towardMiddle > 0 ? -1 : 1) / length;
55198
- const normal = [nx * sign2, ny * sign2, nz * sign2];
55199
- return { normal, offset: normal[0] * old[a * 3] + normal[1] * old[a * 3 + 1] + normal[2] * old[a * 3 + 2] + slop };
55200
- });
55201
- let out;
55202
- for (let i = 0; i < next.length; i += 3) {
55203
- const p = [next[i], next[i + 1], next[i + 2]];
55204
- let moved = false;
55205
- for (let pass = 0; pass < PASSES; pass++) {
55206
- let clean = true;
55207
- for (const { normal, offset } of planes) {
55208
- const past = normal[0] * p[0] + normal[1] * p[1] + normal[2] * p[2] - offset;
55209
- if (past <= 0) continue;
55210
- p[0] = p[0] - normal[0] * past;
55211
- p[1] = p[1] - normal[1] * past;
55212
- p[2] = p[2] - normal[2] * past;
55213
- moved = true;
55214
- clean = false;
55215
- }
55216
- if (clean) break;
55217
- }
55218
- if (!moved) continue;
55219
- out ?? (out = Float32Array.from(next));
55220
- out[i] = p[0];
55221
- out[i + 1] = p[1];
55222
- out[i + 2] = p[2];
55223
- }
55224
- return out ?? next;
55225
- };
55226
-
55227
- // src/crash-parts-chassis.ts
55228
- var HULL_SLOP = 2e-3;
55229
- var convexOf = (points) => ({ kind: "convex", points });
55230
- var createChassisHulls = (colliders, partIndexOf) => {
55231
- let children = [];
55232
- const shape = () => {
55233
- const hulls = colliders.hulls();
55234
- if (hulls.length === 0) throw new Error("crash parts: the car has no hulls yet (its plan is still in the worker: wait for `isHullPending()`)");
55235
- children = hulls.map((hull, handle) => ({ handle, key: hull.key, partId: hull.id, partIndex: partIndexOf.get(hull.id) ?? -1, applied: hull.points, wanted: hull, isHeld: false }));
55236
- return { kind: "compound", mutable: true, children: children.map((child) => ({ shape: convexOf(child.applied), position: [0, 0, 0], tag: child.partIndex })) };
55237
- };
55238
- const release = (world, chassis, partId) => {
55239
- const mine = children.filter((child) => child.partId === partId);
55240
- for (const child of mine) world.setChildShape(chassis, child.handle, null);
55241
- children = children.filter((child) => child.partId !== partId);
55242
- return mine.map((child) => child.applied);
55243
- };
55244
- const settle = (world, chassis, isTouching) => {
55245
- const now = new Map(colliders.hulls().map((hull) => [hull.key, hull]));
55246
- let remade = 0;
55247
- let held = 0;
55248
- const next = [];
55249
- for (const child of children) {
55250
- const hull = now.get(child.key);
55251
- now.delete(child.key);
55252
- if (!hull) {
55253
- world.setChildShape(chassis, child.handle, null);
55254
- continue;
55255
- }
55256
- next.push(child);
55257
- if (hull === child.wanted && !child.isHeld) continue;
55258
- const points = isTouching(child.applied) ? holdWithin(child.applied, hull.points, HULL_SLOP) : hull.points;
55259
- child.wanted = hull;
55260
- child.isHeld = points !== hull.points;
55261
- if (child.isHeld) held++;
55262
- if (points === child.applied) continue;
55263
- world.setChildShape(chassis, child.handle, convexOf(points));
55264
- child.applied = points;
55265
- remade++;
55266
- }
55267
- children = next;
55268
- return { remade, held, unplaced: now.size };
55269
- };
55270
- return { shape, release, settle, count: () => children.length, hasWork: () => colliders.isHullPending() || children.some((child) => child.isHeld) };
55271
- };
55272
-
55273
- // src/crash-parts-math.ts
55274
- var MIN_INERTIA_PER_MASS = 1e-3;
55275
- var rotateBy = (q, v) => {
55276
- const [x, y, z, w] = q;
55277
- const tx = 2 * (y * v[2] - z * v[1]);
55278
- const ty = 2 * (z * v[0] - x * v[2]);
55279
- const tz = 2 * (x * v[1] - y * v[0]);
55280
- return [v[0] + w * tx + y * tz - z * ty, v[1] + w * ty + z * tx - x * tz, v[2] + w * tz + x * ty - y * tx];
55281
- };
55282
- var multiplyQuat = (a, b) => [
55283
- a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
55284
- a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
55285
- a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
55286
- a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]
55287
- ];
55288
- var boxInertia = (mass, size) => [
55289
- mass / 12 * (size[1] * size[1] + size[2] * size[2]),
55290
- mass / 12 * (size[0] * size[0] + size[2] * size[2]),
55291
- mass / 12 * (size[0] * size[0] + size[1] * size[1])
55292
- ];
55293
- var lever = (mass, d, k) => mass * (d[0] * d[0] + d[1] * d[1] + d[2] * d[2] - d[k] * d[k]);
55294
- var withoutPart = (total, part) => {
55295
- const mass = total.mass - part.mass;
55296
- if (!(mass > 0)) throw new Error(`crash parts: a part of ${part.mass} kg cannot leave a body of ${total.mass} kg`);
55297
- const centre = [0, 1, 2].map((k) => (total.mass * total.centre[k] - part.mass * part.centre[k]) / mass);
55298
- const partOffset = [part.centre[0] - total.centre[0], part.centre[1] - total.centre[1], part.centre[2] - total.centre[2]];
55299
- const shift = [centre[0] - total.centre[0], centre[1] - total.centre[1], centre[2] - total.centre[2]];
55300
- const floor = mass * MIN_INERTIA_PER_MASS;
55301
- const inertia = [0, 1, 2].map((k) => Math.max(floor, total.inertia[k] - (part.inertia[k] + lever(part.mass, partOffset, k)) - lever(mass, shift, k)));
55302
- return { mass, centre, inertia };
55303
- };
55304
-
55305
- // src/crash-parts-bodies.ts
55306
- var CLEAR_CHECK_STEPS = 30;
55307
- var PUSH_OUT_PASSES = 3;
55308
- var createPartBodies = (THREE, world, frame) => {
55309
- const bodies = /* @__PURE__ */ new Map();
55310
- const pose = new Float64Array(7);
55311
- const speed = new Float64Array(6);
55312
- const clearOut = [];
55313
- const push = new Float64Array(3);
55314
- const scratch2 = { box: new THREE.Box3(), corner: new THREE.Vector3(), position: new THREE.Vector3(), quaternion: new THREE.Quaternion(), parent: new THREE.Quaternion() };
55315
- let steps = 0;
55316
- let pushed = 0;
55317
- const extentOf = (holder) => {
55318
- holder.updateWorldMatrix(true, true);
55319
- frame.updateWorldMatrix(true, false);
55320
- const { box: box2, corner } = scratch2;
55321
- box2.setFromObject(holder);
55322
- const low = [Infinity, Infinity, Infinity];
55323
- const high = [-Infinity, -Infinity, -Infinity];
55324
- for (let k = 0; k < 8; k++) {
55325
- corner.set(k & 1 ? box2.max.x : box2.min.x, k & 2 ? box2.max.y : box2.min.y, k & 4 ? box2.max.z : box2.min.z);
55326
- frame.worldToLocal(corner);
55327
- low[0] = Math.min(low[0], corner.x);
55328
- low[1] = Math.min(low[1], corner.y);
55329
- low[2] = Math.min(low[2], corner.z);
55330
- high[0] = Math.max(high[0], corner.x);
55331
- high[1] = Math.max(high[1], corner.y);
55332
- high[2] = Math.max(high[2], corner.z);
55333
- }
55334
- return { centre: [(low[0] + high[0]) / 2, (low[1] + high[1]) / 2, (low[2] + high[2]) / 2], size: [high[0] - low[0], high[1] - low[1], high[2] - low[2]] };
55335
- };
55336
- const holderTurn = (holder) => {
55337
- holder.getWorldQuaternion(scratch2.quaternion);
55338
- frame.getWorldQuaternion(scratch2.parent);
55339
- const turn = scratch2.parent.clone().invert().multiply(scratch2.quaternion);
55340
- return [turn.x, turn.y, turn.z, turn.w];
55341
- };
55342
- const follower = (holder, offset) => ({ holder, offset, turn: holderTurn(holder) });
55343
- const pushOutOfTheFixedWorld = (body) => {
55344
- for (let pass = 0; pass < PUSH_OUT_PASSES; pass++) {
55345
- const depth = world.penetration(body.id, push);
55346
- if (depth <= 0) return;
55347
- world.readPose(body.id, pose, 0);
55348
- world.setPose(body.id, [pose[0] + push[0] * depth, pose[1] + push[1] * depth, pose[2] + push[2] * depth], [pose[3], pose[4], pose[5], pose[6]], true);
55349
- pushed++;
55350
- }
55351
- };
55352
- const create = ({ partIndex, mass, hulls, extent, holder, chassis, chassisMass }) => {
55353
- world.readPose(chassis, pose, 0);
55354
- world.readVelocity(chassis, speed, 0);
55355
- const rotation = [pose[3], pose[4], pose[5], pose[6]];
55356
- const lever2 = rotateBy(rotation, [extent.centre[0] - chassisMass.centre[0], extent.centre[1] - chassisMass.centre[1], extent.centre[2] - chassisMass.centre[2]]);
55357
- const around = [speed[3], speed[4], speed[5]];
55358
- const linear = [speed[0] + around[1] * lever2[2] - around[2] * lever2[1], speed[1] + around[2] * lever2[0] - around[0] * lever2[2], speed[2] + around[0] * lever2[1] - around[1] * lever2[0]];
55359
- const origin = rotateBy(rotation, extent.centre);
55360
- const shape = {
55361
- kind: "compound",
55362
- children: hulls.map((points) => ({ shape: { kind: "convex", points: shifted(points, extent.centre) }, position: [0, 0, 0], tag: partIndex }))
55363
- };
55364
- const id = world.createBody({
55365
- shape,
55366
- motion: "dynamic",
55367
- mass,
55368
- inertia: boxInertia(mass, extent.size),
55369
- centreAtOrigin: true,
55370
- continuous: true,
55371
- layer: "debris",
55372
- user: partIndex,
55373
- position: [pose[0] + origin[0], pose[1] + origin[1], pose[2] + origin[2]],
55374
- rotation,
55375
- linearVelocity: linear,
55376
- angularVelocity: around
55377
- });
55378
- const body = { id, partIndex, shape, extent, followers: [follower(holder, [0, 0, 0])], isIgnoring: true };
55379
- world.ignorePair(id, chassis, true);
55380
- pushOutOfTheFixedWorld(body);
55381
- bodies.set(partIndex, body);
55382
- return body;
55383
- };
55384
- const follow = (body, holder, extent) => {
55385
- const base = body.extent.centre;
55386
- body.followers.push(follower(holder, [extent.centre[0] - base[0], extent.centre[1] - base[1], extent.centre[2] - base[2]]));
55387
- };
55388
- const place = (body) => {
55389
- world.readPose(body.id, pose, 0);
55390
- const rotation = [pose[3], pose[4], pose[5], pose[6]];
55391
- for (const { holder, offset, turn } of body.followers) {
55392
- const at = rotateBy(rotation, offset);
55393
- scratch2.position.set(pose[0] + at[0], pose[1] + at[1], pose[2] + at[2]);
55394
- const turned = multiplyQuat(rotation, turn);
55395
- const parent = holder.parent;
55396
- if (parent) parent.worldToLocal(scratch2.position);
55397
- holder.position.copy(scratch2.position);
55398
- scratch2.quaternion.set(turned[0], turned[1], turned[2], turned[3]);
55399
- if (parent) {
55400
- parent.getWorldQuaternion(scratch2.parent);
55401
- scratch2.quaternion.premultiply(scratch2.parent.invert());
55402
- }
55403
- holder.quaternion.copy(scratch2.quaternion);
55404
- holder.updateMatrix();
55405
- }
55406
- };
55407
- const release = (body, chassis) => {
55408
- world.readPose(body.id, pose, 0);
55409
- world.overlapShape({ kind: "box", half: [body.extent.size[0] / 2, body.extent.size[1] / 2, body.extent.size[2] / 2] }, [pose[0], pose[1], pose[2]], [pose[3], pose[4], pose[5], pose[6]], clearOut, { layers: ["vehicle"] });
55410
- if (clearOut.includes(chassis)) return;
55411
- world.ignorePair(body.id, chassis, false);
55412
- body.isIgnoring = false;
55413
- };
55414
- return {
55415
- extentOf,
55416
- create,
55417
- follow,
55418
- bodies,
55419
- pushedOut: () => pushed,
55420
- step: (chassis) => {
55421
- steps++;
55422
- const check = steps % CLEAR_CHECK_STEPS === 0;
55423
- for (const body of bodies.values()) {
55424
- place(body);
55425
- if (check && body.isIgnoring) release(body, chassis);
55426
- }
55427
- },
55428
- dispose: () => {
55429
- for (const body of bodies.values()) if (world.has(body.id)) world.removeBody(body.id);
55430
- bodies.clear();
55431
- }
55432
- };
55433
- };
55434
- var shifted = (points, by) => {
55435
- const out = new Float32Array(points.length);
55436
- for (let i = 0; i < out.length; i += 3) {
55437
- out[i] = points[i] - by[0];
55438
- out[i + 1] = points[i + 1] - by[1];
55439
- out[i + 2] = points[i + 2] - by[2];
55440
- }
55441
- return out;
55442
- };
55443
-
55444
- // src/crash-parts.ts
55445
- var CONTACT_POINTS = 64;
55446
- var SETTLE_STEPS = 30;
55447
- var SETTLED_SPEED = 0.05;
55448
- var TOUCH_MARGIN = 0.05;
55449
- var createCrashParts = (options) => {
55450
- const { THREE, world, vehicle, frame, colliders } = options;
55451
- const parts = vehicle.model.parts;
55452
- const partIndexOf = new Map(parts.map((part, index) => [part.id, index]));
55453
- const chassisHulls = createChassisHulls(colliders, partIndexOf);
55454
- const bodies = createPartBodies(THREE, world, frame);
55455
- const pose = new Float64Array(7);
55456
- const speed = new Float64Array(6);
55457
- const touches = new Float32Array(CONTACT_READ_STRIDE * CONTACT_POINTS);
55458
- const strongest = /* @__PURE__ */ new Map();
55459
- let chassis;
55460
- let current = options.mass;
55461
- let calm = 0;
55462
- let cooldown = 0;
55463
- let isDirty = false;
55464
- let released = 0;
55465
- let settles = 0;
55466
- let held = 0;
55467
- let remade = 0;
55468
- let unplaced = 0;
55469
- let blows = 0;
55470
- let events = 0;
55471
- const hitParts = /* @__PURE__ */ new Set();
55472
- const numbers = /* @__PURE__ */ new Map();
55473
- const carried = (partIndex) => bodies.bodies.get(partIndex);
55474
- const takePart = (part, partIndex, holder) => {
55475
- const body = carried(partIndex);
55476
- const extent = bodies.extentOf(holder);
55477
- if (body) return bodies.follow(body, holder, extent);
55478
- const hulls = chassisHulls.release(world, chassis, part.id);
55479
- const solid = hulls.length > 0 ? hulls : [boxPoints(extent.size, extent.centre)];
55480
- bodies.create({ partIndex, mass: part.mass, hulls: solid, extent, holder, chassis, chassisMass: current });
55481
- const own = { mass: part.mass, centre: extent.centre, inertia: boxInertia(part.mass, extent.size) };
55482
- numbers.set(part.id, own);
55483
- current = withoutPart(current, own);
55484
- world.setMassProperties(chassis, { mass: current.mass, inertia: current.inertia, centreOfMass: current.centre });
55485
- released++;
55486
- options.onPartLost?.(part);
55487
- };
55488
- const isTouching = (points) => {
55489
- if (!chassis) return false;
55490
- const count2 = world.readContacts(chassis, touches);
55491
- if (count2 === 0) return false;
55492
- world.readPose(chassis, pose, 0);
55493
- let lowX = Infinity, lowY = Infinity, lowZ = Infinity;
55494
- let highX = -Infinity, highY = -Infinity, highZ = -Infinity;
55495
- for (let i = 0; i < points.length; i += 3) {
55496
- const x = points[i], y = points[i + 1], z = points[i + 2];
55497
- if (x < lowX) lowX = x;
55498
- if (x > highX) highX = x;
55499
- if (y < lowY) lowY = y;
55500
- if (y > highY) highY = y;
55501
- if (z < lowZ) lowZ = z;
55502
- if (z > highZ) highZ = z;
55503
- }
55504
- const qx = -pose[3], qy = -pose[4], qz = -pose[5], qw = pose[6];
55505
- for (let c = 0; c < count2; c++) {
55506
- const at = c * CONTACT_READ_STRIDE;
55507
- const vx = touches[at] - pose[0], vy = touches[at + 1] - pose[1], vz = touches[at + 2] - pose[2];
55508
- const tx = 2 * (qy * vz - qz * vy);
55509
- const ty = 2 * (qz * vx - qx * vz);
55510
- const tz = 2 * (qx * vy - qy * vx);
55511
- const x = vx + qw * tx + qy * tz - qz * ty;
55512
- const y = vy + qw * ty + qz * tx - qx * tz;
55513
- const z = vz + qw * tz + qx * ty - qy * tx;
55514
- if (x >= lowX - TOUCH_MARGIN && x <= highX + TOUCH_MARGIN && y >= lowY - TOUCH_MARGIN && y <= highY + TOUCH_MARGIN && z >= lowZ - TOUCH_MARGIN && z <= highZ + TOUCH_MARGIN) return true;
55515
- }
55516
- return false;
55517
- };
55518
- const feedDamage = () => {
55519
- const rows = world.contacts;
55520
- strongest.clear();
55521
- for (let i = 0; i < rows.count; i++) {
55522
- const mine = rows.a[i] === chassis ? 0 : rows.b[i] === chassis ? 1 : -1;
55523
- if (mine < 0) continue;
55524
- const tag = mine === 0 ? rows.tagA[i] : rows.tagB[i];
55525
- if (tag < 0 || tag >= parts.length) continue;
55526
- const at = strongest.get(tag);
55527
- if (at === void 0 || rows.approach[i] > rows.approach[at]) strongest.set(tag, i);
55528
- }
55529
- for (const [tag, i] of strongest) {
55530
- const mine = rows.a[i] === chassis;
55531
- const other = mine ? rows.b[i] : rows.a[i];
55532
- const otherMass = world.has(other) ? world.massOf(other) : 0;
55533
- const sign2 = mine ? 1 : -1;
55534
- const part = parts[tag];
55535
- const result = crashVehicle(vehicle, {
55536
- point: [rows.point[i * 3], rows.point[i * 3 + 1], rows.point[i * 3 + 2]],
55537
- direction: [sign2 * rows.normal[i * 3], sign2 * rows.normal[i * 3 + 1], sign2 * rows.normal[i * 3 + 2]],
55538
- speed: rows.approach[i],
55539
- massKg: current.mass,
55540
- otherMassKg: otherMass > 0 ? otherMass : Infinity,
55541
- contactWidth: Math.max(0.2, part.box.size[2]),
55542
- source: part.id
55543
- });
55544
- blows++;
55545
- events += result.events.length;
55546
- hitParts.add(part.id);
55547
- if (result.events.length > 0) {
55548
- isDirty = true;
55549
- cooldown = 0;
55550
- }
55551
- }
55552
- };
55553
- const settle = () => {
55554
- world.readVelocity(chassis, speed, 0);
55555
- const still = Math.hypot(speed[0], speed[1], speed[2]) < SETTLED_SPEED && Math.hypot(speed[3], speed[4], speed[5]) < SETTLED_SPEED;
55556
- calm = still ? calm + 1 : 0;
55557
- if (calm < SETTLE_STEPS || !(isDirty || chassisHulls.hasWork())) return;
55558
- if (cooldown > 0) {
55559
- cooldown--;
55560
- return;
55561
- }
55562
- const done = chassisHulls.settle(world, chassis, isTouching);
55563
- settles++;
55564
- held += done.held;
55565
- remade += done.remade;
55566
- unplaced += done.unplaced;
55567
- isDirty = done.held > 0 || colliders.isHullPending();
55568
- cooldown = done.held > 0 && !colliders.isHullPending() ? SETTLE_STEPS : 0;
55569
- };
55570
- vehicle.setPartSink((part, holder) => {
55571
- const partIndex = partIndexOf.get(part.id);
55572
- if (chassis === void 0 || partIndex === void 0) return false;
55573
- takePart(part, partIndex, holder);
55574
- return true;
55575
- });
55576
- return {
55577
- shape: () => chassisHulls.shape(),
55578
- bind: (body) => {
55579
- chassis = body;
55580
- world.watchContacts(body, true);
55581
- world.setMassProperties(body, { mass: current.mass, inertia: current.inertia, centreOfMass: current.centre });
55582
- },
55583
- step: () => {
55584
- if (chassis === void 0) return;
55585
- feedDamage();
55586
- bodies.step(chassis);
55587
- settle();
55588
- },
55589
- mass: () => current,
55590
- partNumbers: () => numbers,
55591
- partBodies: () => new Map([...bodies.bodies].map(([index, body]) => [parts[index].id, body.id])),
55592
- stats: () => ({ released, pushedOut: bodies.pushedOut(), settles, remade, held, unplaced, blows, events, hitParts, children: chassisHulls.count() }),
55593
- dispose: () => {
55594
- vehicle.setPartSink(null);
55595
- bodies.dispose();
55596
- if (chassis !== void 0 && world.has(chassis)) world.watchContacts(chassis, false);
55597
- }
55598
- };
55599
- };
55600
- var boxPoints = (size, centre) => {
55601
- const out = new Float32Array(24);
55602
- for (let k = 0; k < 8; k++) {
55603
- out[k * 3] = centre[0] + (k & 1 ? size[0] : -size[0]) / 2;
55604
- out[k * 3 + 1] = centre[1] + (k & 2 ? size[1] : -size[1]) / 2;
55605
- out[k * 3 + 2] = centre[2] + (k & 4 ? size[2] : -size[2]) / 2;
55606
- }
55607
- return out;
55608
- };
55609
-
55610
- // src/hull-plan-cache.ts
55611
- var createHullPlanCache = () => {
55612
- const shared2 = /* @__PURE__ */ new Map();
55613
- const flights = /* @__PURE__ */ new Map();
55614
- let planned = 0, reused = 0;
55615
- let isDisposed = false;
55616
- return {
55617
- get: (key) => shared2.get(key),
55618
- set: (key, value) => {
55619
- if (!isDisposed && !shared2.has(key)) shared2.set(key, value);
55620
- },
55621
- flight: (key) => flights.get(key),
55622
- setFlight: (key, flight) => void (isDisposed || flights.set(key, flight)),
55623
- clearFlight: (key) => void flights.delete(key),
55624
- noteReused: () => void reused++,
55625
- noteDivided: () => void planned++,
55626
- stats: () => ({ models: shared2.size, planned, reused }),
55627
- dispose: () => {
55628
- isDisposed = true;
55629
- shared2.clear();
55630
- flights.clear();
55631
- planned = 0;
55632
- reused = 0;
55633
- }
55634
- };
55635
- };
55636
55844
  return __toCommonJS(index_exports);
55637
55845
  })();
55638
55846
  //# sourceMappingURL=index.global.js.map