Sign in to follow this  
RobAU78

[java] Object Selection in Java

Recommended Posts

Hello everyone, I'm making a space strategy game, so there needs to be a method whereby clicking on an object on the main map will select it. My question is, is there any algorithm for doing this that does not iterate through the entire list of displayed objects? Thanks, Rob

Share this post


Link to post
Share on other sites
You can use one of the many space division structures, like a quadtree. Divide the screen into quadrants, divide each of those into quadrants, etc. and ultimitely have each of your objects linked to a quadrant. Then determine which of the biggest quadrants the user clicked in, then the next biggest, etc. until you know which object the user clicked on. You can go as deep as you want with subdivision.

The benefit to this is, of course, that each time you determine which quadrant the user clicked in, all of the objects in all of the other quadrants can be immediately disregarded. See this tutorial on quadtrees.

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