views:

273

answers:

1

I've got the following method that is called when the user clicks a position on the screen:

        public void setup(int xi, int yi){
    int f = 0;  
    PerspectiveCamera camera = new PerspectiveCamera();
    camera.setViewport(320, 508);
    camera.update();
    Ray touch = camera.getPickRay(xi, yi);

    while(f < GLCamTest.array.length){
        //Vertex 1
        float x1 = GLCamTest.array[f];
        f++;
        float y1 = GLCamTest.array[f];
        f++;
        float z1 = GLCamTest.array[f];
        f++;

        //Vertex 2
        float x2 = GLCamTest.array[f];
        f++;
        float y2 = GLCamTest.array[f];
        f++;
        float z2 = GLCamTest.array[f];
        f++;

        //Vertex 3
        float x4 = GLCamTest.array[f];
        f++;
        float y4 = GLCamTest.array[f];
        f++;
        float z4 = GLCamTest.array[f];
        f++;

        //Vertex 4
        float x5 = GLCamTest.array[f];
        f++;
        float y5 = GLCamTest.array[f];
        f++;
        float z5 = GLCamTest.array[f];
        f++;

        Intersector sect = new Intersector();
        float z3 = 10;
        //Mid-point formula
        float x3 = (x2+x4)/2;
        float y3 = (y2+y4)/2;

        //Triangle one is (x1,y1), (x2,y2), (x3, y3)
        //Triangle two is (x4, y4), (x5, y5), (x3, y3)

        //Now, I have my 2 tri-angles I need for the
        // Polygon test..
        Vector3 t1 = new Vector3(x1,y1,z1);
        Vector3 t2 = new Vector3(x2,y2,z2);
        Vector3 t3 = new Vector3(x3,y3,z3);
        Vector3 t4 = new Vector3(x4,y4,z4);
        Vector3 t5 = new Vector3(x5,y5,z5);

        if(sect.intersectRayTriangle(touch, t1, t2, t3, t4)
            == true){
            System.out.println("TOUCHED AN OBJECT!!");
            if (f <= 12){
                System.out.println("SQUARE 1");
            } else if(f <= 24 && f >=13){
                System.out.println("SQUARE 2");
            } else if(f <= 36 && f >= 25){
                System.out.println("SQUARE 3");
            } 
        }
        if(sect.intersectRayTriangle(touch, t4, t5, t3, t1) 
                == true){
            System.out.println("TOUCHED AN OBJECT!!");
            if (f <= 12){
                System.out.println("SQUARE 1");
            } else if(f <= 24 && f >=13){
                System.out.println("SQUARE 2");
            } else if(f <= 36 && f >= 25){
                System.out.println("SQUARE 3");
            } 
        }else{
            System.out.println("NO TOUCH");
        }
    }
    f = 0;
}

Basically the class takes in the screen co-ords that were clicked, sets up the viewport and all the matrices it then generates the Ray. Once the ray is generated it takes each vertex and puts it in its own Vector3 it then tests does the ray intersect any of the points. The method says the ray intersects "SQUARE2" at certain points on the screen regardless if there is an object there or not.. why? what could be causing this? How do I fix it?

edit:

It's hitting the if statement "SQUARE2" every time it thinks it "finds" an object.It only finds an intersection when I hit the "middle" of the screen (this is where objects could be drawn) but It's only showing an intersection for the first if statement on the left hand side of the middle of the screen and the second for the right hand side of the middle of the screen

So for example it only thinks it's touched square 2 at the points marked X where the Rectangle is the phones screen.

  _________
  |       |
  |       |
  |X     X|
  |       |
  |_______|

The other methods I'm calling to for the ray tracing are located in another project.. http://code.google.com/p/libgdx/source/browse/#svn/trunk/gdx/src/com/badlogic/gdx/math (The folder Graphics has Perspective Camera which contains getPickRay(); and Intersector contains the other).

edit2: It appears it is picking up right location where objects COULD be but they currently may not be due to the world rotating so it appears to be just having issues with the world rotating.. Here's how I'm rotating the world...

    public void onDrawFrame(GL10 gl) {
    onDrawFrameCounter++;
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    bindCameraTexture(gl);
    float bear = Global.bearing;
    gl.glLoadIdentity();
    gl.glNormal3f(0,0,1);
    gl.glRotatef(bear, 0, 1, 0);

    int e = 0;  
    for(int q=0; q < Global.cubes;q++){
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, e, 4);
    e = e+4;
    }
}

How would I adjust my code so that it'll recognise when objects are ACTUALLY on the screen?

+1  A: 

rotate the camera - something that your camera class doesnt support - and then use its setMatrices method instead of

gl.glLoadIdentity();
gl.glNormal3f(0,0,1);
gl.glRotatef(bear, 0, 1, 0);

-> You need a better camera class