I have been working on implementing trains with Bullet physics before and I am going to try to bring a similar implementation in OT. Since OT already provides a beautiful world so running trains through it will be just awesome.
My physics implementation in Bullet was here :
Code:
https://code.google.com/p/surface-physics/source/browse/#svn%2Ftrunk%2FTestRig%2FTrainBasically I represented a track as a list of curves, curves were always either straight lines or circular sections. This may not be true in the real world, but should do as a low overhead start for miles and miles of tracks
For more realistic railroad modelling I decided to take a cube and make it float above a point on a plane first. For this I simply use a spring force with the force increasing linearly as the cube gets closer to the surface. So this is good enough to make it float. Since the railroad car has also got to be more or less parallel to the underlying track, I actually apply 2 "up" forces, one each at the center of the axle of the front and rear wheels to keep the bottom surface parallel to the track.
Then there is the rotation of the cars around the y-axis(the "UP" axis). The cars again remain parallel and facing forward through the use of a counter torque, if the car "swings" away from the tracks. This is based on the angle of deviation from the current direction of the track, with a torque applied to try and make this angle 0 always. It was easy to get this angle for straight segments of tracks. For curved segments, the radial vector connecting the center of the curve(curved tracks are always circular) with the center of the car is useful.
I experimented with tracks which gradually curve from 0 curvature(straight segment) to maximum curvature to get around sharp bends. The gradual curvature of track is whats used in real railroad tracks to prevent sudden changes in directions at high speed which would cause the cars to bang hard against the rails. However its not straight forward to get the radial vector at any point along a curve which is not circular. So I thought a gradual bending curve can also be approximated by a series of circular segments whose normals match at the ends. Thats the plan as of now. An automatic track layer would be great to do this matching !
The cars are kept on the track using a simple spring-like force on them of the form kx where x is the deviation of the center point of the axle for the front wheels. These forces try to push the wheels towards the center of the track from outside simulating the track's restraints on the wheels. k is considerably high.
Its easy to get the nearest point on a straight track , to the center point of the axles, and then apply a force towards the track to keep the cars on the rail trajectory. For curving tracks its even easier as a fixed radial distance has to be maintained from the center of the curve. So this is a kind of 'trajectory' constraint.
I really hardened the spring constants using values like 100000 to ensure the force was really great in case any of the cars deviated from the rails.
Now for the engine, the basic engine force is just a force applied on the center of the front most car. The toughest part to fix was the link forces between the cars which pull the car or slow them down.
The link forces are applied based on the distance between 2 cars. Every car apart from the engine, applies a force on itself proportional to its distance from the car ahead of it. This force is also applied on the car ahead as a pull, so the engine has to really pull hard to get a long line of cars moving.
The last thing to add was rail infrastructure in the form of junctions to allow track changes. Basically the train is diverted to a circular section thats touching a straight segment, when the junction is active but there are multiple cases. For example the junction must also work for cars rolling backwards etc, overlapping active junctions must not confuse the cars !!
With that out of the way, I added signals and a basic cab controller. The controller looks at the signal ahead of it on the same track and reaches a certain speed setpoint based on yellow or green. It applies brakes on red which is applied on all the cars.
The system is stable at high speeds and for curves there are speed limits. Sharp curves taken at high speeds cause the link force to ramp up suddenly to unmanageable levels so currently I have clamped the maximum link forces, that helped.
The curves segments currently are along the ground plane but can be adapted to follow any kind of terrain, the math needs to be figured out to keep the train constrained, thats all.
Then adding multiple cabs to the train and ensuring the entire thing moves properly.
Simulating static and rolling friction that requires the loco to correctly apply larger forces to get things rolling.
Applying up forces on all 4 ends of a cab to simulate the suspension.
Interesting times up ahead.