# Handling train tracks in game

This topic is 2561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi!

I'm doing little 2d tile-based game with trains. I want to have map with train tracks and player would have to steer trains in branchings, or have ability to stop them on some of the tracks to avoid crashes of those trains. I wonder how to handle programming part of this, while I have to draw multiple linked cars, determine position of each car depending to track, and handle tracks. Any ideas?

I hope, I putted this topic in proper section.

Thanks for any help!

##### Share on other sites
Cars follow the engine. Every time the engine moves, store its position/facing in a list. Every car in the train then just follows a number of steps behind in that list.

Having the engine follow round the tiles can be done with a lookup table.

For each tile type, for each direction of entry (up,down,left,right), you can have a lists of how to move the engine round the tile. Each tick, the engine gets the next entry off the list, and moves there (feeding into the rest of the train via its list).

To change a set of points, change the tile for different one.

The beauty of this is that it makes it really easy to have all sorts of variations -- corners, crossovers, points, 3-way switches, wobbly track, S-curves... all sorts of things.

Thanks

##### Share on other sites
The tiles define pieces of the tracks. Tiles for X crossing provides 2 independent pieces. Tiles like track switches (AFAIK the correct name, but I'm not quite sure) provide several possibilities from which just one is current. However, the in summary the tracks are given as curves. This can be implemented by attaching (and switching where necessary) curves to the tiles, so that a sequence of curves defines a track.

Now assume that a locomotive and the wagons have a length and 2 points of undercarriages defined to lie inside the length on the longitudinal axis of the respective vehicle. E.g. it shows a length of 5m with the 2 undercarriages at 1m and 4m. The "1m point" of the locomotive is known to currently be placed at a specific location La on the track, and the direction of the train on the track is also known. Then the other point is 4m-1m=3m away from the location La in direction of the end of the train. So step back on the track to find the location Lb that is (on a straight line) 3m away from La. After determining this location, you have a position as well as a direction to render the image of the locomotive.

Similarly, a wagon with length of 5m and undercarriages at 1m and 4m. The distance from the 2nd undercarriage of the locomotive to the 1st one of the wagon is (5m-4m)+1m=2m. Hence step back from L2 another straight 2m distance on the track to get location W1a, and from there another 3m to determine W1b. You again have a location and a direction for rendering, this time the 1st wagon. Continue so to render the entire train.

A location on the track may be given as the distance from the beginning of the curve that defines the track on a given tile. Hence the location of e.g. the locomotive is something like "N length units on curve M of tile T", and the trains direction is either normal or reverse the direction of the curve. (Please notice that directions may vary from tile to tile.)

##### Share on other sites
The mechanics of this game sound very similar to a game i am working on (see my signature). I would generally do this the same way Katie recommends, however what would happen if you change the points when the train is half way across them, will it derail?
The mechanics of that could be interesting.

If you want any help or advice on games of this nature with mechanics such as this just drop me a line and i will be happy to impart advice

##### Share on other sites
I think it would work better if, given one car is connected to a pulling car (could be the engine), that the center of the car is placed on the curve (or path) of rail that its underlying tile defines, half its carriage length away from the rear of the earlier car. With the front oriented to the rear of the earlier car.

At detatch, simply inherit the velocity of the car before, but don't lock to drive half a car length from the earlier car no more.

Does it make sense? It saves memory (no chain of nodes to follow), and easy to extend train physics onto. Also the orientation is fair, and cars can have any length as long as their wheels are...

Oh well, I'm just rambling. That weird center wheel thing could easily be solved, though.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631740
• Total Posts
3001962
×