0
votes

This video will show my current situation, and I currently can't find any answers to it online.

https://www.youtube.com/watch?v=O8Mh-1Emoc8&feature=youtu.be

My Code:

    public Vector3D pickBlock() {
    glDisable(GL_TEXTURE);
    IntBuffer viewport = BufferUtils.createIntBuffer(16);
    FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
    FloatBuffer projection = BufferUtils.createFloatBuffer(16);
    FloatBuffer winZ = BufferUtils.createFloatBuffer(1);
    float winX, winY;
    FloatBuffer position = BufferUtils.createFloatBuffer(3);
    glGetFloat(GL_MODELVIEW_MATRIX, modelview);
    glGetFloat(GL_PROJECTION_MATRIX, projection);
    glGetInteger(GL_VIEWPORT, viewport);
    winX = (float)Display.getWidth() / 2;
    winY = (float)viewport.get(3) - (float)Display.getHeight() / 2;
    glReadPixels(Display.getWidth() / 2, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, winZ);
    gluUnProject(winX, winY, winZ.get(), modelview, projection, viewport, position);

    glEnable(GL_TEXTURE);
    return new Vector3D(position.get(0) / 2 + 0.5f, position.get(1) / 2 + 0.5f, position.get(2) / 2 + 0.5f);
}

It returns "/ 2 + 0.5f" because that is needed because of the offsets I have for the blocks (if I removed the 0.5f, the offset would be in the center instead of the corner)

1

1 Answers

0
votes

I seams to me that the error, based on the video, comes from when you are facing in the positive z direction (or whatever your back direction is). My guess is that you aren't taking the facing direction into account as I see in your code that you are just adding a constant 0.5F to the position of your cursor.

Therfore, when you are facing backwards, it adds 0.5 which makes it be behind the wall (since back is negative Z). one simple check would be weather the Z component of your forward vector is positive or negative, and deciding the factor added to the cursor based on that, then doing the same for the X.

Depending on how you implemented your camera (IE: if you used Euler angles (rx, ry, rz) or if you used Quaternions / forward vectors), the way you would do that check would vary, feel free to ask me for examples based on your system if you need.

hope this helped!

PS: if you're using angles, you can either check for the range of the y-axis rotation value and determine which direction you are facing and thus weather to add or subtract, OR you can calculate the forward vector based on your angles, and then check the for sign of the component.