@adatechnology/game-vehicles-driving 0.2.28 → 0.2.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/dist/index.d.ts +189 -3
- package/dist/index.global.js +454 -22
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +450 -20
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.global.js
CHANGED
|
@@ -21,11 +21,13 @@ var GameVehiclesDriving = (() => {
|
|
|
21
21
|
// src/index.ts
|
|
22
22
|
var index_exports = {};
|
|
23
23
|
__export(index_exports, {
|
|
24
|
+
CYCLIST: () => CYCLIST,
|
|
24
25
|
DEFAULT_CONTROL_KEYS: () => DEFAULT_CONTROL_KEYS,
|
|
25
26
|
DEFAULT_CRUISE: () => DEFAULT_CRUISE,
|
|
26
27
|
DEFAULT_EJECTION: () => DEFAULT_EJECTION,
|
|
27
28
|
DEFAULT_LOADS: () => DEFAULT_LOADS,
|
|
28
29
|
DEFAULT_PARKING_SENSOR: () => DEFAULT_PARKING_SENSOR,
|
|
30
|
+
DEFAULT_PHYSICS_INPUT: () => DEFAULT_PHYSICS_INPUT,
|
|
29
31
|
DEFAULT_TRACTION: () => DEFAULT_TRACTION,
|
|
30
32
|
DRIVE_PROFILES: () => DRIVE_PROFILES,
|
|
31
33
|
GLOVEBOX: () => GLOVEBOX,
|
|
@@ -33,6 +35,8 @@ var GameVehiclesDriving = (() => {
|
|
|
33
35
|
bumperDistance: () => bumperDistance,
|
|
34
36
|
conditionFromDamage: () => conditionFromDamage,
|
|
35
37
|
createArcadeDriver: () => createArcadeDriver,
|
|
38
|
+
createPhysicsInputMapper: () => createPhysicsInputMapper,
|
|
39
|
+
createRunOver: () => createRunOver,
|
|
36
40
|
createSemiSimDriver: () => createSemiSimDriver,
|
|
37
41
|
createSurfaces: () => createSurfaces,
|
|
38
42
|
createVehicleControls: () => createVehicleControls,
|
|
@@ -48,6 +52,7 @@ var GameVehiclesDriving = (() => {
|
|
|
48
52
|
fuelRate: () => fuelRate,
|
|
49
53
|
impactEnergy: () => impactEnergy,
|
|
50
54
|
parkingBeepInterval: () => parkingBeepInterval,
|
|
55
|
+
resistanceImpulse: () => resistanceImpulse,
|
|
51
56
|
runOver: () => runOver,
|
|
52
57
|
semiSpecFromStats: () => semiSpecFromStats,
|
|
53
58
|
speedLoss: () => speedLoss,
|
|
@@ -55,7 +60,8 @@ var GameVehiclesDriving = (() => {
|
|
|
55
60
|
tractionLimit: () => tractionLimit,
|
|
56
61
|
trunkFor: () => trunkFor,
|
|
57
62
|
vehicleCorners: () => vehicleCorners,
|
|
58
|
-
vehicleDamageFromImpact: () => vehicleDamageFromImpact
|
|
63
|
+
vehicleDamageFromImpact: () => vehicleDamageFromImpact,
|
|
64
|
+
vehicleDescFromSemi: () => vehicleDescFromSemi
|
|
59
65
|
});
|
|
60
66
|
|
|
61
67
|
// src/arcade.ts
|
|
@@ -321,7 +327,8 @@ var GameVehiclesDriving = (() => {
|
|
|
321
327
|
PIECE_BOXES: "destruction.piece-boxes",
|
|
322
328
|
STATIC_COLLIDERS: "destruction.static-colliders",
|
|
323
329
|
SHAPES_GENERATE: "shapes.generate",
|
|
324
|
-
WORLD_REGION: "world.region"
|
|
330
|
+
WORLD_REGION: "world.region",
|
|
331
|
+
VEHICLE_HULL_PLAN: "vehicles.hull-plan"
|
|
325
332
|
};
|
|
326
333
|
var WORKER_KIND_DEFAULTS = {
|
|
327
334
|
[WORKER_KIND_ID.GROUND_TILES]: { lane: "sim", priority: 300, latencyFrames: 2, late: "sync" },
|
|
@@ -329,7 +336,8 @@ var GameVehiclesDriving = (() => {
|
|
|
329
336
|
[WORKER_KIND_ID.PIECE_BOXES]: { lane: "sim", priority: 200, latencyFrames: 2, late: "sync" },
|
|
330
337
|
[WORKER_KIND_ID.STATIC_COLLIDERS]: { lane: "build", priority: 150, late: "sync" },
|
|
331
338
|
[WORKER_KIND_ID.SHAPES_GENERATE]: { lane: "build", priority: 100, late: "sync" },
|
|
332
|
-
[WORKER_KIND_ID.WORLD_REGION]: { lane: "background", priority: 10, latencyFrames: 10, late: "drop" }
|
|
339
|
+
[WORKER_KIND_ID.WORLD_REGION]: { lane: "background", priority: 10, latencyFrames: 10, late: "drop" },
|
|
340
|
+
[WORKER_KIND_ID.VEHICLE_HULL_PLAN]: { lane: "build", priority: 120, late: "keep-waiting" }
|
|
333
341
|
};
|
|
334
342
|
|
|
335
343
|
// src/semi/surfaces.ts
|
|
@@ -665,31 +673,115 @@ var GameVehiclesDriving = (() => {
|
|
|
665
673
|
var vehicleDamageFromImpact = (energy, frontArmor, factor = 0.04) => energy * factor * (1 - frontArmor);
|
|
666
674
|
var speedLoss = (bodyMass, vehicleMass, speed) => bodyMass / (vehicleMass + bodyMass) * speed;
|
|
667
675
|
|
|
676
|
+
// src/semi/vehicle-type.ts
|
|
677
|
+
var CYCLIST = { watts: 250, cadenceRpm: 80, gears: [1.5, 2.5, 3.5] };
|
|
678
|
+
var MOTORCYCLE = { engine: { idleRpm: 1300, peakRpm: 7500, redlineRpm: 1e4 }, gears: [2.8, 2, 1.5, 1.2, 1], finalDrive: 3, efficiency: 0.9, step: 1.25, topShare: 0.8, upshift: 0.6, downshift: 0.3 };
|
|
679
|
+
var TRUCK = { engine: { idleRpm: 600, peakRpm: 1500, redlineRpm: 2500 }, gears: [7, 4.7, 3.2, 2.2, 1.5], finalDrive: 4.5, efficiency: 0.85, step: 1.5, topShare: 0.95, upshift: 0.85, downshift: 0.35 };
|
|
680
|
+
var TRACTOR = { engine: { idleRpm: 800, peakRpm: 1600, redlineRpm: 2300 }, gears: [7, 4.7, 3.2, 2.2, 1.5], finalDrive: 4.5, efficiency: 0.85, step: 1.5, topShare: 0.95, upshift: 0.85, downshift: 0.35 };
|
|
681
|
+
var TRACKED = { engine: { idleRpm: 600, peakRpm: 1500, redlineRpm: 2500 }, gears: [7, 4.7, 3.2, 2.2, 1.5], finalDrive: 4.5, efficiency: 0.85, step: 1.5, topShare: 0.95, upshift: 0.85, downshift: 0.35 };
|
|
682
|
+
var drivetrainOfType = (type) => {
|
|
683
|
+
if (type === "motorcycle") return MOTORCYCLE;
|
|
684
|
+
if (type === "truck") return TRUCK;
|
|
685
|
+
if (type === "tractor") return TRACTOR;
|
|
686
|
+
if (type === "tracked") return TRACKED;
|
|
687
|
+
return void 0;
|
|
688
|
+
};
|
|
689
|
+
var isTwoWheeler = (type) => type === "motorcycle" || type === "bicycle";
|
|
690
|
+
var wheelsOfAxles = ({ axles, fourWheelDrive, twoWheeler }) => {
|
|
691
|
+
const front = axles[0].x;
|
|
692
|
+
const rear = axles[axles.length - 1].x;
|
|
693
|
+
const middle = (front + rear) / 2;
|
|
694
|
+
return axles.flatMap((axle, index) => {
|
|
695
|
+
const isFront = index === 0;
|
|
696
|
+
const isRear = index === axles.length - 1;
|
|
697
|
+
const common = { x: axle.x - middle, ...isFront ? { steer: true } : {}, ...isRear ? { handbrake: true } : {} };
|
|
698
|
+
const drive = twoWheeler ? isRear : fourWheelDrive || !isFront;
|
|
699
|
+
const wheel = (z) => ({ ...common, z, ...drive ? { drive: true } : {} });
|
|
700
|
+
return axle.track > 0 ? [wheel(-axle.track / 2), wheel(axle.track / 2)] : [wheel(0)];
|
|
701
|
+
});
|
|
702
|
+
};
|
|
703
|
+
var trackStandIn = (tracks) => {
|
|
704
|
+
const side = tracks.gauge / 2;
|
|
705
|
+
return [
|
|
706
|
+
{ x: tracks.frontX, z: -side, steer: true, drive: true },
|
|
707
|
+
{ x: tracks.frontX, z: side, steer: true, drive: true },
|
|
708
|
+
{ x: tracks.rearX, z: -side, drive: true, handbrake: true },
|
|
709
|
+
{ x: tracks.rearX, z: side, drive: true, handbrake: true }
|
|
710
|
+
];
|
|
711
|
+
};
|
|
712
|
+
var tracksOfBody = (tracks) => {
|
|
713
|
+
const middle = (tracks.frontX + tracks.rearX) / 2;
|
|
714
|
+
return { gauge: tracks.gauge, rearX: tracks.rearX - middle, frontX: tracks.frontX - middle, roadWheels: tracks.roadWheels, roadRadius: tracks.radius };
|
|
715
|
+
};
|
|
716
|
+
|
|
668
717
|
// src/semi/from-stats.ts
|
|
718
|
+
var RIDER_MASS_KG = 75;
|
|
719
|
+
var MOTORCYCLE_POWER_FACTOR = 1.3;
|
|
720
|
+
var MOTORCYCLE_POWER_RATING_1 = 11800 / (132 + RIDER_MASS_KG);
|
|
721
|
+
var MOTORCYCLE_POWER_RATING_10 = 33400 / (168 + RIDER_MASS_KG);
|
|
722
|
+
var motorcyclePower = (rating) => MOTORCYCLE_POWER_FACTOR * (MOTORCYCLE_POWER_RATING_1 + (MOTORCYCLE_POWER_RATING_10 - MOTORCYCLE_POWER_RATING_1) * (rating - 1) / 9);
|
|
723
|
+
var carPower = (rating) => (0.06 + 0.015 * rating) * (defaultSemiSpec.engine.peakRpm * 2 * Math.PI / 60);
|
|
724
|
+
var peakTorqueOf = (type, rating, mass, peakRpm) => {
|
|
725
|
+
const power = type === "motorcycle" ? motorcyclePower(rating) : carPower(rating);
|
|
726
|
+
return mass * power * 60 / (2 * Math.PI * peakRpm);
|
|
727
|
+
};
|
|
728
|
+
var cyclistFor = (top, wheelRadius) => {
|
|
729
|
+
const topGear = top / (2 * Math.PI * wheelRadius) / (CYCLIST.cadenceRpm / 60);
|
|
730
|
+
return { ...CYCLIST, gears: [topGear / 1.4 ** 2, topGear / 1.4, topGear] };
|
|
731
|
+
};
|
|
732
|
+
var axlesOfBody = (body, twoWheeler) => body.axles ?? [{ x: body.wheelbase / 2, track: twoWheeler ? 0 : body.track, radius: defaultSemiSpec.wheelRadius }, { x: -body.wheelbase / 2, track: twoWheeler ? 0 : body.track, radius: defaultSemiSpec.wheelRadius }];
|
|
733
|
+
var drivenRadius = (axles, driveAll, twoWheeler) => {
|
|
734
|
+
const driven = twoWheeler ? axles.slice(-1) : driveAll ? axles : axles.slice(1);
|
|
735
|
+
return driven.reduce((sum, axle) => sum + axle.radius, 0) / driven.length;
|
|
736
|
+
};
|
|
669
737
|
function semiSpecFromStats(stats, body, base = {}) {
|
|
670
|
-
const
|
|
671
|
-
const
|
|
672
|
-
const
|
|
738
|
+
const type = body.type ?? "car";
|
|
739
|
+
const mass = body.mass + (type === "motorcycle" ? RIDER_MASS_KG : 0);
|
|
740
|
+
const twoWheeler = isTwoWheeler(type);
|
|
741
|
+
const drivetrain = drivetrainOfType(type);
|
|
742
|
+
const tracks = type === "tracked" && body.tracks ? tracksOfBody(body.tracks) : void 0;
|
|
743
|
+
const axles = axlesOfBody(body, twoWheeler);
|
|
673
744
|
const driveAll = !!body.fourWheelDrive;
|
|
674
|
-
const
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
{
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
745
|
+
const wheelRadius = tracks?.roadRadius ?? drivenRadius(axles, driveAll, twoWheeler);
|
|
746
|
+
const spec = {
|
|
747
|
+
...defaultSemiSpec,
|
|
748
|
+
...drivetrain ? { engine: { ...defaultSemiSpec.engine, ...drivetrain.engine }, gears: drivetrain.gears, finalDrive: drivetrain.finalDrive, efficiency: drivetrain.efficiency, upshiftFraction: drivetrain.upshift, downshiftFraction: drivetrain.downshift } : {},
|
|
749
|
+
...type === "bicycle" ? { finalDrive: 1, efficiency: 0.95 } : {},
|
|
750
|
+
wheelRadius,
|
|
751
|
+
centreHeight: twoWheeler ? 0.6 : Math.max(defaultSemiSpec.centreHeight, wheelRadius + 0.1),
|
|
752
|
+
...base
|
|
753
|
+
};
|
|
754
|
+
const wheels = tracks ? trackStandIn(tracks) : wheelsOfAxles({ axles, fourWheelDrive: driveAll, twoWheeler });
|
|
755
|
+
const pedal = type === "bicycle" ? cyclistFor(stats.maxSpeedKmh / 3.6, wheelRadius) : void 0;
|
|
756
|
+
const peakTorque = pedal ? pedal.watts * 60 / (2 * Math.PI * pedal.cadenceRpm) : peakTorqueOf(type, stats.acceleration, mass, spec.engine.peakRpm);
|
|
757
|
+
const engine = pedal ? { idleRpm: 40, peakRpm: pedal.cadenceRpm, redlineRpm: pedal.cadenceRpm * 1.5, peakTorque } : { ...spec.engine, peakTorque };
|
|
682
758
|
const top = stats.maxSpeedKmh / 3.6;
|
|
683
|
-
const
|
|
684
|
-
const
|
|
685
|
-
const
|
|
686
|
-
const
|
|
687
|
-
const
|
|
759
|
+
const topShare = drivetrain?.topShare ?? 0.95;
|
|
760
|
+
const lastRatio = engine.redlineRpm * topShare * 2 * Math.PI * spec.wheelRadius / (60 * top * spec.finalDrive);
|
|
761
|
+
const stack = drivetrain?.gears ?? spec.gears;
|
|
762
|
+
const step = drivetrain?.step ?? 1.25;
|
|
763
|
+
const gears = pedal ? pedal.gears.map((ratio) => 1 / ratio) : [...stack.slice(0, -1).map((g, i, all) => Math.max(g, lastRatio * step ** (all.length - i))), lastRatio];
|
|
764
|
+
const torqueAtTop = type === "car" ? peakTorque * 0.76 : torqueAt({ ...spec, engine }, engine.redlineRpm * topShare);
|
|
765
|
+
const topRatio = pedal ? gears[gears.length - 1] : lastRatio;
|
|
766
|
+
const thrust = pedal ? pedal.watts * spec.efficiency / top : torqueAtTop * topRatio * spec.finalDrive * spec.efficiency / spec.wheelRadius;
|
|
767
|
+
const rollingForce = spec.rolling * mass * 9.81;
|
|
688
768
|
const drag = Math.max(0.01, (thrust - rollingForce) / (top * top));
|
|
689
769
|
const carRatio = defaultSemiSpec.brakeForce / (defaultSemiSpec.mass * 9.81);
|
|
690
|
-
const heavyShare = Math.max(0, Math.min(1, (
|
|
691
|
-
const brakeForce = base.brakeForce ??
|
|
692
|
-
return {
|
|
770
|
+
const heavyShare = Math.max(0, Math.min(1, (mass - 2e3) / 8e3));
|
|
771
|
+
const brakeForce = base.brakeForce ?? mass * 9.81 * (carRatio + (0.6 - carRatio) * heavyShare);
|
|
772
|
+
return {
|
|
773
|
+
...spec,
|
|
774
|
+
mass,
|
|
775
|
+
wheels,
|
|
776
|
+
fourWheelDrive: driveAll,
|
|
777
|
+
engine,
|
|
778
|
+
gears,
|
|
779
|
+
drag,
|
|
780
|
+
brakeForce,
|
|
781
|
+
tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling },
|
|
782
|
+
...pedal ? { pedal } : {},
|
|
783
|
+
...tracks ? { tracks } : {}
|
|
784
|
+
};
|
|
693
785
|
}
|
|
694
786
|
|
|
695
787
|
// src/driving-aids.ts
|
|
@@ -1272,6 +1364,346 @@ var GameVehiclesDriving = (() => {
|
|
|
1272
1364
|
}
|
|
1273
1365
|
};
|
|
1274
1366
|
}
|
|
1367
|
+
|
|
1368
|
+
// src/physics-desc.ts
|
|
1369
|
+
var GRAVITY = 9.81;
|
|
1370
|
+
var CURVE_CUT_GAP_RPM = 1;
|
|
1371
|
+
var FRONT_BRAKE_SHARE = 0.6;
|
|
1372
|
+
var HANDBRAKE_SHARE = 0.5;
|
|
1373
|
+
var SUSPENSION_MIN_SHARE = 0.4;
|
|
1374
|
+
var MAX_SAG = 0.5;
|
|
1375
|
+
var LEAN_SPRING_PER_INERTIA = 400;
|
|
1376
|
+
var LEAN_DAMPING_PER_INERTIA = 40;
|
|
1377
|
+
var LEAN_LIMIT = 0.7;
|
|
1378
|
+
var TRACK_LATERAL_FRICTION = 0.5;
|
|
1379
|
+
var WHEEL_WIDTH = 0.2;
|
|
1380
|
+
var CLUTCH_STRENGTH = 10;
|
|
1381
|
+
var CLUTCH_SLIP = 0.03;
|
|
1382
|
+
var BICYCLE_WHEEL_INERTIA = 0.1;
|
|
1383
|
+
var WHEEL_INERTIA_PER_MASS = 8e-4;
|
|
1384
|
+
var REFERENCE_RADIUS = 0.32;
|
|
1385
|
+
var MIN_WHEEL_INERTIA = 0.02;
|
|
1386
|
+
var WHEEL_DAMPING = 0.02;
|
|
1387
|
+
var ENGINE_DAMPING = 5e-3;
|
|
1388
|
+
var ENGINE_INERTIA_PER_TORQUE = 1e-3;
|
|
1389
|
+
var ENGINE_FLOOR = 0.55;
|
|
1390
|
+
var SUSPENSION_FLOOR = 0.5;
|
|
1391
|
+
var CAR_TRANSMISSION = { switchTime: 0.1, clutchReleaseTime: 0.1, switchLatency: 0.1 };
|
|
1392
|
+
var MOTORCYCLE_TRANSMISSION = { switchTime: 0.05, clutchReleaseTime: 0.07, switchLatency: 0.05 };
|
|
1393
|
+
var clutchFor = (peakTorque, maxRpm) => Math.max(CLUTCH_STRENGTH, peakTorque / (CLUTCH_SLIP * (maxRpm * 2 * Math.PI / 60)));
|
|
1394
|
+
var clamp5 = (x, low, high) => Math.max(low, Math.min(high, x));
|
|
1395
|
+
var torqueCurve = (spec) => {
|
|
1396
|
+
const { idleRpm, peakRpm, redlineRpm, peakTorque } = spec.engine;
|
|
1397
|
+
const at = (rpm) => torqueAt(spec, rpm) / peakTorque;
|
|
1398
|
+
return [[0, at(0)], [idleRpm / redlineRpm, at(idleRpm)], [peakRpm / redlineRpm, at(peakRpm)], [(redlineRpm - CURVE_CUT_GAP_RPM) / redlineRpm, at(redlineRpm - CURVE_CUT_GAP_RPM)], [1, 0]];
|
|
1399
|
+
};
|
|
1400
|
+
var lateralCurve = (friction, corneringStiffness) => [[0, 0], [friction / corneringStiffness * 180 / Math.PI, friction], [90, 0.75 * friction]];
|
|
1401
|
+
var longitudinalCurve = (friction) => [[0, 0], [0.06, friction], [1, 0.8 * friction]];
|
|
1402
|
+
var isFlat = (condition, index) => (condition.tyres?.[index] ?? 1) < FLAT_BELOW || condition.rims?.[index] === true;
|
|
1403
|
+
var wheelOf = (wheel, index, context) => {
|
|
1404
|
+
const { spec, condition, options } = context;
|
|
1405
|
+
const cornerMass = spec.mass / spec.wheels.length;
|
|
1406
|
+
const wear = SUSPENSION_FLOOR + (1 - SUSPENSION_FLOOR) * clamp5(condition.suspension ?? 1, 0, 1);
|
|
1407
|
+
const free = spec.suspension.restLength;
|
|
1408
|
+
const stiffening = Math.max(1, cornerMass * GRAVITY / (MAX_SAG * free * spec.suspension.spring * wear));
|
|
1409
|
+
const spring = spec.suspension.spring * wear * stiffening;
|
|
1410
|
+
const damper = spec.suspension.damper * wear * stiffening;
|
|
1411
|
+
const sag = cornerMass * GRAVITY / spring;
|
|
1412
|
+
const flat = isFlat(condition, index);
|
|
1413
|
+
const rimRadius = spec.rimRadius ?? RIM_RADIUS_SHARE * spec.wheelRadius;
|
|
1414
|
+
const friction = flat ? RIM_FRICTION : spec.tyre.friction;
|
|
1415
|
+
const front = wheel.x >= context.frontFrom;
|
|
1416
|
+
const brakeShare = front ? FRONT_BRAKE_SHARE / context.frontCount : (1 - FRONT_BRAKE_SHARE) / context.rearCount;
|
|
1417
|
+
const brakeTorque = spec.brakeForce * clamp5(condition.brakes ?? 1, 0, 1) * spec.wheelRadius * brakeShare;
|
|
1418
|
+
return {
|
|
1419
|
+
// x is to the vehicle's left and z ahead; the semi-simulation has z to the right
|
|
1420
|
+
position: [-wheel.z, spec.wheelRadius - spec.centreHeight + (free - sag), wheel.x],
|
|
1421
|
+
radius: flat ? rimRadius : spec.wheelRadius,
|
|
1422
|
+
width: options.wheelWidth ?? WHEEL_WIDTH,
|
|
1423
|
+
suspensionMin: free * SUSPENSION_MIN_SHARE,
|
|
1424
|
+
suspensionMax: free,
|
|
1425
|
+
frequency: Math.sqrt(spring / cornerMass) / (2 * Math.PI),
|
|
1426
|
+
damping: damper / (2 * Math.sqrt(spring * cornerMass)),
|
|
1427
|
+
maxSteer: wheel.steer ? spec.maxSteer : 0,
|
|
1428
|
+
driven: spec.fourWheelDrive || wheel.drive === true,
|
|
1429
|
+
maxBrakeTorque: brakeTorque,
|
|
1430
|
+
maxHandBrakeTorque: wheel.handbrake ? spec.brakeForce * spec.wheelRadius * HANDBRAKE_SHARE / context.handbrakeCount : 0,
|
|
1431
|
+
inertia: options.wheelInertia ?? (options.pedal ? BICYCLE_WHEEL_INERTIA : Math.max(MIN_WHEEL_INERTIA, WHEEL_INERTIA_PER_MASS * spec.mass * (spec.wheelRadius / REFERENCE_RADIUS) ** 2)),
|
|
1432
|
+
angularDamping: WHEEL_DAMPING,
|
|
1433
|
+
longitudinalFriction: longitudinalCurve(friction),
|
|
1434
|
+
lateralFriction: lateralCurve(friction, spec.tyre.corneringStiffness)
|
|
1435
|
+
};
|
|
1436
|
+
};
|
|
1437
|
+
var wheelContext = (spec, options) => {
|
|
1438
|
+
const meanX = spec.wheels.reduce((sum, wheel) => sum + wheel.x, 0) / spec.wheels.length;
|
|
1439
|
+
const frontCount = spec.wheels.filter((wheel) => wheel.x >= meanX).length;
|
|
1440
|
+
return {
|
|
1441
|
+
spec,
|
|
1442
|
+
condition: options.condition ?? {},
|
|
1443
|
+
options,
|
|
1444
|
+
frontFrom: meanX,
|
|
1445
|
+
frontCount,
|
|
1446
|
+
rearCount: Math.max(1, spec.wheels.length - frontCount),
|
|
1447
|
+
handbrakeCount: Math.max(1, spec.wheels.filter((wheel) => wheel.handbrake).length)
|
|
1448
|
+
};
|
|
1449
|
+
};
|
|
1450
|
+
var inertiaOf = (spec) => {
|
|
1451
|
+
const xs = spec.wheels.map((wheel) => wheel.x);
|
|
1452
|
+
const zs = spec.wheels.map((wheel) => wheel.z);
|
|
1453
|
+
const wheelbase = Math.max(...xs) - Math.min(...xs);
|
|
1454
|
+
const track = Math.max(...zs) - Math.min(...zs);
|
|
1455
|
+
return [spec.mass * (wheelbase * wheelbase + 1) / 12, spec.yawInertia ?? spec.mass * (wheelbase * wheelbase + track * track) / 12, spec.mass * (track * track + 1) / 12];
|
|
1456
|
+
};
|
|
1457
|
+
var drivetrainOf = (spec, options) => {
|
|
1458
|
+
const pedal = options.pedal ?? spec.pedal;
|
|
1459
|
+
if (pedal) {
|
|
1460
|
+
const crankSpeed = pedal.cadenceRpm * 2 * Math.PI / 60;
|
|
1461
|
+
return {
|
|
1462
|
+
engineDamping: ENGINE_DAMPING,
|
|
1463
|
+
engineInertia: ENGINE_INERTIA_PER_TORQUE * (pedal.watts / crankSpeed),
|
|
1464
|
+
maxTorque: pedal.watts / crankSpeed * spec.efficiency,
|
|
1465
|
+
minRpm: 0,
|
|
1466
|
+
maxRpm: pedal.cadenceRpm * 1.5,
|
|
1467
|
+
// the engine turns once for every wheel turn / ratio: a gear of 2 turns the wheel twice a pedal turn
|
|
1468
|
+
gears: pedal.gears.map((ratio) => 1 / ratio),
|
|
1469
|
+
reverseGears: [],
|
|
1470
|
+
differentialRatio: 1,
|
|
1471
|
+
torqueCurve: [[0, 1], [0.67, 1], [1, 0]],
|
|
1472
|
+
transmission: { shiftUpRpm: pedal.cadenceRpm * 0.95, shiftDownRpm: pedal.cadenceRpm * 0.5, clutchStrength: clutchFor(pedal.watts / crankSpeed, pedal.cadenceRpm * 1.5) }
|
|
1473
|
+
};
|
|
1474
|
+
}
|
|
1475
|
+
const health = clamp5(options.condition?.engine ?? 1, 0, 1);
|
|
1476
|
+
return {
|
|
1477
|
+
engineDamping: ENGINE_DAMPING,
|
|
1478
|
+
engineInertia: ENGINE_INERTIA_PER_TORQUE * spec.engine.peakTorque,
|
|
1479
|
+
maxTorque: spec.engine.peakTorque * spec.efficiency * (ENGINE_FLOOR + (1 - ENGINE_FLOOR) * health),
|
|
1480
|
+
minRpm: spec.engine.idleRpm,
|
|
1481
|
+
maxRpm: spec.engine.redlineRpm,
|
|
1482
|
+
gears: spec.gears,
|
|
1483
|
+
reverseGears: [-spec.reverseRatio],
|
|
1484
|
+
differentialRatio: spec.finalDrive,
|
|
1485
|
+
torqueCurve: torqueCurve(spec),
|
|
1486
|
+
transmission: {
|
|
1487
|
+
shiftUpRpm: spec.engine.redlineRpm * spec.upshiftFraction,
|
|
1488
|
+
shiftDownRpm: spec.engine.redlineRpm * spec.downshiftFraction,
|
|
1489
|
+
clutchStrength: clutchFor(spec.engine.peakTorque, spec.engine.redlineRpm),
|
|
1490
|
+
...spec.wheels.length === 2 ? MOTORCYCLE_TRANSMISSION : CAR_TRANSMISSION
|
|
1491
|
+
}
|
|
1492
|
+
};
|
|
1493
|
+
};
|
|
1494
|
+
var trackedOf = (spec, tracks) => {
|
|
1495
|
+
const count = Math.max(2, tracks.roadWheels);
|
|
1496
|
+
const alongX = (index) => tracks.rearX + (tracks.frontX - tracks.rearX) * index / (count - 1);
|
|
1497
|
+
const wheels = [-1, 1].flatMap((side2) => Array.from({ length: count }, (_, index) => ({ x: alongX(index), z: side2 * tracks.gauge / 2 })));
|
|
1498
|
+
const friction = spec.tyre.friction;
|
|
1499
|
+
const side = (first) => ({ wheels: Array.from({ length: count }, (_, index) => first + index), drivenWheel: first, longitudinalFriction: friction, lateralFriction: TRACK_LATERAL_FRICTION, maxBrakeTorque: spec.brakeForce * spec.wheelRadius / 2 });
|
|
1500
|
+
return { spec: { ...spec, wheels }, tracks: [side(0), side(count)] };
|
|
1501
|
+
};
|
|
1502
|
+
var vehicleDescFromSemi = (spec, options) => {
|
|
1503
|
+
const tracked = spec.tracks ? trackedOf(spec, spec.tracks) : void 0;
|
|
1504
|
+
const body = tracked ? tracked.spec : spec;
|
|
1505
|
+
const context = wheelContext(body, options);
|
|
1506
|
+
const inertia = inertiaOf(spec);
|
|
1507
|
+
const bike = body.wheels.length === 2;
|
|
1508
|
+
return {
|
|
1509
|
+
// no damping of the body's own: it would be a drag of 0.05 m/s per m/s (1.8 kN at 28 m/s on a 1.3 t car) the spec never asked for; `resistanceImpulse` is the drag
|
|
1510
|
+
chassis: { linearDamping: 0, angularDamping: 0, ...options.chassis, motion: "dynamic", mass: spec.mass, inertia, centreAtOrigin: true, allowSleep: true },
|
|
1511
|
+
wheels: body.wheels.map((wheel, index) => wheelOf(wheel, index, context)),
|
|
1512
|
+
...drivetrainOf(spec, options),
|
|
1513
|
+
...tracked ? { controller: "tracked", tracks: tracked.tracks } : {},
|
|
1514
|
+
...bike ? { controller: "motorcycle", motorcycle: { maxLeanAngle: LEAN_LIMIT, leanSpring: LEAN_SPRING_PER_INERTIA * inertia[2], leanDamping: LEAN_DAMPING_PER_INERTIA * inertia[2] } } : {}
|
|
1515
|
+
};
|
|
1516
|
+
};
|
|
1517
|
+
|
|
1518
|
+
// src/physics-input.ts
|
|
1519
|
+
var DEFAULT_PHYSICS_INPUT = { steerResponse: 5, steerFalloff: 0.012, steerFloor: 0.4, reverseBelow: 0.5, pullShare: 0.2 };
|
|
1520
|
+
var clamp6 = (x, low, high) => Math.max(low, Math.min(high, x));
|
|
1521
|
+
var createPhysicsInputMapper = (options = {}) => {
|
|
1522
|
+
const rules = { ...DEFAULT_PHYSICS_INPUT, ...options };
|
|
1523
|
+
let steering = 0;
|
|
1524
|
+
return {
|
|
1525
|
+
reset() {
|
|
1526
|
+
steering = 0;
|
|
1527
|
+
},
|
|
1528
|
+
map({ input, speed, dt, status }) {
|
|
1529
|
+
const pedal = clamp6(input.throttle, 0, 1) - clamp6(input.brake, 0, 1);
|
|
1530
|
+
const powerLimit = clamp6(input.powerLimit ?? 1, 0, 1);
|
|
1531
|
+
const canDrive = status?.canDrive ?? true;
|
|
1532
|
+
const brakeSoftness = 1 / Math.max(1, status?.brakeFactor ?? 1);
|
|
1533
|
+
let forward = 0;
|
|
1534
|
+
let brake = 0;
|
|
1535
|
+
if (pedal > 0) {
|
|
1536
|
+
if (speed < -rules.reverseBelow) brake = pedal * brakeSoftness;
|
|
1537
|
+
else forward = pedal * powerLimit;
|
|
1538
|
+
} else if (pedal < 0) {
|
|
1539
|
+
if (speed > rules.reverseBelow) brake = -pedal * brakeSoftness;
|
|
1540
|
+
else forward = pedal * powerLimit;
|
|
1541
|
+
}
|
|
1542
|
+
const lock = Math.max(rules.steerFloor, 1 - rules.steerFalloff * Math.abs(speed));
|
|
1543
|
+
const target = -clamp6(input.steer, -1, 1) * lock;
|
|
1544
|
+
steering += (target - steering) * Math.min(1, dt * rules.steerResponse);
|
|
1545
|
+
const pull = clamp6(status?.steerPull ?? 0, -1, 1) * rules.pullShare;
|
|
1546
|
+
return { forward: canDrive ? forward : 0, right: clamp6(steering + pull, -1, 1), brake, handBrake: input.handbrake ? 1 : 0 };
|
|
1547
|
+
}
|
|
1548
|
+
};
|
|
1549
|
+
};
|
|
1550
|
+
|
|
1551
|
+
// src/physics-resistance.ts
|
|
1552
|
+
var GRAVITY2 = 9.81;
|
|
1553
|
+
var ROLLING_FADE_SPEED = 0.5;
|
|
1554
|
+
var resistanceImpulse = (spec, velocity, dt) => {
|
|
1555
|
+
const speed = Math.hypot(velocity[0], velocity[2]);
|
|
1556
|
+
if (speed === 0) return [0, 0, 0];
|
|
1557
|
+
const rolling = spec.rolling * spec.mass * GRAVITY2 * Math.min(1, speed / ROLLING_FADE_SPEED);
|
|
1558
|
+
const along = -(spec.drag * speed * speed + rolling) * dt;
|
|
1559
|
+
return [velocity[0] / speed * along, 0, velocity[2] / speed * along];
|
|
1560
|
+
};
|
|
1561
|
+
|
|
1562
|
+
// src/physics-run-over.ts
|
|
1563
|
+
var byIndexDescending = (a, b) => b - a;
|
|
1564
|
+
var MIN_SPEED = 1;
|
|
1565
|
+
var REFERENCE_MASS = 70;
|
|
1566
|
+
var PUSH_ASIDE_SPEED = 1;
|
|
1567
|
+
var createRunOver = (loop, options) => {
|
|
1568
|
+
const { crowd, agents } = options;
|
|
1569
|
+
const rules = { ...defaultArcadeSpec.runOver, ...options.rules };
|
|
1570
|
+
const world = loop.world;
|
|
1571
|
+
const vehicles = [];
|
|
1572
|
+
const struck = /* @__PURE__ */ new Map();
|
|
1573
|
+
const hits = [];
|
|
1574
|
+
const pose = new Float64Array(7);
|
|
1575
|
+
const velocity = new Float64Array(6);
|
|
1576
|
+
const box = { kind: "box", half: [0, 0, 0] };
|
|
1577
|
+
const centre = [0, 0, 0];
|
|
1578
|
+
const rotation = [0, 0, 0, 1];
|
|
1579
|
+
const impulse = [0, 0, 0];
|
|
1580
|
+
const point = [0, 0, 0];
|
|
1581
|
+
const direction = [0, 0];
|
|
1582
|
+
const car = { speed: 0, mass: 0 };
|
|
1583
|
+
const found = [];
|
|
1584
|
+
const now = /* @__PURE__ */ new Set();
|
|
1585
|
+
const indexOfBody = /* @__PURE__ */ new Map();
|
|
1586
|
+
let isIndexStale = true;
|
|
1587
|
+
let dying;
|
|
1588
|
+
let diedLast = false;
|
|
1589
|
+
const agentsOf = (bodies) => {
|
|
1590
|
+
if (isIndexStale) {
|
|
1591
|
+
indexOfBody.clear();
|
|
1592
|
+
for (let index = 0; index < agents.count; index++) {
|
|
1593
|
+
const body = agents.bodyOf(index);
|
|
1594
|
+
if (body !== void 0) indexOfBody.set(body, index);
|
|
1595
|
+
}
|
|
1596
|
+
isIndexStale = false;
|
|
1597
|
+
}
|
|
1598
|
+
found.length = 0;
|
|
1599
|
+
for (const body of bodies) {
|
|
1600
|
+
const index = indexOfBody.get(body);
|
|
1601
|
+
if (index !== void 0) found.push(index);
|
|
1602
|
+
}
|
|
1603
|
+
return found.sort(byIndexDescending);
|
|
1604
|
+
};
|
|
1605
|
+
const strike = (index, outcome, direction2, hit, h) => {
|
|
1606
|
+
const { store } = crowd;
|
|
1607
|
+
if (outcome === "push") {
|
|
1608
|
+
store.x[index] += -direction2[1] * PUSH_ASIDE_SPEED * h;
|
|
1609
|
+
store.z[index] += direction2[0] * PUSH_ASIDE_SPEED * h;
|
|
1610
|
+
return hit.carMomentum * (1 - Math.exp(-rules.holdDrag * h));
|
|
1611
|
+
}
|
|
1612
|
+
dying = [direction2[0] * hit.common, 0, direction2[1] * hit.common];
|
|
1613
|
+
if (outcome === "kill") crowd.kill(index);
|
|
1614
|
+
else if (!crowd.damage(index, { region: 1, amount: hit.reduced / REFERENCE_MASS * hit.closing * hit.closing * rules.knockDamage, distance: 0 }).died) {
|
|
1615
|
+
dying = void 0;
|
|
1616
|
+
crowd.knockDown(index, { x: direction2[0] * hit.common, z: direction2[1] * hit.common });
|
|
1617
|
+
}
|
|
1618
|
+
const died = dying !== void 0;
|
|
1619
|
+
dying = void 0;
|
|
1620
|
+
diedLast = died;
|
|
1621
|
+
return hit.reduced * hit.closing;
|
|
1622
|
+
};
|
|
1623
|
+
const hitOne = (vehicle, index, direction2, car2, h) => {
|
|
1624
|
+
const { store } = crowd;
|
|
1625
|
+
const mass = store.mass[index] > 0 ? store.mass[index] : REFERENCE_MASS;
|
|
1626
|
+
const closing = car2.speed - (store.vx[index] * direction2[0] + store.vz[index] * direction2[1]);
|
|
1627
|
+
if (closing <= 0) return void 0;
|
|
1628
|
+
const reduced = mass * car2.mass / (mass + car2.mass);
|
|
1629
|
+
const outcome = runOver(closing, rules);
|
|
1630
|
+
diedLast = false;
|
|
1631
|
+
const lost = strike(index, outcome, direction2, { reduced, closing, common: car2.speed - mass / (mass + car2.mass) * closing, carMomentum: car2.mass * car2.speed }, h);
|
|
1632
|
+
car2.speed -= lost / car2.mass;
|
|
1633
|
+
impulse[0] = -direction2[0] * lost;
|
|
1634
|
+
impulse[2] = -direction2[1] * lost;
|
|
1635
|
+
world.addImpulse(vehicle.body, impulse, point);
|
|
1636
|
+
options.onHit?.({ vehicle: vehicle.body, agent: index, outcome, speed: closing, energy: 0.5 * reduced * closing * closing, vehicleDamage: diedLast ? rules.killVehicleDamage : 0 });
|
|
1637
|
+
return outcome;
|
|
1638
|
+
};
|
|
1639
|
+
const sweep = (vehicle, h) => {
|
|
1640
|
+
world.readVelocity(vehicle.body, velocity, 0);
|
|
1641
|
+
const speed = Math.hypot(velocity[0], velocity[2]);
|
|
1642
|
+
if (speed < MIN_SPEED) return;
|
|
1643
|
+
world.readPose(vehicle.body, pose, 0);
|
|
1644
|
+
direction[0] = velocity[0] / speed;
|
|
1645
|
+
direction[1] = velocity[2] / speed;
|
|
1646
|
+
const reach = speed * h / 2;
|
|
1647
|
+
box.half[0] = vehicle.half[0] + rules.margin;
|
|
1648
|
+
box.half[1] = vehicle.half[1];
|
|
1649
|
+
box.half[2] = vehicle.half[2] + rules.margin + reach;
|
|
1650
|
+
centre[0] = pose[0] + direction[0] * reach;
|
|
1651
|
+
centre[1] = pose[1];
|
|
1652
|
+
centre[2] = pose[2] + direction[1] * reach;
|
|
1653
|
+
for (let k = 0; k < 4; k++) rotation[k] = pose[3 + k];
|
|
1654
|
+
point[1] = pose[1];
|
|
1655
|
+
if (world.overlapShape(box, centre, rotation, hits, { layers: ["agent"] }) === 0) {
|
|
1656
|
+
struck.delete(vehicle);
|
|
1657
|
+
return;
|
|
1658
|
+
}
|
|
1659
|
+
car.speed = speed;
|
|
1660
|
+
car.mass = world.massOf(vehicle.body);
|
|
1661
|
+
let before = struck.get(vehicle);
|
|
1662
|
+
if (!before) {
|
|
1663
|
+
before = /* @__PURE__ */ new Set();
|
|
1664
|
+
struck.set(vehicle, before);
|
|
1665
|
+
}
|
|
1666
|
+
for (const index of agentsOf(hits)) {
|
|
1667
|
+
const identity = crowd.store.identity[index];
|
|
1668
|
+
if (before.has(identity)) {
|
|
1669
|
+
now.add(identity);
|
|
1670
|
+
continue;
|
|
1671
|
+
}
|
|
1672
|
+
point[0] = crowd.store.x[index];
|
|
1673
|
+
point[2] = crowd.store.z[index];
|
|
1674
|
+
const outcome = hitOne(vehicle, index, direction, car, h);
|
|
1675
|
+
if (outcome === "push") continue;
|
|
1676
|
+
now.add(identity);
|
|
1677
|
+
if (outcome !== void 0) isIndexStale = true;
|
|
1678
|
+
}
|
|
1679
|
+
before.clear();
|
|
1680
|
+
for (const identity of now) before.add(identity);
|
|
1681
|
+
now.clear();
|
|
1682
|
+
};
|
|
1683
|
+
const beforeStep = (h) => {
|
|
1684
|
+
isIndexStale = true;
|
|
1685
|
+
for (const vehicle of vehicles) sweep(vehicle, h);
|
|
1686
|
+
};
|
|
1687
|
+
const removePhase = loop.add({ beforeStep }, "actors");
|
|
1688
|
+
return {
|
|
1689
|
+
track: (vehicle) => {
|
|
1690
|
+
vehicles.push(vehicle);
|
|
1691
|
+
return () => {
|
|
1692
|
+
const at = vehicles.indexOf(vehicle);
|
|
1693
|
+
if (at >= 0) vehicles.splice(at, 1);
|
|
1694
|
+
struck.delete(vehicle);
|
|
1695
|
+
};
|
|
1696
|
+
},
|
|
1697
|
+
get dyingVelocity() {
|
|
1698
|
+
return dying;
|
|
1699
|
+
},
|
|
1700
|
+
dispose: () => {
|
|
1701
|
+
removePhase();
|
|
1702
|
+
vehicles.length = 0;
|
|
1703
|
+
struck.clear();
|
|
1704
|
+
}
|
|
1705
|
+
};
|
|
1706
|
+
};
|
|
1275
1707
|
return __toCommonJS(index_exports);
|
|
1276
1708
|
})();
|
|
1277
1709
|
//# sourceMappingURL=index.global.js.map
|