This commit is contained in:
Valera 2025-11-18 21:53:51 +07:00
parent 97fcb29bc0
commit bbc479be33
11 changed files with 152 additions and 42 deletions

View File

@ -1,8 +1,8 @@
{ {
"B": 8.36, "B": 9,
"C": 2.15, "C": 2.15,
"D": 0.833, "D": 0.933,
"E": 0.992, "E": 0.971,
"__references": [], "__references": [],
"__version": 0 "__version": 0
} }

View File

@ -543,13 +543,14 @@ public class Transmission : PowertrainComponent
// Run the first half of shift timer // Run the first half of shift timer
float shiftTimer = 0; float shiftTimer = 0;
float halfDuration = ShiftDuration * 0.5f; float halfDuration = ShiftDuration * 0.5f;
if ( !instant )
while ( shiftTimer < halfDuration ) //if ( !instant )
{ // while ( shiftTimer < halfDuration )
ShiftProgress = shiftTimer / ShiftDuration; // {
shiftTimer += dt; // ShiftProgress = shiftTimer / ShiftDuration;
await Task.DelayRealtimeSeconds( dt ); // shiftTimer += dt;
} // await Task.DelayRealtimeSeconds( dt );
// }
// Do the shift at the half point of shift duration // Do the shift at the half point of shift duration
Gear = targetGear; Gear = targetGear;
@ -603,28 +604,28 @@ public class Transmission : PowertrainComponent
float brakeInput = car.SwappedBrakes; float brakeInput = car.SwappedBrakes;
int currentGear = Gear; int currentGear = Gear;
// Assign base shift points // Assign base shift points
_targetDownshiftRPM = _downshiftRPM; _targetDownshiftRPM = car.Engine.EstimatedPeakPowerRPM - 2000;
_targetUpshiftRPM = _upshiftRPM; _targetUpshiftRPM = car.Engine.EstimatedPeakPowerRPM;
// Calculate shift points for variable shift RPM // Calculate shift points for variable shift RPM
if ( VariableShiftPoint ) //if ( VariableShiftPoint )
{ //{
// Smooth throttle input so that the variable shift point does not shift suddenly and cause gear hunting // // Smooth throttle input so that the variable shift point does not shift suddenly and cause gear hunting
_smoothedThrottleInput = MathX.Lerp( _smoothedThrottleInput, throttleInput, Time.Delta * 2f ); // _smoothedThrottleInput = MathX.Lerp( _smoothedThrottleInput, throttleInput, Time.Delta * 2f );
float revLimiterRPM = car.Engine.RevLimiterRPM; // float revLimiterRPM = car.Engine.RevLimiterRPM;
_targetUpshiftRPM = _upshiftRPM + Math.Clamp( _smoothedThrottleInput * VariableShiftIntensity, 0f, 1f ) * _upshiftRPM; // _targetUpshiftRPM = _upshiftRPM + Math.Clamp( _smoothedThrottleInput * VariableShiftIntensity, 0f, 1f ) * _upshiftRPM;
_targetUpshiftRPM = Math.Clamp( _targetUpshiftRPM, _upshiftRPM, revLimiterRPM * 0.97f ); // _targetUpshiftRPM = Math.Clamp( _targetUpshiftRPM, _upshiftRPM, revLimiterRPM * 0.97f );
_targetDownshiftRPM = _downshiftRPM + Math.Clamp( _smoothedThrottleInput * VariableShiftIntensity, 0f, 1f ) * _downshiftRPM; // _targetDownshiftRPM = _downshiftRPM + Math.Clamp( _smoothedThrottleInput * VariableShiftIntensity, 0f, 1f ) * _downshiftRPM;
_targetDownshiftRPM = Math.Clamp( _targetDownshiftRPM, car.Engine.IdleRPM * 1.1f, _targetUpshiftRPM * 0.7f ); // _targetDownshiftRPM = Math.Clamp( _targetDownshiftRPM, car.Engine.IdleRPM * 1.1f, _targetUpshiftRPM * 0.7f );
// Add incline modifier // // Add incline modifier
float inclineModifier = Math.Clamp( car.WorldRotation.Forward.Dot( Vector3.Up ) * InclineEffectCoeff, 0f, 1f ); // float inclineModifier = Math.Clamp( car.WorldRotation.Forward.Dot( Vector3.Up ) * InclineEffectCoeff, 0f, 1f );
_targetUpshiftRPM += revLimiterRPM * inclineModifier; // _targetUpshiftRPM += revLimiterRPM * inclineModifier;
_targetDownshiftRPM += revLimiterRPM * inclineModifier; // _targetDownshiftRPM += revLimiterRPM * inclineModifier;
} //}
// In neutral // In neutral

