@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.
@@ -4,7 +4,7 @@ import { discardObject3D } from "@adatechnology/game-shapes-three";
4
4
  import { buildVehicleItem, crackedGlassMaterial, createCreaseQueue, createVehicleDestructible as createVehicleDestructible2, createVehicleKit } from "@adatechnology/game-vehicles";
5
5
  import { isRenderPools } from "@adatechnology/game-render-three";
6
6
  import { createArcadeDriver as createArcadeDriver2, createSemiSimDriver as createSemiSimDriver2, createSurfaces as createSurfaces2, vehicleCorners as vehicleCorners2 } from "@adatechnology/game-vehicles-driving";
7
- import { colorFromSeed, fleetDims, installFleet, paintPalettes, paletteOf } from "@adatechnology/game-vehicles-fleet";
7
+ import { colorFromSeed, fleetDims, fleetHullCeilingOf, installFleet, paintPalettes, paletteOf } from "@adatechnology/game-vehicles-fleet";
8
8
 
9
9
  // src/build.ts
10
10
  import { markKitOrigin } from "@adatechnology/game-contracts";
@@ -891,6 +891,33 @@ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
891
891
  var vehicleColliders = (THREE, vehicle, frame) => createVehicleColliders(THREE, vehicle, frame).pieces();
892
892
  var vehicleHulls = (THREE, vehicle, frame) => createVehicleColliders(THREE, vehicle, frame, { hulls: true }).hulls();
893
893
 
894
+ // src/hull-plan-cache.ts
895
+ var createHullPlanCache = () => {
896
+ const shared = /* @__PURE__ */ new Map();
897
+ const flights = /* @__PURE__ */ new Map();
898
+ let planned = 0, reused = 0;
899
+ let isDisposed = false;
900
+ return {
901
+ get: (key) => shared.get(key),
902
+ set: (key, value) => {
903
+ if (!isDisposed && !shared.has(key)) shared.set(key, value);
904
+ },
905
+ flight: (key) => flights.get(key),
906
+ setFlight: (key, flight) => void (isDisposed || flights.set(key, flight)),
907
+ clearFlight: (key) => void flights.delete(key),
908
+ noteReused: () => void reused++,
909
+ noteDivided: () => void planned++,
910
+ stats: () => ({ models: shared.size, planned, reused }),
911
+ dispose: () => {
912
+ isDisposed = true;
913
+ shared.clear();
914
+ flights.clear();
915
+ planned = 0;
916
+ reused = 0;
917
+ }
918
+ };
919
+ };
920
+
894
921
  // src/vehicle-lod.ts
895
922
  import { DEFERRED_QUEUE_ID, markKitOrigin as markKitOrigin2 } from "@adatechnology/game-contracts";
896
923
  import { BATCHED_LAYER, createStaticBatchQueue, createStaticBatcher } from "@adatechnology/game-render-three";
@@ -1229,6 +1256,561 @@ function createLaneDamage(params) {
1229
1256
  };
1230
1257
  }
1231
1258
 
