Skip to content

Commit 39abd77

Browse files
committed
style: Format physics wind code
1 parent 1e6d676 commit 39abd77

File tree

2 files changed

+86
-93
lines changed

2 files changed

+86
-93
lines changed

Source/Orts.Simulation/Simulation/Physics/Train.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,42 +2111,41 @@ public virtual void physicsUpdate(float elapsedClockSeconds)
21112111
public void UpdateWindComponents()
21122112
{
21132113
// Gets wind direction and speed, and determines HUD display values for the train as a whole.
2114-
//These will be representative of the train whilst it is on a straight track, but each wagon will vary when going around a curve.
2114+
// These will be representative of the train whilst it is on a straight track, but each wagon will vary when going around a curve.
21152115
// Note both train and wind direction will be positive between 0 (north) and 180 (south) through east, and negative between 0 (north) and 180 (south) through west
21162116
// Wind and train direction to be converted to an angle between 0 and 360 deg.
2117-
// Calculate Wind speed and direction, and train direction
2118-
// Update the value of the Wind Speed and Direction for the train
2119-
PhysicsWindDirectionDeg = MathHelper.ToDegrees(Simulator.Weather.WindDirectionRad);
2120-
PhysicsWindSpeedMpS = Simulator.Weather.WindSpeedMpS.Length();
2121-
float TrainSpeedMpS = Math.Abs(SpeedMpS);
2122-
2123-
// If a westerly direction (ie -ve) convert to an angle between 0 and 360
2124-
if (PhysicsWindDirectionDeg < 0)
2125-
PhysicsWindDirectionDeg += 360;
2126-
2127-
if (PhysicsTrainLocoDirectionDeg < 0)
2128-
PhysicsTrainLocoDirectionDeg += 360;
2129-
2130-
// calculate angle between train and eind direction
2131-
if (PhysicsWindDirectionDeg > PhysicsTrainLocoDirectionDeg)
2132-
ResultantWindComponentDeg = PhysicsWindDirectionDeg - PhysicsTrainLocoDirectionDeg;
2133-
else if (PhysicsTrainLocoDirectionDeg > PhysicsWindDirectionDeg)
2134-
ResultantWindComponentDeg = PhysicsTrainLocoDirectionDeg - PhysicsWindDirectionDeg;
2135-
else
2136-
ResultantWindComponentDeg = 0.0f;
2137-
2138-
// Correct wind direction if it is greater then 360 deg, then correct to a value less then 360
2139-
if (Math.Abs(ResultantWindComponentDeg) > 360)
2140-
ResultantWindComponentDeg = ResultantWindComponentDeg - 360.0f;
2117+
// Calculate Wind speed and direction, and train direction
2118+
// Update the value of the Wind Speed and Direction for the train
2119+
PhysicsWindDirectionDeg = MathHelper.ToDegrees(Simulator.Weather.WindDirectionRad);
2120+
PhysicsWindSpeedMpS = Simulator.Weather.WindSpeedMpS.Length();
2121+
var speedMpS = Math.Abs(SpeedMpS);
2122+
2123+
// If a westerly direction (ie -ve) convert to an angle between 0 and 360
2124+
if (PhysicsWindDirectionDeg < 0)
2125+
PhysicsWindDirectionDeg += 360;
2126+
2127+
if (PhysicsTrainLocoDirectionDeg < 0)
2128+
PhysicsTrainLocoDirectionDeg += 360;
2129+
2130+
// Calculate angle between train and wind direction
2131+
if (PhysicsWindDirectionDeg > PhysicsTrainLocoDirectionDeg)
2132+
ResultantWindComponentDeg = PhysicsWindDirectionDeg - PhysicsTrainLocoDirectionDeg;
2133+
else if (PhysicsTrainLocoDirectionDeg > PhysicsWindDirectionDeg)
2134+
ResultantWindComponentDeg = PhysicsTrainLocoDirectionDeg - PhysicsWindDirectionDeg;
2135+
else
2136+
ResultantWindComponentDeg = 0.0f;
21412137

2142-
// Wind angle should be kept between 0 and 180 the formulas do not cope with angles > 180. If angle > 180, denotes wind of "other" side of train
2143-
if (ResultantWindComponentDeg > 180)
2144-
ResultantWindComponentDeg = 360 - ResultantWindComponentDeg;
2138+
// Correct wind direction if it is greater then 360 deg, then correct to a value less then 360
2139+
if (Math.Abs(ResultantWindComponentDeg) > 360)
2140+
ResultantWindComponentDeg = ResultantWindComponentDeg - 360.0f;
21452141

2146-
float WindAngleRad = MathHelper.ToRadians(ResultantWindComponentDeg);
2142+
// Wind angle should be kept between 0 and 180 the formulas do not cope with angles > 180. If angle > 180, denotes wind of "other" side of train
2143+
if (ResultantWindComponentDeg > 180)
2144+
ResultantWindComponentDeg = 360 - ResultantWindComponentDeg;
21472145

2148-
WindResultantSpeedMpS = (float)Math.Sqrt(TrainSpeedMpS * TrainSpeedMpS + PhysicsWindSpeedMpS * PhysicsWindSpeedMpS + 2.0f * TrainSpeedMpS * PhysicsWindSpeedMpS * (float)Math.Cos(WindAngleRad));
2149-
}
2146+
var windAngleRad = MathHelper.ToRadians(ResultantWindComponentDeg);
2147+
WindResultantSpeedMpS = (float)Math.Sqrt(speedMpS * speedMpS + PhysicsWindSpeedMpS * PhysicsWindSpeedMpS + 2.0f * speedMpS * PhysicsWindSpeedMpS * (float)Math.Cos(windAngleRad));
2148+
}
21502149

