@aura3d/physics 1.3.3 → 1.4.1
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/package.json +34 -2
- package/src/CharacterController.ts +0 -304
- package/src/ClothFixtures.ts +0 -359
- package/src/Collider.ts +0 -67
- package/src/CollisionEvents.ts +0 -70
- package/src/CollisionVolumes.ts +0 -303
- package/src/Constraint.ts +0 -120
- package/src/Constraints.ts +0 -1
- package/src/Crowd.ts +0 -283
- package/src/ECSPhysicsBridge.ts +0 -53
- package/src/FireSmokeFixtures.ts +0 -382
- package/src/FluidFixtures.ts +0 -301
- package/src/FractureFixtures.ts +0 -277
- package/src/HitboxWorld.ts +0 -1179
- package/src/KinematicBody.ts +0 -597
- package/src/KinematicWorld.ts +0 -316
- package/src/Navigation.ts +0 -321
- package/src/PhysicsDebugDraw.ts +0 -71
- package/src/PhysicsSandboxFixtures.ts +0 -394
- package/src/PhysicsStepper.ts +0 -47
- package/src/PhysicsWorld.ts +0 -986
- package/src/PlatformerFixtures.ts +0 -205
- package/src/Raycast.ts +0 -345
- package/src/RigidBody.ts +0 -311
- package/src/ScenePhysicsBridge.ts +0 -207
- package/src/Shape.ts +0 -195
- package/src/SoftBodyFixtures.ts +0 -363
- package/src/Steering.ts +0 -531
- package/src/VehicleDynamics.ts +0 -524
- package/src/index.ts +0 -28
package/src/VehicleDynamics.ts
DELETED
|
@@ -1,524 +0,0 @@
|
|
|
1
|
-
export interface ArcadeVehicleDynamicsInput {
|
|
2
|
-
readonly elapsedSeconds: number;
|
|
3
|
-
readonly throttle?: number;
|
|
4
|
-
readonly brake?: number;
|
|
5
|
-
readonly steer?: number;
|
|
6
|
-
readonly handbrake?: boolean;
|
|
7
|
-
readonly nitro?: boolean;
|
|
8
|
-
readonly maxSpeedKph?: number;
|
|
9
|
-
readonly acceleration?: number;
|
|
10
|
-
readonly brakeForce?: number;
|
|
11
|
-
readonly gripFactor?: number;
|
|
12
|
-
readonly driftFactor?: number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface ArcadeVehicleDynamicsSample {
|
|
16
|
-
readonly speedKph: number;
|
|
17
|
-
readonly rpm: number;
|
|
18
|
-
readonly nitro: number;
|
|
19
|
-
readonly steerAngle: number;
|
|
20
|
-
readonly driftSlip: number;
|
|
21
|
-
readonly grip: number;
|
|
22
|
-
readonly wheelSpin: number;
|
|
23
|
-
readonly suspensionCompression: readonly [number, number, number, number];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type PacejkaTirePreset = "street" | "sport" | "racing" | "offroad";
|
|
27
|
-
export type RacingAiDifficulty = "easy" | "medium" | "hard";
|
|
28
|
-
export type DrivetrainDifferential = "open" | "limited-slip" | "locked" | "electronic";
|
|
29
|
-
|
|
30
|
-
export interface PacejkaTireCoefficients {
|
|
31
|
-
readonly b: number;
|
|
32
|
-
readonly c: number;
|
|
33
|
-
readonly d: number;
|
|
34
|
-
readonly e: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface PacejkaTireForceInput {
|
|
38
|
-
readonly longitudinalVelocity: number;
|
|
39
|
-
readonly lateralVelocity?: number;
|
|
40
|
-
readonly angularVelocity: number;
|
|
41
|
-
readonly normalForce: number;
|
|
42
|
-
readonly steeringAngle?: number;
|
|
43
|
-
readonly camberAngle?: number;
|
|
44
|
-
readonly radius?: number;
|
|
45
|
-
readonly width?: number;
|
|
46
|
-
readonly maxLoad?: number;
|
|
47
|
-
readonly loadSensitivity?: number;
|
|
48
|
-
readonly rollingResistance?: number;
|
|
49
|
-
readonly longitudinal?: PacejkaTireCoefficients | PacejkaTirePreset;
|
|
50
|
-
readonly lateral?: PacejkaTireCoefficients | PacejkaTirePreset;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface PacejkaTireForceSample {
|
|
54
|
-
readonly longitudinalForce: number;
|
|
55
|
-
readonly lateralForce: number;
|
|
56
|
-
readonly combinedForce: number;
|
|
57
|
-
readonly slipRatio: number;
|
|
58
|
-
readonly slipAngle: number;
|
|
59
|
-
readonly aligningTorque: number;
|
|
60
|
-
readonly optimalSlipRatio: number;
|
|
61
|
-
readonly optimalSlipAngle: number;
|
|
62
|
-
readonly preset: {
|
|
63
|
-
readonly longitudinal: PacejkaTirePreset | "custom";
|
|
64
|
-
readonly lateral: PacejkaTirePreset | "custom";
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface RacingAiDriverInput {
|
|
69
|
-
readonly difficulty?: RacingAiDifficulty;
|
|
70
|
-
readonly elapsedSeconds: number;
|
|
71
|
-
readonly progress: number;
|
|
72
|
-
readonly speedKph: number;
|
|
73
|
-
readonly targetSpeedKph?: number;
|
|
74
|
-
readonly trackCurvature?: number;
|
|
75
|
-
readonly opponentDistance?: number;
|
|
76
|
-
readonly opponentAhead?: boolean;
|
|
77
|
-
readonly playerGapSeconds?: number;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export interface RacingAiDriverSample {
|
|
81
|
-
readonly difficulty: RacingAiDifficulty;
|
|
82
|
-
readonly throttle: number;
|
|
83
|
-
readonly brake: number;
|
|
84
|
-
readonly steer: number;
|
|
85
|
-
readonly targetSpeedKph: number;
|
|
86
|
-
readonly lookaheadDistance: number;
|
|
87
|
-
readonly reactionTime: number;
|
|
88
|
-
readonly aggressiveness: number;
|
|
89
|
-
readonly overtaking: boolean;
|
|
90
|
-
readonly overtakeOffset: number;
|
|
91
|
-
readonly rubberbandBoost: number;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export interface VehicleDrivetrainInput {
|
|
95
|
-
readonly speedKph: number;
|
|
96
|
-
readonly throttle?: number;
|
|
97
|
-
readonly clutch?: number;
|
|
98
|
-
readonly gearRatios?: readonly number[];
|
|
99
|
-
readonly finalDriveRatio?: number;
|
|
100
|
-
readonly idleRpm?: number;
|
|
101
|
-
readonly maxRpm?: number;
|
|
102
|
-
readonly peakTorque?: number;
|
|
103
|
-
readonly peakTorqueRpm?: number;
|
|
104
|
-
readonly upshiftRpm?: number;
|
|
105
|
-
readonly downshiftRpm?: number;
|
|
106
|
-
readonly differential?: DrivetrainDifferential;
|
|
107
|
-
readonly frontRearSplit?: number;
|
|
108
|
-
readonly lockingFactor?: number;
|
|
109
|
-
readonly dragCoefficient?: number;
|
|
110
|
-
readonly frontalArea?: number;
|
|
111
|
-
readonly downforceCoefficient?: number;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export interface VehicleDrivetrainSample {
|
|
115
|
-
readonly gear: number;
|
|
116
|
-
readonly gearRatio: number;
|
|
117
|
-
readonly engineRpm: number;
|
|
118
|
-
readonly engineTorque: number;
|
|
119
|
-
readonly wheelTorque: number;
|
|
120
|
-
readonly frontTorque: number;
|
|
121
|
-
readonly rearTorque: number;
|
|
122
|
-
readonly dragForce: number;
|
|
123
|
-
readonly downforce: number;
|
|
124
|
-
readonly shiftState: "hold" | "upshift" | "downshift";
|
|
125
|
-
readonly differential: DrivetrainDifferential;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export interface VehicleEffectEmitterInput {
|
|
129
|
-
readonly speedKph: number;
|
|
130
|
-
readonly throttle?: number;
|
|
131
|
-
readonly steer?: number;
|
|
132
|
-
readonly handbrake?: boolean;
|
|
133
|
-
readonly nitroActive?: boolean;
|
|
134
|
-
readonly wheelOnGround?: readonly [boolean, boolean, boolean, boolean];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export interface VehicleEffectEmitterSample {
|
|
138
|
-
readonly tireSmokeRates: readonly [number, number, number, number];
|
|
139
|
-
readonly totalTireSmokeRate: number;
|
|
140
|
-
readonly nitroFlameRate: number;
|
|
141
|
-
readonly visibleEffectEmitters: number;
|
|
142
|
-
readonly smokeReason: "none" | "handbrake" | "launch-wheelspin" | "high-speed-steer";
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export interface VehicleDamageInput {
|
|
146
|
-
readonly health?: number;
|
|
147
|
-
readonly impactSpeedKph?: number;
|
|
148
|
-
readonly collisionSeverity?: number;
|
|
149
|
-
readonly repair?: number;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export interface VehicleDamageSample {
|
|
153
|
-
readonly health: number;
|
|
154
|
-
readonly damage: number;
|
|
155
|
-
readonly impactDamage: number;
|
|
156
|
-
readonly visualDamageLevel: "none" | "scratched" | "dented" | "critical";
|
|
157
|
-
readonly disabled: boolean;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function sampleArcadeVehicleDynamics(input: ArcadeVehicleDynamicsInput): ArcadeVehicleDynamicsSample {
|
|
161
|
-
const elapsed = finiteNonNegative(input.elapsedSeconds, "elapsedSeconds");
|
|
162
|
-
const throttle = clamp(input.throttle ?? 1, -1, 1);
|
|
163
|
-
const brake = clamp(input.brake ?? 0, 0, 1);
|
|
164
|
-
const steer = clamp(input.steer ?? 0, -1, 1);
|
|
165
|
-
const maxSpeedKph = positive(input.maxSpeedKph ?? 220, "maxSpeedKph");
|
|
166
|
-
const acceleration = positive(input.acceleration ?? 9.5, "acceleration");
|
|
167
|
-
const brakeForce = positive(input.brakeForce ?? 14, "brakeForce");
|
|
168
|
-
const gripFactor = clamp(input.gripFactor ?? 0.82, 0, 1);
|
|
169
|
-
const driftFactor = clamp(input.driftFactor ?? 0.36, 0, 1);
|
|
170
|
-
const maxSpeedMps = maxSpeedKph / 3.6;
|
|
171
|
-
const accelerationCurve = 1 - Math.exp(-elapsed * acceleration / Math.max(1, maxSpeedMps));
|
|
172
|
-
const braking = Math.min(maxSpeedMps, brakeForce * brake * Math.min(elapsed, 4));
|
|
173
|
-
const nitroAvailable = Math.max(0, 100 - elapsed * 10.5);
|
|
174
|
-
const nitroBoost = input.nitro === true && nitroAvailable > 0 ? 1.18 : 1;
|
|
175
|
-
const signedSpeedMps = clamp((maxSpeedMps * accelerationCurve * throttle * nitroBoost) - braking, -maxSpeedMps * 0.32, maxSpeedMps * nitroBoost);
|
|
176
|
-
const speedRatio = Math.min(1, Math.abs(signedSpeedMps) / maxSpeedMps);
|
|
177
|
-
const highSpeedSteerLimit = 1 - speedRatio * 0.55;
|
|
178
|
-
const steerAngle = steer * 0.62 * highSpeedSteerLimit;
|
|
179
|
-
const handbrakeSlip = input.handbrake === true ? driftFactor * (0.45 + speedRatio * 0.55) : 0;
|
|
180
|
-
const steeringSlip = Math.abs(steer) * speedRatio * (1 - gripFactor) * 0.75;
|
|
181
|
-
const driftSlip = clamp(handbrakeSlip + steeringSlip, 0, 1);
|
|
182
|
-
const grip = clamp(gripFactor * (1 - driftSlip * 0.62), 0, 1);
|
|
183
|
-
const rpm = 1000 + speedRatio * 6100 + (input.nitro === true ? 650 : 0) + driftSlip * 450;
|
|
184
|
-
const wheelSpin = elapsed * Math.abs(signedSpeedMps) * (1 + driftSlip * 1.8);
|
|
185
|
-
const suspensionCompression = suspensionFor(steer, speedRatio, driftSlip);
|
|
186
|
-
return {
|
|
187
|
-
speedKph: Number((signedSpeedMps * 3.6).toFixed(3)),
|
|
188
|
-
rpm: Number(rpm.toFixed(3)),
|
|
189
|
-
nitro: Number(nitroAvailable.toFixed(3)),
|
|
190
|
-
steerAngle: Number(steerAngle.toFixed(5)),
|
|
191
|
-
driftSlip: Number(driftSlip.toFixed(5)),
|
|
192
|
-
grip: Number(grip.toFixed(5)),
|
|
193
|
-
wheelSpin: Number(wheelSpin.toFixed(5)),
|
|
194
|
-
suspensionCompression
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
export function sampleVehicleDamage(input: VehicleDamageInput): VehicleDamageSample {
|
|
199
|
-
const health = clamp(finiteNonNegative(input.health ?? 100, "health"), 0, 100);
|
|
200
|
-
const impactSpeedKph = finiteNonNegative(input.impactSpeedKph ?? 0, "impactSpeedKph");
|
|
201
|
-
const collisionSeverity = clamp(input.collisionSeverity ?? 0, 0, 1);
|
|
202
|
-
const repair = clamp(input.repair ?? 0, 0, 100);
|
|
203
|
-
const impactDamage = Math.max(0, (impactSpeedKph - 18) * 0.34 * collisionSeverity);
|
|
204
|
-
const nextHealth = clamp(health - impactDamage + repair, 0, 100);
|
|
205
|
-
const damage = 100 - nextHealth;
|
|
206
|
-
return {
|
|
207
|
-
health: Number(nextHealth.toFixed(3)),
|
|
208
|
-
damage: Number(damage.toFixed(3)),
|
|
209
|
-
impactDamage: Number(impactDamage.toFixed(3)),
|
|
210
|
-
visualDamageLevel: damage >= 72 ? "critical" : damage >= 42 ? "dented" : damage > 0 ? "scratched" : "none",
|
|
211
|
-
disabled: nextHealth <= 0
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export function sampleVehicleEffectEmitters(input: VehicleEffectEmitterInput): VehicleEffectEmitterSample {
|
|
216
|
-
const speedKph = finiteNonNegative(input.speedKph, "speedKph");
|
|
217
|
-
const throttle = clamp(input.throttle ?? 0, -1, 1);
|
|
218
|
-
const steer = clamp(input.steer ?? 0, -1, 1);
|
|
219
|
-
const wheelOnGround = input.wheelOnGround ?? [true, true, true, true];
|
|
220
|
-
const smokeReason = input.handbrake === true
|
|
221
|
-
? "handbrake"
|
|
222
|
-
: Math.abs(throttle) > 0.7 && speedKph < 30
|
|
223
|
-
? "launch-wheelspin"
|
|
224
|
-
: Math.abs(steer) > 0.7 && speedKph > 50
|
|
225
|
-
? "high-speed-steer"
|
|
226
|
-
: "none";
|
|
227
|
-
const baseSmokeRate = smokeReason === "none" ? 0 : smokeReason === "handbrake" ? 64 : smokeReason === "launch-wheelspin" ? 46 : 38;
|
|
228
|
-
const tireSmokeRates = wheelOnGround.map((onGround, index) => {
|
|
229
|
-
const rearBias = index >= 2 ? 1.18 : 0.86;
|
|
230
|
-
return onGround ? Number((baseSmokeRate * rearBias).toFixed(3)) : 0;
|
|
231
|
-
}) as [number, number, number, number];
|
|
232
|
-
const nitroFlameRate = input.nitroActive === true ? Number((160 + Math.abs(throttle) * 80).toFixed(3)) : 0;
|
|
233
|
-
const totalTireSmokeRate = Number(tireSmokeRates.reduce((sum, rate) => sum + rate, 0).toFixed(3));
|
|
234
|
-
return {
|
|
235
|
-
tireSmokeRates,
|
|
236
|
-
totalTireSmokeRate,
|
|
237
|
-
nitroFlameRate,
|
|
238
|
-
visibleEffectEmitters: tireSmokeRates.filter((rate) => rate > 0).length + (nitroFlameRate > 0 ? 1 : 0),
|
|
239
|
-
smokeReason
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
export function sampleVehicleDrivetrain(input: VehicleDrivetrainInput): VehicleDrivetrainSample {
|
|
244
|
-
const speedKph = finiteNonNegative(input.speedKph, "speedKph");
|
|
245
|
-
const throttle = clamp(input.throttle ?? 1, 0, 1);
|
|
246
|
-
const clutch = clamp(input.clutch ?? 1, 0, 1);
|
|
247
|
-
const gearRatios = validateGearRatios(input.gearRatios ?? [3.42, 2.21, 1.52, 1.14, 0.89, 0.71]);
|
|
248
|
-
const finalDriveRatio = positive(input.finalDriveRatio ?? 3.55, "finalDriveRatio");
|
|
249
|
-
const idleRpm = positive(input.idleRpm ?? 950, "idleRpm");
|
|
250
|
-
const maxRpm = Math.max(idleRpm + 1, positive(input.maxRpm ?? 7200, "maxRpm"));
|
|
251
|
-
const peakTorque = positive(input.peakTorque ?? 420, "peakTorque");
|
|
252
|
-
const peakTorqueRpm = clamp(positive(input.peakTorqueRpm ?? 4200, "peakTorqueRpm"), idleRpm, maxRpm);
|
|
253
|
-
const upshiftRpm = clamp(input.upshiftRpm ?? maxRpm * 0.86, idleRpm, maxRpm);
|
|
254
|
-
const downshiftRpm = clamp(input.downshiftRpm ?? peakTorqueRpm * 0.55, idleRpm, upshiftRpm);
|
|
255
|
-
const differential = input.differential ?? "limited-slip";
|
|
256
|
-
const frontRearSplit = clamp(input.frontRearSplit ?? 0.42, 0, 1);
|
|
257
|
-
const lockingFactor = clamp(input.lockingFactor ?? 0.35, 0, 1);
|
|
258
|
-
const dragCoefficient = positive(input.dragCoefficient ?? 0.32, "dragCoefficient");
|
|
259
|
-
const frontalArea = positive(input.frontalArea ?? 2.1, "frontalArea");
|
|
260
|
-
const downforceCoefficient = finiteNonNegative(input.downforceCoefficient ?? 0.58, "downforceCoefficient");
|
|
261
|
-
const speedMps = speedKph / 3.6;
|
|
262
|
-
const wheelRadius = 0.34;
|
|
263
|
-
const wheelRpm = speedMps <= 0 ? 0 : (speedMps / (Math.PI * 2 * wheelRadius)) * 60;
|
|
264
|
-
const selected = selectAutomaticGear(gearRatios, wheelRpm, finalDriveRatio, idleRpm, maxRpm, upshiftRpm, downshiftRpm);
|
|
265
|
-
const engineRpm = clamp(Math.max(idleRpm, wheelRpm * selected.ratio * finalDriveRatio), idleRpm, maxRpm);
|
|
266
|
-
const engineTorque = engineTorqueAt(engineRpm, peakTorque, peakTorqueRpm, idleRpm, maxRpm) * throttle;
|
|
267
|
-
const differentialEfficiency = differential === "open"
|
|
268
|
-
? 0.92
|
|
269
|
-
: differential === "limited-slip"
|
|
270
|
-
? 0.95 + lockingFactor * 0.025
|
|
271
|
-
: differential === "locked"
|
|
272
|
-
? 0.96 + lockingFactor * 0.02
|
|
273
|
-
: 0.94 + lockingFactor * 0.04;
|
|
274
|
-
const wheelTorque = engineTorque * selected.ratio * finalDriveRatio * clutch * differentialEfficiency;
|
|
275
|
-
const torqueBias = differential === "open"
|
|
276
|
-
? 0
|
|
277
|
-
: differential === "limited-slip"
|
|
278
|
-
? lockingFactor * 0.04
|
|
279
|
-
: differential === "locked"
|
|
280
|
-
? lockingFactor * 0.02
|
|
281
|
-
: lockingFactor * 0.06;
|
|
282
|
-
const frontSplit = clamp(frontRearSplit - torqueBias, 0, 1);
|
|
283
|
-
const dragForce = 0.5 * 1.225 * dragCoefficient * frontalArea * speedMps * speedMps;
|
|
284
|
-
const downforce = 0.5 * 1.225 * downforceCoefficient * frontalArea * speedMps * speedMps;
|
|
285
|
-
return {
|
|
286
|
-
gear: selected.gear,
|
|
287
|
-
gearRatio: Number(selected.ratio.toFixed(4)),
|
|
288
|
-
engineRpm: Number(engineRpm.toFixed(3)),
|
|
289
|
-
engineTorque: Number(engineTorque.toFixed(3)),
|
|
290
|
-
wheelTorque: Number(wheelTorque.toFixed(3)),
|
|
291
|
-
frontTorque: Number((wheelTorque * frontSplit).toFixed(3)),
|
|
292
|
-
rearTorque: Number((wheelTorque * (1 - frontSplit)).toFixed(3)),
|
|
293
|
-
dragForce: Number(dragForce.toFixed(3)),
|
|
294
|
-
downforce: Number(downforce.toFixed(3)),
|
|
295
|
-
shiftState: selected.shiftState,
|
|
296
|
-
differential
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
export function sampleRacingAiDriver(input: RacingAiDriverInput): RacingAiDriverSample {
|
|
301
|
-
const elapsed = finiteNonNegative(input.elapsedSeconds, "elapsedSeconds");
|
|
302
|
-
const progress = clamp(finite(input.progress, "progress"), 0, 1);
|
|
303
|
-
const speedKph = finiteNonNegative(input.speedKph, "speedKph");
|
|
304
|
-
const targetSpeedKph = positive(input.targetSpeedKph ?? 220, "targetSpeedKph");
|
|
305
|
-
const curvature = clamp(input.trackCurvature ?? 0, -1, 1);
|
|
306
|
-
const opponentDistance = input.opponentDistance === undefined ? Number.POSITIVE_INFINITY : finiteNonNegative(input.opponentDistance, "opponentDistance");
|
|
307
|
-
const playerGapSeconds = finite(input.playerGapSeconds ?? 0, "playerGapSeconds");
|
|
308
|
-
const difficulty = input.difficulty ?? "medium";
|
|
309
|
-
const settings = racingAiSettings(difficulty);
|
|
310
|
-
const rubberbandBoost = clamp((-playerGapSeconds) * settings.rubberbandStrength * 0.08, -0.08, 0.18);
|
|
311
|
-
const desiredSpeed = targetSpeedKph * settings.targetSpeedFactor * (1 - Math.abs(curvature) * 0.24) * (1 + rubberbandBoost);
|
|
312
|
-
const speedError = desiredSpeed - speedKph;
|
|
313
|
-
const overtaking = input.opponentAhead === true && opponentDistance < 22 && settings.aggressiveness >= 0.55;
|
|
314
|
-
const deterministicSide = Math.sin((progress + elapsed * 0.13) * Math.PI * 6) >= 0 ? 1 : -1;
|
|
315
|
-
const overtakeOffset = overtaking ? deterministicSide * (0.28 + settings.aggressiveness * 0.18) : 0;
|
|
316
|
-
const racingLineSteer = Math.sin(progress * Math.PI * 2) * 0.18 + curvature * 0.62;
|
|
317
|
-
const rawSteer = racingLineSteer + overtakeOffset;
|
|
318
|
-
const steer = clamp(rawSteer * settings.steerSmoothing, -1, 1);
|
|
319
|
-
return {
|
|
320
|
-
difficulty,
|
|
321
|
-
throttle: Number(clamp(speedError / 48 + 0.5, 0, 1).toFixed(4)),
|
|
322
|
-
brake: Number(clamp(-speedError / 38 + Math.abs(curvature) * 0.18, 0, 1).toFixed(4)),
|
|
323
|
-
steer: Number(steer.toFixed(4)),
|
|
324
|
-
targetSpeedKph: Number(desiredSpeed.toFixed(3)),
|
|
325
|
-
lookaheadDistance: Number((15 + speedKph / 10 + (overtaking ? -4 : 0)).toFixed(3)),
|
|
326
|
-
reactionTime: settings.reactionTime,
|
|
327
|
-
aggressiveness: settings.aggressiveness,
|
|
328
|
-
overtaking,
|
|
329
|
-
overtakeOffset: Number(overtakeOffset.toFixed(4)),
|
|
330
|
-
rubberbandBoost: Number(rubberbandBoost.toFixed(4))
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
export function samplePacejkaTireForces(input: PacejkaTireForceInput): PacejkaTireForceSample {
|
|
335
|
-
const normalForce = finiteNonNegative(input.normalForce, "normalForce");
|
|
336
|
-
const longitudinalVelocity = finite(input.longitudinalVelocity, "longitudinalVelocity");
|
|
337
|
-
const lateralVelocity = finite(input.lateralVelocity ?? 0, "lateralVelocity");
|
|
338
|
-
const angularVelocity = finite(input.angularVelocity, "angularVelocity");
|
|
339
|
-
const steeringAngle = finite(input.steeringAngle ?? 0, "steeringAngle");
|
|
340
|
-
const camberAngle = finite(input.camberAngle ?? 0, "camberAngle");
|
|
341
|
-
const radius = positive(input.radius ?? 0.33, "radius");
|
|
342
|
-
const width = positive(input.width ?? 0.225, "width");
|
|
343
|
-
const maxLoad = positive(input.maxLoad ?? 5000, "maxLoad");
|
|
344
|
-
const loadSensitivity = positive(input.loadSensitivity ?? 1, "loadSensitivity");
|
|
345
|
-
const rollingResistance = clamp(input.rollingResistance ?? 0.015, 0, 1);
|
|
346
|
-
const longitudinal = coefficients(input.longitudinal ?? "sport");
|
|
347
|
-
const lateral = coefficients(input.lateral ?? "sport");
|
|
348
|
-
if (normalForce === 0) {
|
|
349
|
-
return {
|
|
350
|
-
longitudinalForce: 0,
|
|
351
|
-
lateralForce: 0,
|
|
352
|
-
combinedForce: 0,
|
|
353
|
-
slipRatio: 0,
|
|
354
|
-
slipAngle: 0,
|
|
355
|
-
aligningTorque: 0,
|
|
356
|
-
optimalSlipRatio: optimalSlipRatio(longitudinal.value),
|
|
357
|
-
optimalSlipAngle: optimalSlipAngle(lateral.value),
|
|
358
|
-
preset: { longitudinal: longitudinal.preset, lateral: lateral.preset }
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
const slipRatio = tireSlipRatio(longitudinalVelocity, angularVelocity, radius);
|
|
363
|
-
const slipAngle = tireSlipAngle(longitudinalVelocity, lateralVelocity, steeringAngle);
|
|
364
|
-
const loadFactor = clamp(Math.pow(normalForce / maxLoad, loadSensitivity), 0.1, 1.5);
|
|
365
|
-
const camberGrip = clamp(1 - Math.abs(camberAngle) * 0.1, 0.75, 1.08);
|
|
366
|
-
const longitudinalPure = pacejka(slipRatio, longitudinal.value) * normalForce * loadFactor * camberGrip
|
|
367
|
-
- Math.sign(longitudinalVelocity) * normalForce * rollingResistance;
|
|
368
|
-
const lateralPure = pacejka(slipAngle * 180 / Math.PI, lateral.value) * normalForce * loadFactor * clamp(1 + camberAngle * 0.1, 0.75, 1.15);
|
|
369
|
-
const slipMagnitude = Math.hypot(Math.abs(slipRatio), Math.abs(slipAngle));
|
|
370
|
-
const longitudinalScale = slipMagnitude < 0.001 ? 1 : Math.sqrt(Math.max(0, 1 - Math.pow((Math.abs(slipAngle) / slipMagnitude) * 0.9, 2)));
|
|
371
|
-
const lateralScale = slipMagnitude < 0.001 ? 1 : Math.sqrt(Math.max(0, 1 - Math.pow((Math.abs(slipRatio) / slipMagnitude) * 0.9, 2)));
|
|
372
|
-
const longitudinalForce = longitudinalPure * longitudinalScale;
|
|
373
|
-
const lateralForce = lateralPure * lateralScale;
|
|
374
|
-
const pneumaticTrail = width * 0.5;
|
|
375
|
-
const torqueCoeff = -pneumaticTrail * clamp(1 - Math.abs(slipAngle * 180 / Math.PI) / 20, 0, 1);
|
|
376
|
-
const aligningTorque = clamp(lateralForce * torqueCoeff, -normalForce * width, normalForce * width);
|
|
377
|
-
return {
|
|
378
|
-
longitudinalForce: Number(longitudinalForce.toFixed(3)),
|
|
379
|
-
lateralForce: Number(lateralForce.toFixed(3)),
|
|
380
|
-
combinedForce: Number(Math.hypot(longitudinalForce, lateralForce).toFixed(3)),
|
|
381
|
-
slipRatio: Number(slipRatio.toFixed(5)),
|
|
382
|
-
slipAngle: Number(slipAngle.toFixed(5)),
|
|
383
|
-
aligningTorque: Number(aligningTorque.toFixed(3)),
|
|
384
|
-
optimalSlipRatio: Number(optimalSlipRatio(longitudinal.value).toFixed(5)),
|
|
385
|
-
optimalSlipAngle: Number(optimalSlipAngle(lateral.value).toFixed(5)),
|
|
386
|
-
preset: { longitudinal: longitudinal.preset, lateral: lateral.preset }
|
|
387
|
-
};
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
function validateGearRatios(values: readonly number[]): readonly number[] {
|
|
391
|
-
if (values.length === 0) {
|
|
392
|
-
throw new RangeError("VehicleDynamics gearRatios must contain at least one ratio.");
|
|
393
|
-
}
|
|
394
|
-
return values.map((value, index) => positive(value, `gearRatios[${index}]`));
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
function selectAutomaticGear(
|
|
398
|
-
gearRatios: readonly number[],
|
|
399
|
-
wheelRpm: number,
|
|
400
|
-
finalDriveRatio: number,
|
|
401
|
-
idleRpm: number,
|
|
402
|
-
maxRpm: number,
|
|
403
|
-
upshiftRpm: number,
|
|
404
|
-
downshiftRpm: number
|
|
405
|
-
): { readonly gear: number; readonly ratio: number; readonly shiftState: "hold" | "upshift" | "downshift" } {
|
|
406
|
-
if (wheelRpm <= 1) return { gear: 1, ratio: gearRatios[0] ?? 1, shiftState: "hold" };
|
|
407
|
-
let gearIndex = 0;
|
|
408
|
-
let rpm = wheelRpm * (gearRatios[gearIndex] ?? 1) * finalDriveRatio;
|
|
409
|
-
while (gearIndex < gearRatios.length - 1 && rpm > upshiftRpm) {
|
|
410
|
-
gearIndex += 1;
|
|
411
|
-
rpm = wheelRpm * (gearRatios[gearIndex] ?? 1) * finalDriveRatio;
|
|
412
|
-
}
|
|
413
|
-
while (gearIndex > 0 && rpm < downshiftRpm) {
|
|
414
|
-
gearIndex -= 1;
|
|
415
|
-
rpm = wheelRpm * (gearRatios[gearIndex] ?? 1) * finalDriveRatio;
|
|
416
|
-
}
|
|
417
|
-
const clampedRpm = clamp(rpm, idleRpm, maxRpm);
|
|
418
|
-
return {
|
|
419
|
-
gear: gearIndex + 1,
|
|
420
|
-
ratio: gearRatios[gearIndex] ?? 1,
|
|
421
|
-
shiftState: clampedRpm >= upshiftRpm && gearIndex < gearRatios.length - 1 ? "upshift" : clampedRpm <= downshiftRpm && gearIndex > 0 ? "downshift" : "hold"
|
|
422
|
-
};
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
function engineTorqueAt(engineRpm: number, peakTorque: number, peakTorqueRpm: number, idleRpm: number, maxRpm: number): number {
|
|
426
|
-
if (engineRpm <= peakTorqueRpm) {
|
|
427
|
-
const t = clamp((engineRpm - idleRpm) / Math.max(1, peakTorqueRpm - idleRpm), 0, 1);
|
|
428
|
-
return peakTorque * (0.8 + 0.2 * t);
|
|
429
|
-
}
|
|
430
|
-
const t = clamp((engineRpm - peakTorqueRpm) / Math.max(1, maxRpm - peakTorqueRpm), 0, 1);
|
|
431
|
-
return peakTorque * (1 - 0.4 * t);
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
function racingAiSettings(difficulty: RacingAiDifficulty): {
|
|
435
|
-
readonly targetSpeedFactor: number;
|
|
436
|
-
readonly steerSmoothing: number;
|
|
437
|
-
readonly reactionTime: number;
|
|
438
|
-
readonly aggressiveness: number;
|
|
439
|
-
readonly rubberbandStrength: number;
|
|
440
|
-
} {
|
|
441
|
-
switch (difficulty) {
|
|
442
|
-
case "easy":
|
|
443
|
-
return { targetSpeedFactor: 0.7, steerSmoothing: 0.3, reactionTime: 0.3, aggressiveness: 0.3, rubberbandStrength: 0.5 };
|
|
444
|
-
case "hard":
|
|
445
|
-
return { targetSpeedFactor: 0.95, steerSmoothing: 0.7, reactionTime: 0.05, aggressiveness: 0.9, rubberbandStrength: 0.1 };
|
|
446
|
-
case "medium":
|
|
447
|
-
return { targetSpeedFactor: 0.85, steerSmoothing: 0.5, reactionTime: 0.15, aggressiveness: 0.6, rubberbandStrength: 0.3 };
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
function suspensionFor(steer: number, speedRatio: number, driftSlip: number): readonly [number, number, number, number] {
|
|
452
|
-
const loadTransfer = steer * speedRatio * 0.18;
|
|
453
|
-
const brakeDive = driftSlip * 0.08;
|
|
454
|
-
return [
|
|
455
|
-
clamp(Number((0.48 - loadTransfer + brakeDive).toFixed(5)), 0, 1),
|
|
456
|
-
clamp(Number((0.48 + loadTransfer + brakeDive).toFixed(5)), 0, 1),
|
|
457
|
-
clamp(Number((0.42 - loadTransfer * 0.55).toFixed(5)), 0, 1),
|
|
458
|
-
clamp(Number((0.42 + loadTransfer * 0.55).toFixed(5)), 0, 1)
|
|
459
|
-
];
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
function finite(value: number, label: string): number {
|
|
463
|
-
if (!Number.isFinite(value)) {
|
|
464
|
-
throw new RangeError(`VehicleDynamics ${label} must be finite.`);
|
|
465
|
-
}
|
|
466
|
-
return value;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
function finiteNonNegative(value: number, label: string): number {
|
|
470
|
-
if (!Number.isFinite(value) || value < 0) {
|
|
471
|
-
throw new RangeError(`VehicleDynamics ${label} must be finite and non-negative.`);
|
|
472
|
-
}
|
|
473
|
-
return value;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
function positive(value: number, label: string): number {
|
|
477
|
-
if (!Number.isFinite(value) || value <= 0) {
|
|
478
|
-
throw new RangeError(`VehicleDynamics ${label} must be finite and positive.`);
|
|
479
|
-
}
|
|
480
|
-
return value;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
function coefficients(value: PacejkaTireCoefficients | PacejkaTirePreset): { readonly preset: PacejkaTirePreset | "custom"; readonly value: PacejkaTireCoefficients } {
|
|
484
|
-
if (typeof value !== "string") return { preset: "custom", value };
|
|
485
|
-
switch (value) {
|
|
486
|
-
case "street":
|
|
487
|
-
return { preset: value, value: { b: 10, c: 1.9, d: 1, e: 0.97 } };
|
|
488
|
-
case "sport":
|
|
489
|
-
return { preset: value, value: { b: 12, c: 2, d: 1.3, e: 0.95 } };
|
|
490
|
-
case "racing":
|
|
491
|
-
return { preset: value, value: { b: 14, c: 2.1, d: 1.6, e: 0.9 } };
|
|
492
|
-
case "offroad":
|
|
493
|
-
return { preset: value, value: { b: 7, c: 1.7, d: 0.9, e: 1 } };
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
function tireSlipRatio(longitudinalVelocity: number, angularVelocity: number, radius: number): number {
|
|
498
|
-
const wheelVelocity = angularVelocity * radius;
|
|
499
|
-
const denominator = Math.max(Math.abs(wheelVelocity), Math.abs(longitudinalVelocity));
|
|
500
|
-
if (denominator < 0.1) return 0;
|
|
501
|
-
return clamp((wheelVelocity - longitudinalVelocity) / denominator, -1, 1);
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
function tireSlipAngle(longitudinalVelocity: number, lateralVelocity: number, steeringAngle: number): number {
|
|
505
|
-
if (Math.abs(longitudinalVelocity) < 0.1) return 0;
|
|
506
|
-
return clamp(Math.atan2(lateralVelocity, longitudinalVelocity) - steeringAngle, -Math.PI * 0.4, Math.PI * 0.4);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
function pacejka(slip: number, coeff: PacejkaTireCoefficients): number {
|
|
510
|
-
const bx = coeff.b * slip;
|
|
511
|
-
return coeff.d * Math.sin(coeff.c * Math.atan(bx - coeff.e * (bx - Math.atan(bx))));
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
function optimalSlipRatio(coeff: PacejkaTireCoefficients): number {
|
|
515
|
-
return Math.min(Math.tan(Math.PI / (2 * coeff.c)) / coeff.b, 0.2);
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
function optimalSlipAngle(coeff: PacejkaTireCoefficients): number {
|
|
519
|
-
return Math.min((Math.tan(Math.PI / (2 * coeff.c)) / coeff.b) * Math.PI / 180, 0.3);
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
function clamp(value: number, min: number, max: number): number {
|
|
523
|
-
return Math.max(min, Math.min(max, value));
|
|
524
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
export * from "./Shape.js";
|
|
2
|
-
export * from "./RigidBody.js";
|
|
3
|
-
export * from "./Collider.js";
|
|
4
|
-
export * from "./CollisionEvents.js";
|
|
5
|
-
export * from "./Constraint.js";
|
|
6
|
-
export * from "./Constraints.js";
|
|
7
|
-
export * from "./Raycast.js";
|
|
8
|
-
export * from "./PhysicsWorld.js";
|
|
9
|
-
export * from "./PhysicsStepper.js";
|
|
10
|
-
export * from "./ScenePhysicsBridge.js";
|
|
11
|
-
export * from "./ECSPhysicsBridge.js";
|
|
12
|
-
export * from "./PhysicsDebugDraw.js";
|
|
13
|
-
export * from "./CharacterController.js";
|
|
14
|
-
export * from "./Navigation.js";
|
|
15
|
-
export * from "./Steering.js";
|
|
16
|
-
export * from "./Crowd.js";
|
|
17
|
-
export * from "./VehicleDynamics.js";
|
|
18
|
-
export * from "./PlatformerFixtures.js";
|
|
19
|
-
export * from "./PhysicsSandboxFixtures.js";
|
|
20
|
-
export * from "./ClothFixtures.js";
|
|
21
|
-
export * from "./SoftBodyFixtures.js";
|
|
22
|
-
export * from "./FractureFixtures.js";
|
|
23
|
-
export * from "./FluidFixtures.js";
|
|
24
|
-
export * from "./FireSmokeFixtures.js";
|
|
25
|
-
export * from "./CollisionVolumes.js";
|
|
26
|
-
export * from "./KinematicBody.js";
|
|
27
|
-
export * from "./KinematicWorld.js";
|
|
28
|
-
export * from "./HitboxWorld.js";
|