1259
+ // src/test-area/physical-car.ts
1260
+ import { isPhysicsServices } from "@adatechnology/game-physics";
1261
+
1262
+ // src/crash-parts.ts
1263
+ import { CONTACT_READ_STRIDE } from "@adatechnology/game-physics";
1264
+ import { crashVehicle as crashVehicle2 } from "@adatechnology/game-vehicles";
1265
+
1266
+ // src/crash-parts-clamp.ts
1267
+ import { convexFaces } from "@adatechnology/game-vehicles";
1268
+ var PASSES = 8;
1269
+ var holdWithin = (old, next, slop) => {
1270
+ const faces = convexFaces(old);
1271
+ if (!faces) return next;
1272
+ const middle = [0, 1, 2].map((k) => {
1273
+ let sum = 0;
1274
+ for (let i = k; i < old.length; i += 3) sum += old[i];
1275
+ return sum / (old.length / 3);
1276
+ });
1277
+ const planes = faces.map(([a, b, c]) => {
1278
+ 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];
1279
+ 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];
1280
+ const nx = uy * wz - uz * wy, ny = uz * wx - ux * wz, nz = ux * wy - uy * wx;
1281
+ const length = Math.hypot(nx, ny, nz) || 1;
1282
+ const towardMiddle = nx * (middle[0] - old[a * 3]) + ny * (middle[1] - old[a * 3 + 1]) + nz * (middle[2] - old[a * 3 + 2]);
1283
+ const sign = (towardMiddle > 0 ? -1 : 1) / length;
1284
+ const normal = [nx * sign, ny * sign, nz * sign];
1285
+ return { normal, offset: normal[0] * old[a * 3] + normal[1] * old[a * 3 + 1] + normal[2] * old[a * 3 + 2] + slop };
1286
+ });
1287
+ let out;
1288
+ for (let i = 0; i < next.length; i += 3) {
1289
+ const p = [next[i], next[i + 1], next[i + 2]];
1290
+ let moved = false;
1291
+ for (let pass = 0; pass < PASSES; pass++) {
1292
+ let clean = true;
1293
+ for (const { normal, offset } of planes) {
1294
+ const past = normal[0] * p[0] + normal[1] * p[1] + normal[2] * p[2] - offset;
1295
+ if (past <= 0) continue;
1296
+ p[0] = p[0] - normal[0] * past;
1297
+ p[1] = p[1] - normal[1] * past;
1298
+ p[2] = p[2] - normal[2] * past;
1299
+ moved = true;
1300
+ clean = false;
1301
+ }
1302
+ if (clean) break;
1303
+ }
1304
+ if (!moved) continue;
1305
+ out ?? (out = Float32Array.from(next));
1306
+ out[i] = p[0];
1307
+ out[i + 1] = p[1];
1308
+ out[i + 2] = p[2];
1309
+ }
1310
+ return out ?? next;
1311
+ };
1312
+
1313
+ // src/crash-parts-chassis.ts
1314
+ var HULL_SLOP = 2e-3;
1315
+ var convexOf = (points) => ({ kind: "convex", points });
1316
+ var createChassisHulls = (colliders, partIndexOf) => {
1317
+ let children = [];
1318
+ const shape = () => {
1319
+ const hulls = colliders.hulls();
1320
+ 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()`)");
1321
+ 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 }));
1322
+ return { kind: "compound", mutable: true, children: children.map((child) => ({ shape: convexOf(child.applied), position: [0, 0, 0], tag: child.partIndex })) };
1323
+ };
1324
+ const release = (world, chassis, partId) => {
1325
+ const mine = children.filter((child) => child.partId === partId);
1326
+ for (const child of mine) world.setChildShape(chassis, child.handle, null);
1327
+ children = children.filter((child) => child.partId !== partId);
1328
+ return mine.map((child) => child.applied);
1329
+ };
1330
+ const settle = (world, chassis, isTouching) => {
1331
+ const now = new Map(colliders.hulls().map((hull) => [hull.key, hull]));
1332
+ let remade = 0;
1333
+ let held = 0;
1334
+ const next = [];
1335
+ for (const child of children) {
1336
+ const hull = now.get(child.key);
1337
+ now.delete(child.key);
1338
+ if (!hull) {
1339
+ world.setChildShape(chassis, child.handle, null);
1340
+ continue;
1341
+ }
1342
+ next.push(child);
1343
+ if (hull === child.wanted && !child.isHeld) continue;
1344
+ const points = isTouching(child.applied) ? holdWithin(child.applied, hull.points, HULL_SLOP) : hull.points;
1345
+ child.wanted = hull;
1346
+ child.isHeld = points !== hull.points;
1347
+ if (child.isHeld) held++;
1348
+ if (points === child.applied) continue;
1349
+ world.setChildShape(chassis, child.handle, convexOf(points));
1350
+ child.applied = points;
1351
+ remade++;
1352
+ }
1353
+ children = next;
1354
+ return { remade, held, unplaced: now.size };
1355
+ };
1356
+ return { shape, release, settle, count: () => children.length, hasWork: () => colliders.isHullPending() || children.some((child) => child.isHeld) };
1357
+ };
1358
+
1359
+ // src/crash-parts-math.ts
1360
+ var MIN_INERTIA_PER_MASS = 1e-3;
1361
+ var rotateBy = (q, v) => {
1362
+ const [x, y, z, w] = q;
1363
+ const tx = 2 * (y * v[2] - z * v[1]);
1364
+ const ty = 2 * (z * v[0] - x * v[2]);
1365
+ const tz = 2 * (x * v[1] - y * v[0]);
1366
+ 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];
1367
+ };
1368
+ var multiplyQuat = (a, b) => [
1369
+ a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
1370
+ a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
1371
+ a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
1372
+ a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]
1373
+ ];
1374
+ var boxInertia = (mass, size) => [
1375
+ mass / 12 * (size[1] * size[1] + size[2] * size[2]),
1376
+ mass / 12 * (size[0] * size[0] + size[2] * size[2]),
1377
+ mass / 12 * (size[0] * size[0] + size[1] * size[1])
1378
+ ];
1379
+ var lever = (mass, d, k) => mass * (d[0] * d[0] + d[1] * d[1] + d[2] * d[2] - d[k] * d[k]);
1380
+ var withoutPart = (total, part) => {
1381
+ const mass = total.mass - part.mass;
1382
+ if (!(mass > 0)) throw new Error(`crash parts: a part of ${part.mass} kg cannot leave a body of ${total.mass} kg`);
1383
+ const centre = [0, 1, 2].map((k) => (total.mass * total.centre[k] - part.mass * part.centre[k]) / mass);
1384
+ const partOffset = [part.centre[0] - total.centre[0], part.centre[1] - total.centre[1], part.centre[2] - total.centre[2]];
1385
+ const shift = [centre[0] - total.centre[0], centre[1] - total.centre[1], centre[2] - total.centre[2]];
1386
+ const floor = mass * MIN_INERTIA_PER_MASS;
1387
+ 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)));
1388
+ return { mass, centre, inertia };
1389
+ };
1390
+ var bodyMassOf = (parts) => {
1391
+ const mass = parts.reduce((sum, part) => sum + part.mass, 0);
1392
+ const centre = [0, 1, 2].map((k) => parts.reduce((sum, part) => sum + part.mass * part.box.center[k], 0) / mass);
1393
+ const inertia = [0, 1, 2].map((k) => parts.reduce((sum, part) => {
1394
+ const offset = [part.box.center[0] - centre[0], part.box.center[1] - centre[1], part.box.center[2] - centre[2]];
1395
+ return sum + boxInertia(part.mass, part.box.size)[k] + lever(part.mass, offset, k);
1396
+ }, 0));
1397
+ return { mass, centre, inertia };
1398
+ };
1399
+
1400
+ // src/crash-parts-bodies.ts
1401
+ var CLEAR_CHECK_STEPS = 30;
1402
+ var PUSH_OUT_PASSES = 3;
1403
+ var createPartBodies = (THREE, world, frame) => {
1404
+ const bodies = /* @__PURE__ */ new Map();
1405
+ const pose = new Float64Array(7);
1406
+ const speed = new Float64Array(6);
1407
+ const clearOut = [];
1408
+ const push = new Float64Array(3);
1409
+ const scratch = { box: new THREE.Box3(), corner: new THREE.Vector3(), position: new THREE.Vector3(), quaternion: new THREE.Quaternion(), parent: new THREE.Quaternion() };
1410
+ let steps = 0;
1411
+ let pushed = 0;
1412
+ const extentOf = (holder) => {
1413
+ holder.updateWorldMatrix(true, true);
1414
+ frame.updateWorldMatrix(true, false);
1415
+ const { box, corner } = scratch;
1416
+ box.setFromObject(holder);
1417
+ const low = [Infinity, Infinity, Infinity];
1418
+ const high = [-Infinity, -Infinity, -Infinity];
1419
+ for (let k = 0; k < 8; k++) {
1420
+ corner.set(k & 1 ? box.max.x : box.min.x, k & 2 ? box.max.y : box.min.y, k & 4 ? box.max.z : box.min.z);
1421
+ frame.worldToLocal(corner);
1422
+ low[0] = Math.min(low[0], corner.x);
1423
+ low[1] = Math.min(low[1], corner.y);
1424
+ low[2] = Math.min(low[2], corner.z);
1425
+ high[0] = Math.max(high[0], corner.x);
1426
+ high[1] = Math.max(high[1], corner.y);
1427
+ high[2] = Math.max(high[2], corner.z);
1428
+ }
1429
+ 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]] };
1430
+ };
1431
+ const holderTurn = (holder) => {
1432
+ holder.getWorldQuaternion(scratch.quaternion);
1433
+ frame.getWorldQuaternion(scratch.parent);
1434
+ const turn = scratch.parent.clone().invert().multiply(scratch.quaternion);
1435
+ return [turn.x, turn.y, turn.z, turn.w];
1436
+ };
1437
+ const follower = (holder, offset) => ({ holder, offset, turn: holderTurn(holder) });
1438
+ const pushOutOfTheFixedWorld = (body) => {
1439
+ for (let pass = 0; pass < PUSH_OUT_PASSES; pass++) {
1440
+ const depth = world.penetration(body.id, push);
1441
+ if (depth <= 0) return;
1442
+ world.readPose(body.id, pose, 0);
1443
+ 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);
1444
+ pushed++;
1445
+ }
1446
+ };
1447
+ const create = ({ partIndex, mass, hulls, extent, holder, chassis, chassisMass }) => {
1448
+ world.readPose(chassis, pose, 0);
1449
+ world.readVelocity(chassis, speed, 0);
1450
+ const rotation = [pose[3], pose[4], pose[5], pose[6]];
1451
+ const lever2 = rotateBy(rotation, [extent.centre[0] - chassisMass.centre[0], extent.centre[1] - chassisMass.centre[1], extent.centre[2] - chassisMass.centre[2]]);
1452
+ const around = [speed[3], speed[4], speed[5]];
1453
+ 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]];
1454
+ const origin = rotateBy(rotation, extent.centre);
1455
+ const shape = {
1456
+ kind: "compound",
1457
+ children: hulls.map((points) => ({ shape: { kind: "convex", points: shifted(points, extent.centre) }, position: [0, 0, 0], tag: partIndex }))
1458
+ };
1459
+ const id = world.createBody({
1460
+ shape,
1461
+ motion: "dynamic",
1462
+ mass,
1463
+ inertia: boxInertia(mass, extent.size),
1464
+ centreAtOrigin: true,
1465
+ continuous: true,
1466
+ layer: "debris",
1467
+ user: partIndex,
1468
+ position: [pose[0] + origin[0], pose[1] + origin[1], pose[2] + origin[2]],
1469
+ rotation,
1470
+ linearVelocity: linear,
1471
+ angularVelocity: around
1472
+ });
1473
+ const body = { id, partIndex, shape, extent, followers: [follower(holder, [0, 0, 0])], isIgnoring: true };
1474
+ world.ignorePair(id, chassis, true);
1475
+ pushOutOfTheFixedWorld(body);
1476
+ bodies.set(partIndex, body);
1477
+ return body;
1478
+ };
1479
+ const follow = (body, holder, extent) => {
1480
+ const base = body.extent.centre;
1481
+ body.followers.push(follower(holder, [extent.centre[0] - base[0], extent.centre[1] - base[1], extent.centre[2] - base[2]]));
1482
+ };
1483
+ const place = (body) => {
1484
+ world.readPose(body.id, pose, 0);
1485
+ const rotation = [pose[3], pose[4], pose[5], pose[6]];
1486
+ for (const { holder, offset, turn } of body.followers) {
1487
+ const at = rotateBy(rotation, offset);
1488
+ scratch.position.set(pose[0] + at[0], pose[1] + at[1], pose[2] + at[2]);
1489
+ const turned = multiplyQuat(rotation, turn);
1490
+ const parent = holder.parent;
1491
+ if (parent) parent.worldToLocal(scratch.position);
1492
+ holder.position.copy(scratch.position);
1493
+ scratch.quaternion.set(turned[0], turned[1], turned[2], turned[3]);
1494
+ if (parent) {
1495
+ parent.getWorldQuaternion(scratch.parent);
1496
+ scratch.quaternion.premultiply(scratch.parent.invert());
1497
+ }
1498
+ holder.quaternion.copy(scratch.quaternion);
1499
+ holder.updateMatrix();
1500
+ }
1501
+ };
1502
+ const release = (body, chassis) => {
1503
+ world.readPose(body.id, pose, 0);
1504
+ 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"] });
1505
+ if (clearOut.includes(chassis)) return;
1506
+ world.ignorePair(body.id, chassis, false);
1507
+ body.isIgnoring = false;
1508
+ };
1509
+ return {
1510
+ extentOf,
1511
+ create,
1512
+ follow,
1513
+ bodies,
1514
+ pushedOut: () => pushed,
1515
+ step: (chassis) => {
1516
+ steps++;
1517
+ const check = steps % CLEAR_CHECK_STEPS === 0;
1518
+ for (const body of bodies.values()) {
1519
+ place(body);
1520
+ if (check && body.isIgnoring) release(body, chassis);
1521
+ }
1522
+ },
1523
+ dispose: () => {
1524
+ for (const body of bodies.values()) if (world.has(body.id)) world.removeBody(body.id);
1525
+ bodies.clear();
1526
+ }
1527
+ };
1528
+ };
1529
+ var shifted = (points, by) => {
1530
+ const out = new Float32Array(points.length);
1531
+ for (let i = 0; i < out.length; i += 3) {
1532
+ out[i] = points[i] - by[0];
1533
+ out[i + 1] = points[i + 1] - by[1];
1534
+ out[i + 2] = points[i + 2] - by[2];
1535
+ }
1536
+ return out;
1537
+ };
1538
+
1539
+ // src/crash-parts.ts
1540
+ var CONTACT_POINTS = 64;
1541
+ var SETTLE_STEPS = 30;
1542
+ var SETTLED_SPEED = 0.05;
1543
+ var TOUCH_MARGIN = 0.05;
1544
+ var createCrashParts = (options) => {
1545
+ const { THREE, world, vehicle, frame, colliders } = options;
1546
+ const parts = vehicle.model.parts;
1547
+ const partIndexOf = new Map(parts.map((part, index) => [part.id, index]));
1548
+ const chassisHulls = createChassisHulls(colliders, partIndexOf);
1549
+ const bodies = createPartBodies(THREE, world, frame);
1550
+ const pose = new Float64Array(7);
1551
+ const speed = new Float64Array(6);
1552
+ const touches = new Float32Array(CONTACT_READ_STRIDE * CONTACT_POINTS);
1553
+ const strongest = /* @__PURE__ */ new Map();
1554
+ let chassis;
1555
+ let current = options.mass;
1556
+ let calm = 0;
1557
+ let cooldown = 0;
1558
+ let isDirty = false;
1559
+ let released = 0;
1560
+ let settles = 0;
1561
+ let held = 0;
1562
+ let remade = 0;
1563
+ let unplaced = 0;
1564
+ let blows = 0;
1565
+ let events = 0;
1566
+ const hitParts = /* @__PURE__ */ new Set();
1567
+ const numbers = /* @__PURE__ */ new Map();
1568
+ const carried = (partIndex) => bodies.bodies.get(partIndex);
1569
+ const takePart = (part, partIndex, holder) => {
1570
+ const body = carried(partIndex);
1571
+ const extent = bodies.extentOf(holder);
1572
+ if (body) return bodies.follow(body, holder, extent);
1573
+ const hulls = chassisHulls.release(world, chassis, part.id);
1574
+ const solid = hulls.length > 0 ? hulls : [boxPoints(extent.size, extent.centre)];
1575
+ bodies.create({ partIndex, mass: part.mass, hulls: solid, extent, holder, chassis, chassisMass: current });
1576
+ const own = { mass: part.mass, centre: extent.centre, inertia: boxInertia(part.mass, extent.size) };
1577
+ numbers.set(part.id, own);
1578
+ current = withoutPart(current, own);
1579
+ world.setMassProperties(chassis, { mass: current.mass, inertia: current.inertia, centreOfMass: current.centre });
1580
+ released++;
1581
+ options.onPartLost?.(part);
1582
+ };
1583
+ const isTouching = (points) => {
1584
+ if (!chassis) return false;
1585
+ const count2 = world.readContacts(chassis, touches);
1586
+ if (count2 === 0) return false;
1587
+ world.readPose(chassis, pose, 0);
1588
+ let lowX = Infinity, lowY = Infinity, lowZ = Infinity;
1589
+ let highX = -Infinity, highY = -Infinity, highZ = -Infinity;
1590
+ for (let i = 0; i < points.length; i += 3) {
1591
+ const x = points[i], y = points[i + 1], z = points[i + 2];
1592
+ if (x < lowX) lowX = x;
1593
+ if (x > highX) highX = x;
1594
+ if (y < lowY) lowY = y;
1595
+ if (y > highY) highY = y;
1596
+ if (z < lowZ) lowZ = z;
1597
+ if (z > highZ) highZ = z;
1598
+ }
1599
+ const qx = -pose[3], qy = -pose[4], qz = -pose[5], qw = pose[6];
1600
+ for (let c = 0; c < count2; c++) {
1601
+ const at = c * CONTACT_READ_STRIDE;
1602
+ const vx = touches[at] - pose[0], vy = touches[at + 1] - pose[1], vz = touches[at + 2] - pose[2];
1603
+ const tx = 2 * (qy * vz - qz * vy);
1604
+ const ty = 2 * (qz * vx - qx * vz);
1605
+ const tz = 2 * (qx * vy - qy * vx);
1606
+ const x = vx + qw * tx + qy * tz - qz * ty;
1607
+ const y = vy + qw * ty + qz * tx - qx * tz;
1608
+ const z = vz + qw * tz + qx * ty - qy * tx;
1609
+ 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;
1610
+ }
1611
+ return false;
1612
+ };
1613
+ const feedDamage = () => {
1614
+ const rows = world.contacts;
1615
+ strongest.clear();
1616
+ for (let i = 0; i < rows.count; i++) {
1617
+ const mine = rows.a[i] === chassis ? 0 : rows.b[i] === chassis ? 1 : -1;
1618
+ if (mine < 0) continue;
1619
+ const tag = mine === 0 ? rows.tagA[i] : rows.tagB[i];
1620
+ if (tag < 0 || tag >= parts.length) continue;
1621
+ const at = strongest.get(tag);
1622
+ if (at === void 0 || rows.approach[i] > rows.approach[at]) strongest.set(tag, i);
1623
+ }
1624
+ for (const [tag, i] of strongest) {
1625
+ const mine = rows.a[i] === chassis;
1626
+ const other = mine ? rows.b[i] : rows.a[i];
1627
+ const otherMass = world.has(other) ? world.massOf(other) : 0;
1628
+ const sign = mine ? 1 : -1;
1629
+ const part = parts[tag];
1630
+ const result = crashVehicle2(vehicle, {
1631
+ point: [rows.point[i * 3], rows.point[i * 3 + 1], rows.point[i * 3 + 2]],
1632
+ direction: [sign * rows.normal[i * 3], sign * rows.normal[i * 3 + 1], sign * rows.normal[i * 3 + 2]],
1633
+ speed: rows.approach[i],
1634
+ massKg: current.mass,
1635
+ otherMassKg: otherMass > 0 ? otherMass : Infinity,
1636
+ contactWidth: Math.max(0.2, part.box.size[2]),
1637
+ source: part.id
1638
+ });
1639
+ blows++;
1640
+ events += result.events.length;
1641
+ hitParts.add(part.id);
1642
+ if (result.events.length > 0) {
1643
+ isDirty = true;
1644
+ cooldown = 0;
1645
+ }
1646
+ }
1647
+ };
1648
+ const settle = () => {
1649
+ world.readVelocity(chassis, speed, 0);
1650
+ const still = Math.hypot(speed[0], speed[1], speed[2]) < SETTLED_SPEED && Math.hypot(speed[3], speed[4], speed[5]) < SETTLED_SPEED;
1651
+ calm = still ? calm + 1 : 0;
1652
+ if (calm < SETTLE_STEPS || !(isDirty || chassisHulls.hasWork())) return;
1653
+ if (cooldown > 0) {
1654
+ cooldown--;
1655
+ return;
1656
+ }
1657
+ const done = chassisHulls.settle(world, chassis, isTouching);
1658
+ settles++;
1659
+ held += done.held;
1660
+ remade += done.remade;
1661
+ unplaced += done.unplaced;
1662
+ isDirty = done.held > 0 || colliders.isHullPending();
1663
+ cooldown = done.held > 0 && !colliders.isHullPending() ? SETTLE_STEPS : 0;
1664
+ };
1665
+ vehicle.setPartSink((part, holder) => {
1666
+ const partIndex = partIndexOf.get(part.id);
1667
+ if (chassis === void 0 || partIndex === void 0) return false;
1668
+ takePart(part, partIndex, holder);
1669
+ return true;
1670
+ });
1671
+ return {
1672
+ shape: () => chassisHulls.shape(),
1673
+ bind: (body) => {
1674
+ chassis = body;
1675
+ world.watchContacts(body, true);
1676
+ world.setMassProperties(body, { mass: current.mass, inertia: current.inertia, centreOfMass: current.centre });
1677
+ },
1678
+ step: () => {
1679
+ if (chassis === void 0) return;
1680
+ feedDamage();
1681
+ bodies.step(chassis);
1682
+ settle();
1683
+ },
1684
+ mass: () => current,
1685
+ partNumbers: () => numbers,
1686
+ partBodies: () => new Map([...bodies.bodies].map(([index, body]) => [parts[index].id, body.id])),
1687
+ stats: () => ({ released, pushedOut: bodies.pushedOut(), settles, remade, held, unplaced, blows, events, hitParts, children: chassisHulls.count() }),
1688
+ dispose: () => {
1689
+ vehicle.setPartSink(null);
1690
+ bodies.dispose();
1691
+ if (chassis !== void 0 && world.has(chassis)) world.watchContacts(chassis, false);
1692
+ }
1693
+ };
1694
+ };
1695
+ var boxPoints = (size, centre) => {
1696
+ const out = new Float32Array(24);
1697
+ for (let k = 0; k < 8; k++) {
1698
+ out[k * 3] = centre[0] + (k & 1 ? size[0] : -size[0]) / 2;
1699
+ out[k * 3 + 1] = centre[1] + (k & 2 ? size[1] : -size[1]) / 2;
1700
+ out[k * 3 + 2] = centre[2] + (k & 4 ? size[2] : -size[2]) / 2;
1701
+ }
1702
+ return out;
1703
+ };
1704
+
1705
+ // src/test-area/physical-car.ts
1706
+ var DEFAULT_HULL_CEILING = 550;
1707
+ var HALF_TURN = Math.SQRT1_2;
1708
+ var wheelChildOf = (size, center) => {
1709
+ const axis = size[0] <= size[1] && size[0] <= size[2] ? 0 : size[2] <= size[1] ? 2 : 1;
1710
+ const radius = Math.max(...size.filter((_, side) => side !== axis)) / 2;
1711
+ const shape = { kind: "cylinder", halfHeight: size[axis] / 2, radius };
1712
+ if (axis === 1) return { shape, position: center };
1713
+ return { shape, position: center, rotation: axis === 0 ? [0, 0, HALF_TURN, HALF_TURN] : [HALF_TURN, 0, 0, HALF_TURN] };
1714
+ };
1715
+ var physicsServicesOf = (host) => host && isPhysicsServices(host.physics) ? host.physics : void 0;
1716
+ var physicalCarColliders = (THREE, vehicle, root, options) => createVehicleColliders(THREE, vehicle, root, {
1717
+ budget: options.budget,
1718
+ hulls: true,
1719
+ maxHulls: options.hullCeilingOf?.(options.fleetKey) ?? DEFAULT_HULL_CEILING,
1720
+ planCache: options.planCache,
1721
+ ...options.workers ? { workers: options.workers } : {}
1722
+ });
1723
+ var createPhysicalCar = ({ THREE, services, vehicle, root, colliders }) => {
1724
+ const { world } = services;
1725
+ const pose = new Float64Array(7);
1726
+ const matrix = new THREE.Matrix4();
1727
+ const parentInverse = new THREE.Matrix4();
1728
+ const position = new THREE.Vector3();
1729
+ const rotation = new THREE.Quaternion();
1730
+ const unusedScale = new THREE.Vector3();
1731
+ const wheelHandles = /* @__PURE__ */ new Map();
1732
+ let crash;
1733
+ let chassis;
1734
+ const withWheels = (hulls) => {
1735
+ if (hulls.kind !== "compound") return hulls;
1736
+ const tyres = vehicle.model.parts.filter((part) => part.role === "tyre");
1737
+ tyres.forEach((part, index) => wheelHandles.set(part.id, hulls.children.length + index));
1738
+ const inverse = new THREE.Matrix4().copy(root.matrixWorld).invert();
1739
+ const drawn = new THREE.Box3();
1740
+ const piece = new THREE.Box3();
1741
+ const centre = new THREE.Vector3();
1742
+ const size = new THREE.Vector3();
1743
+ const children = tyres.map((part) => {
1744
+ drawn.makeEmpty();
1745
+ for (const object of vehicle.objectsOf(part.id)) drawn.union(piece.setFromObject(object).applyMatrix4(inverse));
1746
+ if (drawn.isEmpty()) return wheelChildOf(part.box.size, part.box.center);
1747
+ return wheelChildOf(drawn.getSize(size).toArray(), drawn.getCenter(centre).toArray());
1748
+ });
1749
+ return { ...hulls, children: [...hulls.children, ...children] };
1750
+ };
1751
+ const build = () => {
1752
+ if (!root.parent) return;
1753
+ colliders.pieces();
1754
+ if (colliders.isPending()) return;
1755
+ colliders.hulls();
1756
+ if (colliders.isHullPending()) return;
1757
+ root.updateWorldMatrix(true, false);
1758
+ root.matrixWorld.decompose(position, rotation, unusedScale);
1759
+ const mass = bodyMassOf(vehicle.model.parts);
1760
+ crash = createCrashParts({
1761
+ THREE,
1762
+ world,
1763
+ vehicle,
1764
+ frame: root,
1765
+ colliders,
1766
+ mass,
1767
+ onPartLost: (part) => {
1768
+ const handle = wheelHandles.get(part.id);
1769
+ if (handle !== void 0 && chassis !== void 0) world.setChildShape(chassis, handle, null);
1770
+ }
1771
+ });
1772
+ chassis = world.createBody({
1773
+ shape: withWheels(crash.shape()),
1774
+ motion: "dynamic",
1775
+ mass: mass.mass,
1776
+ inertia: mass.inertia,
1777
+ layer: "vehicle",
1778
+ user: 0,
1779
+ position: [position.x, position.y, position.z],
1780
+ rotation: [rotation.x, rotation.y, rotation.z, rotation.w]
1781
+ });
1782
+ crash.bind(chassis);
1783
+ };
1784
+ const follow = () => {
1785
+ world.readPose(chassis, pose, 0);
1786
+ 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));
1787
+ root.parent.updateWorldMatrix(true, false);
1788
+ matrix.premultiply(parentInverse.copy(root.parent.matrixWorld).invert());
1789
+ matrix.decompose(root.position, root.quaternion, unusedScale);
1790
+ root.updateMatrixWorld(true);
1791
+ crash.step();
1792
+ };
1793
+ const stopPhase = services.loop.add({
1794
+ afterSteps: (_dt, frame) => {
1795
+ if (!chassis) return build();
1796
+ if (frame.steps > 0 && root.parent) follow();
1797
+ }
1798
+ }, "contacts");
1799
+ return {
1800
+ colliders: () => [],
1801
+ isBuilt: () => chassis !== void 0,
1802
+ chassis: () => chassis,
1803
+ parts: () => crash,
1804
+ dispose: () => {
1805
+ stopPhase();
1806
+ crash?.dispose();
1807
+ if (chassis !== void 0 && world.has(chassis)) world.removeBody(chassis);
1808
+ crash = void 0;
1809
+ chassis = void 0;
1810
+ }
1811
+ };
1812
+ };
1813
+
1232
1814
  // src/test-area/scenarios.ts
1233
1815
  import { createArcadeDriver, createSemiSimDriver, createSurfaces, vehicleCorners } from "@adatechnology/game-vehicles-driving";
1234
1816
 
@@ -2043,7 +2625,7 @@ var LANE_START_S = 1.2;
2043
2625
  var ZONE_COLOURS = { door: 5627221, hood: 14540083, lid: 14522675, bumper: 14505437, glass: 4500223, light: 16777215, wheel: 16742195 };
2044
2626
  var BODY_HEIGHT = 1.7;
2045
2627
  function createVehiclesTestModule(options = {}) {
2046
- const fleet = options.fleet ?? { colorFromSeed, fleetDims, installFleet, paintPalettes, paletteOf };
2628
+ const fleet = options.fleet ?? { colorFromSeed, fleetDims, hullCeilingOf: fleetHullCeilingOf, installFleet, paintPalettes, paletteOf };
2047
2629
  const locale = options.locale ?? "pt-BR";
2048
2630
  const text = TEXT[locale] ?? TEXT["pt-BR"];
2049
2631
  const keys = options.vehicles ?? DEFAULT_TEST_VEHICLES;
@@ -2057,6 +2639,8 @@ function createVehiclesTestModule(options = {}) {
2057
2639
  const cabins = createCabinSmoke();
2058
2640
  const rainCovers = createVehicleRainCovers();
2059
2641
  let host = null;
2642
+ const physicalCars = /* @__PURE__ */ new Set();
2643
+ const planCache = createHullPlanCache();
2060
2644
  let three = null;
2061
2645
  let counter = 0;
2062
2646
  let helpersShown = "";
@@ -2127,9 +2711,25 @@ function createVehiclesTestModule(options = {}) {
2127
2711
  const kit = r.userData.kit;
2128
2712
  const d = createVehicleDestructible2(kit, r.userData.vehicle, { id, frame: root, name: text.items[itemKey] ?? key, creaseQueue, puddles: false });
2129
2713
  tapEffects(d);
2130
- const colliders = createVehicleColliders(kit.THREE, d, root, { budget: COLLIDER_BUDGET });
2131
- d.colliders = colliders.pieces;
2132
- d.collidersPending = colliders.isPending;
2714
+ const services = physicsServicesOf(host);
2715
+ if (services) {
2716
+ const colliders = physicalCarColliders(kit.THREE, d, root, { budget: COLLIDER_BUDGET, fleetKey: key, hullCeilingOf: fleet.hullCeilingOf, planCache, workers: host?.workers });
2717
+ const car = createPhysicalCar({ THREE: kit.THREE, services, vehicle: d, root, colliders });
2718
+ const disposeVehicle = d.dispose?.bind(d);
2719
+ d.dispose = () => {
2720
+ physicalCars.delete(car);
2721
+ car.dispose();
2722
+ disposeVehicle?.();
2723
+ };
2724
+ physicalCars.add(car);
2725
+ r.userData.physicalCar = car;
2726
+ d.colliders = car.colliders;
2727
+ d.collidersPending = () => false;
2728
+ } else {
2729
+ const colliders = createVehicleColliders(kit.THREE, d, root, { budget: COLLIDER_BUDGET });
2730
+ d.colliders = colliders.pieces;
2731
+ d.collidersPending = colliders.isPending;
2732
+ }
2133
2733
  const setPartPose = d.setPartPose?.bind(d);
2134
2734
  {
2135
2735
  const rests = new Map(d.describe().parts.map((part) => [part.id, part.rotation ?? [0, 0, 0]]));
@@ -2574,6 +3174,8 @@ function createVehiclesTestModule(options = {}) {
2574
3174
  stopBlocker?.();
2575
3175
  stopCrease?.();
2576
3176
  stopLodBatch?.();
3177
+ for (const car of physicalCars) car.dispose();
3178
+ physicalCars.clear();
2577
3179
  cabins.clear(effects());
2578
3180
  rainCovers.clear();
2579
3181
  clearHelpers();
@@ -4674,9 +5276,14 @@ export {
4674
5276
  createVehicleColliders,
4675
5277
  vehicleColliders,
4676
5278
  vehicleHulls,
5279
+ createHullPlanCache,
4677
5280
  VEHICLE_LOD_DISTANCE,
4678
5281
  createVehicleLodQueue,
4679
5282
  createVehicleLod,
5283
+ holdWithin,
5284
+ boxInertia,
5285
+ withoutPart,
5286
+ createCrashParts,
4680
5287
  VEHICLE_SCENARIO_IDS,
4681
5288
  PAGE_SCENARIO_ALIASES,
4682
5289
  PAGE_ITEM_ALIASES,
@@ -4701,4 +5308,4 @@ export {
4701
5308
  createProvingModule,
4702
5309
  loadProvingModule
4703
5310
  };
4704
- //# sourceMappingURL=chunk-TZ23KYPZ.js.map
5311
+ //# sourceMappingURL=chunk-LVDWW5KZ.js.map