Improve engine friction, add a rev limiter

This commit is contained in:
sergeypdev 2025-05-02 21:11:48 +04:00
parent 08b6748d76
commit 1ea89fb2c9
5 changed files with 78 additions and 52 deletions

View File

@ -1,6 +1,6 @@
b
1.6
-80
-120
1100
0
300

1 b
2 1.6
3 -80 -120
4 1100
5 0
6 300

View File

@ -434,13 +434,15 @@ update_runtime_world :: proc(runtime_world: ^Runtime_World, dt: f32) {
"assets/ae86_rpm_torque.csv",
),
lowest_rpm = 1100,
inertia = 0.264,
internal_friction = 0.00,
rev_limit_rpm = 7800,
rev_limit_interval = 0.025,
inertia = 0.264 * 0.5,
internal_friction = 0.01,
gear_ratios = []f32{3.48, 3.587, 2.022, 1.384, 1, 0.861},
axle = physics.Drive_Axle_Config {
wheels = {wheel_rl, wheel_rr},
wheel_count = 2,
diff_type = .Open,
diff_type = .Fixed,
final_drive_ratio = 4.1,
},
},

View File

@ -151,7 +151,7 @@ draw_debug_ui :: proc(ctx: ^ui.Context, scene: ^Scene, config: Solver_Config) {
sim_state := get_sim_state(scene)
active_wheels := []int{2, 3}
active_wheels := []int{}
w, h: i32 = 200, 200

View File

@ -221,6 +221,10 @@ Engine :: struct {
// Engine Params
rpm_torque_curve: Engine_Curve_Handle,
lowest_rpm: f32,
// Rpm when rev limiter activates
rev_limit_rpm: f32,
// Time in seconds for how long rev limiter cuts the throttle
rev_limit_interval: f32,
inertia: f32,
internal_friction: f32,
@ -234,6 +238,9 @@ Engine :: struct {
q: f32,
w: f32,
// Reset to -rev_limit_interval when rpm exceeds rev_limit_rpm, has to be >= 0 for throttle to work
rev_limit_time: f32,
// Impulse applied when engine is stalling (rpm < lowest_rpm)
unstall_impulse: f32,
// Friction that makes rpm go down when you're not accelerating
@ -380,6 +387,8 @@ Drive_Axle_Config :: struct {
Engine_Config :: struct {
rpm_torque_curve: [][2]f32,
lowest_rpm: f32,
rev_limit_rpm: f32,
rev_limit_interval: f32,
inertia: f32,
internal_friction: f32,
@ -500,6 +509,8 @@ update_engine_from_config :: proc(
engine.gear_ratios = add_gear_ratios(sim_state, config.gear_ratios)
engine.lowest_rpm = config.lowest_rpm
engine.rev_limit_rpm = config.rev_limit_rpm
engine.rev_limit_interval = config.rev_limit_interval
engine.inertia = config.inertia
engine.internal_friction = config.internal_friction

View File

@ -571,11 +571,22 @@ pgs_solve_engines :: proc(sim_state: ^Sim_State, config: Solver_Config, dt: f32,
engine.w += applied_impulse / engine.inertia
}
rpm := angular_velocity_to_rpm(engine.w)
throttle := engine.throttle
if engine.rev_limit_time < 0.0 {
engine.rev_limit_time += dt
throttle = 0
} else if (rpm >= engine.rev_limit_rpm) {
engine.rev_limit_time = -engine.rev_limit_interval
throttle = 0
}
// Throttle
{
rpm := angular_velocity_to_rpm(engine.w)
{
torque: f32
idx, _ := slice.binary_search_by(
@ -598,10 +609,11 @@ pgs_solve_engines :: proc(sim_state: ^Sim_State, config: Solver_Config, dt: f32,
}
// log.debugf("torque: %v Nm", torque)
torque *= engine.throttle
torque *= throttle
engine.w += (torque / engine.inertia) * dt
}
}
// Internal Friction
{
@ -610,13 +622,14 @@ pgs_solve_engines :: proc(sim_state: ^Sim_State, config: Solver_Config, dt: f32,
inv_w := engine.inertia
friction :=
math.pow(
max(engine.w - rpm_to_angular_velocity(engine.lowest_rpm), 0) * 0.002,
4,
) *
math.pow(max(engine.w - rpm_to_angular_velocity(engine.lowest_rpm), 0), 2) *
0.0001 *
engine.internal_friction +
engine.internal_friction
// Not physically based, but we assume when throttle is open there is no friction
friction *= (1.0 - throttle)
incremental_impulse := inv_w * delta_omega
new_total_impulse := math.clamp(
engine.friction_impulse + incremental_impulse,