View File

@ -59,7 +59,7 @@ public abstract partial class VeloXBase
/// Throttle axis. /// Throttle axis.
/// For combined throttle/brake input use 'VerticalInput' instead. /// For combined throttle/brake input use 'VerticalInput' instead.
/// </summary> /// </summary>
[Sync( SyncFlags.Interpolate ), Range( 0, 1 ), Property] [Sync( SyncFlags.Interpolate ), Range( 0, 1 ), Property, ReadOnly]
public float Throttle public float Throttle
{ {
get => throttle; get => throttle;
@ -70,19 +70,20 @@ public abstract partial class VeloXBase
/// Brake axis. /// Brake axis.
/// For combined throttle/brake input use 'VerticalInput' instead. /// For combined throttle/brake input use 'VerticalInput' instead.
/// </summary> /// </summary>
[Sync] [Range( 0, 1 ), Property, ReadOnly]
public float Brakes public float Brakes
{ {
get => brakes; get => brakes;
set => brakes = Math.Clamp( value, 0, 1 ); set => brakes = Math.Clamp( value, 0, 1 );
} }
[Sync] [Sync( SyncFlags.Interpolate ), Range( 0, 1 ), Property, ReadOnly]
public float SteeringAngle public float SteeringAngle
{ {
get => steerAngle; get => steerAngle;
set => steerAngle = Math.Clamp( value, -1, 1 ); set => steerAngle = Math.Clamp( value, -1, 1 );
} }
[Sync] [Sync]
public float Handbrake public float Handbrake
{ {
@ -103,7 +104,7 @@ public abstract partial class VeloXBase
IsShiftingUp = false; IsShiftingUp = false;
IsShiftingDown = false; IsShiftingDown = false;
} }
private void UpdateInput() protected void UpdateInput()
{ {
VerticalInput = Input.AnalogMove.x; VerticalInput = Input.AnalogMove.x;
Handbrake = Input.Down( "Handbrake" ) ? 1 : 0; Handbrake = Input.Down( "Handbrake" ) ? 1 : 0;

View File

@ -7,9 +7,9 @@ public abstract partial class VeloXBase
private Vector3 linForce; private Vector3 linForce;
private Vector3 angForce; private Vector3 angForce;
[Property] float BrakeForce { get; set; } = 1500f; protected const float BrakeForce = 4500f;
[Property] float HandbrakeForce { get; set; } = 3500f; protected const float HandbrakeForce = 35000f;
private void PhysicsSimulate() protected void PhysicsSimulate()
{ {
if ( Body.Sleeping && Input.AnalogMove.x == 0 ) if ( Body.Sleeping && Input.AnalogMove.x == 0 )
return; return;
@ -36,10 +36,22 @@ public abstract partial class VeloXBase
CombinedLoad += v.Fz; CombinedLoad += v.Fz;
foreach ( var v in Wheels ) foreach ( var v in Wheels )
{ {
v.BrakeTorque = SwappedBrakes * BrakeForce; if ( v.IsFront )
if ( !v.IsFront ) {
v.BrakeTorque = SwappedBrakes * BrakeForce * 1.3f;
}
else
{
v.BrakeTorque = SwappedBrakes * BrakeForce * 0.7f;
v.BrakeTorque += Handbrake * HandbrakeForce; v.BrakeTorque += Handbrake * HandbrakeForce;
}
if ( TotalSpeed < 1 && Input.AnalogMove.x == 0 )
{
v.BrakeTorque = HandbrakeForce;
}
v.Update( this, in dt ); v.Update( this, in dt );
v.DoPhysics( in dt ); v.DoPhysics( in dt );
} }

View File

@ -30,9 +30,14 @@ public abstract partial class VeloXBase : Component
TotalSpeed = LocalVelocity.Length; TotalSpeed = LocalVelocity.Length;
Body.PhysicsBody.Mass = Mass; Body.PhysicsBody.Mass = Mass;
FixedUpdate();
}
protected virtual void FixedUpdate()
{
UpdateInput(); UpdateInput();
PhysicsSimulate(); PhysicsSimulate();
} }
} }

View File

@ -34,7 +34,7 @@ public partial class VeloXWheel
/// </summary> /// </summary>
public float CounterTorque { get; private set; } public float CounterTorque { get; private set; }
[Property, Range( 0, 2 )] public float BrakeMult { get; set; } = 1f; //[Property, Range( 0, 2 )] public float BrakeMult { get; set; } = 1f;
public Friction ForwardFriction = new(); public Friction ForwardFriction = new();
public Friction SidewayFriction = new(); public Friction SidewayFriction = new();
public Vector3 FrictionForce; public Vector3 FrictionForce;
@ -100,7 +100,7 @@ public partial class VeloXWheel
private void UpdateFriction( float dt ) private void UpdateFriction( float dt )
{ {
var motorTorque = DriveTorque; var motorTorque = DriveTorque;
var brakeTorque = BrakeTorque * BrakeMult; var brakeTorque = BrakeTorque;
float allWheelLoadSum = Vehicle.CombinedLoad; float allWheelLoadSum = Vehicle.CombinedLoad;

View File

@ -16,7 +16,7 @@ internal sealed class WheelManager : GameObjectSystem
private void UpdateWheels() private void UpdateWheels()
{ {
if ( !Game.IsPlaying ) if ( !Game.IsPlaying || Scene.IsEditor )
return; return;
//Stopwatch sw = Stopwatch.StartNew(); //Stopwatch sw = Stopwatch.StartNew();
@ -38,7 +38,7 @@ internal sealed class WheelManager : GameObjectSystem
} }
private void UpdateEngine() private void UpdateEngine()
{ {
if ( !Game.IsPlaying ) if ( !Game.IsPlaying || Scene.IsEditor )
return; return;
//Stopwatch sw = Stopwatch.StartNew(); //Stopwatch sw = Stopwatch.StartNew();

38
Code/Car/VeloXCar.ABS.cs Normal file
View File

@ -0,0 +1,38 @@
using Sandbox;
using System;
namespace VeloX;
public partial class VeloXCar
{
public bool ABSActive { get; private set; } = true;
public static bool UseABS = true;
private void UpdateABS()
{
if ( !UseABS )
return;
ABSActive = false;
if ( TotalSpeed < 100 || ABSActive )
return;
if ( Brakes == 0 || CarDirection != 1 || Engine.RevLimiterActive || Handbrake >= 0.1f )
return;
foreach ( var wheel in Wheels )
{
if ( !wheel.IsOnGround )
continue;
if ( wheel.NormalizedLongitudinalSlip >= 0.55f )
{
ABSActive = true;
wheel.BrakeTorque *= 0.25f;
}
}
}
}

44
Code/Car/VeloXCar.ESC.cs Normal file
View File

@ -0,0 +1,44 @@
using Sandbox;
using Sandbox.VR;
using System;
using VeloX.Utils;
namespace VeloX;
public partial class VeloXCar
{
public bool ESCActive { get; private set; } = true;
public static bool UseESC = true;
private void UpdateESC()
{
if ( !UseESC )
return;
ESCActive = false;
if ( TotalSpeed < 100 || CarDirection != 1 )
return;
float angle = Body.Velocity.SignedAngle( WorldRotation.Forward, WorldRotation.Up ); ;
angle -= SteerAngle.yaw * 0.5f;
float absAngle = angle < 0 ? -angle : angle;
if ( Engine.RevLimiterActive || absAngle < 2f )
return;
foreach ( var wheel in Wheels )
{
if ( !wheel.IsOnGround )
continue;
float additionalBrakeTorque = -angle * Math.Sign( wheel.LocalPosition.y ) * 20f;
if ( additionalBrakeTorque > 0 )
{
ESCActive = true;
wheel.BrakeTorque += additionalBrakeTorque;
}
}
}
}

View File

@ -33,7 +33,7 @@ public partial class VeloXCar
float targetSteerAngle = inputSteer * MaxSteerAngle; float targetSteerAngle = inputSteer * MaxSteerAngle;
if ( !Input.Down( "Jump" ) ) if ( !Input.Down( "Jump" ) )
targetSteerAngle *= Math.Clamp( 1 - Math.Clamp( TotalSpeed / 3000, 0.01f, 0.9f ), -1, 1 ); targetSteerAngle *= Math.Clamp( 1 - Math.Clamp( TotalSpeed / 3000, 0f, 0.85f ), -1, 1 );
VelocityAngle = -Body.Velocity.SignedAngle( WorldRotation.Forward, WorldRotation.Up ); VelocityAngle = -Body.Velocity.SignedAngle( WorldRotation.Forward, WorldRotation.Up );

View File

@ -8,6 +8,15 @@ namespace VeloX;
public partial class VeloXCar : VeloXBase public partial class VeloXCar : VeloXBase
{ {
protected override void FixedUpdate()
{
UpdateInput();
PhysicsSimulate();
UpdateABS();
UpdateESC();
}
protected override void OnFixedUpdate() protected override void OnFixedUpdate()
{ {
if ( IsProxy ) if ( IsProxy )