Tile Collision
Tile map collision is a fundamental concept in 2D games that helps determine when and where a game entity (such as a player, an enemy, or an object) interacts with the environment, represented by tiles on a grid. In games like platformers (e.g., Super Mario) or top-down games (e.g., Zelda), the world is often made up of a grid of tiles, each representing solid ground, walls, or passable areas.
Tile map collision detection checks whether a moving entity collides with one of these tiles, helping to define what parts of the environment the entity can move through and where its movement is restricted.

What is a Tile Map?
Section titled “What is a Tile Map?”A tile map is a grid of square-shaped tiles arranged in rows and columns. Each tile can represent a different element of the game world, such as:
- Solid tiles that block movement (e.g., walls or platforms).
- Empty or passable tiles that allow movement (e.g., air, water, or pathways).
In most cases, a tile map is represented as a 2D array where each element corresponds to a specific tile. For example, a map that is 5 tiles wide and 7 tiles high is a 5x7 array of tiles. This one has ground along the bottom, a short wall on the right, and a single tile floating up as a ceiling.
Tile map (5 x 7):0 0 0 0 00 1 0 0 00 0 0 0 00 0 0 0 10 0 0 0 10 0 0 0 11 1 1 1 1
Legend:0 = Empty tile1 = Solid tile1D Array Tile Map Representation
Section titled “1D Array Tile Map Representation”Tile maps can also be represented in a 1D array rather than a 2D array. Using a 1D array simplifies storage and processing in some cases. You can calculate the position of a tile by using the formula:
tileIndex = x + y * mapWidthWhere:
xis the horizontal position (column) of the tile.yis the vertical position (row) of the tile.mapWidthis the total number of columns in the tile map.
For example, in the 5x7 tile map, the ceiling tile at position (1, 1) would be:
tileIndex = 1 + 1 * 5 = 6This formula allows you to retrieve a tile from a 1D array representation of the map. Here’s a visual example:
1D array representation of the tile map:[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
Getting the tile at (1, 1):tileIndex = 1 + 1 * 5 = 6Value at index 6 is 1, so it’s a solid tile.How Tile Map Collision Works
Section titled “How Tile Map Collision Works”Instead of testing the player against every tile in the level, tile map collision uses the grid to jump straight to the handful of tiles the player could be touching. Dividing a pixel position by the tile size (and rounding down) gives the column or row that pixel falls in:
column = floor(x / tileSize)row = floor(y / tileSize)Doing this for each edge of the player’s bounding box gives the range of tiles they overlap. Only those tiles need to be checked, no matter how big the level is.
Each frame, the process looks like this:
- Move the player along one axis.
- Find the tiles along the edge they moved towards.
- Check each of those tiles in turn, stopping at the first solid one. Check every tile along the edge, not just the two corners: a player wider than one tile can otherwise slip through a solid tile under their middle.
- Resolve the collision by snapping the player back to the edge of the tile and stopping their movement on that axis.
- Repeat for the other axis.
Why One Axis at a Time?
Section titled “Why One Axis at a Time?”If the player moves diagonally into a corner, a single overlap doesn’t say whether they hit a wall (stop moving horizontally) or the floor (stop moving vertically). Resolving each axis separately removes the guesswork. A collision found after the horizontal move must be a wall, and one found after the vertical move must be a floor or ceiling.
Tiles Outside the Map
Section titled “Tiles Outside the Map”With a 1D array, a column outside the map doesn’t cause an error. It wraps around to the neighbouring row and returns the wrong tile. Check that a column and row are inside the map before looking them up, and decide what the outside of the map should behave like (usually empty).
Kinds of Tiles
Section titled “Kinds of Tiles”Not every tile has to be simply solid or empty. Common variations include:
- One-way platforms that the player can jump up through from below but land on from above. They only count as solid when the player is falling and their feet were above the platform on the previous frame.
- Interactive tiles, such as question blocks, that do something when hit from a particular side.
- Hazards, such as spikes or lava, that hurt the player instead of stopping them.
These all build on the same idea, find the tiles the player overlaps, then decide what each kind of tile does.
Tile Map Collision vs. AABB Collision Detection
Section titled “Tile Map Collision vs. AABB Collision Detection”Axis-Aligned Bounding Box (AABB) collision detection is a general collision detection algorithm used for detecting overlaps between rectangular objects. It works by checking if the bounding boxes of two objects overlap along the x and y axes. This is used when two free-moving objects need to be checked for collision.
- Tile Map Collision focuses on detecting whether an entity collides with predefined tiles on a grid. It’s best for games where the world is constructed from a grid of tiles.
- AABB Collision Detection checks if two rectangles overlap, making it more suited for detecting collisions between moving objects, such as projectiles and characters.
- Tile map collision uses a tile-based grid, while AABB works on arbitrary rectangles.
- Tile map collision is optimized for grid-based games, while AABB can handle any rectangle-to-rectangle collision.
For example, in tile map collision, a player moves within a grid, and collision checks are done only for the surrounding tiles, making it more efficient for games with a fixed world structure. In contrast, AABB would involve checking the player’s bounding box against every other object’s bounding box in the scene.