I was experimenting last week with handling the coupler distance between train cars and found something interesting.
In the picture above, you can clearly see the issue related to how a train car sits on a curved piece of track. The issue is that the linear distance between the trucks or wheels of the car is not at all the same as the curved distance between them. As the curve gets tighter and tighter, the difference in the distances gets greater and greater. Trains don’t really go around corners this tight but it does show the effect clearly.
My experiment was with the coupler distances. The couplers of real rail cars are mounted on the ends of the cars. This can’t be quickly simulated because the end of the car and the coupler pivot point might not be lined up with the center line of the track. I can know the position of the coupler on one car but the next car could sit on an S curve and the coupler position there can’t be known with simple geometry. There is no way to draw a circle that defines the possible locations of the coupler pivot on the next car and then determine the car location from that. The circle thing only works when the coupler pivot is guaranteed to be on the center line of the track. I see no way to do the calculations in a single step and running an algorithm that moves the car along the track until it is in the right spot is out of the question.
Anyhow, I had been using the truck pivot locations as the coupler pivot locations and using an appropriate coupler length to get the knuckle in the right spot when the cars are lined up on a straight line. This worked fine. Now for the interesting part, Instead of using a linear distance and some circle/circle intersection math to find the location of the second car pivot point, I switched to using curved distance. I guessed that the curved distance might actually make the cars behave a little more like real cars. This made the cars behave almost the same. The visual difference was smaller than I thought when watching cars on carious small curves.
Another interesting thing is that when I compute the length of the train, I take the lengths of all of the cars, coupler to coupler, and add them together. If the train is sitting on a bunch of small S curves, I would expect the distance on the track to be covered by the train to be greater than that straight line difference. I expect that because the wheels or trucks on the car allow the car to use the cord of a circle instead of following around it. This short cut should make the train appear longer in track distance than the actual length of the train. It does. It must. Interestingly, it doesn’t make enough difference to see it except in the most extreme circumstances. It certainly doesn’t make enough differences to screw up the simulation unless the track configurations is bizarre and otherwise unusuable.
Here is the result of an experiment I did:
The small green line at the pointy end of the train (the locomotive) should be visible where it touches the center line of the track. That’s because the train looks ahead from the back of the train to see when it should stop, not from the front of the train. In this case, the back of the train plus the train length is not the same as the actual train length around the insanely sharp curves. This causes the from of the train to be further along the track than the train thinks and the train doesn’t stop soon enough. If we were stopping to avoid hitting another train on the track, we would have hit it.
The couplers are not drawn in these pictures but you can assume that they stick out a little in front of the locomotive. The train stopped about one coupler length, the length of the coupler from the end of the car, past the green waypoint marker instead of stopping before it.
Here is a similar situation with only one extremely sharp curve. Notice how the train stopped before the waypoint in what looks like the exact location desired:
The effect is similar on a very broad curve with the entire train on the curve. The train appears to stop right where expected. The problem only shows up when every car is sitting on an unreasonably sharp curve.
I have no plan to change this. I don’t really want to add code to search ahead for waypoints from each location in the train that could be used as a stopping point. The player could have specified stopping with a set of cars over a waypoint and I would need to look at all of the upcoming waypoints to see which ones have cars specified in this way. I would then need to find the proper location in the train for the given car selection, taking into account the curves, and then see if the train is close enough to warrant slowing down.
On more appropriate curves, the error rate is minimal and does not affect game play. If I find a solution or even a kludge, to make this error become less significant, I’ll write about it. For now, I’m stuck with it.