48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using System;
|
|
|
|
public class EngineControlUnit
|
|
{
|
|
public double Throttle;
|
|
public bool IsInNeutral = true;
|
|
|
|
private const double _revLimit = 7000;
|
|
private const double _idleRpm = 800;
|
|
private const double _idleSensitivity = 1e-2;
|
|
private const double _throttleSensitivity = 20;
|
|
private const double _neutralThrottleLimit = 0.2;
|
|
|
|
private double _idlePosition = 0;
|
|
private double _targetThrottle = 0;
|
|
private double _currentThrottle = 0;
|
|
|
|
public EngineControlUnit()
|
|
{
|
|
|
|
}
|
|
|
|
public void Update(double dt, Engine engine)
|
|
{
|
|
if (engine.RPM > _revLimit)
|
|
{
|
|
engine.ThrottlePosition = 0; return;
|
|
}
|
|
|
|
_targetThrottle = Math.Clamp(GetIdleThrottle(dt, engine.RPM) + Throttle, 0, 1);
|
|
_currentThrottle = Util.Lerp(_currentThrottle, _targetThrottle, dt * _throttleSensitivity);
|
|
|
|
if (IsInNeutral) _currentThrottle = Math.Min(_currentThrottle, _neutralThrottleLimit);
|
|
|
|
engine.ThrottlePosition = _currentThrottle;
|
|
}
|
|
|
|
public double GetIdleThrottle(double dt, double rpm)
|
|
{
|
|
if (rpm > _idleRpm) return 0;
|
|
|
|
double diff = rpm - _idleRpm;
|
|
|
|
_idlePosition = Math.Clamp(_idlePosition - (diff * _idleSensitivity * dt), 0, 1.0);
|
|
|
|
return _idlePosition;
|
|
}
|
|
} |