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#

{ diagram for tracks }

{ diagram for track switch - https://observablehq.com/@redblobgames/biarc-path-on-grid[4] }

tracks cannot run N-S so no 90° turns, no diagonals

tracks always run west to east

{ a topic to explore later }

 3  Roads#

{ diagram for road lanes }

{ diagram for lane dividers }

{ diagram for 90° turn road }

{ a topic to explore later }

 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#

{ a topic to explore later }

  1. How do I represent the data? Doesn’t have to be efficient for this project.
  2. How do I handle collision? I think I need to reserve grid blocks.

I should start with just 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.

Having only east-west running train tracks seems like it should help simplify some of this. Can branches overlap?

A B C D
E F G H
I J K L

If there’s a track EFGH and then a branch FGCD and another branch EFJK and also a crossing track CGK, what should happen?

Oh, I have no crossing tracks in this simulation. So maybe it’s simpler.

Beatle suggests doing the algorithms on the track segments instead of the grid. I’ll have to think about this. I think it’s mostly an issue at crossings.

But maybe I’m getting ahead of myself. I think just implementing the simplest thing without any of that is going to take some mental work for me, so I should do that first, and then come back and rewrite it to support the other features.

What is the data structure? Well, first, a grid. Maybe a Map that has row and col turned into a key. That’d let me work with negative indices too. But I need to be able to store non-1⨉1 objects.

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 turn this into a game, some ideas:

You’re given a train station (container port) to manage. The trains (ships) are not under your control. The left/right grid squares 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. It’s not like Prison Architect where you plan everything at the beginning, build it, and then mostly watch the game.

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.

 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: