Archived

This topic is now archived and is closed to further replies.

DevLiquidKnight

Collison detection in a tiled based engin

Recommended Posts

Hi I am having difficutlys blocking certain tiles i have a map i made using an array and it has like 01010101 and stuff for the tiles each number is a differnt tile and i was trying to get it so it would block your movement to certain tiles by adding in a bool fuction that checked ur current location in the array of tiles and checked if u could move there if u couldnt it would move u back so u couldnt moev forward u know? and i did it in the area that checks for key down and stuff and it is not working for some strange reason does anyone know what i could do? -XSpartacus

Share this post


Link to post
Share on other sites
Wow, I was out of breath just reading that post

You''ll have to tell us more than "it doesn''t work". Your routine sounds fine, so there''s an error in your implementation somewhere.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Specify what it is that is not working.
Does it not block the character from moving onto the tiles that should block him/her/it?
Does it not allow the character to move even if he should be able to?
Does it just crash?


Share this post


Link to post
Share on other sites
Well it does the following when I try and walk on a tile that is not allowed to be "walked upon" it will let me walk on it it dont crash or anything at all I think it might have to due with the fact the way I have the movement setup is not with using integer values instead im using float values so it cannot determain when i step on a tile if my location is 1.23 and i have the thing check for the location 1 instead of 1.23 or something it cannot determain the extra decimal values it only checks the 1 and i dont want that

// this is within the area i check a key is down
// lets say object.x = 1.23 and object.y = 1.23
// if keydown up or left or right ect..
if(checkCollison(object.x,object.y))
{ //let him move
} //otherwize dont move
bool checkCollison(int x, int y)
{
// 3 is lets say a wall
// object is a object that we are checking for collison
if(map[x][y] == 3)// tile is a wall return false for movement
{
retun false;
}
else // tile is not a wall return true to let him move
{
return true;
}
}//end of checkCollison();

as you can see it would then convert the float values into integers and not get the remainder of the decimal values lol.. and its making me very very mad

Share this post


Link to post
Share on other sites
Question why are you passing floats when you have checkCollison(int x, int y) and it wants integers? for my tile movement I use regular ints, and it works fine I dont think you will need floating point precision for movement. Also when checking to see if a tile is walked upon take the object's x and y coordinates
and then make a box out of it using its width and height then once you have that box you can test to see if any points are contained within a tile that is non-walkable if so dont let the player move.
  
Bounding box:
(x,y)--------(x+width,y)
| |
| |
| |
| |
(x,y+heght---- (x+width, y+height)

then you could do something like,
    

if( tile_array[object.y/tile_height][object.x/tile_width].flags == BLOCKED)
{
return 0;//cant move

}

You can just check each point like that and it will return 0 if any point of the play is in a non walkable area. This isn't the best way I'm just trying to give a basic idea this is how I started out doing it. Hope this helps!



Edited by - Derilect101 on January 14, 2002 6:38:17 PM

Share this post


Link to post
Share on other sites