Question about tile map collision

Started by
5 comments, last by Danny Pearson 11 years, 12 months ago
Areet :) i'm relatively new to game development and i'm having a go at making a small tile based top down game just for learning purposes. This is where i have come to a standstill, i understand the concept of how my collision should work like. if my player moves by 32 px over each tile check the tile it will move to to see if it is passable which in my case is 0 for floor and 1 for walls in the map array. my problem is making this work for just the tiles containing a 1. im on my phone so i cant copy and paste code but basically i have... if (mapArray[j] == 1) then if player.x + 32 > j*32 && etc for the other sides returns true else return false then in my movement code... if key down then if !engine.collision() then player.y +32. sorry if this is all a bit disjointed any help would be appreciated.
Advertisement
oh aye i'm using c++ with the allegro library baha cant believe i forgot that...
If your character moves one tile at a time then just check the tile ahead of the player in the direction that the player wants to move. Something like this.

bool Map::CheckCollision( int x, int y ) {

if( mapArray[x][y] > 0 )
return true;

return false;
}

if( keydown( VK_RIGHT ) ) {
if( !dungeon->CheckCollision( ++player.x, player.y ) )
player->MoveRight( 1 ); //1 would be for the number of tiles to move
}

if( keydown( VK_UP ) ) {
if( !dungeon->CheckCollision( player.x, --player.y ) ) //Assuming positive y moves down the map
player->MoveUp( 1 );
}

Hope that gives you an idea.

Brendon Glanzer

cheers for the reply :) i read somewhere to keep the player and the map seperate so the player just starts at 32,32 then moves 32 pixels (the size of the tiles) each time so i'm not sure how it can know what tile it's on. should i spawn the player on the map in the array? cheers
The player will store the tile that its on through the x and y values. Or you can have a special tile on the map that is a spawn tile that you search for in the level when it starts.

Somethin like this below would do it. Its not the fastest search method but it will get it done.


for( int y= 0; y < map->GetHeight(); ++y ) {
for( int x = 0; x< mapt->GetWidth(); ++x ) {

if( map[y][x] == 3 ) {
player->SetPosition( x, y );
return ();
}
}

Brendon Glanzer


cheers for the reply smile.png i read somewhere to keep the player and the map seperate so the player just starts at 32,32 then moves 32 pixels (the size of the tiles) each time so i'm not sure how it can know what tile it's on. should i spawn the player on the map in the array? cheers


The source of your reading was probably talking about object oriented principles relating to code organization, but some part of your game logic is going to have to know about both the player and the map. Likely, the player (object) just has to know what their X and Y value is, a map just has to know its contents (which rooms exist at which X and Y locations) and game logic will use that information to permit/deny movements, update enemy AI if there are any (like answering questions of "can enemy skeleton see player?") and other game loop actions.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

got it working pretty much the same way as bglanzer showed me :) feel sick now that its blagged me for a full day and it turns out as simple as that :P cheers for the help :)

This topic is closed to new replies.

Advertisement