Line Intersection in Tiled Map

Started by
5 comments, last by Sparkon 11 years, 5 months ago
Hello guys! my game is going really well and everything is almost complete, but i've just realized that i need a way to determine if a path between two points ( a line ) is obstructed. The problem is that i have no idea how to implement it! As the title says i'm using a tiled map, but everything else is not tile-based.

The basic idea would be to do a bruteforce check for every tile, but it's too slow, and for the line check i should check every 4 side of every tile. I could use a quadtree ( already using it for all the others collisions ), but have no idea how to make it work with a line.

Another clue i have, but not sure how to implement it or make it work, is to check for which tiles this line passes ( as if they were single pixels ) and then check if those are walls or not.

Hope you guys can help me or simply give me a hint!
Advertisement
We grok not your implementation.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
A partial answer to this part: "Another clue i have, but not sure how to implement it or make it work, is to check for which tiles this line passes ( as if they were single pixels ) and then check if those are walls or not."

The algorithm you're looking for is "grid traversal", there's tons of stuff if you just google for that. (e.g. the first hit http://www.gamerendering.com/2009/07/20/grid-traversal/ looks OK, but it's Java)
If your map is static, you could precompute a signed distance field for it. A signed distance field is basically a map, where each pixel tells you, how far away the nearest edge is. This way, you don't need to check every pixel. If the ray you're tracing is on a pixel where the nearest edge is 10 pixels away, you can skip checking the next 9 pixels and just check the pixel which is 10 pixels away. You do this process over and over again, until you find a collision.
Pixels which are inside your objects have a negative distance. So you basically found your collision, if the distance is less than zero.


Point Ray::trace()
{
do
{
distance = signedDistanceField.getDistance(getPosition());
march(distance);
} while (distance > 0);
return getPosition();
}
I think he means whether or not the line intersects an impassible side of a tile.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Well, he could modify the algorithm to only accept a negative distance as a collision, if the ray actually hits a solid side of the tile. Otherwise he simply traverses the ray forward. Since he doesn't have a usable distance at this point, he would need to move the ray just a single pixel forward.

A partial answer to this part: "Another clue i have, but not sure how to implement it or make it work, is to check for which tiles this line passes ( as if they were single pixels ) and then check if those are walls or not."

The algorithm you're looking for is "grid traversal", there's tons of stuff if you just google for that. (e.g. the first hit http://www.gamerendering.com/2009/07/20/grid-traversal/ looks OK, but it's Java)


That is exactly what i was looking for. The normal line-drawing algorithm is not enough :)
Thanks for the other replies too!

This topic is closed to new replies.

Advertisement