tags:

views:

22

answers:

2

Hi everyone,

is it possible to check if ONLY THE opengl object on the iphone screen has been touched? I can only figure it out for the whole screen, but thats not what I want :-(

e.g. a coin game: we have several coins in the opengl world. if the user pressed one coin he gets a point.... and so on ;-)

is this possible ?

thanks marinka ;-)

A: 

It is a lot like detecting collisions(But a lot faster :). Find the position of the tap and then compare it to the position of the coin. If they are with in a certain radius then you have tapped the coin. I am not sure if OpenGLES (but it's very unlikely) that it provides any mechanism.

Shadow
A: 

An old school method of picking involves rendering your openGL scene to a off-screen buffer each frame, using a specific color for each object. Figure out the pixel the user clicked on, and grab the color at that pixel from your buffer. Simple reverse map that color to the object that was rendered with it, and you know which object (if any) was clicked on.

It does have the benefit of doing depth sorting for you (for example, if your coins are on top of each other).

Code example found here: http://gpwiki.org/index.php/OpenGL_Selection_Using_Unique_Color_IDs

Nicholas M T Elliott
I like the version with the color. I do have one more question ;-) When I am using a texture for my squad. Do I have to pick the color of that? then just check if it has a color or not right?
Marinka
Generally you'll turn off texturing when rendering into the off-screen buffer, and just pick an arbitrary (but unique to that object) color. I found a simple overview here: http://gpwiki.org/index.php/OpenGL_Selection_Using_Unique_Color_IDs
Nicholas M T Elliott