[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] [Poll] Hex Grid



On Thu, 17 Jun 2004, [iso-8859-1] Nicolás Sanguinetti wrote:

> I know this is not a pygame question .... but still, since the question
> arised in my head due to one of the previous posts .... here it goes:
> 
> What's (in your opinion) the most efficient / effective way to implement a
> hex-grid?

As it happens I've put some thought into this...

For efficiency using pygame clearly openGL is the way to go, as it allows
fast and smooth zooming and scrolling.  I bind my pygame surface to an
opengl texture and go from there; the only significant problem I've run into
is maximum texture size which requires stitching several textured quads
together.

What's most effective is subjective, but here's what I do.  I store my hexes
as a dictionary of cells keyed by (x,y) location.  I then have a method that
takes location + move direction and returns a destination;  I use this as a
default for all hexes, allowing specific hexes to have local overrides, and
also the existance of cells that aren't hexes (e.g. I have rivers as cells
between hexes).

For Rendering IMHO it's best to avoid drawing hexes mathmatically.  I
instead have hex bitmaps that I blit onto a surface, using a method that
calculates pixel location from hex coordinates.  This is simpler, avoids
round off errors, makes for an easy method to varry hex shape and fill
texture, and allows you to work in things that aren't hexes.  It also
dovetails nicely with calculating which hex is under a mouse click, for
which I recommend reading:
http://gamedev.net/reference/articles/article747.asp#MOUSE

The followup article is also usefull:
http://gamedev.net/reference/articles/article748.asp

-Jasper