[LWJGL] Wrong Y Coordinates.


    int screenWidth = 600;
    int screenHeight = 400;
    int nsqr = 40; // Full length of sides
    int hsqr = nsqr/2; // Half length of sides
    int xcoor = screenWidth / 2;
    int ycoor = screenHeight / 2;

    public void drawQuad() {
        GL11.glColor3f(0.5f,0.5f,1.0f);
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex2f(xcoor-hsqr,ycoor-hsqr);
            GL11.glVertex2f(xcoor+hsqr,ycoor-hsqr);
            GL11.glVertex2f(xcoor+hsqr,ycoor+hsqr);
            GL11.glVertex2f(xcoor-hsqr,ycoor+hsqr);
        GL11.glEnd();

    }

    public void detectMouse() {
        if(Mouse.isButtonDown(0)) {
            xcoor = Mouse.getX();
            ycoor = Mouse.getY();
            drawQuad();
        }
    }

Basically, when I click the left mouse button, it should draw a square (40x40. Mouse pointer at the center of the square).

The location when it comes to the x coordinate is fine; however, at the y coordinate, it’s all messed up.

Let’s say that if I clicked at the lower half of the screen, the square will appear at the upper half like a mirror.

In LWJGL coordinates (0,0) is the bottom left of the screen (same as OpenGL and maths in general uses by default).

Cas :slight_smile:

Pretty easy to convert to Java2D style top left 0,0 coordinates anyway.

Just do the following:

[quote]xcoor = Mouse.getX();
ycoor = screenHeight - Mouse.getY();
[/quote]

Thanks a lot. I have to keep those in mind. :smiley:

[/quote]

[quote]xcoor = Mouse.getX();
ycoor = (screenHeight-1) - Mouse.getY();
[/quote]
:persecutioncomplex:

[quote]xcoor = Mouse.getX();
ycoor = (screenHeight-1) - Mouse.getY();
[/quote]
:persecutioncomplex:
[/quote]
Damn. I’ve been making that mistake until now… Just damn…

It seems that we’re not the only ones :), the issue is quiet widespread, a bit of digging has revealed that the following libraries have also fallen foul to it LibGDX, Slick2D, Nifty Gui, GTGE, JMugen, etc. I’m sure there are probably tons more examples out there. Its really not that serious an issue, 1px off doesn’t really matter much in 2D or even noticeable to most.

I know, but in the same time, failing to make the mouse click at the correct position seems to be a so big fail that it should be illegal. I mean, it’s a mouse, it has a location and I can’t f*ckin’ get it right? Come on!

The more sensible solution is just to start thinking in terms of (0,0) being in the bottom left anyway, like we were all taught in Maths :slight_smile:

Cas :slight_smile:

I still remember drawing my sailing boat upside down when doing an coordinate system exercise in 5th grade. We got a list of coordinates and had to draw lines between them. ;D

Libgdx is fixed. Oopsie!