@adatechnology/game-vehicles-driving 0.2.27 → 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.js
CHANGED
|
@@ -467,31 +467,115 @@ var bodyDamage = (energy, bumperMultiplier, zoneFactor) => energy * bumperMultip
|
|
|
467
467
|
var vehicleDamageFromImpact = (energy, frontArmor, factor = 0.04) => energy * factor * (1 - frontArmor);
|
|
468
468
|
var speedLoss = (bodyMass, vehicleMass, speed) => bodyMass / (vehicleMass + bodyMass) * speed;
|
|
469
469
|
|
|
470
|
+
// src/semi/vehicle-type.ts
|
|
471
|
+
var CYCLIST = { watts: 250, cadenceRpm: 80, gears: [1.5, 2.5, 3.5] };
|
|
472
|
+
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 };
|
|
473
|
+
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 };
|
|
474
|
+
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 };
|
|
475
|
+
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 };
|
|
476
|
+
var drivetrainOfType = (type) => {
|
|
477
|
+
if (type === "motorcycle") return MOTORCYCLE;
|
|
478
|
+
if (type === "truck") return TRUCK;
|
|
479
|
+
if (type === "tractor") return TRACTOR;
|
|
480
|
+
if (type === "tracked") return TRACKED;
|
|
481
|
+
return void 0;
|
|
482
|
+
};
|
|
483
|
+
var isTwoWheeler = (type) => type === "motorcycle" || type === "bicycle";
|
|
484
|
+
var wheelsOfAxles = ({ axles, fourWheelDrive, twoWheeler }) => {
|
|
485
|
+
const front = axles[0].x;
|
|
486
|
+
const rear = axles[axles.length - 1].x;
|
|
487
|
+
const middle = (front + rear) / 2;
|
|
488
|
+
return axles.flatMap((axle, index) => {
|
|
489
|
+
const isFront = index === 0;
|
|
490
|
+
const isRear = index === axles.length - 1;
|
|
491
|
+
const common = { x: axle.x - middle, ...isFront ? { steer: true } : {}, ...isRear ? { handbrake: true } : {} };
|
|
492
|
+
const drive = twoWheeler ? isRear : fourWheelDrive || !isFront;
|
|
493
|
+
const wheel = (z) => ({ ...common, z, ...drive ? { drive: true } : {} });
|
|
494
|
+
return axle.track > 0 ? [wheel(-axle.track / 2), wheel(axle.track / 2)] : [wheel(0)];
|
|
495
|
+
});
|
|
496
|
+
};
|
|
497
|
+
var trackStandIn = (tracks) => {
|
|
498
|
+
const side = tracks.gauge / 2;
|
|
499
|
+
return [
|
|
500
|
+
{ x: tracks.frontX, z: -side, steer: true, drive: true },
|
|
501
|
+
{ x: tracks.frontX, z: side, steer: true, drive: true },
|
|
502
|
+
{ x: tracks.rearX, z: -side, drive: true, handbrake: true },
|
|
503
|
+
{ x: tracks.rearX, z: side, drive: true, handbrake: true }
|
|
504
|
+
];
|
|
505
|
+
};
|
|
506
|
+
var tracksOfBody = (tracks) => {
|
|
507
|
+
const middle = (tracks.frontX + tracks.rearX) / 2;
|
|
508
|
+
return { gauge: tracks.gauge, rearX: tracks.rearX - middle, frontX: tracks.frontX - middle, roadWheels: tracks.roadWheels, roadRadius: tracks.radius };
|
|
509
|
+
};
|
|
510
|
+
|
|
470
511
|
// src/semi/from-stats.ts
|
|
512
|
+
var RIDER_MASS_KG = 75;
|
|
513
|
+
var MOTORCYCLE_POWER_FACTOR = 1.3;
|
|
514
|
+
var MOTORCYCLE_POWER_RATING_1 = 11800 / (132 + RIDER_MASS_KG);
|
|
515
|
+
var MOTORCYCLE_POWER_RATING_10 = 33400 / (168 + RIDER_MASS_KG);
|
|
516
|
+
var motorcyclePower = (rating) => MOTORCYCLE_POWER_FACTOR * (MOTORCYCLE_POWER_RATING_1 + (MOTORCYCLE_POWER_RATING_10 - MOTORCYCLE_POWER_RATING_1) * (rating - 1) / 9);
|
|
517
|
+
var carPower = (rating) => (0.06 + 0.015 * rating) * (defaultSemiSpec.engine.peakRpm * 2 * Math.PI / 60);
|
|
518
|
+
var peakTorqueOf = (type, rating, mass, peakRpm) => {
|
|
519
|
+
const power = type === "motorcycle" ? motorcyclePower(rating) : carPower(rating);
|
|
520
|
+
return mass * power * 60 / (2 * Math.PI * peakRpm);
|
|
521
|
+
};
|
|
522
|
+
var cyclistFor = (top, wheelRadius) => {
|
|
523
|
+
const topGear = top / (2 * Math.PI * wheelRadius) / (CYCLIST.cadenceRpm / 60);
|
|
524
|
+
return { ...CYCLIST, gears: [topGear / 1.4 ** 2, topGear / 1.4, topGear] };
|
|
525
|
+
};
|
|
526
|
+
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 }];
|
|
527
|
+
var drivenRadius = (axles, driveAll, twoWheeler) => {
|
|
528
|
+
const driven = twoWheeler ? axles.slice(-1) : driveAll ? axles : axles.slice(1);
|
|
529
|
+
return driven.reduce((sum, axle) => sum + axle.radius, 0) / driven.length;
|
|
530
|
+
};
|
|
471
531
|
function semiSpecFromStats(stats, body, base = {}) {
|
|
472
|
-
const
|
|
473
|
-
const
|
|
474
|
-
const
|
|
532
|
+
const type = body.type ?? "car";
|
|
533
|
+
const mass = body.mass + (type === "motorcycle" ? RIDER_MASS_KG : 0);
|
|
534
|
+
const twoWheeler = isTwoWheeler(type);
|
|
535
|
+
const drivetrain = drivetrainOfType(type);
|
|
536
|
+
const tracks = type === "tracked" && body.tracks ? tracksOfBody(body.tracks) : void 0;
|
|
537
|
+
const axles = axlesOfBody(body, twoWheeler);
|
|
475
538
|
const driveAll = !!body.fourWheelDrive;
|
|
476
|
-
const
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
{
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
539
|
+
const wheelRadius = tracks?.roadRadius ?? drivenRadius(axles, driveAll, twoWheeler);
|
|
540
|
+
const spec = {
|
|
541
|
+
...defaultSemiSpec,
|
|
542
|
+
...drivetrain ? { engine: { ...defaultSemiSpec.engine, ...drivetrain.engine }, gears: drivetrain.gears, finalDrive: drivetrain.finalDrive, efficiency: drivetrain.efficiency, upshiftFraction: drivetrain.upshift, downshiftFraction: drivetrain.downshift } : {},
|
|
543
|
+
...type === "bicycle" ? { finalDrive: 1, efficiency: 0.95 } : {},
|
|
544
|
+
wheelRadius,
|
|
545
|
+
centreHeight: twoWheeler ? 0.6 : Math.max(defaultSemiSpec.centreHeight, wheelRadius + 0.1),
|
|
546
|
+
...base
|
|
547
|
+
};
|
|
548
|
+
const wheels = tracks ? trackStandIn(tracks) : wheelsOfAxles({ axles, fourWheelDrive: driveAll, twoWheeler });
|
|
549
|
+
const pedal = type === "bicycle" ? cyclistFor(stats.maxSpeedKmh / 3.6, wheelRadius) : void 0;
|
|
550
|
+
const peakTorque = pedal ? pedal.watts * 60 / (2 * Math.PI * pedal.cadenceRpm) : peakTorqueOf(type, stats.acceleration, mass, spec.engine.peakRpm);
|
|
551
|
+
const engine = pedal ? { idleRpm: 40, peakRpm: pedal.cadenceRpm, redlineRpm: pedal.cadenceRpm * 1.5, peakTorque } : { ...spec.engine, peakTorque };
|
|
484
552
|
const top = stats.maxSpeedKmh / 3.6;
|
|
485
|
-
const
|
|
486
|
-
const
|
|
487
|
-
const
|
|
488
|
-
const
|
|
489
|
-
const
|
|
553
|
+
const topShare = drivetrain?.topShare ?? 0.95;
|
|
554
|
+
const lastRatio = engine.redlineRpm * topShare * 2 * Math.PI * spec.wheelRadius / (60 * top * spec.finalDrive);
|
|
555
|
+
const stack = drivetrain?.gears ?? spec.gears;
|
|
556
|
+
const step = drivetrain?.step ?? 1.25;
|
|
557
|
+
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];
|
|
558
|
+
const torqueAtTop = type === "car" ? peakTorque * 0.76 : torqueAt({ ...spec, engine }, engine.redlineRpm * topShare);
|
|
559
|
+
const topRatio = pedal ? gears[gears.length - 1] : lastRatio;
|
|
560
|
+
const thrust = pedal ? pedal.watts * spec.efficiency / top : torqueAtTop * topRatio * spec.finalDrive * spec.efficiency / spec.wheelRadius;
|
|
561
|
+
const rollingForce = spec.rolling * mass * 9.81;
|
|
490
562
|
const drag = Math.max(0.01, (thrust - rollingForce) / (top * top));
|
|
491
563
|
const carRatio = defaultSemiSpec.brakeForce / (defaultSemiSpec.mass * 9.81);
|
|
492
|
-
const heavyShare = Math.max(0, Math.min(1, (
|
|
493
|
-
const brakeForce = base.brakeForce ??
|
|
494
|
-
return {
|
|
564
|
+
const heavyShare = Math.max(0, Math.min(1, (mass - 2e3) / 8e3));
|
|
565
|
+
const brakeForce = base.brakeForce ?? mass * 9.81 * (carRatio + (0.6 - carRatio) * heavyShare);
|
|
566
|
+
return {
|
|
567
|
+
...spec,
|
|
568
|
+
mass,
|
|
569
|
+
wheels,
|
|
570
|
+
fourWheelDrive: driveAll,
|
|
571
|
+
engine,
|
|
572
|
+
gears,
|
|
573
|
+
drag,
|
|
574
|
+
brakeForce,
|
|
575
|
+
tyre: { ...spec.tyre, friction: 0.8 + 0.03 * stats.handling },
|
|
576
|
+
...pedal ? { pedal } : {},
|
|
577
|
+
...tracks ? { tracks } : {}
|
|
578
|
+
};
|
|
495
579
|
}
|
|
496
580
|
|
|
497
581
|
// src/driving-aids.ts
|
|
@@ -1074,12 +1158,354 @@ function createWindshieldFog(options = {}) {
|
|
|
1074
1158
|
}
|
|
1075
1159
|
};
|
|
1076
1160
|
}
|
|
1161
|
+
|
|
1162
|
+
// src/physics-desc.ts
|
|
1163
|
+
var GRAVITY = 9.81;
|
|
1164
|
+
var CURVE_CUT_GAP_RPM = 1;
|
|
1165
|
+
var FRONT_BRAKE_SHARE = 0.6;
|
|
1166
|
+
var HANDBRAKE_SHARE = 0.5;
|
|
1167
|
+
var SUSPENSION_MIN_SHARE = 0.4;
|
|
1168
|
+
var MAX_SAG = 0.5;
|
|
1169
|
+
var LEAN_SPRING_PER_INERTIA = 400;
|
|
1170
|
+
var LEAN_DAMPING_PER_INERTIA = 40;
|
|
1171
|
+
var LEAN_LIMIT = 0.7;
|
|
1172
|
+
var TRACK_LATERAL_FRICTION = 0.5;
|
|
1173
|
+
var WHEEL_WIDTH = 0.2;
|
|
1174
|
+
var CLUTCH_STRENGTH = 10;
|
|
1175
|
+
var CLUTCH_SLIP = 0.03;
|
|
1176
|
+
var BICYCLE_WHEEL_INERTIA = 0.1;
|
|
1177
|
+
var WHEEL_INERTIA_PER_MASS = 8e-4;
|
|
1178
|
+
var REFERENCE_RADIUS = 0.32;
|
|
1179
|
+
var MIN_WHEEL_INERTIA = 0.02;
|
|
1180
|
+
var WHEEL_DAMPING = 0.02;
|
|
1181
|
+
var ENGINE_DAMPING = 5e-3;
|
|
1182
|
+
var ENGINE_INERTIA_PER_TORQUE = 1e-3;
|
|
1183
|
+
var ENGINE_FLOOR = 0.55;
|
|
1184
|
+
var SUSPENSION_FLOOR = 0.5;
|
|
1185
|
+
var CAR_TRANSMISSION = { switchTime: 0.1, clutchReleaseTime: 0.1, switchLatency: 0.1 };
|
|
1186
|
+
var MOTORCYCLE_TRANSMISSION = { switchTime: 0.05, clutchReleaseTime: 0.07, switchLatency: 0.05 };
|
|
1187
|
+
var clutchFor = (peakTorque, maxRpm) => Math.max(CLUTCH_STRENGTH, peakTorque / (CLUTCH_SLIP * (maxRpm * 2 * Math.PI / 60)));
|
|
1188
|
+
var clamp5 = (x, low, high) => Math.max(low, Math.min(high, x));
|
|
1189
|
+
var torqueCurve = (spec) => {
|
|
1190
|
+
const { idleRpm, peakRpm, redlineRpm, peakTorque } = spec.engine;
|
|
1191
|
+
const at = (rpm) => torqueAt(spec, rpm) / peakTorque;
|
|
1192
|
+
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]];
|
|
1193
|
+
};
|
|
1194
|
+
var lateralCurve = (friction, corneringStiffness) => [[0, 0], [friction / corneringStiffness * 180 / Math.PI, friction], [90, 0.75 * friction]];
|
|
1195
|
+
var longitudinalCurve = (friction) => [[0, 0], [0.06, friction], [1, 0.8 * friction]];
|
|
1196
|
+
var isFlat = (condition, index) => (condition.tyres?.[index] ?? 1) < FLAT_BELOW || condition.rims?.[index] === true;
|
|
1197
|
+
var wheelOf = (wheel, index, context) => {
|
|
1198
|
+
const { spec, condition, options } = context;
|
|
1199
|
+
const cornerMass = spec.mass / spec.wheels.length;
|
|
1200
|
+
const wear = SUSPENSION_FLOOR + (1 - SUSPENSION_FLOOR) * clamp5(condition.suspension ?? 1, 0, 1);
|
|
1201
|
+
const free = spec.suspension.restLength;
|
|
1202
|
+
const stiffening = Math.max(1, cornerMass * GRAVITY / (MAX_SAG * free * spec.suspension.spring * wear));
|
|
1203
|
+
const spring = spec.suspension.spring * wear * stiffening;
|
|
1204
|
+
const damper = spec.suspension.damper * wear * stiffening;
|
|
1205
|
+
const sag = cornerMass * GRAVITY / spring;
|
|
1206
|
+
const flat = isFlat(condition, index);
|
|
1207
|
+
const rimRadius = spec.rimRadius ?? RIM_RADIUS_SHARE * spec.wheelRadius;
|
|
1208
|
+
const friction = flat ? RIM_FRICTION : spec.tyre.friction;
|
|
1209
|
+
const front = wheel.x >= context.frontFrom;
|
|
1210
|
+
const brakeShare = front ? FRONT_BRAKE_SHARE / context.frontCount : (1 - FRONT_BRAKE_SHARE) / context.rearCount;
|
|
1211
|
+
const brakeTorque = spec.brakeForce * clamp5(condition.brakes ?? 1, 0, 1) * spec.wheelRadius * brakeShare;
|
|
1212
|
+
return {
|
|
1213
|
+
// x is to the vehicle's left and z ahead; the semi-simulation has z to the right
|
|
1214
|
+
position: [-wheel.z, spec.wheelRadius - spec.centreHeight + (free - sag), wheel.x],
|
|
1215
|
+
radius: flat ? rimRadius : spec.wheelRadius,
|
|
1216
|
+
width: options.wheelWidth ?? WHEEL_WIDTH,
|
|
1217
|
+
suspensionMin: free * SUSPENSION_MIN_SHARE,
|
|
1218
|
+
suspensionMax: free,
|
|
1219
|
+
frequency: Math.sqrt(spring / cornerMass) / (2 * Math.PI),
|
|
1220
|
+
damping: damper / (2 * Math.sqrt(spring * cornerMass)),
|
|
1221
|
+
maxSteer: wheel.steer ? spec.maxSteer : 0,
|
|
1222
|
+
driven: spec.fourWheelDrive || wheel.drive === true,
|
|
1223
|
+
maxBrakeTorque: brakeTorque,
|
|
1224
|
+
maxHandBrakeTorque: wheel.handbrake ? spec.brakeForce * spec.wheelRadius * HANDBRAKE_SHARE / context.handbrakeCount : 0,
|
|
1225
|
+
inertia: options.wheelInertia ?? (options.pedal ? BICYCLE_WHEEL_INERTIA : Math.max(MIN_WHEEL_INERTIA, WHEEL_INERTIA_PER_MASS * spec.mass * (spec.wheelRadius / REFERENCE_RADIUS) ** 2)),
|
|
1226
|
+
angularDamping: WHEEL_DAMPING,
|
|
1227
|
+
longitudinalFriction: longitudinalCurve(friction),
|
|
1228
|
+
lateralFriction: lateralCurve(friction, spec.tyre.corneringStiffness)
|
|
1229
|
+
};
|
|
1230
|
+
};
|
|
1231
|
+
var wheelContext = (spec, options) => {
|
|
1232
|
+
const meanX = spec.wheels.reduce((sum, wheel) => sum + wheel.x, 0) / spec.wheels.length;
|
|
1233
|
+
const frontCount = spec.wheels.filter((wheel) => wheel.x >= meanX).length;
|
|
1234
|
+
return {
|
|
1235
|
+
spec,
|
|
1236
|
+
condition: options.condition ?? {},
|
|
1237
|
+
options,
|
|
1238
|
+
frontFrom: meanX,
|
|
1239
|
+
frontCount,
|
|
1240
|
+
rearCount: Math.max(1, spec.wheels.length - frontCount),
|
|
1241
|
+
handbrakeCount: Math.max(1, spec.wheels.filter((wheel) => wheel.handbrake).length)
|
|
1242
|
+
};
|
|
1243
|
+
};
|
|
1244
|
+
var inertiaOf = (spec) => {
|
|
1245
|
+
const xs = spec.wheels.map((wheel) => wheel.x);
|
|
1246
|
+
const zs = spec.wheels.map((wheel) => wheel.z);
|
|
1247
|
+
const wheelbase = Math.max(...xs) - Math.min(...xs);
|
|
1248
|
+
const track = Math.max(...zs) - Math.min(...zs);
|
|
1249
|
+
return [spec.mass * (wheelbase * wheelbase + 1) / 12, spec.yawInertia ?? spec.mass * (wheelbase * wheelbase + track * track) / 12, spec.mass * (track * track + 1) / 12];
|
|
1250
|
+
};
|
|
1251
|
+
var drivetrainOf = (spec, options) => {
|
|
1252
|
+
const pedal = options.pedal ?? spec.pedal;
|
|
1253
|
+
if (pedal) {
|
|
1254
|
+
const crankSpeed = pedal.cadenceRpm * 2 * Math.PI / 60;
|
|
1255
|
+
return {
|
|
1256
|
+
engineDamping: ENGINE_DAMPING,
|
|
1257
|
+
engineInertia: ENGINE_INERTIA_PER_TORQUE * (pedal.watts / crankSpeed),
|
|
1258
|
+
maxTorque: pedal.watts / crankSpeed * spec.efficiency,
|
|
1259
|
+
minRpm: 0,
|
|
1260
|
+
maxRpm: pedal.cadenceRpm * 1.5,
|
|
1261
|
+
// the engine turns once for every wheel turn / ratio: a gear of 2 turns the wheel twice a pedal turn
|
|
1262
|
+
gears: pedal.gears.map((ratio) => 1 / ratio),
|
|
1263
|
+
reverseGears: [],
|
|
1264
|
+
differentialRatio: 1,
|
|
1265
|
+
torqueCurve: [[0, 1], [0.67, 1], [1, 0]],
|
|
1266
|
+
transmission: { shiftUpRpm: pedal.cadenceRpm * 0.95, shiftDownRpm: pedal.cadenceRpm * 0.5, clutchStrength: clutchFor(pedal.watts / crankSpeed, pedal.cadenceRpm * 1.5) }
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
const health = clamp5(options.condition?.engine ?? 1, 0, 1);
|
|
1270
|
+
return {
|
|
1271
|
+
engineDamping: ENGINE_DAMPING,
|
|
1272
|
+
engineInertia: ENGINE_INERTIA_PER_TORQUE * spec.engine.peakTorque,
|
|
1273
|
+
maxTorque: spec.engine.peakTorque * spec.efficiency * (ENGINE_FLOOR + (1 - ENGINE_FLOOR) * health),
|
|
1274
|
+
minRpm: spec.engine.idleRpm,
|
|
1275
|
+
maxRpm: spec.engine.redlineRpm,
|
|
1276
|
+
gears: spec.gears,
|
|
1277
|
+
reverseGears: [-spec.reverseRatio],
|
|
1278
|
+
differentialRatio: spec.finalDrive,
|
|
1279
|
+
torqueCurve: torqueCurve(spec),
|
|
1280
|
+
transmission: {
|
|
1281
|
+
shiftUpRpm: spec.engine.redlineRpm * spec.upshiftFraction,
|
|
1282
|
+
shiftDownRpm: spec.engine.redlineRpm * spec.downshiftFraction,
|
|
1283
|
+
clutchStrength: clutchFor(spec.engine.peakTorque, spec.engine.redlineRpm),
|
|
1284
|
+
...spec.wheels.length === 2 ? MOTORCYCLE_TRANSMISSION : CAR_TRANSMISSION
|
|
1285
|
+
}
|
|
1286
|
+
};
|
|
1287
|
+
};
|
|
1288
|
+
var trackedOf = (spec, tracks) => {
|
|
1289
|
+
const count = Math.max(2, tracks.roadWheels);
|
|
1290
|
+
const alongX = (index) => tracks.rearX + (tracks.frontX - tracks.rearX) * index / (count - 1);
|
|
1291
|
+
const wheels = [-1, 1].flatMap((side2) => Array.from({ length: count }, (_, index) => ({ x: alongX(index), z: side2 * tracks.gauge / 2 })));
|
|
1292
|
+
const friction = spec.tyre.friction;
|
|
1293
|
+
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 });
|
|
1294
|
+
return { spec: { ...spec, wheels }, tracks: [side(0), side(count)] };
|
|
1295
|
+
};
|
|
1296
|
+
var vehicleDescFromSemi = (spec, options) => {
|
|
1297
|
+
const tracked = spec.tracks ? trackedOf(spec, spec.tracks) : void 0;
|
|
1298
|
+
const body = tracked ? tracked.spec : spec;
|
|
1299
|
+
const context = wheelContext(body, options);
|
|
1300
|
+
const inertia = inertiaOf(spec);
|
|
1301
|
+
const bike = body.wheels.length === 2;
|
|
1302
|
+
return {
|
|
1303
|
+
// 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
|
|
1304
|
+
chassis: { linearDamping: 0, angularDamping: 0, ...options.chassis, motion: "dynamic", mass: spec.mass, inertia, centreAtOrigin: true, allowSleep: true },
|
|
1305
|
+
wheels: body.wheels.map((wheel, index) => wheelOf(wheel, index, context)),
|
|
1306
|
+
...drivetrainOf(spec, options),
|
|
1307
|
+
...tracked ? { controller: "tracked", tracks: tracked.tracks } : {},
|
|
1308
|
+
...bike ? { controller: "motorcycle", motorcycle: { maxLeanAngle: LEAN_LIMIT, leanSpring: LEAN_SPRING_PER_INERTIA * inertia[2], leanDamping: LEAN_DAMPING_PER_INERTIA * inertia[2] } } : {}
|
|
1309
|
+
};
|
|
1310
|
+
};
|
|
1311
|
+
|
|
1312
|
+
// src/physics-input.ts
|
|
1313
|
+
var DEFAULT_PHYSICS_INPUT = { steerResponse: 5, steerFalloff: 0.012, steerFloor: 0.4, reverseBelow: 0.5, pullShare: 0.2 };
|
|
1314
|
+
var clamp6 = (x, low, high) => Math.max(low, Math.min(high, x));
|
|
1315
|
+
var createPhysicsInputMapper = (options = {}) => {
|
|
1316
|
+
const rules = { ...DEFAULT_PHYSICS_INPUT, ...options };
|
|
1317
|
+
let steering = 0;
|
|
1318
|
+
return {
|
|
1319
|
+
reset() {
|
|
1320
|
+
steering = 0;
|
|
1321
|
+
},
|
|
1322
|
+
map({ input, speed, dt, status }) {
|
|
1323
|
+
const pedal = clamp6(input.throttle, 0, 1) - clamp6(input.brake, 0, 1);
|
|
1324
|
+
const powerLimit = clamp6(input.powerLimit ?? 1, 0, 1);
|
|
1325
|
+
const canDrive = status?.canDrive ?? true;
|
|
1326
|
+
const brakeSoftness = 1 / Math.max(1, status?.brakeFactor ?? 1);
|
|
1327
|
+
let forward = 0;
|
|
1328
|
+
let brake = 0;
|
|
1329
|
+
if (pedal > 0) {
|
|
1330
|
+
if (speed < -rules.reverseBelow) brake = pedal * brakeSoftness;
|
|
1331
|
+
else forward = pedal * powerLimit;
|
|
1332
|
+
} else if (pedal < 0) {
|
|
1333
|
+
if (speed > rules.reverseBelow) brake = -pedal * brakeSoftness;
|
|
1334
|
+
else forward = pedal * powerLimit;
|
|
1335
|
+
}
|
|
1336
|
+
const lock = Math.max(rules.steerFloor, 1 - rules.steerFalloff * Math.abs(speed));
|
|
1337
|
+
const target = -clamp6(input.steer, -1, 1) * lock;
|
|
1338
|
+
steering += (target - steering) * Math.min(1, dt * rules.steerResponse);
|
|
1339
|
+
const pull = clamp6(status?.steerPull ?? 0, -1, 1) * rules.pullShare;
|
|
1340
|
+
return { forward: canDrive ? forward : 0, right: clamp6(steering + pull, -1, 1), brake, handBrake: input.handbrake ? 1 : 0 };
|
|
1341
|
+
}
|
|
1342
|
+
};
|
|
1343
|
+
};
|
|
1344
|
+
|
|
1345
|
+
// src/physics-resistance.ts
|
|
1346
|
+
var GRAVITY2 = 9.81;
|
|
1347
|
+
var ROLLING_FADE_SPEED = 0.5;
|
|
1348
|
+
var resistanceImpulse = (spec, velocity, dt) => {
|
|
1349
|
+
const speed = Math.hypot(velocity[0], velocity[2]);
|
|
1350
|
+
if (speed === 0) return [0, 0, 0];
|
|
1351
|
+
const rolling = spec.rolling * spec.mass * GRAVITY2 * Math.min(1, speed / ROLLING_FADE_SPEED);
|
|
1352
|
+
const along = -(spec.drag * speed * speed + rolling) * dt;
|
|
1353
|
+
return [velocity[0] / speed * along, 0, velocity[2] / speed * along];
|
|
1354
|
+
};
|
|
1355
|
+
|
|
1356
|
+
// src/physics-run-over.ts
|
|
1357
|
+
var byIndexDescending = (a, b) => b - a;
|
|
1358
|
+
var MIN_SPEED = 1;
|
|
1359
|
+
var REFERENCE_MASS = 70;
|
|
1360
|
+
var PUSH_ASIDE_SPEED = 1;
|
|
1361
|
+
var createRunOver = (loop, options) => {
|
|
1362
|
+
const { crowd, agents } = options;
|
|
1363
|
+
const rules = { ...defaultArcadeSpec.runOver, ...options.rules };
|
|
1364
|
+
const world = loop.world;
|
|
1365
|
+
const vehicles = [];
|
|
1366
|
+
const struck = /* @__PURE__ */ new Map();
|
|
1367
|
+
const hits = [];
|
|
1368
|
+
const pose = new Float64Array(7);
|
|
1369
|
+
const velocity = new Float64Array(6);
|
|
1370
|
+
const box = { kind: "box", half: [0, 0, 0] };
|
|
1371
|
+
const centre = [0, 0, 0];
|
|
1372
|
+
const rotation = [0, 0, 0, 1];
|
|
1373
|
+
const impulse = [0, 0, 0];
|
|
1374
|
+
const point = [0, 0, 0];
|
|
1375
|
+
const direction = [0, 0];
|
|
1376
|
+
const car = { speed: 0, mass: 0 };
|
|
1377
|
+
const found = [];
|
|
1378
|
+
const now = /* @__PURE__ */ new Set();
|
|
1379
|
+
const indexOfBody = /* @__PURE__ */ new Map();
|
|
1380
|
+
let isIndexStale = true;
|
|
1381
|
+
let dying;
|
|
1382
|
+
let diedLast = false;
|
|
1383
|
+
const agentsOf = (bodies) => {
|
|
1384
|
+
if (isIndexStale) {
|
|
1385
|
+
indexOfBody.clear();
|
|
1386
|
+
for (let index = 0; index < agents.count; index++) {
|
|
1387
|
+
const body = agents.bodyOf(index);
|
|
1388
|
+
if (body !== void 0) indexOfBody.set(body, index);
|
|
1389
|
+
}
|
|
1390
|
+
isIndexStale = false;
|
|
1391
|
+
}
|
|
1392
|
+
found.length = 0;
|
|
1393
|
+
for (const body of bodies) {
|
|
1394
|
+
const index = indexOfBody.get(body);
|
|
1395
|
+
if (index !== void 0) found.push(index);
|
|
1396
|
+
}
|
|
1397
|
+
return found.sort(byIndexDescending);
|
|
1398
|
+
};
|
|
1399
|
+
const strike = (index, outcome, direction2, hit, h) => {
|
|
1400
|
+
const { store } = crowd;
|
|
1401
|
+
if (outcome === "push") {
|
|
1402
|
+
store.x[index] += -direction2[1] * PUSH_ASIDE_SPEED * h;
|
|
1403
|
+
store.z[index] += direction2[0] * PUSH_ASIDE_SPEED * h;
|
|
1404
|
+
return hit.carMomentum * (1 - Math.exp(-rules.holdDrag * h));
|
|
1405
|
+
}
|
|
1406
|
+
dying = [direction2[0] * hit.common, 0, direction2[1] * hit.common];
|
|
1407
|
+
if (outcome === "kill") crowd.kill(index);
|
|
1408
|
+
else if (!crowd.damage(index, { region: 1, amount: hit.reduced / REFERENCE_MASS * hit.closing * hit.closing * rules.knockDamage, distance: 0 }).died) {
|
|
1409
|
+
dying = void 0;
|
|
1410
|
+
crowd.knockDown(index, { x: direction2[0] * hit.common, z: direction2[1] * hit.common });
|
|
1411
|
+
}
|
|
1412
|
+
const died = dying !== void 0;
|
|
1413
|
+
dying = void 0;
|
|
1414
|
+
diedLast = died;
|
|
1415
|
+
return hit.reduced * hit.closing;
|
|
1416
|
+
};
|
|
1417
|
+
const hitOne = (vehicle, index, direction2, car2, h) => {
|
|
1418
|
+
const { store } = crowd;
|
|
1419
|
+
const mass = store.mass[index] > 0 ? store.mass[index] : REFERENCE_MASS;
|
|
1420
|
+
const closing = car2.speed - (store.vx[index] * direction2[0] + store.vz[index] * direction2[1]);
|
|
1421
|
+
if (closing <= 0) return void 0;
|
|
1422
|
+
const reduced = mass * car2.mass / (mass + car2.mass);
|
|
1423
|
+
const outcome = runOver(closing, rules);
|
|
1424
|
+
diedLast = false;
|
|
1425
|
+
const lost = strike(index, outcome, direction2, { reduced, closing, common: car2.speed - mass / (mass + car2.mass) * closing, carMomentum: car2.mass * car2.speed }, h);
|
|
1426
|
+
car2.speed -= lost / car2.mass;
|
|
1427
|
+
impulse[0] = -direction2[0] * lost;
|
|
1428
|
+
impulse[2] = -direction2[1] * lost;
|
|
1429
|
+
world.addImpulse(vehicle.body, impulse, point);
|
|
1430
|
+
options.onHit?.({ vehicle: vehicle.body, agent: index, outcome, speed: closing, energy: 0.5 * reduced * closing * closing, vehicleDamage: diedLast ? rules.killVehicleDamage : 0 });
|
|
1431
|
+
return outcome;
|
|
1432
|
+
};
|
|
1433
|
+
const sweep = (vehicle, h) => {
|
|
1434
|
+
world.readVelocity(vehicle.body, velocity, 0);
|
|
1435
|
+
const speed = Math.hypot(velocity[0], velocity[2]);
|
|
1436
|
+
if (speed < MIN_SPEED) return;
|
|
1437
|
+
world.readPose(vehicle.body, pose, 0);
|
|
1438
|
+
direction[0] = velocity[0] / speed;
|
|
1439
|
+
direction[1] = velocity[2] / speed;
|
|
1440
|
+
const reach = speed * h / 2;
|
|
1441
|
+
box.half[0] = vehicle.half[0] + rules.margin;
|
|
1442
|
+
box.half[1] = vehicle.half[1];
|
|
1443
|
+
box.half[2] = vehicle.half[2] + rules.margin + reach;
|
|
1444
|
+
centre[0] = pose[0] + direction[0] * reach;
|
|
1445
|
+
centre[1] = pose[1];
|
|
1446
|
+
centre[2] = pose[2] + direction[1] * reach;
|
|
1447
|
+
for (let k = 0; k < 4; k++) rotation[k] = pose[3 + k];
|
|
1448
|
+
point[1] = pose[1];
|
|
1449
|
+
if (world.overlapShape(box, centre, rotation, hits, { layers: ["agent"] }) === 0) {
|
|
1450
|
+
struck.delete(vehicle);
|
|
1451
|
+
return;
|
|
1452
|
+
}
|
|
1453
|
+
car.speed = speed;
|
|
1454
|
+
car.mass = world.massOf(vehicle.body);
|
|
1455
|
+
let before = struck.get(vehicle);
|
|
1456
|
+
if (!before) {
|
|
1457
|
+
before = /* @__PURE__ */ new Set();
|
|
1458
|
+
struck.set(vehicle, before);
|
|
1459
|
+
}
|
|
1460
|
+
for (const index of agentsOf(hits)) {
|
|
1461
|
+
const identity = crowd.store.identity[index];
|
|
1462
|
+
if (before.has(identity)) {
|
|
1463
|
+
now.add(identity);
|
|
1464
|
+
continue;
|
|
1465
|
+
}
|
|
1466
|
+
point[0] = crowd.store.x[index];
|
|
1467
|
+
point[2] = crowd.store.z[index];
|
|
1468
|
+
const outcome = hitOne(vehicle, index, direction, car, h);
|
|
1469
|
+
if (outcome === "push") continue;
|
|
1470
|
+
now.add(identity);
|
|
1471
|
+
if (outcome !== void 0) isIndexStale = true;
|
|
1472
|
+
}
|
|
1473
|
+
before.clear();
|
|
1474
|
+
for (const identity of now) before.add(identity);
|
|
1475
|
+
now.clear();
|
|
1476
|
+
};
|
|
1477
|
+
const beforeStep = (h) => {
|
|
1478
|
+
isIndexStale = true;
|
|
1479
|
+
for (const vehicle of vehicles) sweep(vehicle, h);
|
|
1480
|
+
};
|
|
1481
|
+
const removePhase = loop.add({ beforeStep }, "actors");
|
|
1482
|
+
return {
|
|
1483
|
+
track: (vehicle) => {
|
|
1484
|
+
vehicles.push(vehicle);
|
|
1485
|
+
return () => {
|
|
1486
|
+
const at = vehicles.indexOf(vehicle);
|
|
1487
|
+
if (at >= 0) vehicles.splice(at, 1);
|
|
1488
|
+
struck.delete(vehicle);
|
|
1489
|
+
};
|
|
1490
|
+
},
|
|
1491
|
+
get dyingVelocity() {
|
|
1492
|
+
return dying;
|
|
1493
|
+
},
|
|
1494
|
+
dispose: () => {
|
|
1495
|
+
removePhase();
|
|
1496
|
+
vehicles.length = 0;
|
|
1497
|
+
struck.clear();
|
|
1498
|
+
}
|
|
1499
|
+
};
|
|
1500
|
+
};
|
|
1077
1501
|
export {
|
|
1502
|
+
CYCLIST,
|
|
1078
1503
|
DEFAULT_CONTROL_KEYS,
|
|
1079
1504
|
DEFAULT_CRUISE,
|
|
1080
1505
|
DEFAULT_EJECTION,
|
|
1081
1506
|
DEFAULT_LOADS,
|
|
1082
1507
|
DEFAULT_PARKING_SENSOR,
|
|
1508
|
+
DEFAULT_PHYSICS_INPUT,
|
|
1083
1509
|
DEFAULT_TRACTION,
|
|
1084
1510
|
DRIVE_PROFILES,
|
|
1085
1511
|
GLOVEBOX,
|
|
@@ -1087,6 +1513,8 @@ export {
|
|
|
1087
1513
|
bumperDistance,
|
|
1088
1514
|
conditionFromDamage,
|
|
1089
1515
|
createArcadeDriver,
|
|
1516
|
+
createPhysicsInputMapper,
|
|
1517
|
+
createRunOver,
|
|
1090
1518
|
createSemiSimDriver,
|
|
1091
1519
|
createSurfaces,
|
|
1092
1520
|
createVehicleControls,
|
|
@@ -1102,6 +1530,7 @@ export {
|
|
|
1102
1530
|
fuelRate,
|
|
1103
1531
|
impactEnergy,
|
|
1104
1532
|
parkingBeepInterval,
|
|
1533
|
+
resistanceImpulse,
|
|
1105
1534
|
runOver,
|
|
1106
1535
|
semiSpecFromStats,
|
|
1107
1536
|
speedLoss,
|
|
@@ -1109,6 +1538,7 @@ export {
|
|
|
1109
1538
|
tractionLimit,
|
|
1110
1539
|
trunkFor,
|
|
1111
1540
|
vehicleCorners,
|
|
1112
|
-
vehicleDamageFromImpact
|
|
1541
|
+
vehicleDamageFromImpact,
|
|
1542
|
+
vehicleDescFromSemi
|
|
1113
1543
|
};
|
|
1114
1544
|
//# sourceMappingURL=index.js.map
|