Collision detection in tilebased game
I am having a problem with collision detection with sprites travelling at high speed.
The current method I am using for collision detection with tiles is to take the player''s desired location, translate that into tile coordinates, and see if the tile (or tiles)are passable or solid object.
the problem is that when the sprite is moving at say 10 pixels at a time. The next position might even be on the other side of a wall (assuming wall is 8 pixels wide) and the sprite is not allowed to move through walls.
This same technique causes problems where the sprite may end up either 10 pixels away from or past a wall or hill.
Can anyone describe a method so that the sprite will stop at the beginning of the wall rather than inside it or 10 pixels away from it?
Thanks
use smaller blocks to test with. if your basic wall size is 8, test for collisions, say, 4 pixels at a time.
__________________________________________________________America seems to like crap because its what we make popular. - Goober King
June 11, 2002 10:45 AM
what about:
in (float x, float y, float destX, float destY)
float len = sqrt((x-destX)*(x-destX)+(y-destY)*(y-destY));
float dx = (destX-x)/len;
float dy = (destY-y)/len;
if(dx > 0)
while(x <= destX)
{
check if blocked (x,y)
x += dx;
y += dy;
}
else
while(x >= destX)
{
check if blocked (x,y)
x += dx;
y += dy;
}
in (float x, float y, float destX, float destY)
float len = sqrt((x-destX)*(x-destX)+(y-destY)*(y-destY));
float dx = (destX-x)/len;
float dy = (destY-y)/len;
if(dx > 0)
while(x <= destX)
{
check if blocked (x,y)
x += dx;
y += dy;
}
else
while(x >= destX)
{
check if blocked (x,y)
x += dx;
y += dy;
}
You should first of check if he can move to a tile before you move him on that tile.
June 14, 2002 05:07 AM
What you should do is enlarge the collision detection rectangle of your player to allow for it''s velocity. So, for example, if he''s moving at 5m/s in the positive X direction, increase his X size by 5 * TimeScale in that direction to allow for it. If this collides with anything, I then step through in divisions of 10 to start with, and getting finer and finer if it finds anything until I have the exact position and velocity of the impact literally just before it happens.
Detect it like this:
a) find out if that player IS on a non-walkable tile, not whether he is about to move onto one.
b) move him off the tile in the direction that he entered before blitting/drawing him.
HOW TO DO THIS:
(for the right side protruding over a non-walkable tile)
player.x -= player.x%tilewidth;
(for the left side)
player.x += tilewidth - (player.x%tilewidth);
(for the top side)
player.y -= player.y%tileheight;
(for the bottom side)
player.y += tileheight - (player.y%tileheight);
a) find out if that player IS on a non-walkable tile, not whether he is about to move onto one.
b) move him off the tile in the direction that he entered before blitting/drawing him.
HOW TO DO THIS:
(for the right side protruding over a non-walkable tile)
player.x -= player.x%tilewidth;
(for the left side)
player.x += tilewidth - (player.x%tilewidth);
(for the top side)
player.y -= player.y%tileheight;
(for the bottom side)
player.y += tileheight - (player.y%tileheight);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement