Skip to main content
gamedev.net
🔒 Locked

tile-based collision detection

Started by apexsolar Dec 15, 2001 at 5:19 PM 6 replies 350+ views
Original Post
apexsolar
apexsolar
how do i detect collision in a tile-based game? i''m doing an over view look, and i am using this kind of thing to set my tiles for(int i = 0; i{ for(int x = 0; x { map[x][y] = tileid; blt(x,y,tileid); } } i want to be able to move my player, and then if there is a collision, just to make him stop, any suggestions?
a person
a person
do just as you said. when you detect a collision dont let the player move in that direction. mark solid tiles with a flag so you know. then before ech move, check the tile the player is moving to, if its solid dont let the player move.
kmsixpence
kmsixpence
Maybe try and create a minimap offscreen surface. On the minimap, for each tile could be a certain color. For example, a warp could be blue, un walkable could be red and walkable could be green. Then when you need to check up a type of collision, just get the color at the location on the minimap offscreen
apexsolar
apexsolar
how would i do that?
kmsixpence
kmsixpence
Okay, you don''t even have to do the minimap color idea. All you have to do is create a two dimensional array of integers holding map types. At the top you could create defines that hold for each type of tile. Then you fill the values when you initialize the program. As your character walks, just test what tile its on and find what type it is. Then you can do whatever you want to. Here''s some psuedo code:

  
#define WALKABLE 1
#define STOPABLE 2
#define SWIMABLE 3

int maptype[AmountOfXTiles][AmountOfYTiles];

//this is in the initialization

for(int indexx=0; indexx<AmountOfXTiles; indexx++)
{
for(int indexy=0; indexy<AmountOfYTiles; indexy++)
{
maptype[indexx][indexy]=WALKABLE;
}
}

//in the game loop

int character_x_position, character_y_position;

if(maptype[character_x_position][character_x_position]==WALKABLE)
{
DoWalking();
}
apexsolar
apexsolar
Hey thanks! but i don''t think that will work, because that checks the current tile you are standing on using my x & y pos, i would have to make an equation, (my tiles are 64x64) like
if(x.pos < 64)
{
character_x_position = 0;
}
if(x.pos > 64 && x.pos < 128)
{
character_x_position = 1;
}

would that work?
then the same for the y''s, is there an easier way?
darookie
darookie
BAD wayto do this!
Since your tiles are 64x64 you can simply check the current tile by performing a shift and a logical and

  

// get hor./vert. tile number from screen offset

int TileXY ( int nScreenPos )
{
return (nScreenPos >> 6); // equals to nScreenPos/64

}

// get hor./vert. tile offset from screen offset

int TileOfsXY ( int nScreenPos )
{
return (nScreenPos & 0x3F); // equals nScreenPos % 64

}

apexsolar
apexsolar
thanks, it works!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.