I have 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 the coordinates of the screen that were clicked, sets the viewport and all the matrices that it then generates. Once a ray is generated, it takes each vertex and places it in its own Vector3 , then tests whether the ray intersects any of the points. The method says that the beam crosses "SQUARE2" at certain points on the screen, regardless of whether the object is there or not. Why? What could be the reason for this? How to fix it?
edit:
He clicks on the if SQUARE2 statement every time he thinks he is "finding" an object. It only finds the intersection when I click on the "middle" screen (objects can be drawn here), but it shows only the intersection for the first if statement on the left side of the middle of the screen, and the second for the right side of the middle of the screen.
So, for example, he thinks that he touched square 2 at the points indicated by X, where Rectangle is the phoneβs screen.
_________ | | | | |XX| | | |_______|
Other methods that I call for ray tracing are in another project .. http://code.google.com/p/libgdx/source/browse/#svn/trunk/gdx/src/com/badlogic/gdx/math (in the Graphics folder there is a Perspective Camera containing getPickRay(); and Intersector , contains another).
edit2: It looks like he is choosing the right place where the objects MAY be, but currently they cannot be caused by the world spinning, so it seems to be having problems with the spinning world. This is how I rotate 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 do I configure my code to recognize when the objects on the ACTUALLY screen really are?