how to select regions on a map

Previous topic - Next topic

Andrik

Hi there,

I'm making a 2D game (well trying to) in which I use a map of a universe divided in about 80 regions. The regions have all kinds of random shapes and is an png image.

I'm having trouble figuring out how I know which region I'm in when clicking on it with the mousepointer. I want to show information about the selected region.
My first thought was to create acolor map by creating a duplicate of the map, give all the regions on this map a different RGB en drawing it outside the viewport. Now when I click on a region of the real map, I transpose those X,Y to the color map to find out what color is in the region (getpixel) and thus know which region I'm in and show the right information.

I've read on the forum however that getpixel is slow and inaccurate in that it can give different colors back when it should be the same. So my question is, is there an alternative to 'know' which region you click on a map (composed of random regionsizes).
To complicate matters, the map needs to be zoomable (one level of zoom-in) and moveable on x and y axis (when zoomed-in ).

I hope someone can help me out!

thanks in advance!

Kitty Hello

Make a temp image thats only 1/8th of the size and use loadspritemem. Then get the colour information from the rgba pixel array.

erico

Can your map be tranported to a squared one?
If so, you could use an array to check the positions.

I used such on a board game I never finished.  :S

The color method you mentioned was also my first guess.
If your game is somehow turn based, it would not be a problem.

There is also a chance Getpixel may not be that slow nowadays either.

Moru

Just a couple of ideas to play with. I would go the second idea :-)
You could trace the regions with polygons and calculate if the point clicked is inside the polygon. I have never done this sort of code so I have no idea if it's slow or not. http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

Second idea is to cut out each single region and make them as sprites with transparency. Test the mouses coordinate for collision against all sprites. You do not have to paint the sprites on the screen for this, just use them for the collision test.


Kitty Hello

I prefer the pixel thing though, its much easier to programm and make the map.

Andrik

Thanks guys for all your input!

As soon as I have some time I'm first gonna try the suggestions from Kitty Hello, that seems do-able. ;)

And as for you comment Erico, the map is made up of all different kind of shapes so no way to use squares unfortunately.

Andrik

Kitty,

I'm still a bit confused. I made a smaller image (357 X 308 pixels) and used loadspritemem to put the pixels in a an array. This works. But I expected to be using a 2 dimensional array, but the array is one dimensional and 357 x 308 = 109956 in size. How do I now retrieve the pixel color from this single dimensional array for let's say pixel at xy 25,25?

hope my confusion makes sense  :S

kanonet

Its stored in a one dimension array, because one dimension arrays are faster than two dimension ones.

If you want to access a pixel on that sprite you need to know its xsize:
pixel(x,y) = array[y*xsize+x]

in your example its this:
pixel(25,25) = array[25*357+25] = array[8950]
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64