21512150

21522151
//================================================================================================//

Source/Orts.Simulation/Simulation/RollingStocks/MSTSWagon.cs

Lines changed: 56 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ public class MSTSWagon : TrainCar
127127
float TenderWagonMaxCoalMassKG;
128128
float TenderWagonMaxWaterMassKG;
129129

130-
// Wind Impacts
131-
float WagonDirectionDeg;
132-
float WagonResultantWindComponentDeg;
133-
float WagonWindResultantSpeedMpS;
134-
135130
protected float FrictionC1; // MSTS Friction parameters
136131
protected float FrictionE1; // MSTS Friction parameters
137132
protected float FrictionV2; // MSTS Friction parameters
@@ -2870,135 +2865,134 @@ private void UpdateWheelBearingTemperature(float elapsedClockSeconds)
28702865
private void UpdateWindForce()
28712866
{
28722867
// Calculate compensation for wind
2873-
// There are two components due to wind -
2868+
// There are two components due to wind -
28742869
// Drag, impact of wind on train, will increase resistance when head on, will decrease resistance when acting as a tailwind.
28752870
// Lateral resistance - due to wheel flange being pushed against rail due to side wind.
28762871
// Calculation based upon information provided in AREA 1942 Proceedings - https://archive.org/details/proceedingsofann431942amer - pg 56
28772872

2878-
if (!CarTunnelData.FrontPositionBeyondStartOfTunnel.HasValue && AbsSpeedMpS > 2.2352) // Only calculate wind resistance if option selected in options menu, and not in a tunnel, and speed is sufficient for wind effects (>5mph)
2873+
// Only calculate wind resistance if option selected in options menu, and not in a tunnel, and speed is sufficient for wind effects (>5mph)
2874+
if (!CarTunnelData.FrontPositionBeyondStartOfTunnel.HasValue && AbsSpeedMpS > 2.2352)
28792875
{
2880-
28812876
// Wagon Direction
2882-
float direction = (float)Math.Atan2(WorldPosition.XNAMatrix.M13, WorldPosition.XNAMatrix.M11);
2883-
WagonDirectionDeg = MathHelper.ToDegrees((float)direction);
2877+
var directionRad = (float)Math.Atan2(WorldPosition.XNAMatrix.M13, WorldPosition.XNAMatrix.M11);
2878+
var directionDeg = MathHelper.ToDegrees(directionRad);
28842879

2885-
// If car is flipped, then the car's direction will be reversed by 180 compared to the rest of the train, and thus for calculation purposes only,
2880+
// If car is flipped, then the car's direction will be reversed by 180 compared to the rest of the train, and thus for calculation purposes only,
28862881
// it is necessary to reverse the "assumed" direction of the car back again. This shouldn't impact the visual appearance of the car.
28872882
if (Flipped)
28882883
{
2889-
WagonDirectionDeg += 180.0f; // Reverse direction of car
2890-
if (WagonDirectionDeg > 360) // If this results in an angle greater then 360, then convert it back to an angle between 0 & 360.
2891-
{
2892-
WagonDirectionDeg -= 360;
2893-
}
2894-
}
2884+
// Reverse direction of car
2885+
directionDeg += 180.0f;
2886+
2887+
// If this results in an angle greater then 360, then convert it back to an angle between 0 & 360.
2888+
if (directionDeg > 360)
2889+
directionDeg -= 360;
2890+
}
28952891

28962892
// If a westerly direction (ie -ve) convert to an angle between 0 and 360
2897-
if (WagonDirectionDeg < 0)
2898-
WagonDirectionDeg += 360;
2893+
if (directionDeg < 0)
2894+
directionDeg += 360;
28992895

2900-
float TrainSpeedMpS = Math.Abs(SpeedMpS);
2901-
29022896
// Find angle between wind and direction of train
2903-
if (Train.PhysicsWindDirectionDeg > WagonDirectionDeg)
2904-
WagonResultantWindComponentDeg = Train.PhysicsWindDirectionDeg - WagonDirectionDeg;
2905-
else if (WagonDirectionDeg > Train.PhysicsWindDirectionDeg)
2906-
WagonResultantWindComponentDeg = WagonDirectionDeg - Train.PhysicsWindDirectionDeg;
2907-
else
2908-
WagonResultantWindComponentDeg = 0.0f;
2897+
var resultantWindComponentDeg = 0.0f;
2898+
if (Train.PhysicsWindDirectionDeg > directionDeg)
2899+
resultantWindComponentDeg = Train.PhysicsWindDirectionDeg - directionDeg;
2900+
else if (directionDeg > Train.PhysicsWindDirectionDeg)
2901+
resultantWindComponentDeg = directionDeg - Train.PhysicsWindDirectionDeg;
29092902

29102903
// Correct wind direction if it is greater then 360 deg, then correct to a value less then 360
2911-
if (Math.Abs(WagonResultantWindComponentDeg) > 360)
2912-
WagonResultantWindComponentDeg = WagonResultantWindComponentDeg - 360.0f;
2904+
if (Math.Abs(resultantWindComponentDeg) > 360)
2905+
resultantWindComponentDeg -= 360.0f;
29132906

29142907
// Wind angle should be kept between 0 and 180 the formulas do not cope with angles > 180. If angle > 180, denotes wind of "other" side of train
2915-
if (WagonResultantWindComponentDeg > 180)
2916-
WagonResultantWindComponentDeg = 360 - WagonResultantWindComponentDeg;
2908+
if (resultantWindComponentDeg > 180)
2909+
resultantWindComponentDeg = 360 - resultantWindComponentDeg;
29172910

2918-
float ResultantWindComponentRad = MathHelper.ToRadians(WagonResultantWindComponentDeg);
2911+
var resultantWindComponentRad = MathHelper.ToRadians(resultantWindComponentDeg);
29192912

29202913
// Find the resultand wind vector for the combination of wind and train speed
2921-
WagonWindResultantSpeedMpS = (float)Math.Sqrt(TrainSpeedMpS * TrainSpeedMpS + Train.PhysicsWindSpeedMpS * Train.PhysicsWindSpeedMpS + 2.0f * TrainSpeedMpS * Train.PhysicsWindSpeedMpS * (float)Math.Cos(ResultantWindComponentRad));
2914+
var windResultantSpeedMpS = (float)Math.Sqrt(AbsSpeedMpS * AbsSpeedMpS + Train.PhysicsWindSpeedMpS * Train.PhysicsWindSpeedMpS + 2.0f * AbsSpeedMpS * Train.PhysicsWindSpeedMpS * (float)Math.Cos(resultantWindComponentRad));
29222915

29232916
// Calculate Drag Resistance
2924-
// The drag resistance will be the difference between the STILL firction calculated using the standard Davies equation,
2917+
// The drag resistance will be the difference between the STILL firction calculated using the standard Davies equation,
29252918
// and that produced using the wind resultant speed (combination of wind speed and train speed)
2926-
float TempStillDragResistanceForceN = AbsSpeedMpS * AbsSpeedMpS * DavisCNSSpMM;
2927-
float TempCombinedDragResistanceForceN = WagonWindResultantSpeedMpS * WagonWindResultantSpeedMpS * DavisCNSSpMM; // R3 of Davis formula taking into account wind
2928-
float WindDragResistanceForceN = 0.0f;
2919+
var tempStillDragResistanceForceN = AbsSpeedMpS * AbsSpeedMpS * DavisCNSSpMM;
2920+
var tempCombinedDragResistanceForceN = windResultantSpeedMpS * windResultantSpeedMpS * DavisCNSSpMM; // R3 of Davis formula taking into account wind
2921+
float windDragResistanceForceN;
29292922

29302923
// Find the difference between the Still and combined resistances
29312924
// This difference will be added or subtracted from the overall friction force depending upon the estimated wind direction.
2932-
// Wind typically headon to train - increase resistance - +ve differential
2933-
if (TempCombinedDragResistanceForceN > TempStillDragResistanceForceN)
2925+
if (tempCombinedDragResistanceForceN > tempStillDragResistanceForceN)
29342926
{
2935-
WindDragResistanceForceN = TempCombinedDragResistanceForceN - TempStillDragResistanceForceN;
2927+
// Wind typically headon to train - increase resistance - +ve differential
2928+
windDragResistanceForceN = tempCombinedDragResistanceForceN - tempStillDragResistanceForceN;
29362929
}
2937-
else // wind typically following train - reduce resistance - -ve differential
2930+
else
29382931
{
2939-
WindDragResistanceForceN = TempStillDragResistanceForceN - TempCombinedDragResistanceForceN;
2940-
WindDragResistanceForceN *= -1.0f; // Convert to negative number to allow subtraction from ForceN
2932+
// Wind typically following train - reduce resistance - -ve differential
2933+
windDragResistanceForceN = tempStillDragResistanceForceN - tempCombinedDragResistanceForceN;
2934+
windDragResistanceForceN *= -1.0f; // Convert to negative number to allow subtraction from ForceN
29412935
}
29422936

29432937
// Calculate Lateral Resistance
29442938

29452939
// Calculate lateral resistance due to wind
29462940
// Resistance is due to the wheel flanges being pushed further onto rails when a cross wind is experienced by a train
2947-
float A = Train.PhysicsWindSpeedMpS / AbsSpeedMpS;
2948-
float C = (float)Math.Sqrt((1 + (A * A) + 2.0f * A * Math.Cos(ResultantWindComponentRad)));
2949-
float WindConstant = 8.25f;
2950-
float TrainSpeedMpH = Me.ToMi(pS.TopH(AbsSpeedMpS));
2951-
float WindSpeedMpH = Me.ToMi(pS.TopH(Train.PhysicsWindSpeedMpS));
2941+
var a = Train.PhysicsWindSpeedMpS / AbsSpeedMpS;
2942+
var c = (float)Math.Sqrt((1 + (a * a) + 2.0f * a * Math.Cos(resultantWindComponentRad)));
2943+
var windConstant = 8.25f;
2944+
var speedMpH = Me.ToMi(pS.TopH(AbsSpeedMpS));
29522945

2953-
float WagonFrontalAreaFt2 = Me2.ToFt2(WagonFrontalAreaM2);
2946+
var wagonFrontalAreaFt2 = Me2.ToFt2(WagonFrontalAreaM2);
29542947

2955-
LateralWindForceN = N.FromLbf(WindConstant * A * (float)Math.Sin(ResultantWindComponentRad) * DavisDragConstant * WagonFrontalAreaFt2 * TrainSpeedMpH * TrainSpeedMpH * C);
2948+
LateralWindForceN = N.FromLbf(windConstant * a * (float)Math.Sin(resultantWindComponentRad) * DavisDragConstant * wagonFrontalAreaFt2 * speedMpH * speedMpH * c);
29562949

2957-
float LateralWindResistanceForceN = N.FromLbf(WindConstant * A * (float)Math.Sin(ResultantWindComponentRad) * DavisDragConstant * WagonFrontalAreaFt2 * TrainSpeedMpH * TrainSpeedMpH * C * Train.WagonCoefficientFriction);
2950+
var lateralWindResistanceForceN = N.FromLbf(windConstant * a * (float)Math.Sin(resultantWindComponentRad) * DavisDragConstant * wagonFrontalAreaFt2 * speedMpH * speedMpH * c * Train.WagonCoefficientFriction);
29582951

29592952
// if this car is a locomotive, but not the lead one then recalculate the resistance with lower C value as drag will not be as high on trailing locomotives
29602953
if (WagonType == WagonTypes.Engine && Train.LeadLocomotive != this)
29612954
{
2962-
LateralWindResistanceForceN *= TrailLocoResistanceFactor;
2955+
lateralWindResistanceForceN *= TrailLocoResistanceFactor;
29632956
}
29642957

29652958
// Test to identify whether a tender is attached to the leading engine, if not then the resistance should also be derated as for the locomotive
2966-
bool IsLeadTender = false;
2959+
var isLeadTender = false;
29672960
if (WagonType == WagonTypes.Tender)
29682961
{
2969-
bool PrevCarLead = false;
2962+
var prevCarLead = false;
29702963
foreach (var car in Train.Cars)
29712964
{
29722965
// If this car is a tender and the previous car is the lead locomotive then set the flag so that resistance will be reduced
2973-
if (car == this && PrevCarLead)
2966+
if (car == this && prevCarLead)
29742967
{
2975-
IsLeadTender = true;
2968+
isLeadTender = true;
29762969
break; // If the tender has been identified then break out of the loop, otherwise keep going until whole train is done.
29772970
}
2971+
29782972
// Identify whether car is a lead locomotive or not. This is kept for when the next iteration (next car) is checked.
29792973
if (Train.LeadLocomotive == car)
29802974
{
2981-
PrevCarLead = true;
2975+
prevCarLead = true;
29822976
}
29832977
else
29842978
{
2985-
PrevCarLead = false;
2979+
prevCarLead = false;
29862980
}
29872981
}
29882982

29892983
// If tender is coupled to a trailing locomotive then reduce resistance
2990-
if (!IsLeadTender)
2984+
if (!isLeadTender)
29912985
{
2992-
LateralWindResistanceForceN *= TrailLocoResistanceFactor;
2986+
lateralWindResistanceForceN *= TrailLocoResistanceFactor;
29932987
}
29942988
}
2995-
WindForceN = LateralWindResistanceForceN + WindDragResistanceForceN;
2989+
2990+
WindForceN = lateralWindResistanceForceN + windDragResistanceForceN;
29962991
}
29972992
else
29982993
{
29992994
WindForceN = 0.0f; // Set to zero if wind resistance is not to be calculated
30002995
}
3001-
30022996
}
30032997

30042998
private void UpdateTenderLoad()

0 commit comments

Comments
 (0)