Sunday, March 17, 2013

Le Grande Hexagon


Yesterday I laid out the hexes for the Cyvasse game board. One problem I ignored was that, although my hexes were in the right pattern, I was basically printing a square of them.

There are probably a few ways to deal with this. One would be to define each hexagon to display manually, something like

board = { {x=7, y= 1}, {x=6, y=2}, {x=8, y=2}, ...}

This way seemed tedious. And if I ever need to change the coordinates of each hex, tedious again.

I know the coordinates of each corner of the board, so I thought I might be able to use something simple like checking whether the coordinates of a particular hex are greater than the coordinates of the corners... but it's not so simple.

I can instead check to see which side of a line a point falls on, and determine if any particular point is within my larger hexagon. The solution relies on using the cross product, a mathematical concept I don't understand in the least :)

It looks something like this:

left_of_line = ((point_b.x - point_a.x) * 
                    (point_c.y - point_a.y) -
                    (point_b.y - point_a.y) * 
                    (point_c.x - point_a.x)) >= 0

Wiring all of my edges up, I get something like:

One key point is that the order of these points seem to matter. I'm not sure of the intuition behind this, or what I would do if I had any points with negative coordinates, but point_b always needs to be larger than point_a in order for the sign of the answer to come out right.

(The answer seems to be that you are actually taking the cross product of vectors, so direction does matter. More here.)


I've gone and messed up my color scheme. I think this happened because previously I was incrementing colors each time through a nested loop of 13x13; now I'm only displaying hexes that fall within the coordinates, and so no longer bumping the colors correctly. I should definitely assign each hexagon a color when I initially create it, so that it keeps track of itself.

I'll also take the opportunity to set the window size and caption. This is easy:

  love.graphics.setCaption( "Cyvasse" )
  love.graphics.setMode( 525, 700 )

While I'll probably want to do make the sizes of things dependent on the window size, I'll worry about that later. Here are the results:


I still need to make each hexagon symmetrical from all sides, so that they are not all oblong. I'd love to start to get into the gameplay as well, but before I do that I need to learn how to separate functions into different files, and if possible classes, in Lua.

No comments: