public Vector3 getPickingRay(float cursorX, float cursorY){IntBuffer viewport = ByteBuffer.allocateDirect((Integer.SIZE/8)*16).order(ByteOrder.nativeOrder()).asIntBuffer();FloatBuffer modelview = ByteBuffer.allocateDirect((Float.SIZE/8)*16).order(ByteOrder.nativeOrder()).asFloatBuffer();FloatBuffer projection = ByteBuffer.allocateDirect((Float.SIZE/8)*16).order(ByteOrder.nativeOrder()).asFloatBuffer();FloatBuffer pickingRayBuffer = ByteBuffer.allocateDirect((Float.SIZE/8)*3).order(ByteOrder.nativeOrder()).asFloatBuffer();FloatBuffer zBuffer = ByteBuffer.allocateDirect((Float.SIZE/8)*1).order(ByteOrder.nativeOrder()).asFloatBuffer();glGetFloat(GL_MODELVIEW_MATRIX, modelview);glGetFloat(GL_PROJECTION_MATRIX, projection);glGetInteger(GL_VIEWPORT, viewport);float winX = (float) cursorX;// convert window coordinates to opengl coordinates (top left to bottom left for (0,0)float winY = (float) viewport.get(3) - (float) cursorY;// now unproject this to get the vector in to the screen// take the frustrm and unproject in to the screen// frustrum has a near plane and a far plane// first the near vectorgluUnProject(winX, winY, 0, modelview, projection, viewport, pickingRayBuffer);Vector3 nearVector = new Vector3(pickingRayBuffer.get(0),pickingRayBuffer.get(1),pickingRayBuffer.get(2));pickingRayBuffer.rewind();// now the far vectorgluUnProject(winX, winY, 1, modelview, projection, viewport, pickingRayBuffer);Vector3 farVector = new Vector3(pickingRayBuffer.get(0),pickingRayBuffer.get(1),pickingRayBuffer.get(2));//save the results in a vector, far-nearreturn farVector.sub(nearVector).normalize();}abcdef gave me this code. And here how I use it:

The only things I can think of right now are:1) You are applying transforms to your drawn objects in rendering but not in picking.2) You don't have the correct projection / modelview / viewport matrices setup when calling getPickingRay. Essentially are you calling it after calling glInitMatrix but before all the glFrustum, glTranslate and glRotate.

These seem unlikely and I'm probably missing something. I'll do something else for a few hours then take another look, maybe I'll see something. On a side note, in your second code extract, you call getPickingRay inside the loop, despite it being the same each time. I don't know for sure but I don't see it as being a very computationally "fast" function.

Ray rayPick = new Ray(new Vector3(control.position.x, control.position.y, control.position.z), destination);As ray begin I'm using camera position and picking works good at yaw > 90 and yaw < 180. But if I set ray begin Y position to player Y position, not camera:

But actually that couldn't be the problem, because in other game where I'm testing ray picking, I'm sure that camera position always stays in 0, 0, 0 and when creating picking ray I'm also using 0, 0, 0 as begin position. Now thinking about that, what you said:

Quote

1) You are applying transforms to your drawn objects in rendering but not in picking.

So I'm applying transforms in rendering, but not elsewhere. If that could be the problem, so where I should apply transforms elsewhere?

You need to apply the transforms to your cubes before you test them for picking. Are these transforms the viewing transforms (as in translating to camera position then rotating by camera's pitch and yaw)? If they are then it would probably be better to transform the camera position by the opposite of these. But that's up to you. Probably best to get it working before worrying too much about performance.

It shouldn't matter that the ray isn't normalized, in fact I don't think it was before. Is it not working at all now?And could you post the code that calls this method. Essentially anywhere where you do any matrix stuff.

farVector.sub(nearVector).normalize();And it worked, but now it doesn't, just if I would stay in center of world and then just sensitive, when I move mouse just per 1 pixels, so it picks through 20 cubes far.

And I'm doing matrix only before ray picking check when rendering world objects.