diff --git a/Assets/frictions/default.tire b/Assets/frictions/default.tire index 97ba89f..ec91978 100644 --- a/Assets/frictions/default.tire +++ b/Assets/frictions/default.tire @@ -1,8 +1,8 @@ { - "B": 8.36, + "B": 9, "C": 2.15, - "D": 0.833, - "E": 0.992, + "D": 0.933, + "E": 0.971, "__references": [], "__version": 0 } \ No newline at end of file diff --git a/Code/Base/Powertrain/Transmission.cs b/Code/Base/Powertrain/Transmission.cs index a60888f..53b25c6 100644 --- a/Code/Base/Powertrain/Transmission.cs +++ b/Code/Base/Powertrain/Transmission.cs @@ -543,13 +543,14 @@ public class Transmission : PowertrainComponent // Run the first half of shift timer float shiftTimer = 0; float halfDuration = ShiftDuration * 0.5f; - if ( !instant ) - while ( shiftTimer < halfDuration ) - { - ShiftProgress = shiftTimer / ShiftDuration; - shiftTimer += dt; - await Task.DelayRealtimeSeconds( dt ); - } + + //if ( !instant ) + // while ( shiftTimer < halfDuration ) + // { + // ShiftProgress = shiftTimer / ShiftDuration; + // shiftTimer += dt; + // await Task.DelayRealtimeSeconds( dt ); + // } // Do the shift at the half point of shift duration Gear = targetGear; @@ -603,28 +604,28 @@ public class Transmission : PowertrainComponent float brakeInput = car.SwappedBrakes; int currentGear = Gear; // Assign base shift points - _targetDownshiftRPM = _downshiftRPM; - _targetUpshiftRPM = _upshiftRPM; - + _targetDownshiftRPM = car.Engine.EstimatedPeakPowerRPM - 2000; + _targetUpshiftRPM = car.Engine.EstimatedPeakPowerRPM; + // Calculate shift points for variable shift RPM - if ( VariableShiftPoint ) - { - // 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 ); - float revLimiterRPM = car.Engine.RevLimiterRPM; + //if ( VariableShiftPoint ) + //{ + // // 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 ); + // float revLimiterRPM = car.Engine.RevLimiterRPM; - _targetUpshiftRPM = _upshiftRPM + Math.Clamp( _smoothedThrottleInput * VariableShiftIntensity, 0f, 1f ) * _upshiftRPM; - _targetUpshiftRPM = Math.Clamp( _targetUpshiftRPM, _upshiftRPM, revLimiterRPM * 0.97f ); + // _targetUpshiftRPM = _upshiftRPM + Math.Clamp( _smoothedThrottleInput * VariableShiftIntensity, 0f, 1f ) * _upshiftRPM; + // _targetUpshiftRPM = Math.Clamp( _targetUpshiftRPM, _upshiftRPM, revLimiterRPM * 0.97f ); - _targetDownshiftRPM = _downshiftRPM + Math.Clamp( _smoothedThrottleInput * VariableShiftIntensity, 0f, 1f ) * _downshiftRPM; - _targetDownshiftRPM = Math.Clamp( _targetDownshiftRPM, car.Engine.IdleRPM * 1.1f, _targetUpshiftRPM * 0.7f ); + // _targetDownshiftRPM = _downshiftRPM + Math.Clamp( _smoothedThrottleInput * VariableShiftIntensity, 0f, 1f ) * _downshiftRPM; + // _targetDownshiftRPM = Math.Clamp( _targetDownshiftRPM, car.Engine.IdleRPM * 1.1f, _targetUpshiftRPM * 0.7f ); - // Add incline modifier - float inclineModifier = Math.Clamp( car.WorldRotation.Forward.Dot( Vector3.Up ) * InclineEffectCoeff, 0f, 1f ); + // // Add incline modifier + // float inclineModifier = Math.Clamp( car.WorldRotation.Forward.Dot( Vector3.Up ) * InclineEffectCoeff, 0f, 1f ); - _targetUpshiftRPM += revLimiterRPM * inclineModifier; - _targetDownshiftRPM += revLimiterRPM * inclineModifier; - } + // _targetUpshiftRPM += revLimiterRPM * inclineModifier; + // _targetDownshiftRPM += revLimiterRPM * inclineModifier; + //} // In neutral diff --git a/Code/Base/VeloXBase.Input.cs b/Code/Base/VeloXBase.Input.cs index 4b69920..50413f9 100644 --- a/Code/Base/VeloXBase.Input.cs +++ b/Code/Base/VeloXBase.Input.cs @@ -59,7 +59,7 @@ public abstract partial class VeloXBase /// Throttle axis. /// For combined throttle/brake input use 'VerticalInput' instead. /// - [Sync( SyncFlags.Interpolate ), Range( 0, 1 ), Property] + [Sync( SyncFlags.Interpolate ), Range( 0, 1 ), Property, ReadOnly] public float Throttle { get => throttle; @@ -70,19 +70,20 @@ public abstract partial class VeloXBase /// Brake axis. /// For combined throttle/brake input use 'VerticalInput' instead. /// - [Sync] + [Range( 0, 1 ), Property, ReadOnly] public float Brakes { get => brakes; set => brakes = Math.Clamp( value, 0, 1 ); } - [Sync] + [Sync( SyncFlags.Interpolate ), Range( 0, 1 ), Property, ReadOnly] public float SteeringAngle { get => steerAngle; set => steerAngle = Math.Clamp( value, -1, 1 ); } + [Sync] public float Handbrake { @@ -103,7 +104,7 @@ public abstract partial class VeloXBase IsShiftingUp = false; IsShiftingDown = false; } - private void UpdateInput() + protected void UpdateInput() { VerticalInput = Input.AnalogMove.x; Handbrake = Input.Down( "Handbrake" ) ? 1 : 0; diff --git a/Code/Base/VeloXBase.Phys.cs b/Code/Base/VeloXBase.Phys.cs index 2f85862..16e1ea0 100644 --- a/Code/Base/VeloXBase.Phys.cs +++ b/Code/Base/VeloXBase.Phys.cs @@ -7,9 +7,9 @@ public abstract partial class VeloXBase private Vector3 linForce; private Vector3 angForce; - [Property] float BrakeForce { get; set; } = 1500f; - [Property] float HandbrakeForce { get; set; } = 3500f; - private void PhysicsSimulate() + protected const float BrakeForce = 4500f; + protected const float HandbrakeForce = 35000f; + protected void PhysicsSimulate() { if ( Body.Sleeping && Input.AnalogMove.x == 0 ) return; @@ -36,10 +36,22 @@ public abstract partial class VeloXBase CombinedLoad += v.Fz; 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; + } + + if ( TotalSpeed < 1 && Input.AnalogMove.x == 0 ) + { + v.BrakeTorque = HandbrakeForce; + } + v.Update( this, in dt ); v.DoPhysics( in dt ); } diff --git a/Code/Base/VeloXBase.cs b/Code/Base/VeloXBase.cs index 8a87bef..112fa7a 100644 --- a/Code/Base/VeloXBase.cs +++ b/Code/Base/VeloXBase.cs @@ -30,9 +30,14 @@ public abstract partial class VeloXBase : Component TotalSpeed = LocalVelocity.Length; Body.PhysicsBody.Mass = Mass; + FixedUpdate(); + } + + protected virtual void FixedUpdate() + { + UpdateInput(); PhysicsSimulate(); - } } diff --git a/Code/Base/Wheel/VeloXWheel.Friction.cs b/Code/Base/Wheel/VeloXWheel.Friction.cs index 2dd02e3..f7d338b 100644 --- a/Code/Base/Wheel/VeloXWheel.Friction.cs +++ b/Code/Base/Wheel/VeloXWheel.Friction.cs @@ -34,7 +34,7 @@ public partial class VeloXWheel /// 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 SidewayFriction = new(); public Vector3 FrictionForce; @@ -100,7 +100,7 @@ public partial class VeloXWheel private void UpdateFriction( float dt ) { var motorTorque = DriveTorque; - var brakeTorque = BrakeTorque * BrakeMult; + var brakeTorque = BrakeTorque; float allWheelLoadSum = Vehicle.CombinedLoad; diff --git a/Code/Base/Wheel/WheelManager.cs b/Code/Base/Wheel/WheelManager.cs index a8d5d6f..533c774 100644 --- a/Code/Base/Wheel/WheelManager.cs +++ b/Code/Base/Wheel/WheelManager.cs @@ -16,7 +16,7 @@ internal sealed class WheelManager : GameObjectSystem private void UpdateWheels() { - if ( !Game.IsPlaying ) + if ( !Game.IsPlaying || Scene.IsEditor ) return; //Stopwatch sw = Stopwatch.StartNew(); @@ -38,7 +38,7 @@ internal sealed class WheelManager : GameObjectSystem } private void UpdateEngine() { - if ( !Game.IsPlaying ) + if ( !Game.IsPlaying || Scene.IsEditor ) return; //Stopwatch sw = Stopwatch.StartNew(); diff --git a/Code/Car/VeloXCar.ABS.cs b/Code/Car/VeloXCar.ABS.cs new file mode 100644 index 0000000..5306d71 --- /dev/null +++ b/Code/Car/VeloXCar.ABS.cs @@ -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; + } + + } + } +} diff --git a/Code/Car/VeloXCar.ESC.cs b/Code/Car/VeloXCar.ESC.cs new file mode 100644 index 0000000..084ffdd --- /dev/null +++ b/Code/Car/VeloXCar.ESC.cs @@ -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; + } + + } + } +} diff --git a/Code/Car/VeloXCar.Steering.cs b/Code/Car/VeloXCar.Steering.cs index 5671228..602fefa 100644 --- a/Code/Car/VeloXCar.Steering.cs +++ b/Code/Car/VeloXCar.Steering.cs @@ -33,7 +33,7 @@ public partial class VeloXCar float targetSteerAngle = inputSteer * MaxSteerAngle; 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 ); diff --git a/Code/Car/VeloXCar.cs b/Code/Car/VeloXCar.cs index d2f7dfc..212dd5d 100644 --- a/Code/Car/VeloXCar.cs +++ b/Code/Car/VeloXCar.cs @@ -8,6 +8,15 @@ namespace VeloX; public partial class VeloXCar : VeloXBase { + protected override void FixedUpdate() + { + + UpdateInput(); + PhysicsSimulate(); + UpdateABS(); + UpdateESC(); + } + protected override void OnFixedUpdate() { if ( IsProxy )