I created a cube in opengl and it rotates in ontouch event. To to this I created a CustomSurfaceView as follows

public class CustomSurfaceView extends GLSurfaceView {
    public boolean onTouchEvent(MotionEvent e) {
        float x = e.getX()
        float y = e.getY();

Here x and y are screen coordinates. How can I get 3D coordinated from this? I have already looked gluProject and NeHe. But I dont know how to implement this in my project, it shows that there is no GLdouble,GLfloat class.

  • \$\begingroup\$ I may be wrong, but I think what you want [gluUnproject)[developer.android.com/reference/android/opengl/…, float, float, float[], int, float[], int, int[], int, float[], int)]. My understanding is that turning 3D models into a 2D image is called projection, and it uses the modelview and projection matrices. To go the other direction, you have to give that function the same matrices so that it can do the math. I think that function just does the work @XGouchet described. \$\endgroup\$ Commented Mar 15, 2012 at 13:22
  • \$\begingroup\$ FYI, I think the general term for this is called "Picking." \$\endgroup\$
    – notlesh
    Commented Apr 13, 2012 at 19:58

2 Answers 2


I'm not sure if there is an OpenGL method doing this already but here is how you should work it :

  1. First from your touch position, find the corresponding X and Y angle from your camera. The center of the screen is at angle (0; 0), and the corners are at the max angle defined by your camera's field of view. Be carefull cause the max angle in Y and X are different because of your aspect ratio.
  2. Using this angle, your camera's position and orientation, you can create a virtual Ray in space.
  3. then you must loop on each of your object to detect if your ray collides with your object (you can use the object geometry, or a sphere or box approximation for faster computation). You can find Line/Sphere, Line/Box and Line/Triangle intersection algorithm easily on the net.
  4. keep the nearest object intersecting your ray

Check out Chapter 9 in the book OpenGL ES 2.0 for Android by Kevin Brothaler. It's easy to read and explains how to use an inverse perspective matrix to make this process simple:

  1. Multiply the normalized touch event ( x , y ) by the inverted MVP matrix—twice with two arbitrarily different z values—to create a ray. (Pages 171-172.)
  2. Intersect with your plane and you have a 3d location in world space.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .