# Line Intersection in Tiled Map

This topic is 2213 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 http://www.gamerendering.com/2009/07/20/grid-traversal/ looks OK, but it's Java)

##### 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.

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

##### Share on other sites
I think he means whether or not the line intersects an impassible side of a tile.

##### 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 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 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!

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013652
×