This commit is contained in:
2025-11-25 19:16:40 +07:00
parent 68626640c2
commit e12b75be45
11 changed files with 119 additions and 107 deletions

View File

@@ -78,7 +78,7 @@ public class Engine : PowertrainComponent, IScenePhysicsEvents
/// </summary>
[Property] public bool FlyingStartEnabled { get; set; }
[Property] public bool Ignition { get; set; }
[Property] public bool Ignition { get; private set; }
/// <summary>
/// Power curve with RPM range [0,1] on the X axis and power coefficient [0,1] on Y axis.
@@ -225,13 +225,21 @@ public class Engine : PowertrainComponent, IScenePhysicsEvents
if ( FlyingStartEnabled )
{
FlyingStart();
IsRunning = true;
IsActive = true;
}
else if ( !StarterActive && Controller != null )
{
StarterCoroutine();
}
}
else
{
IsRunning = true;
IsActive = true;
}
}
private async void StarterCoroutine()
{
if ( Type == EngineType.Electric || StarterActive )
@@ -251,6 +259,11 @@ public class Engine : PowertrainComponent, IScenePhysicsEvents
{
startTimer += 0.1f;
await Task.DelaySeconds( 0.1f );
if ( OutputAngularVelocity >= _idleAngularVelocity * 0.8f )
{
break;
}
}
}
finally
@@ -258,6 +271,7 @@ public class Engine : PowertrainComponent, IScenePhysicsEvents
_starterTorque = 0;
StarterActive = false;
IsActive = true;
IsRunning = true;
}
}
@@ -267,12 +281,15 @@ public class Engine : PowertrainComponent, IScenePhysicsEvents
Ignition = true;
StarterActive = false;
OutputAngularVelocity = IdleRPM.RPMToAngularVelocity();
IsRunning = true;
IsActive = true;
}
public void StopEngine()
{
Ignition = false;
IsActive = true;
IsRunning = false;
IsActive = false;
OnEngineStop?.Invoke();
}
@@ -308,11 +325,6 @@ public class Engine : PowertrainComponent, IScenePhysicsEvents
_revLimiterAngularVelocity = RevLimiterRPM.RPMToAngularVelocity();
if ( _revLimiterAngularVelocity == 0f )
return;
// Check for start on throttle
if ( !IsRunning && !StarterActive && AutoStartOnThrottle && ThrottlePosition > 0.2f )
StartEngine();
bool wasRunning = IsRunning;
IsRunning = Ignition;
@@ -334,13 +346,14 @@ public class Engine : PowertrainComponent, IScenePhysicsEvents
// Calculate generated torque and power
float generatedTorque = CalculateTorqueICE( OutputAngularVelocity, dt );
generatedPower = TorqueToPowerInKW( in OutputAngularVelocity, in generatedTorque );
// Calculate reaction torque
float reactionTorque = (targetAngularVelocity - OutputAngularVelocity) * Inertia / dt;
// Calculate/get torque returned from wheels
OutputTorque = generatedTorque - reactionTorque;
float returnTorque = ForwardStep( OutputTorque, 0, dt );
float totalTorque = generatedTorque + returnTorque + reactionTorque;
@@ -408,7 +421,7 @@ public class Engine : PowertrainComponent, IScenePhysicsEvents
// Apply idle throttle correction to keep the engine running
else
ApplyICEIdleCorrection();
// Trigger rev limiter if needed
if ( angularVelocity >= _revLimiterAngularVelocity && !RevLimiterActive )
RevLimiter();
@@ -454,7 +467,7 @@ public class Engine : PowertrainComponent, IScenePhysicsEvents
// if the idle RPM is below the target RPM.
float idleCorrection = _idleAngularVelocity * 1.08f - OutputAngularVelocity;
idleCorrection = idleCorrection < 0f ? 0f : idleCorrection;
float idleThrottlePosition = Math.Clamp( idleCorrection * 0.01f, 0, 1 );
float idleThrottlePosition = Math.Clamp( idleCorrection * 0.01f, 0, 1f );
ThrottlePosition = Math.Max( _userThrottleInput, idleThrottlePosition );
}
}