Sign in to follow this  
Sparkon

Line Intersection in Tiled Map

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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 [url="http://www.gamerendering.com/2009/07/20/grid-traversal/"]http://www.gamerendering.com/2009/07/20/grid-traversal/[/url] looks OK, but it's Java)

Share this post


Link to post
Share on other sites
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.

[CODE]
Point Ray::trace()
{
do
{
distance = signedDistanceField.getDistance(getPosition());
march(distance);
} while (distance > 0);
return getPosition();
}
[/CODE] Edited by CryZe

Share this post


Link to post
Share on other sites
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. Edited by CryZe

Share this post


Link to post
Share on other sites
[quote name='C0lumbo' timestamp='1353402168' post='5002618']
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 [url="http://www.gamerendering.com/2009/07/20/grid-traversal/"]http://www.gamerendering.com/2009/07/20/grid-traversal/[/url] looks OK, but it's Java)
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this