• Create Account

## Line Intersection in Tiled Map

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Sparkon  Members

509
Like
0Likes
Like

Posted 17 November 2012 - 02:52 PM

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!

### #2Khatharr  Members

7735
Like
0Likes
Like

Posted 19 November 2012 - 07:17 PM

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.

### #3C0lumbo  Members

4073
Like
0Likes
Like

Posted 20 November 2012 - 03:02 AM

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)

### #4CryZe  Members

773
Like
0Likes
Like

Posted 20 November 2012 - 03:36 AM

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, 20 November 2012 - 03:45 AM.

### #5Khatharr  Members

7735
Like
0Likes
Like

Posted 20 November 2012 - 03:40 AM

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.

### #6CryZe  Members

773
Like
0Likes
Like

Posted 20 November 2012 - 03:52 AM

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, 20 November 2012 - 03:53 AM.

### #7Sparkon  Members

509
Like
0Likes
Like

Posted 25 November 2012 - 01:53 AM

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!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.