Mouse box checking not working in the Y axis

Started by bawat, June 05, 2016, 03:20:52

Previous topic - Next topic

bawat

I have a HUD that I'm making for a 3D game using LWJGL 2 on Windows 10.
I am having some trouble making buttons, the mouse checking isn't working properly in the Y axis!
This only occurs in windowed mode.

It seems that the further away from Y=0, the more difference there is between where the button is drawn and where it's mouse checking box is.

Here is a video representing the problem
https://youtu.be/Jn_NPq5fBY8

And here is the Eclipse project file, please excuse the bad code practices. This was quite a spontaneous project and some parts use immediate mode.
http://www.bawat.net/Project.zip

One of the buttons was placed specifically to be 0.01f away from the top of the screen, it is possible to see an almost 1 pixel gap between the top of the screen and the image. This shows me that the drawing code is doing what is expected of it. Might that the problem is probably in the bounds checking.

I might just be tired, but the strange thing is that the mouse check and drawing use the same co-ordinates. If anyone could point out where I've went wrong, I'd be very great full! Any and all advice is welcome.

[edit]
After more googling with still no results, I have found these three posts experiencing the same problem.

http://forum.lwjgl.org/index.php?topic=3937.0
http://forum.lwjgl.org/index.php?topic=5671.0
http://forum.lwjgl.org/index.php?topic=2898.0

It appears that if using windowed mode, and the resolution is close to the screen resolution or larger,
then the window will be re-scaled and stretched. This causes the difference between the screen co-ordinates axis
and mouse co-ordinates axis.

Changing the window resolution to 1820 by 980 on my 1920 by 1080 monitor fixes this problem.

I would still like to find a fix to this, or a way to prevent this from happening when selecting screen resolutions.
[/edit]

public boolean isInside(float xp, float yp){
	if(xp > x - width/2.0f && xp < x + width/2.0f){
		if(yp > y - height/2.0f && yp < y + height/2.0f){
			return true;
		}
	}
	return false;
}

public boolean isMouseInside(){
	return isInside(Mouse.getX(),Mouse.getY());
}


And the drawing code uses this

Simplified
glBegin(GL_QUADS);
		glVertex2f(x-width/2.0f,y-height/2.0f);
		glVertex2f(x-width/2.0f,y+height/2.0f);
		glVertex2f(x+width/2.0f,y+height/2.0f);
		glVertex2f(x+width/2.0f,y-height/2.0f);
		glEnd();
	}


Full code
public void draw(){
		
		float middle 	= 0.5f;
		float xside 	= 0.5f;
		float yside 	= 0.5f;
		
		glColor4f(1.0f,1.0f,1.0f,1.0f);
		
		if(isMouseInside()){
			highlighted.bind();
		}else{
			normal.bind();
		}
		
		if(mirrorHorizontalAfterClick && clicks%2 == 1){
			xside = -xside;
		}
		if(mirrorVerticalAfterClick && clicks%2 == 1){
			yside = -yside;
		}
		
		glBegin(GL_QUADS);
		glTexCoord2f(
				(middle-xside)* maxX/((float)normal.getImageWidth()),
				(middle-yside)* maxY/((float)normal.getImageHeight()));
		glVertex2f(x-width/2.0f,y-height/2.0f);
		glTexCoord2f(
				(middle-xside)* maxX/((float)normal.getImageWidth()),
				(middle+yside)* maxY/((float)normal.getImageHeight()));
		glVertex2f(x-width/2.0f,y+height/2.0f);
		glTexCoord2f(
				(middle+xside)* maxX/((float)normal.getImageWidth()),
				(middle+yside)* maxY/((float)normal.getImageHeight()));
		glVertex2f(x+width/2.0f,y+height/2.0f);
		glTexCoord2f(
				(middle+xside)* maxX/((float)normal.getImageWidth()),
				(middle-yside)* maxY/((float)normal.getImageHeight()));
		glVertex2f(x+width/2.0f,y-height/2.0f);
		glEnd();
	}

Daslee

Did you inverted mouse coordinate's y value? As I remember, when I did the same thing, I had problems with y axis collision testing. Just invert it like this:
mouseY = windowHeight - originalMouseY;

bawat

Quote from: Daslee on June 09, 2016, 19:56:17
Did you inverted mouse coordinate's y value? As I remember, when I did the same thing, I had problems with y axis collision testing. Just invert it like this:
mouseY = windowHeight - originalMouseY;


I have no need to invert the mouseY and haven't done so in my code