View Full Version : Help detecting when a 3d element is touched on iPhone

02-25-2010, 08:21 PM
Hi, I've got some experience doing 3D programming, but it's OLD experience, when I did all the math, transformations and projections manually.

Now I'm writing some software for the iPhone, using Open GL ES 2.0.. I have learned a lot about managing my objects, rendering, and all that, but now I'm trying to create some interaction.

I know how to detect the touch events, and can determine where a touch starts/stops on the screen, but I'm having a hard time finding a way of determining if a touch is 'on' a given object in the scene. I assume there is some function to determine if a touch 'collides' with the an element in the space, based on what was rendered there, but I can't seem to find it.

I guess I could write an algorithm to work it out, but that seems both redundant, and slow.

Please let me know if there is a function for this, and I can probably figure it out from there, although any examples/further instruction would be appreciated.


09-14-2010, 08:48 PM
You're going to have to write code to do it. Turn your touch point into a ray that casts into the screen and checks for collisions. Search for info on Google about "3D picking."

09-14-2010, 08:52 PM
Another easier way is to render each object a special color to a texture, then check the color of the pixel you touch and return the object using the ID. Ideally use a binary search when looking up the ID. Search "picking painter's algorithm."