Mouse Y is inverted?

Started by steve4448, January 11, 2011, 05:49:37

Previous topic - Next topic

steve4448

Hello, I've been having an issue with my 2D java game where the mouse Y is inverted, I was reading up on this for awhile and did a few pages of searches on this site and I've found that it's because LWJGL uses 0, 0 starting at bottom left.

My question is how can I fix this?
I've heard of the way to do it with glOthro, where you invert the height, but this causes everything to be drawn upside-down.
EX:
This would draw correct, but the mouse will be inverted:
glOrtho(0, width, height, 0, 0f, 0.1f);

And this would draw incorrect, but the mouse will be correct.
glOrtho(0, width, 0, height, 0f, 0.1f);


The other way I've tried is using:
mouseY = windowHeight - Mouse.getY();

Which works, but I'm wondering if there's an official way or better way to do it.

I've created an example of my code for you so if you want you can test what I mean:
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;

public class MouseTest {
	public static void main(String args[]) {
		new MouseTest();
	}
	public MouseTest() {
		try {
			//Create the window..
			DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(400, 200, -1, -1, -1, -1, 60, 60);
			org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
					"width=" + 400,
					"height=" + 200
			});
			Display.setTitle("MouseTest");
			Display.setFullscreen(false);
			Display.create();
			//GL Sets
			glDisable(GL_DEPTH_TEST);
			glOrtho(0, 400, 200, 0, 0f, 0.1f);
			//glOrtho(left, right, bottom, top, zNear, zFar)
			glViewport(0, 0, 400, 200);
			//Start the loop.
			doLoop();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void doLoop() {
		while(true) {
			Display.sync(5);
			glClear(GL_COLOR_BUFFER_BIT);
			System.out.println("MouseX: " + Mouse.getX() + ", MouseY: " + Mouse.getY());
			Display.update();
			if(Display.isCloseRequested()) {
				Display.destroy();
				break;
			}
		}
	}
}

Thanks for taking your time to read, hope you can give me some information.
~Steve

kappa

0,0 in the bottom left of the window is traditional OpenGL style.

mouseY = windowHeight - Mouse.getY();


The above is the way to go in most cases.

steve4448

Quote from: kappa on January 11, 2011, 09:23:00
0,0 in the bottom left of the window is traditional OpenGL style.

mouseY = windowHeight - Mouse.getY();


The above is the way to go in most cases.
Thanks for your post.

As I mentioned, I knew of this I was just wondering if there's another way other from I listed?

kappa

well there is not extra built in functionality into LWJGL to allow this, its a very bare bones library, so it expects you to write your own wrapper classes.

See the Slick2D library for example, it builds its own clone of the AWT input API on top of LWJGL Input.

steve4448

Quote from: kappa on January 11, 2011, 17:55:22
well there is not extra built in functionality into LWJGL to allow this, its a very bare bones library, so it expects you to write your own wrapper classes.
Okay, thanks for your help.