tile based collision detection?

i cannot seem to figure out how to do tile based collision detection, no matter how i do it i seem to fail.

any sugestions on how i can do this?
can no one help me out? :3

what i mean with the question is how can i chose what tiles to be solid (each tile being 32x32 pixels)?
Assign a boolean to each tile that determines whether or not it's solid.

Then don't let things move over solid tiles.

1
2
3
4
5
6
7
8
if( tile.issolid )
{
  // don't let object move here
}
else
{
  // it's OK to move here
}
can you specify a bit more how to not let stuff move over it? im kinda not managing to get it to work atm :/
It depends utterly on how you are implementing it.
Is this a platformer like SMB? Or a tile based RPG like old school Final Fantasy?

The latter is very simple. When the user tries to move, you get the coordinates they want to move to, check to see if the tile is solid, and if it is, don't let them move.

If it's the former, things are far more complicated. Platformers are actually quite difficult to make by comparison.
im trying to do an old school rpg like game, kinda like the old zelda games and stuff.
the way i have it implemented right now is:

i have three multidimensional arrays, each displaying a different layer of the map, wich are all in the same class (in different functions in that class). then my movement is inside of my player class. i do not yet have any tile based movment implemented, so the player mogve independently from the tiles, so would you sugest i try to implement the tile based movement first?

oh and right now i have no class for the tiles, i simply disply different surfases acording to what number is in that spot in the array.
Yes, I'd implement movement. From there, it's just a simple check like Disch suggested before actually moving to the tile. Really, the movement attempt should just adjust one coordinate depending on the direction the character tries to move.
ah ok, i guess i went about this whole thing the wrong way then :(
thanks for the help, i love how helpfull everyone is at this forum :D
Last edited on
okay stupid question, but what would be the best way to go about making movement tile based? the only thing i can think of right now is to have a loop like this:

1
2
3
4
5
if (keystate [SDLK_DOWN]){ //get the keypress, in this case its the down button
     for (int move = 0; move == 32; move++){ //runs loop 32 times
            player.y++; // adds one to players y coordinate every time the loop runs.
     }
}


is this a decent way to do it, or am i completely wrong? (sorry, kinda new to the whole tile based stuff)
for (int move = 0; move == 32; move++){ //does nothing

fixed.

I don't know why you'd WANT let your player go down 32 steps every single frame as long as the down button is held though.
Last edited on
since every tile = 32 pixels, and i dont want to have my player walk down 32 pixels every frame, thats why i want a loop, so i have a smoothe transition between tiles, and not just jump 32 pixels every single time i move, wich would look very ugly, in my opinion. but as i said im new to tile based stuff, so im of course open for sugestions :)
since every tile = 32 pixels, and i dont want to have my player walk down 32 pixels every frame, thats why i want a loop


First of all, you should seperate the logical layer (the position of the player in tiles) from the representation (the position you draw the sprite at).

Doing a smooth transition then becomes easy - you immediately increase the players y position by 1 (meaning, the player is 1 lower than before) and set the player into a moving state (in which he doesn't accept input), then play an animation that shows the player moving forward, and then set the player back into the normal state.

That is, if you want to make the entire logic tile based. I don't actually remember it so well right now, but I believe that while the environemts in the 2D Zelda games were tilemaps, the movement itself was smooth, not between tiles.
well that is sorta the type of movement i was aiming for. when i said "an rpg like zelda" i just meant the general style of gameplay, like you would be able to move along the y axis as well as the x axis, wheras in a sidescroller you are pretty much limited to the x axis (not completely correct way of explaining, but i hope that you know what i mean)

as for your sugestion, how would that work? like i can guess how to do the animation itself (having about 4 images of each direction, then scrolling throught them as i move from one tile to the other) but how can i move something smoothly across the sreen without changing the object (in this case the sprte) coordinates gradually?
wow, i suck...

earlier when i was writing the for loop i wrote:
for (int move = 0; move == 32; move++){}

but what i really meant was:
for (int move; move != 32; move++){}

*facepalm*
Why do you have that for loop at all? Why not just do player.y += 32; ?

Anyway, from the looks of that code, it looks a if you are doing coarse movement. That is, objects are restricted to a grid of tiles, and they can only be on 1 tile at any one time. In that case, movement is very simple.


You should have a struct or class to represent individual tile data. Your map should then be an array of that struct, or an array which contains an index/pointer or some other means by which to obtain that tile data stored elsewhere. That sounds kind of confusing, but it's simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// the Tile structure:
struct Tile
{
  bool IsWall;
  int GraphicsX;
  int GraphicsY;
  int AnyOtherInfoAboutThisTileYouNeed;
};

// The "tileset"
Tile tileset[] = {
 { false, 0, 0, whatever } // the floor
 { false, 1, 0, whatever } // another floor that looks different
 { true, 2, 0, whatever } // a wall
 { true, 0, 1, whatever } // another wall that looks different
};

// The map (note I use a 2D array here because you are probably using them.
//  it should be noted that you should not use them and instead use a
//  container class which simulates a multidimentional array.  See this thread
//  for an example class:
//  http://cplusplus.com/forum/articles/17108/#msg85595

int map[5][5] = {
 { 2,2,2,2,2 },
 { 2,0,0,0,2 },
 { 2,1,3,1,2 },
 { 2,0,0,0,2 },
 { 2,2,2,2,2 } };


// now to check to see if the player is walking into a wall, you just check the tile data:

newplayerpos = playerpos;
if( player_wants_to_move_left ) newplayerpos.x -= 1;
 // ... do same for right, up, down

int tileid = map[ newplayerpos.y ][ newplayerpos.x ];
const Tile& tile = tileset[ tileid ];

if(tile.IsWall)
{
  // player trying to walk onto a wall, don't let them
}
else
{
  // player walking onto OK ground.
  playerpos = newplayerpos;  // allow the move
}


Note in this example, 'playerpos' is the player position in TILES, not in pixels. If you are keeping track of the position in pixels the you will have to do some division to translate.



Animation is an entirely different story and should be handled totally separately by your program. It's another topic I don't feel like getting into in this post as it's already pretty long.
ah that makes sense :)
thanks a bunch.
Topic archived. No new replies allowed.