   Tile map collision - C++/Allegro
  Hi. I have troubles making tile map to not let my player pass to particular tiles.Tile map is a big array full of numbers. 0-5. Only for number 1 i can give a pass. Other numbers shouldn't be able to walk onto. So basically what i am trying to do is check the tile map for numbers and return true of false, which differs from the number stored in the array. If the number is 1, then return true, else return false. I cannot make it work. Here is snippets from my code:if(key[KEY_UP] && move == TRUE){player_y --;}And the code to check the array:for(int mapX = 0; mapX < 15; mapX ++){for(int mapY = 0; mapY < 20; mapY ++){if(map[mapX][mapY + 1] == 1){move = TRUE;}if(map[mapX][mapY - 1] == 1){move = TRUE;}if(map[mapX + 1][mapY] == 1){move = TRUE;}if(map[mapX - 1][mapY] == 1){move = TRUE;}else{move = FALSE;}}}
  There's a few problems.For one, you seem to set Move to FALSE for every cell (unless one of the neighbouring tiles is 1). Probably the last cell has no 1 neighbours not allowing the player to move.You also should only check the tile you want to move onto, not the whole map.Also, BUG: In your for loops you check over the boundaries of the map array (what's mapY - 1 when mapY equals 0?);  Thanks for your answer, so basically i should add the player into the array and manipulate the array like:if(player_move_top){player[x][y - 1]}Something like this? Don't have an idea if that would work and if this is correct way, and if it is possible.Or is there some better way to do this?   ;  There is no need to iterate over the whole map. The player is at one specific position (I assume player_x,player_y), so only check the map tile that the player moves on to:if ( key[KEY_UP] ){  if ( player_y == 0 )  {    // TODO - Player at the border, what to do?  }  else if ( map[player_x][player_y - 1] == 1 )  {    --player_y;  }}You might want to put the check for a passable tile in a separate function so there's only one spot that would need change:bool IsTilePassable( int X, int Y ){  if ( ( X < 0 )  ||   ( X >= 15 )  ||   ( Y < 0 )  ||   ( Y >= 20 ) )  {    // outside    return false;  }  if ( map[X][Y] == 1 )  {    return true;  }  return false;}...if ( key[KEY_UP] ){  if ( IsTilePassable( player_x, player_y - 1 ) )  {    --player_y;  }};  That's what i get on this part:  else if ( map[player_x][player_y - 1] == 1 )  {First-chance exception at 0x0131103a in Labyrinth.exe: 0xC0000005: Access violation reading location 0x0131d030.Unhandled exception at 0x0131103a in Labyrinth.exe: 0xC0000005: Access violation reading location 0x0131d030.As for the different function, it gives no errors, but it returns false from:  if ( ( X < 0 )  ||   ( X >= 15 )  ||   ( Y < 0 )  ||   ( Y >= 20 ) )  {    // outside    return false;  }  if ( map[X][Y] == 1 )  {    return true;  }both of these statements.And it checks then, but since it false, the character is forbidden to move. And i have made a clean spot, around the guy 2 tiles, where he spawns, to make sure it doesn't works that bad way... it cannot pass anyway. So basically it returns false even if the tile is good for movement. :/   ;  When the crash occurs, which values do player_x and player_y have? They are probably not in the range of the map.Note that the IsTilePassable function would return false when the passes coordinates are outside of the map.   ;  The player is in the map, even in the middle of the map... There you go: http://img186.imageshack.us/slideshow/webplayer.php?id=inthemap.png.I still don't get how it actually calculates pixels and arrays together though.   ;  Although (playerX, playerY) may be a valid coordinate, for instance (playerX, playerY - 1) may not. So before indexing by playerY - 1 you must check that playerY - 1 >= 0. Essentially, you must make the following constraints hold:0 <= x < mapX0 <= y < mapYWhere (x, y) is the position you intend to index into the map array with.If you don't check these before indexing by a translated player position you risk getting out of bounds.   ;  Are you correctly translating from pixel positions to tile positions and back?Are you sure you're not confusing x, y, row and column at some point? For example, that you're not treating the x as the row position?;  Beyond_Repair, thanks for the tip, i will add this as soon, as i figure out how to make it work at all. Because it is not at 0 now anyway.kvee, for "Are you correctly translating from pixel positions to tile positions and back?" this i am not even sure how this is done and how it is possible, that's why i was surprised by this code."Are you sure you're not confusing x, y, row and column at some point? For example, that you're not treating the x as the row position?" Well, x/y looks okay to me. As far, as i understand the code, it looks okay for me.   