2D Map with Provinces

Started by
9 comments, last by the_hoff1 12 years, 2 months ago
Hi,

I wanted to use a 2D map of a random country with provinces. Each province would represent an area you could click, construct buildings there etc... The part that alludes me is how would I go about determining which province I clicked? If I did know which (province/sprite/texture??) how would I then still determine who that belonged to since all I determined was which graphic was selected? Would I then do some type of search comparing or something?? Graphics type programming isn't my specially.

I don't want any code, just general idea's to point me in the right direction. I haven't done something like this before so any tips is appreciated!

Thanks,
Advertisement
Hi,

some ideas:

- first of all you wouldn't want to determine which texture you clicked on. Graphics and gameplay should be seperated as much as possible. And unless you have rectangular provinces, using texture to find out what you clicked on is useless anyway. So you already know what province you clicked on right away in the best case.

- Other than that, you might use a std::map<> to store the dependencies.
Ok, Im not sure how to check which province I am selecting since they would all be different shapes, not square. I thought I had remembered reading that if I wanted to do this I would have to do picking on the province sprite to know which province I selected then do some type of comparison etc..

Is there a way(algorithm?) to check which province I selected regardless of its shape?
The easiest solution that comes to mind is having an array with one entry per pixel, which contains a small integer identifying which province has been clicked (you can have an array with more information about the provinces). This reduces the algorithm to a lookup or two.

Generating the array consists of drawing the map using the province ids as colors.
The part that alludes me is how would I go about determining which province I clicked?
Let's assume we get the map from wikipedia. This is not readily usable.
I would convert it to some polygonal form using an editor outputting XML, or perhaps I could have a point set on paper (hopefully one does not need a lot of points).
Then I would import the points and triangulate them. That would allow to resolve ray-tri testing with ease.
Using pixel ids is an option but it involves a dense data representation. I understand this is probably not going to be a problem but I still think this approach needs to be mentioned as well.

Previously "Krohm"

One solution would be to figure out the "middle" of each province, and add a button at that location. Then, you know what button is tied with what province.

An easy method of approximating the "middle" of a province would be finding the leftmost and rightmost point of a province and use (right - left)/2 as the middle x, and do the same for top and bottom to find middle y.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Im not sure I like the idea of story every single pixel in an array. Wouldn't that be extremely inefficient trying to search through it?

...Let's assume we get the map from wikipedia. This is not readily usable.
I would convert it to some polygonal form using an editor outputting XML, or perhaps I could have a point set on paper (hopefully one does not need a lot of points).
Then I would import the points and triangulate them. That would allow to resolve ray-tri testing with ease....

Could you explain more about your idea of polygonal form?

Edit: http://en.wikipedia....oint_in_polygon

Where you referring to something like this?

Edit 2: http://stackoverflow.com/questions/924171/geo-fencing-point-inside-outside-polygon

I believed I solved my problem with this link. Thanks for the tips guys, I just needed a nudge in the right direction. :)

Thanks!!!

Im not sure I like the idea of story every single pixel in an array. Wouldn't that be extremely inefficient trying to search through it?


No, it's about the most efficient way to do it. You don't need to search through it:
pixel_number = mouse_y * width + mouse_x;
province_id = province_id_map[pixel_number];

No, it's about the most efficient way to do it. You don't need to search through it:
pixel_number = mouse_y * width + mouse_x;
province_id = province_id_map[pixel_number];


Hmm interesting, but im confused, what does the width represent? How would that return the pixel number?
I understand line 2, but perhaps I don't understand the formula to get the pixel number.

Thanks!!
If your screen's resolution is 1024x768, the width is 1024. The pixels are then numbered
0, 1, 2, ..., 1023
1024, 1024, ..., 2047,
2048, 2049, ...
etc.

Your array just tells you which province each pixel belongs to.

This topic is closed to new replies.

Advertisement