Well I have got a basic train implementation running :http://www.youtube.com/watch?v=L1LoaMKagdk&feature=youtu.be
It has got curves,line change junctions, signals, a basic cab controller, engine speed variation and brakes and I am working on going up hills.
So I basically ditched the raycast vehicles linked together thing, because inpsite of the fact that I was able to control the wheels of the cars using a simple controller to turn them around sharp turns, there was an issues with linking multiple cars together. The point-to-point connectors in Bullet are just too springy for real railroad car weights. But its easy to make a set of light cars follow a trajectory, maybe a toy train example later
I used the railway car dimensions from my country : http://in.answers.yahoo.com/question/in ... 537AAtXY0C
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 perfectly 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.
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 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 easy 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 later perhaps
The cars are kept on the track using a simple spring force too of the form kx where x is the deviation of the center point of the axle for the front (and rear) wheels. 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.
This is the trajectory constraint discussed earlier. 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 through applyCentralForce(). The toughest part to fix which took me over 3 months 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. That took over a week to fix too. 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.
Thats about it, thanks for reading