Question

I've implemented 3d ray picking and have some problems with accuracy. When picking at the bottom or center of the screen - everything works perfect, but at the top of the screen coordinates seem to move a bit away. So when i pick object near the top of the screen i often pick not the object I'm tapping but the lower one.

Here is the screenshot http://img838.imageshack.us/img838/9192/device20120723171612.png

I thought that problem can be in matrices, so here is how I define them:

ModelView

GLU.gluLookAt(gl, 
        camera.position.x, camera.position.y, camera.position.z,
        camera.target.x,   camera.target.y,   camera.target.z,
        camera.upAxis.x,   camera.upAxis.y,   camera.upAxis.z);

Projection

horizontalCenter = 0f;
verticalCenter = 0f;
shortSideLength = 1.0f;
zNear = 1f;
zFar = 200.0f;
surfaceAspectRatio = (float) w / (float) h;
float n = shortSideLength / 2f;
gl.glFrustumf(horizontalCenter - surfaceAspectRatio*n, 
              horizontalCenter + surfaceAspectRatio*n, 
              verticalCenter - n, verticalCenter + n, zNear, zFar);

My picking code:

public static Point3d pickSquare(int size, float step, float tapX, float tapY, float[] modelView, float[] projection)
{

    float[] near = new float[4];
    GLU.gluUnProject(tapX, raypickingViewport[3] - tapY, 0f, modelView, 0, projection, 0, raypickingViewport, 0, near, 0);
    if (near[3] != 0)
    {
        near[0] = near[0] / near[3];
        near[1] = near[1] / near[3];
        near[2] = near[2] / near[3];
    }
    Point3d near3 = new Point3d(near[0], near[1], near[2]);

    float[] far = new float[4];
    GLU.gluUnProject(tapX, raypickingViewport[3] - tapY, 1f, modelView, 0, projection, 0, raypickingViewport, 0, far, 0);
    if (far[3] != 0)
    {
        far[0] = far[0] / far[3];
        far[1] = far[1] / far[3];
        far[2] = far[2] / far[3];
    }
    Point3d far3 = new Point3d(far[0], far[1], far[2]);

    // here I'm searching for intersection with x0z plane using equation 
    // (x-x1)/(x2-x1) = (y-y1)/(y2-y1) = (z-z1)/(z2-z1)
    // My y is 0 so I can find x and y coords.

    float intersectY = (-near3.y) / (far3.y - near3.y);
    float pickX = intersectY * (far3.x - near3.x) + near3.x;
    float pickZ = intersectY * (far3.z - near3.z) + near3.z;
    return  new Point3d(pickX, 0f, pickZ);   
}

Thanks for your response.

Was it helpful?

Solution

The problem was in android status bar. On 480x800 screen it's height is 38 pixels, so I added in picking method this line:

tapY-=38;

and now everything works great

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top