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,
"D": 0.833,
"E": 0.992,
"D": 0.933,
"E": 0.971,
"__references": [],
"__version": 0
}

View File

@ -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

View File

@ -59,7 +59,7 @@ public abstract partial class VeloXBase
/// Throttle axis.
/// For combined throttle/brake input use 'VerticalInput' instead.
/// </summary>
[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.
/// </summary>
[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;

View File

@ -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 );
}

View File

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

View File

@ -34,7 +34,7 @@ public partial class VeloXWheel
/// </summary>
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;

View File

@ -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();

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;
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 );

View File

@ -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 )