• Home
  • About us
  • Articles and Tips
  • Contact us

Articles

Home / Articles / Hex grid line of sight revisited

Hex grid line of sight revisited

Posted on: 01-10-2011 Posted in: Tips

It’s been a long programming day, so I’m in a bit of a technical mood. I’ve gotten several questions regarding the line of sight article, so I wanted to take a moment to elaborate a bit more on the math. Hopefully this will clear things up.

As I had discussed on the article, I’m using a hex grid that is rotated 90 degrees from the usual wargamer style. Its coordinate system looks like this:

HexGridNumbers

When we lay hexes out in this manner, their position is not like if we were to lay a square grid – there is a displacement that is not the same as the hex side.

We can calculate this displacement based on the hex center (the almost invisible center dots in this image):

Hex Zoom

We can do this because each hexagon can be segmented into six equilateral triangles. Assuming a hex’s side length measures d units, then the hex’s center point will also be d units from any of its corners.

Hex Zoom D

Knowing that, we can calculate how many units we need to move vertically for the next hex on the grid. Does this look familiar?

Hex Zoom Vertical

We know that vertical line towards the center of the hex intersects the diagonal from the vertex, which measures d. We also know that since it’s right at the center, the measure c at the bottom equals d / 2. We can therefore calculate that height trivially by using the Pythagorean theorem:

h^2 + dˆ2 / 4 = dˆ2

h^2 = d^2 – d^2/4

h^2 = d^2 * (1 – 1/4)

h = d * sqrt (0.75)

So each hex’s center is (d * sqrt (0.75) * 2) units above the previous one.

Now, suppose we were to subdivide these hexagons once into triangles. We would end up with a segmentation that looks like this:

Hexes Segmented

We can number these triangles using a coordinate system as well. This is what I described on my original article as

“If we renumber these triangles according to a left-right, bottom-top sequence”

They would end up looking like this:

Hexes Segmented Coordinates

Notice that the coordinates would include triangles that are not contained in any of the hexagons. This is something we need to account for later on.

Suppose that we place an imaginary dot at the center of the vertical line bisecting each triangle. The collection of these imaginary dots would form a perfect grid, which is the one that we would traverse using Bresenham’s.

Once you have the list of points covered by a line, you only need to keep in mind that these are not triangle but hex coordinates. You would then apply calculation to find out which hexagon each point belongs to (accounting for empty triangles), which is trivial as the number of both vertical and horizontal triangles per hex is known, and that will give you the hexagons in your line of sight.

LOS Hex

Notice that the hexagon and triangle distance calculations are relevant mostly if we care about finding out a line of sight from a particular world space, and not only from a specific hexagon to another.  If that’s all you care about, your calculations are reduced to the hexagon subdivision.

UPDATE. There’s one issue I’d forgotten to address. From one of the questions I had gotten:
You say Bresenham’s line algorithm is easy to apply to triangles but I’m not so sure. Triangles are not square and these are 60 degrees so cant be converted into squares.
What one needs to do is to stop thinking of them as triangles. If you just consider the center of the triangle as an imaginary pixel, you can see the grid that these imaginary pixels form as the sort of grid that you can apply Bresenham’s to.  Instead of filling in pixels, you would be tracing an imaginary line from one center to the other, and considering as part of your LOS all the hexagons touched by that line.

About the Author

Ricardo J. Méndez

  • Popular Posts
  • Related Posts
  • Quick SteerToFollow example
    Quick SteerToFollow example
  • Screenshot Saturday 20110813
    Screenshot Saturday 20110813
  • Screenshot Saturday 20110806
    Screenshot Saturday 20110806
  • UnitySteer - AutonomousVehicles and Bipeds
    UnitySteer - AutonomousVehicles and Bipeds
  • Screenshot Saturday 20110806
    Screenshot Saturday 20110806
  • Postmortem: Laying tile
    Postmortem: Laying tile
  • Line of sight in hex grid
    Line of sight in hex grid
  • Getting fancy with hex grids
    Getting fancy with hex grids
  • (3) Comments
  • (0) Trackbacks
  1. Peter01-11-11

    You say – imaginary center dots would form a perfect grid – I think the centres of down traingles would be higher than the up triangles – this may still work since the effective hexagons would also be skewed. Still a great technique though I will try to get it working myself.

    (reply)
    • Ricardo J. Méndez01-11-11

      Hi Peter,

      I think I see where my wording might confuse you. I should have written “at the center of the vertical line which splits the triangle”, at (h / 2) height. Does that clear things up?

      (reply)
      • Peter01-11-11

        Sounds good, that was my guess. It seems to be working on paper for me. Thanks for the quick reply.

        (reply)

Leave a Reply

Click here to cancel reply.

Recent posts

  • Hairy Tales Previews
  • Quick SteerToFollow example
  • UnitySteer – AutonomousVehicles and Bipeds
  • Hairy Tales progress – new elements and a boss
  • Public Alpha 2

Recent comments

  • Ricardo J. Méndez on Quick SteerToFollow example
  • Ben Ezard on Quick SteerToFollow example
  • Ricardo J. Méndez on UnitySteer 2.1 released
  • Elie on UnitySteer 2.1 released
  • Ricardo J. Méndez on Upcoming changes to UnitySteer
© 2009-11 Arges Systems Inc.. All Rights Reserved
TwitterStumbleUponRedditDiggdel.icio.usFacebookLinkedIn