5

Possible Duplicate:
How do I determine what is touched in 3D space from the screen?

I'm developing an Android application. I'm also working with OpenGL graphics.

I have a square drawn on the screen and I want to let user move it when he touches over it. In other words, user should be able to move square when he puts his finger on the square. If he touches outside the square nothing happens.

How can I detect when user touches over square?

Thanks.

Community
  • 1
  • 1
VansFannel
  • 45,055
  • 107
  • 359
  • 626

3 Answers3

4

Detect touches can be implemented using ray picking. I had the same problem and I used code on this page: Android OpenGL ES ray picking. Method getViewRay() calculate the direction of ray. If you know the direction from [0,0,0] to ray[x,y,z], it is quite easy to simulate ray cast and detect the collision with bounding object.

petrnohejl
  • 7,581
  • 3
  • 51
  • 63
3

The picking and selection tutorials supplied in other answers aren't applicable here; amongst other glSelectBuffer and glRenderMode aren't part of OpenGL ES.

There are, as stated elsewhere, essentially two approaches. You can figure out how the touch maps into your scene and test your objects mathematically. If your square is flat on, as though you were drawing it in 2d, then you can achieve that quite easily.

Alternatively, you can do something essentially like picking but adapted for the functionality available under OpenGL ES. For example, clear your scene to black and draw your square in solid blue. Use glReadPixels to get the colour value at the touch location. If it's blue then the touch is on the square. This is probably a more expensive way to test because it means an additional draw of the scene, but it means that you don't have to implement any additional maths or other code beyond what GL supplies.

The only slight thing to keep in mind is that OpenGL uses the same way of addressing pixels everywhere — (0, 0) is the bottom left, positive y goes up and positive x goes left. So whatever axes you have your touch coordinates relative to, you need to transform them to be relative to that before doing the glReadPixels.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • It's seem so difficult. I'm going to put some buttons on screen and use them to move OpenGL objects. – VansFannel Nov 12 '10 at 15:29
  • @VansFannel it is not so difficult at all, just follow the tutorial for color picking. A button for doing what the finger can do on the iPhone is a very bad idea – rano Nov 12 '10 at 18:37
  • @rano: I'm doing it for Android not for iPhone. – VansFannel Nov 15 '10 at 10:50
  • 1
    @VansFannel lol clumsy me, my point was: on a touch device not using the user fingers as natural as they could is a bad idea. – rano Nov 15 '10 at 13:21
0

If you have just a square you could check if the touch coordinates lie inside the space formed by the four vertexes of the square. In the case of an nonparallel to the x axis square, you shall project its vertexes using the projection matrix.

More in general there are several techniques to 'pick' an OpenGL model: ray picking and color picking being the most used. Be sure to have a look at this doc

rano
  • 5,616
  • 4
  • 40
  • 66