cleanup code
This commit is contained in:
parent
f0f89ff947
commit
55a178e8c5
@ -151,15 +151,6 @@ public partial class VeloXWheel : Component
|
|||||||
|
|
||||||
var vel = vehicle.Body.GetVelocityAtPoint( contactPos );
|
var vel = vehicle.Body.GetVelocityAtPoint( contactPos );
|
||||||
|
|
||||||
//var vel = vehicle.Body.GetVelocityAtPoint( pos );
|
|
||||||
|
|
||||||
if ( !IsOnGround )
|
|
||||||
{
|
|
||||||
forwardFriction = new Friction();
|
|
||||||
sideFriction = new Friction();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var offset = maxLen - (fraction * maxLen);
|
var offset = maxLen - (fraction * maxLen);
|
||||||
var springForce = (offset * SpringStrength);
|
var springForce = (offset * SpringStrength);
|
||||||
var damperForce = (lastSpringOffset - offset) * SpringDamper;
|
var damperForce = (lastSpringOffset - offset) * SpringDamper;
|
||||||
@ -194,71 +185,65 @@ public partial class VeloXWheel : Component
|
|||||||
|
|
||||||
load = Math.Max( force.z, 0 ).InchToMeter();
|
load = Math.Max( force.z, 0 ).InchToMeter();
|
||||||
|
|
||||||
if ( IsOnGround )
|
|
||||||
{
|
|
||||||
float forwardSpeed = vel.Dot( forward ).InchToMeter();
|
float forwardSpeed = vel.Dot( forward ).InchToMeter();
|
||||||
float sideSpeed = vel.Dot( right ).InchToMeter();
|
float sideSpeed = vel.Dot( right ).InchToMeter();
|
||||||
|
|
||||||
float camber_rad = CamberAngle.DegreeToRadian();
|
float camber_rad = CamberAngle.DegreeToRadian();
|
||||||
float R = Radius.InchToMeter();
|
float R = Radius.InchToMeter();
|
||||||
|
|
||||||
//TirePreset.ComputeState(
|
|
||||||
// load,
|
|
||||||
// angularVelocity,
|
|
||||||
// forwardSpeed,
|
|
||||||
// sideSpeed,
|
|
||||||
// camber_rad,
|
|
||||||
// R,
|
|
||||||
// Inertia,
|
|
||||||
// out var tireState
|
|
||||||
//);
|
|
||||||
|
|
||||||
float linearSpeed = angularVelocity * Radius.InchToMeter();
|
float linearSpeed = angularVelocity * Radius.InchToMeter();
|
||||||
|
|
||||||
float F_roll = TirePreset.GetRollingResistance( linearSpeed, 1.0f );
|
float F_roll = TirePreset.GetRollingResistance( linearSpeed, 1.0f );
|
||||||
F_roll *= -Math.Clamp( linearSpeed * 0.25f, -1, 1 );
|
F_roll *= -Math.Clamp( linearSpeed * 0.25f, -1, 1 );
|
||||||
|
|
||||||
//float Fx_total = tireState.fx + F_roll;
|
|
||||||
|
|
||||||
float T_brake = Brake * BrakePowerMax;
|
float T_brake = Brake * BrakePowerMax;
|
||||||
|
T_brake *= -Math.Clamp( linearSpeed * 0.25f, -1, 1 );
|
||||||
|
|
||||||
if ( angularVelocity > 0 ) T_brake = -T_brake;
|
float Winit = angularVelocity;
|
||||||
else T_brake = angularVelocity < 0 ? T_brake : -MathF.Sign( Torque ) * T_brake;
|
|
||||||
//float totalTorque = Torque + tireState.fx;
|
angularVelocity += F_roll * 9.80665f / Inertia * Time.Delta;
|
||||||
|
|
||||||
angularVelocity += Torque / Inertia * Time.Delta;
|
angularVelocity += Torque / Inertia * Time.Delta;
|
||||||
|
|
||||||
|
|
||||||
angularVelocity += T_brake / Inertia * Time.Delta;
|
angularVelocity += T_brake / Inertia * Time.Delta;
|
||||||
angularVelocity += F_roll * 9.80665f / Inertia * Time.Delta;
|
|
||||||
|
if ( IsOnGround )
|
||||||
|
{
|
||||||
TirePreset.ComputeSlip( forwardSpeed, sideSpeed, angularVelocity, R, out var slip, out var slip_ang );
|
TirePreset.ComputeSlip( forwardSpeed, sideSpeed, angularVelocity, R, out var slip, out var slip_ang );
|
||||||
var fx = TirePreset.Pacejka.PacejkaFx( slip, load, 1, out var maxTorque );
|
var fx = TirePreset.Pacejka.PacejkaFx( slip, load, 1, out var maxTorque );
|
||||||
var fy = TirePreset.Pacejka.PacejkaFy( slip_ang * load, load, camber_rad, 1, out var _ );
|
var fy = TirePreset.Pacejka.PacejkaFy( slip_ang * load, load, camber_rad, 1, out var _ );
|
||||||
|
|
||||||
maxTorque *= R;
|
maxTorque *= R;
|
||||||
var errorTorque = (angularVelocity - forwardSpeed / R) * Inertia / Time.Delta;
|
var errorTorque = (angularVelocity - forwardSpeed / R) * BaseInertia / Time.Delta;
|
||||||
|
|
||||||
var surfaceTorque = Math.Clamp( errorTorque, -maxTorque, maxTorque );
|
var surfaceTorque = Math.Clamp( errorTorque, -maxTorque, maxTorque );
|
||||||
|
|
||||||
angularVelocity -= surfaceTorque / Inertia * Time.Delta;
|
angularVelocity -= surfaceTorque / Inertia * Time.Delta;
|
||||||
|
|
||||||
forwardFriction = new Friction()
|
|
||||||
{
|
|
||||||
Slip = slip,
|
|
||||||
Force = -fx,
|
|
||||||
Speed = forwardSpeed
|
|
||||||
};
|
|
||||||
|
|
||||||
sideFriction = new Friction()
|
float deltaOmegaTorque = (angularVelocity - Winit) * Inertia / Time.Delta;
|
||||||
{
|
|
||||||
Slip = slip_ang,
|
|
||||||
Force = fy,
|
|
||||||
Speed = sideSpeed
|
|
||||||
};
|
|
||||||
|
|
||||||
Vector3 frictionForce = forward * forwardFriction.Force + right * sideFriction.Force;
|
CounterTorque = -surfaceTorque - deltaOmegaTorque;
|
||||||
|
forwardFriction.Slip = slip;
|
||||||
|
forwardFriction.Force = -fx;
|
||||||
|
forwardFriction.Speed = forwardSpeed;
|
||||||
|
sideFriction.Slip = slip_ang;
|
||||||
|
sideFriction.Force = fy;
|
||||||
|
sideFriction.Speed = sideSpeed;
|
||||||
|
|
||||||
vehicle.Body.ApplyForceAt( pos, force / Time.Delta * ProjectSettings.Physics.SubSteps );
|
vehicle.Body.ApplyForceAt( pos, force / Time.Delta * ProjectSettings.Physics.SubSteps );
|
||||||
vehicle.Body.ApplyForceAt( pos, frictionForce * ProjectSettings.Physics.SubSteps );
|
|
||||||
|
|
||||||
|
Vector3 frictionForce = forward * forwardFriction.Force + right * sideFriction.Force;
|
||||||
|
vehicle.Body.ApplyForceAt( pos, frictionForce * ProjectSettings.Physics.SubSteps );
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
forwardFriction = new();
|
||||||
|
sideFriction = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -298,8 +283,8 @@ public partial class VeloXWheel : Component
|
|||||||
// debug
|
// debug
|
||||||
protected override void OnUpdate()
|
protected override void OnUpdate()
|
||||||
{
|
{
|
||||||
DebugOverlay.Normal( contactPos, forward * forwardFriction.Force / 1000f, Color.Red, overlay: true );
|
//DebugOverlay.Normal( contactPos, forward * forwardFriction.Force / 1000f, Color.Red, overlay: true );
|
||||||
DebugOverlay.Normal( contactPos, right * sideFriction.Force / 1000f, Color.Green, overlay: true );
|
//DebugOverlay.Normal( contactPos, right * sideFriction.Force / 1000f, Color.Green, overlay: true );
|
||||||
DebugOverlay.Normal( contactPos, up * force / 1000f, Color.Blue, overlay: true );
|
//DebugOverlay.Normal( contactPos, up * force / 1000f, Color.Blue, overlay: true );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user