Sign in to follow this  

Question about tile map collision

This topic is 2061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i][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.

Share this post


Link to post
Share on other sites
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.

[CODE]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 );
}
[/CODE]
Hope that gives you an idea.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

[CODE]
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 ();
}
}
[/CODE]

Share this post


Link to post
Share on other sites
[quote name='dBuzzin' timestamp='1335051052' post='4933639']
cheers for the reply [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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
[/quote]

The source of your reading was probably talking about object oriented principles relating to code organization, but [i]some [/i]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.

Share this post


Link to post
Share on other sites

This topic is 2061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this