Advertisement

Hex grid line-of-sight

Started by March 02, 2011 06:30 AM
1 comment, last by way2lazy2care 13 years, 11 months ago
k, I've gotten pretty far in my hex-based game programming. I currently working on line-of-sight (or field-of-view).

My goal was to calculate LOS between 2 hexes by checking a line drawn from the center of hex 1 to the center of hex 2. If the line crosses any pixel of a hex that blocks LOS, the LOS would be blocked.

Here's a screenshot:
hex_los_example.jpg

I drew in the red lines by hand to illustrate the issue below. The hexes with the red dots are within 10 hexes of the ork - his max sight range.

I've got my code working - as far as it's doing exactly what I intended it to. Here's a image from current test code. The dark hexes are not in LOS of the ork. As you can see, there are some "floating" hexes that are within LOS - according to center-center line method.

So, there seems to be a flaw with that method. I'm unsure what to do.

I was thinking about making a 2nd check, if center-center line method results in blocked LOS, I was going to then check 6 more lines, from the center of the viewer's hex to the 6 vertices of the "target" hex, and if 3 (or 2, or 4) of the vertices are within LOS, I'll say the hex is in LOS. That would open up more hexes to being in LOS.

Or... I could just make islands like that be NOT in LOS, the ruling being that a valid LOS can not pass through any hexes that block LOS, OR any hexes that are blocked themselves.

Any other ideas?

Is there a commonly used solution for such a problem?
Possibly useful info at

http://www-cs-students.stanford.edu/~amitp/Articles/HexLOS.html
Advertisement

k, I've gotten pretty far in my hex-based game programming. I currently working on line-of-sight (or field-of-view).

My goal was to calculate LOS between 2 hexes by checking a line drawn from the center of hex 1 to the center of hex 2. If the line crosses any pixel of a hex that blocks LOS, the LOS would be blocked.

Here's a screenshot:
hex_los_example.jpg

I drew in the red lines by hand to illustrate the issue below. The hexes with the red dots are within 10 hexes of the ork - his max sight range.

I've got my code working - as far as it's doing exactly what I intended it to. Here's a image from current test code. The dark hexes are not in LOS of the ork. As you can see, there are some "floating" hexes that are within LOS - according to center-center line method.

So, there seems to be a flaw with that method. I'm unsure what to do.

I was thinking about making a 2nd check, if center-center line method results in blocked LOS, I was going to then check 6 more lines, from the center of the viewer's hex to the 6 vertices of the "target" hex, and if 3 (or 2, or 4) of the vertices are within LOS, I'll say the hex is in LOS. That would open up more hexes to being in LOS.

Or... I could just make islands like that be NOT in LOS, the ruling being that a valid LOS can not pass through any hexes that block LOS, OR any hexes that are blocked themselves.

Any other ideas?

Is there a commonly used solution for such a problem?


I think your screenshot is broken, but this is a programming question not a design question.

You don't have any of your code posted, and your screenshot isn't loading, so it's hard to say what the problem might be other than that you are probably doing something wrong. The center line method should work fine though.

This topic is closed to new replies.

Advertisement