ill-regular shaped tiles and their selection

Started by
5 comments, last by Wyrframe 6 years, 1 month ago

so thinking about a map divided into states and the selection of these states. How I have handled this in the past. I have each state as a separate image. I pre-process each state into an array of transparent/non-transparent flags foreach pixel of the image. As the mouse moves across the map it will be over a N amount of states. I check each of these states and I conclude the selected tile is the one with non-transparent flag.

Is there a better approach to this?

Advertisement

You could, render the screen to a render target where each state is a unique color then get the pixel under the mouse cursor and look up which state was clicked on.  Even with a large number of states this would still be very quick.  You didn't provide much information honestly about what software you're using to develop (Unity, UE4, Game Maker, custom D3D11/12 or OpenGL/Vulkan or Metal renderer, etc.)

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

56 minutes ago, Mike2343 said:

You could, render the screen to a render target where each state is a unique color then get the pixel under the mouse cursor and look up which state was clicked on.  Even with a large number of states this would still be very quick.  You didn't provide much information honestly about what software you're using to develop (Unity, UE4, Game Maker, custom D3D11/12 or OpenGL/Vulkan or Metal renderer, etc.)

speed wasn't the issue. My current implementation the maximum number of states overlapping is 5 so looking in 5 arrays is less than 1ms. BUT it is a good deal of work setting it up.  I had hoped I was missing something easier.

Hence my suggestion of rendering it and picking the pixel color.  You're already rendering the screen with the states on it.  Rendering to another frame buffer/render target in the same shader is no big deal and just works.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

No. Not color picking. You do not read back from the GPU if you can avoid it. Memory access is extremely optimized in one direction, from CPU to GPU. 

Since you prepare the map texture yourself somewhere, have a simple color coded version of it in memory. Check the pixel the mouse is over, color = state.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

@OP: Your bitmap approach (literally, bit-as-in-boolean map, not as in graphical texture) is a perfectly fine approach. Your mouse-cursor will be within the bounding rectangle of only a handful of regions at a time in a vast majority of situations, and you can easily quickly test if the cursor is over an activated bit for one of those very easily.

One gotcha is aliasing; if your player can zoom their view in any way, one mouse-cursor position might round to hitting several regions at a time, so be sure there is a predictable order of resolution (e.g. sort states by ID, always pick the north-east most, etc), to prevent possible odd behaviour when the player points at or near a border while zoomed out.

RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

This topic is closed to new replies.

Advertisement