Vehicles on grid

from Red Blob Games
17 Apr 2021

I want to try out vehicle simulation with:

  1. grid layout
  2. grid paths
  3. larger-than-one-tile objects
  4. in-between-grid movement
  5. acceleration and deceleration curves

This means I need to store positions that aren’t on a grid, but maybe reserve the grid blocks so that nobody else can take them. A 2⨉1 object may end up taking 3⨉1 tiles if it’s moving horizontally. But maybe even more if it’s moving along a curved path.

This page is primarily about the size and alignment.

 1  Size and alignment#

I’d like trucks and their containers to be sized to match the grid. Here was my initial thought:

1 teu2 teu

A 1 TEU truck would use two grid tiles and a 2 TEU truck would use three grid tiles.

I would like cargo storage to fit the grid, so I will fit 20⨉10 foot containers in a 20⨉20 grid tile. A normal TEU container is 20⨉8 but 20⨉10 works better here.

I can also make container ships line up with the grid.

How about trains? This is where things get trickier.

If containers are 1 tile wide, then train cars are 2 tiles wide, but the gap between train cars means the cars don’t align with the grid anymore.

To fix this I can make containers 7/8 tile wide instead of 1 tile wide:

Now the trains are aligned with the grid but the containers are no longer sized with the grid. This means container ships don’t quite line up anymore:

I would need to add gaps to the container ships:

This isn’t too bad, but it doesn’t look as nice as the gapless version. I’d also need gaps in the storage areas:

That isn’t too bad either, but I think the gapless version looks better.

Either I make the containers smaller to handle the gap between train cars (but then I don’t like the gaps in storage and ships), or I deal with train car gaps. I decided to make the train cars longer so that they line up with the grid again. In real life, these “well cars”[1] are 68ft long to store a 53 ft container, so they have 15 ft of extra length. If I shrink those cars to fit only 40 ft containers, that’d be 55ft long. Making the gap between cars[2] 5 ft would give us 1-tile-wide containers that align with the grid:

Wikipedia says a “flat car”[3] might be 92 ft long to hold four 20ft containers, so adding an 8 ft gap would give us:

In real life, the well car is double-stacked so it can store 4 TEU in 55ft while the flat car is single-stacked so it can store 4 TEU in 92ft. I’m not sure what I want to do about stacking. Container ships and container storage are typically stacked, but that makes it much harder to see everything on the screen. See the Scale section for why I’m not necessarily following real scales.

I think this would be the best solution to keep the storage, ships, and other systems simpler. When vehicles are stopped, the containers should all be aligned with the grid. That means trucks will be:

1 teu2 teu

All containers are 20⨉10, which is 1 tile long ⨉ ½ tile wide.

 2  Tracks#

My plan is to have tracks running east-west. No north-south tracks, no 90° turns, no diagonals. Trains are out of the player’s control. They come in from the outside.

But it might be useful to have some switches so that there can be multiple parallel tracks. I made a diagram for this[4] to help me calculate the shape.

 3  Roads#

I think roads are going to be more complicated than tracks. One lane will fit inside a tile.

All road tiles need to be one-way. How will vehicles move along them? I think paths get assigned to routes instead of running pathfinding for vehicles on the fly. If there’s something in the way, they’ll stop and wait, not try to move around. I won’t be sure until I try it.

 4  Vehicles#

The initial setting will have vehicles from the outside world and also vehicles that serve the station. I’m thinking the 2teu trucks and the trains and the ships will come from the outside world. The station vehicles will be 1teu trucks, and these:

Container crane: moves container N/S
Gantry crane: moves container within a rectangular storage area
Straddler carrier: picks up container from above and drives it around
Reach stacker: picks up container from the side/front and drives it around

For example, one design would be to use a container crane to move containers off of a train onto 1teu trucks, then have them drive to a container storage area, where a gantry crane picks them up from the truck and moves them into storage.

{I think things will get more complicated if I allow storage to keep containers vertically, because that means I will need both parallel and perpendicular style gantry cranes, but that may be what I need. What if the gantry crane can rotate it at the end? Maybe that would simplify things.}

Other vehicles I see listed in the literature:

 5  Scale#

