Monday, December 30, 2019

algorithm - Creating non-rectangular hotspots and detecting clicks


Say for a game with irregular hotspots (say Risk, or a strategy game like Paradox's Hearts of Iron or Crusader Kings), how do you





  1. Define a non-rectangular hotspot of irregular shape (much like a territory on a map?), in code?




  2. What tool can you use to trace out the hotspot and import into your game?




  3. What is the algorithm for detecting if someone has clicked within such a hotspot?






Answer



Besides the vector/polygon approach, another way to do this is to use a bitmap.


If each "color" in the bitmap represents a territory (France, Belgium etc.), then it's simply a matter of figuring out which pixel in the bitmap was clicked, looking up the color and determining if that color represents the territory of interest. You can even represent overlapping territories with specific colors, or treat your colors as a bitmask of territories.


In terms of tools, have your artists/content producer author the bitmap in any pixel editing tool. For example if you use Photoshop or the Gimp, you could represent different territories (France, Belgium etc.) as either named layers or as 1 layer but distinguishing the territories based on colors. You would then write a tool to convert the image/layers into a format that is suitable for game-internal purposes.


The game internal format would very likely be per-row run-length compressed (see: http://en.wikipedia.org/wiki/Run-length_encoding). You should be able to achieve very good compression ratios this way, as adjacent pixels on rows will very often belong to the same territory. The algorithm then becomes, for the current y coordinate of the mouse, traverse that row and figure out which run it belongs to, and therefore which corresponding territory.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...