Jump to content

  • Log In with Google      Sign In   
  • Create Account


Best way to do mouse picking in 2D OpenGL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 MARS_999   Members   -  Reputation: 1253

Like
0Likes
Like

Posted 15 June 2014 - 01:30 AM

I am trying to decide what is the best way to do picking in OpenGL with glOrtho2D mode on a tile based game. Would using the color id method be better or would doing a ray test against each tile and sprite having an AABB around it?

 

Please advise and if possible give an example of code to do the picking based on your choice.

 

Thanks!



Sponsor:

#2 Lodeman   Members   -  Reputation: 706

Like
0Likes
Like

Posted 15 June 2014 - 03:36 AM

For a simple 2D game it could be as simple as getting the mouse's current position on-screen, and from there go through your game objects and see which one matches that location.



#3 MARS_999   Members   -  Reputation: 1253

Like
0Likes
Like

Posted 15 June 2014 - 03:49 AM

How is that going to work when I can move the map off screen?

 

e.g.

map size is 2048x2048 screen is 1024x768

I move the screen to pan up/down/left/right to see more of the map. The screen coordinates aren't going to match the absolute position of the object on the map... 

 

So far I am thinking the color method is the best and simplest way to go about this... with rgb and 256 colors per channel you have what... ~16.7million colors available for units?



#4 haegarr   Crossbones+   -  Reputation: 3967

Like
0Likes
Like

Posted 15 June 2014 - 05:05 AM


How is that going to work when I can move the map off screen?

Because you need to calculate what portion of the map to display after all that panning (usually called the view transformation), you can do the inverse calculation to relate the screen origin back to the reference for the map. Its just a generic answer, I know, but you haven't provided enough details for a more detailed answer.

 


So far I am thinking the color method is the best and simplest way to go about this... with rgb and 256 colors per channel you have what... ~16.7million colors available for units?

Yes, 24 bits give you 2^24 possible values. However, you need to use another rendering pass. You have to ensure pixel perfect masks, e.g. no AA that blends your color masks. You have to ensure that texels with alpha below 0.5 (i.e. more transparent than opaque) will be discarded, so that transparent texels do not disturb the color mask. And you need to read back the render target to get access on the CPU side. So, after all, the algorithm is probably still less sophisticated, but it does not come for free, and whether testing bounding volumes or looking at pixels is more performant in the end would need to be investigated further.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS