Jump to content
  • Advertisement
quatzequatel

Algorithm ill-regular shaped tiles and their selection

Recommended Posts

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?

Edited by quatzequatel

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

@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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!