Jump to content
  • Advertisement
Sign in to follow this  
polandspringeater

Efficient click detection design

This topic is 816 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a game that has an inventory. This inventory will have multiple things to click on (different slots, options, etc.). i want to know if there is an efficient way to check what the mouse has clicked on.

 

My code right now is checking every single clickable thing in the inventory to see if it was clicked on. I can already see this will be inefficient when more options are added. The code is something along the lines of:

For (Item in Inventory)
{
    if (mouseCoordinates == ItemCoordinates)...
}

How do games that have a ton of clickable things handle this?

Edited by polandspringeater

Share this post


Link to post
Share on other sites
Advertisement
Either through elaborate GUI systems or something as simple by transforming the coordinates into an index into a list of items.

Share this post


Link to post
Share on other sites

My initial take is; it does not "matter" for this. How may inventory items will you have? Even few hundred and iterating over them, would not show up in profile.

I was probably only going to have about 20+ slots. I guess you're right. I'm underestimating the power of a CPU...

Share this post


Link to post
Share on other sites

My homebrew UI library uses multiple Displays. Each display takes up some part of the screen space. A display can be a child of another display. Then, each display holds a number of components(the actual visible/interactive UI elements). So you can Pick your display first(only a few would normally exist), than iterate through the components that only belong to that display(aka a few as opposed to all of the existing components on the screen).

 

Edit: Basically what Aphton said.

Edited by ExErvus

Share this post


Link to post
Share on other sites

Either through elaborate GUI systems or something as simple by transforming the coordinates into an index into a list of items.

Interesting, I never really though of transforming a cursor position to an index value. My immediate though though would be what if you wanted to move the ui element? Would you have to remap it each time? But i guess that could be handled by the element itself?

Share this post


Link to post
Share on other sites

Use a quad tree to store the UI components and travel down the tree based on the position of the cursor.

 

This should have O(log4(n)) complexity.

 

Alternatively, like fastcall said,

xIndex = mouse.x % (number of horizontal boxes);

yIndex = mouse.y % (number of vertical boxes);

 

UI[xIndex][yIndex].processClick();

 

The downside here is your UI must fit into a regular grid, the quad tree method would be an irregular grid, and probably more trouble than it's worth :P

 

I assure you a modern PC can calculate thousands of if(mouse.x < uiElement.width+uiElement.x && mouse.x > uiElement.x) per frame though... No one would ever know that you used anything less than the most efficient code possible in this case.

 

edit --

Roughly 1190476 UI elements could be processed per frame on a 1GHz cpu at 60fps >.>

In practice, probably more than that, I was pretty generous with an estimate of 14 instructions for two of those if statements... (7 each, one addition, two comparisons, one logical and, and three movs (mouse positions will be assumed to have an effectively permanent register), in fact, I think my assembly for that is pretty inefficient, and the compiler would do better work >.>

Edited by Felps

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!