Sign in to follow this  
polandspringeater

Efficient click detection design

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

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

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

Sign in to follow this