Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
I may be wrong, but I think what you want [gluUnproject)[…, 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. – Steve Blackwell Mar 15 '12 at 13:22
FYI, I think the general term for this is called "Picking." – stephelton Apr 13 '12 at 19:58

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.