# How to handle platform collisions in a 2D game?

If I have a map in my platformer game and I want my character be able to jump on some platform, for example. Is there an easy way to tell the game where the platforms are and where the player can jump to and start jumping from?

Also, for games like Super Mario Bros and such, are the maps made in .txt files ? I watched a tutorial for maps and he said all professional game developers make a .txt file for the map and write numbers for example using...

111111111111111111111
111111111111111111111
111111111111111111111
111111111111111111111
222222222222222222222


...to create a map. Is this correct or how did the developers do the maps in the Super Mario Bros or MegaMan games?

What I am wondering is, if I want my player to be able to jump up on some platform or something, how do I do it?

## 3 Answers

I can't really say if there is an easy way to do collisions without knowing everything about your game :) Anyway, it depends on your definition of word "easy". It looks like you knowledge about collisions is rather limited, so chances are that it won't be very easy.

If I guessed right and you're aiming at a simplest form of collision, 2D rectangle tile collision, then there is a simple and quite complete tutorial series. There you will feel how basic things are done with tile-based collisions, jumping and platforms included. No need to code it yourself in Flash, just read it and remember terms for Googling later.

Onto the next question, I highly doubt that MegaMan and SuperMario used .txt map file format considering the fact that games back then were often written in assembly language and had limited memory, so they were probably just storing tile arrangement in some segment of the ROM. Game developers use any map format they want within platform limitations, I've seen bitmap file formats where pixel colors represented tile types, but it's usually some kind of a binary format.

Seriously though, investigate topic of collisions by reading that tutorial link and using help of mighty Google, think and let it sink for a while, come back with specific questions later. For a map file either save/load your data manually into/from any format you want or use serialization.

You're asking at least two questions here so I'm a little thrown off. Your primary question seems to be asking about how 2D tiled levels are stored but your subject line and tags are more directed toward collision detection. Can you clarify your question(s)?

There are myriad ways to represent level data and I certainly wouldn't say that all pro game developers store 2D tiled level data as .txt files. However, for a simple project, a .txt file that's read into a two-dimensional array is certainly a good fit—just not necessarily the only fit.

Over on Stack Overflow, I've posted the same generalized pseudocode for 2D, tile-based collision detection/resolution. Check that out and see if it helps answer your collision question.

i see that this thread is a bit old but maybe i can help anyone. I had the same problem with a super mario game in c++.

Here is the piece of code:

float oldX = m_x;
float oldY = m_y;

moveX(seconds);
moveY(seconds);

float top = m_y,bottom = m_y + m_height,left = m_x,right = m_x + m_width;

//Check collisions with map
if(top >= 0.f && left >= 0.f && bottom / Tile::m_height m_rows && right / Tile::m_width m_cols)
{
//Check new x value with old y
top = oldY+0.1f;
bottom = oldY + m_height-0.1f;
left += 0.1f;
right -= 0.1f;

if(     (map->getDesign(left/Tile::m_width,top/Tile::m_height) && map->getTile(left/Tile::m_width,top/Tile::m_height).m_solid)
||  (map->getDesign(left/Tile::m_width,bottom/Tile::m_height) && map->getTile(left/Tile::m_width,bottom/Tile::m_height).m_solid)
||  (map->getDesign(right/Tile::m_width,top/Tile::m_height) && map->getTile(right/Tile::m_width,top/Tile::m_height).m_solid)
||  (map->getDesign(right/Tile::m_width,bottom/Tile::m_height) && map->getTile(right/Tile::m_width,bottom/Tile::m_height).m_solid)  )
{
m_x = oldX;
}

//Check new y value with old x
top = m_y+0.1f;
bottom = m_y + m_height-0.1f;
left = oldX+0.1f;
right = oldX + m_width-0.1f;

if(     (map->getDesign(left/Tile::m_width,top/Tile::m_height) && map->getTile(left/Tile::m_width,top/Tile::m_height).m_solid)
||  (map->getDesign(left/Tile::m_width,bottom/Tile::m_height) && map->getTile(left/Tile::m_width,bottom/Tile::m_height).m_solid)
||  (map->getDesign(right/Tile::m_width,top/Tile::m_height) && map->getTile(right/Tile::m_width,top/Tile::m_height).m_solid)
||  (map->getDesign(right/Tile::m_width,bottom/Tile::m_height) && map->getTile(right/Tile::m_width,bottom/Tile::m_height).m_solid)  )
{
m_verticalAcceleration = 0.f;
if(m_y > oldY)
{
if(!m_ground)m_ground = true;
m_y = ((int)m_y / Tile::m_height) * Tile::m_height;
}
else
{
m_y = ((int)m_y / Tile::m_height+1) * Tile::m_height;
}
}
}
else
{
m_x = oldX;
m_y = oldY;
}


• Please explain what your code does in detail. Just throwing a piece of code around isn't really that helpful. – user15805 Jan 1 '14 at 21:57