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?
A common reason for not being able to select the object you want is that you are already in edit mode for another object. When you are in edit mode, you can only manipulate and change the geometry and data that belongs to the object that was selected when you entered edit mode.
Select the Object Selection tool (W) from the toolbar in the workspace. In the Options bar at the top of the workspace, choose a selection Mode — Rectangle or Lasso. Hover the mouse pointer over the object in your image that you want to select and click to automatically select the object for you.
When trying to select one or more objects in an AutoCAD drawing, nothing will be selected. There might be several causes for this: The desired objects are within a viewport on a layout and the viewport is not active. The layer of the objects are locked. The edit mode of an xref is active. The program is corrupted.
When you try to add additional objects to a selection, the previously selected objects are unselected. You can only select one item at a time. Multiple objects can still be selected with selection or crossing windows, and by holding the Shift key while clicking items.
This issue occurs with all commands that require selecting objects: ERASE, COPY, TRIM, EXTEND, and so on. The PICKADD system variable is set to 0 (zero).
On the Cursor & Selection tab, clear the Use Shift key to add to selection option. Choose OK to close the Preferences window. If none of the previous fixes the issue, select all the objects in the drawing and copy them to a new file. Post a question.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With