Real life trains and container ships are much larger than what I want in this game. I wrote about this on this blog post[5]. I want to use an unrealistic scale to make the interesting parts of the game larger and the uninteresting parts smaller. I think a 200-car train is not 20 times as interesting/fun as a 10-car train. I’d rather have more 10-car trains. I think a 20,000 TEU ship may be interesting but I think I’ll have to wait and see. I suspect a 2,000 TEU ship will be more fun than a 20,000 TEU ship.

 6  Data representation#

This seems like it “should” be easy but I always work with discrete grid movement or with continuous non-grid movement. I haven’t worked with this type of movement before, so I’m going to take it slow and think through the problem before coding. I try to break things down into simpler problems before trying to combine them into a larger solution.

  1. What are the rules for placing tracks+roads?, except I need to handle road+road crossings and road+track crossings.
    • Tracks. This project may be a little simpler than a general simcity-style simulation because I have only east-west train tracks. But it’s a little more complex because those train tracks can have 3x2 branch sections, and some of those may overlap.
    • Roads. The roads seem like they’ll be relatively straightforward. They’re all one way sections, each taking up one tile. I’m going to assume no multi-lane roads, so no lane changes.
    • Road+track crossings.
    • Road+road crossings. For example, an east-moving road crossing a south-moving road. Are turns always allowed, or is that optional?
  2. What are the rules for vehicle movement?
    • Trucks. The path is predetermined, so a vehicle can only move forwards on its path. The main thing to figure out is how a vehicle knows that the road ahead is occupied. It may be simplest for a vehicle to reserve the tiles it’s on plus the tile it’s about to move onto, and then a vehicle that can’t reserve the next tile will have to stop.
    • Trains. The tile reservation system may work for trains too. I think OpenTTD implements path signals with reservation.

I will start with a straight line road, no curves or branches. After I sketch out some ideas, I can implement something, then go back to sketching out how to handle curves and branches.

Track: shape: EW | branch NE | branch SE size: derived from shape, 1⨉1 | 3⨉2 | 3⨉2 position: of top left corner length xy(offset) → returns the xy position of any value from 0 to length

There’s a set of track objects.

Vehicle: length wheel positions: [offset, …] relative to head = 0 car positions: ? position: [(track, offset), (track, offset), (track, offset), …] when moving onto a new tile, push a new (track, 0) onto the head when moving off an old tile, pop a (track, track.length) off of the tail to draw each car:

Then derived from that is a Map of which tracks (there can be more than one) are at that location. Do I need this?

 7  Game ideas#

Right now my goal is to produce an animation, and then if I like it, I can work on a simulation. If I were to someday turn this into a game:

You’re given a train station / container port / warehouse to manage. The trains, ships, and trailer trucks are not under your control. The tiles where they come in and out is not under your control. Your job is to design the station to efficiently unload and reload the trains (ships). You can build roads, trucks, cranes, storage areas for this.

I want this game to be about updating the design over time. It’s not about planning everything ahead of time and then building it, then moving on to a new area to build something new. Think of Stardew Valley — you live in the same area and keep revisiting and changing things. It’s not like Minecraft where you build stuff and then move on to a new area.

As time goes on, the needs of the world change. Maybe the public wants beanie babies, and there’s a new train line shipping them. Maybe the world stops using coal, so your coal train stops coming through, but instead there are wind turbine blades that need to be stored differently. Maybe there’s a war, and the war mobilization effort requires a lot more trains for a short amount of time. I want you to be adapting to the needs of the world rather than planning out the perfect layout ahead of time.

My experiences with other building games:

 8  Other#

Top-down is what I’d do but I think this kind of game would probably benefit from 3d models with an almost top-down view, so you can see a little bit of the front side of each container and vehicle.

Right now I have square tiles. Would it be interesting to have rectangular tiles, wider than they are high?

Hm, someone has a video showing vehicle simulation in a container port https://www.youtube.com/watch?v=DQM-4yZVe0c[7] but their truck drivers are scary!

https://www.youtube.com/watch?v=KCwdrB6Q5fY[8] - incoming trucks get a ticket telling them which line to go in, then when they go through, the gantry crane knows which truck is arriving so it can get everything ready; trucks are parallel to the storage

ASC yard? E-RTG yard?

Bram Stolk explains how he represented trains in his hex grid game https://gamedev.stackexchange.com/a/177040/2472[9]

Email me , or tweet @redblobgames, or comment: