Mike Wagman wrote:
Ok I am writing a turn based strategy game that uses a hex map. I want
to display a 7x7 grid of polygons (hex's) and then identify which
polygon was right or left clicked on. I also need to be able to modify
the attributes of each hex as they will change as you scroll the map
around. I plan on putting a text object over each hex to indicates it's
coordinates.
If OGL or PyGame work for you, great. Otherwise, this wouldn't be too
hard to do with wxDCs and drawing on a wxPanel or wxScrolledwindow. what
you would need to do is write a "hit test" function that determines what
polygon was clicked on. I know of two fairly simple ways to do this. One
is to get the coordinates of the mouse click, and then compute a
point-in-polygon test for each polygon, to see if it was clicked on. If
you have the polygons arranged in a grid, you should be able to figure
out which ones are close very easily, and then do the test. Code for hte
poin in polygon test can be found at:
THe code is here:
http://www1.acm.org/pubs/tog/GraphicsGems/gemsiv/ptpoly_haines/
It's in C, but wouldn't be hard to translate to Python. I've included in
a C python extension that I wrote , if you want that, but it would
probably be plenty fast in Python anyway.
Another method is a nifty trick that was discussed recetnly on this
list. When you draw your polygons to the screen, draw another copy to an
off-screen bitmap (with a wxMemoryDC), but with each polygon in a
different color. Then when you get the mouser coordinates, you can do a
wxDC.GetPixel(x,y), which will return the color, which you can map to
the polygon. This is very fast, and makes it easy to draw lots of
different shapes without having to write specific hit test code.
I had a test of this method worked up, but I cna't find it at the
moment. I am planning on implimenting it for a project of mine, so if
you're interested I can help you out. If you search the recent archives,
you'll find someone who did a simple version of this using PythonCard,
and posted a link to his code.
Good luck,
-Chris
···
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov