Skip to content

Commit 9249e6c

Browse files
authored
Merge pull request #422 from peternewell/advanced-couplers#2
Change alignment of couplers as they travel through curve. https://blueprints.launchpad.net/or/+spec/advanced-coupler
2 parents f5103b4 + a939764 commit 9249e6c

File tree

3 files changed

+479
-212
lines changed

3 files changed

+479
-212
lines changed

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

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,6 @@ public virtual void physicsUpdate(float elapsedClockSeconds)
17221722
massKg += car.MassKG;
17231723
//TODO: next code line has been modified to flip trainset physics in order to get viewing direction coincident with loco direction when using rear cab.
17241724
// To achieve the same result with other means, without flipping trainset physics, the line should be changed as follows:
1725-
// if (car.Flipped)
17261725
if (car.Flipped ^ (car.IsDriveable && car.Train.IsActualPlayerTrain && ((MSTSLocomotive)car).UsingRearCab))
17271726
{
17281727
car.TotalForceN = -car.TotalForceN;
@@ -5417,10 +5416,6 @@ public void UpdateCouplerSlack(float elapsedTime)
54175416
// update coupler slack distance
54185417
TrainCar car = Cars[i];
54195418

5420-
// Initialise individual car coupler slack values
5421-
car.RearCouplerSlackM = 0;
5422-
car.FrontCouplerSlackM = 0;
5423-
54245419
// Calculate coupler slack - this should be the full amount for both couplers
54255420
car.CouplerSlackM += (car.SpeedMpS - Cars[i + 1].SpeedMpS) * elapsedTime;
54265421

@@ -5446,13 +5441,13 @@ public void UpdateCouplerSlack(float elapsedTime)
54465441
car.CouplerSlackM = max;
54475442
}
54485443

5449-
// Proportion coupler slack across front and rear couplers of this car, and the following car
5444+
// Proportion coupler slack across the rear coupler of this car, and the front coupler of the following car
54505445
car.RearCouplerSlackM = car.CouplerSlackM / AdvancedCouplerDuplicationFactor;
5451-
car.FrontCouplerSlackM = Cars[i + 1].CouplerSlackM / AdvancedCouplerDuplicationFactor;
5446+
Cars[i + 1].FrontCouplerSlackM = car.CouplerSlackM / AdvancedCouplerDuplicationFactor;
54525447

54535448
// Check to see if coupler is opened or closed - only closed or opened couplers have been specified
5454-
// It is assumed that the front coupler on first car will always be opened, and so will coupler on last car. All others on the train will be coupled
5455-
if (i == 0)
5449+
// It is assumed that the front coupler on first car will always be opened, and so will the coupler on last car. All others on the train will be coupled
5450+
if (i == 0) // first car
54565451
{
54575452
if (car.FrontCouplerOpenFitted)
54585453
{
@@ -5469,8 +5464,8 @@ public void UpdateCouplerSlack(float elapsedTime)
54695464
car.FrontCouplerOpen = false;
54705465
}
54715466

5472-
5473-
if (i == Cars.Count - 2)
5467+
// Set up coupler information for last car
5468+
if (i == Cars.Count - 2) // 2nd last car in count, but set up last car, ie i+1
54745469
{
54755470

54765471
if (Cars[i + 1].RearCouplerOpenFitted)
@@ -5549,8 +5544,48 @@ public void UpdateCouplerSlack(float elapsedTime)
55495544
}
55505545

55515546
}
5547+
5548+
int j = 0;
5549+
55525550
foreach (TrainCar car in Cars)
5551+
{
55535552
car.DistanceM += Math.Abs(car.SpeedMpS * elapsedTime);
5553+
5554+
// Identify links to cars ahead and behind for use when animating couplers
5555+
if (j == 0) // typically the locomotive
5556+
{
5557+
car.CarAhead = null;
5558+
if (Cars.Count > j) // if not a single loco
5559+
{
5560+
if (j < Cars.Count - 1) // if > then or =, this is the last car, and hence no further cars behind it
5561+
{
5562+
car.CarBehind = Cars[j + 1];
5563+
}
5564+
else
5565+
{
5566+
car.CarBehind = null;
5567+
}
5568+
}
5569+
else // if a single loco
5570+
{
5571+
car.CarBehind = null;
5572+
}
5573+
}
5574+
else if (j == Cars.Count - 1) // last car in train
5575+
{
5576+
Cars[j].CarAhead = Cars[j - 1];
5577+
Cars[j].CarBehind = null;
5578+
}
5579+
else // Set up coupler information for cars between first and last car
5580+
{
5581+
Cars[j].CarAhead = Cars[j - 1];
5582+
Cars[j].CarBehind = Cars[j + 1];
5583+
5584+
}
5585+
5586+
j = j + 1;
5587+
5588+
}
55545589
}
55555590

55565591
//================================================================================================//

0 commit comments

Comments
 (0)