I'm working on a Java game
where I need to calculate mouse coordinates in world space while changing the resolution
of the game window. I'm currently using the following code to perform the calculation:
Game.s2wX = (int)(((mouseX - (Window.REAL_WIDTH() / 2 - Window.SCALED_WIDTH() / 2)) / s) - (-x - width/2 + (Window.SCALED_WIDTH() / s) / 2));
Game.s2wY = (int)(((mouseY - (Window.REAL_HEIGHT() / 2 - Window.SCALED_HEIGHT() / 2)) / s) - (-y - height / 2 + (Window.SCALED_HEIGHT() / s) / 2));
And the game is drawn centered on the screen using the following code:
g.drawImage(image,
Window.REAL_WIDTH()/2-Window.SCALED_WIDTH()/2,
Window.REAL_HEIGHT()/2-Window.SCALED_HEIGHT()/2,
Window.SCALED_WIDTH(), Window.SCALED_HEIGHT(),null);
I suspect that the calculation of mouse coordinates in the s2wX
and s2wY
variables is incorrect when the resolution changes
. Can someone help me understand how to properly calculate mouse coordinates in world space while accommodating changes in resolution?
It works perfectly fine on 720x480 windowed and full screen as it points 0, 0 to upper left corner. But when I try to increase the resolution, I'm having an offset. Red marker points the mouse cursor.
UPDATE: @HassanUsman is pointing out scaling factors and it's almost done, but with minor issues:
double scalingFactorX = (double) Window.SCALED_WIDTH() / Window.REAL_WIDTH();
double scalingFactorY = (double) Window.SCALED_HEIGHT() / Window.REAL_HEIGHT();
Game.s2wX = (int)(Window.SCALED_WIDTH() +(mouseX/s)+scalingFactorX - (-x - width/2 + (Window.REAL_WIDTH() /s)/2));
Game.s2wY = (int)(Window.SCALED_HEIGHT()+(mouseY/s)+scalingFactorY - (-y - height/2 + (Window.REAL_HEIGHT()/s)/2));
UPDATE (OCT 1): Modified the code again. There's a 1/2 line offset on fullscreen, but now it calculates correctly if it's fixed on 720x480 windowed.
double scalingFactorX = (double) Window.SCALED_WIDTH() / Window.REAL_WIDTH();
double scalingFactorY = (double) Window.SCALED_HEIGHT() / Window.REAL_HEIGHT();
int translationX = (Window.REAL_WIDTH() - Window.SCALED_WIDTH()) / 2;
int translationY = (Window.REAL_HEIGHT() - Window.SCALED_HEIGHT()) / 2;
lineX = (int) (((mouseX - translationX) / scalingFactorX) / s - (-x - width / 2 + (Window.SCALED_WIDTH() / (scalingFactorX * s)) / 2));
lineY = (int) (((mouseY - translationY) / scalingFactorY) / s - (-y - height / 2 + (Window.SCALED_HEIGHT() / (scalingFactorY * s)) / 2));
SOLVED (OCT 2)
double scalingFactorX = (double) Window.SCALED_WIDTH() / 720;
double scalingFactorY = (double) Window.SCALED_HEIGHT() / 480;
int translationX = (Window.REAL_WIDTH() - Window.SCALED_WIDTH()) / 2;
int translationY = (Window.REAL_HEIGHT() - Window.SCALED_HEIGHT()) / 2;
lineX = (int) (((mouseX - translationX) / scalingFactorX) / s - (-x - width / 2 + (Window.SCALED_WIDTH() / (scalingFactorX * s)) / 2));
lineY = (int) (((mouseY - translationY) / scalingFactorY) / s - (-y - height / 2 + (Window.SCALED_HEIGHT() / (scalingFactorY * s)) / 2));
Try this:
int mouseX; // Current mouse X position in screen space
int mouseY; // Current mouse Y position in screen space
double scalingFactorX = (double) Window.SCALED_WIDTH() / Window.REAL_WIDTH();
double scalingFactorY = (double) Window.SCALED_HEIGHT() / Window.REAL_HEIGHT();
int translationX = (Window.REAL_WIDTH() - Window.SCALED_WIDTH()) / 2;
int translationY = (Window.REAL_HEIGHT() - Window.SCALED_HEIGHT()) / 2;
Game.s2wX = (int) (((mouseX - translationX) / scalingFactorX) - (-x - width / 2 + (Window.SCALED_WIDTH() / (scalingFactorX * s)) / 2));
Game.s2wY = (int) (((mouseY - translationY) / scalingFactorY) - (-y - height / 2 + (Window.SCALED_HEIGHT() / (scalingFactorY * s)) / 2));
Updated Code:
double scalingFactorX = (double) Window.SCALED_WIDTH() / Window.REAL_WIDTH();
double scalingFactorY = (double) Window.SCALED_HEIGHT() / Window.REAL_HEIGHT();
int translationX = (Window.REAL_WIDTH() - Window.SCALED_WIDTH()) / 2;
int translationY = (Window.REAL_HEIGHT() - Window.SCALED_HEIGHT()) / 2;
int screenX = mouseX - translationX;
int screenY = mouseY - translationY;
Game.s2wX = (int) (screenX / scalingFactorX);
Game.s2wY = (int) (screenY / scalingFactorY);
In this code I've adjust the mouse coordinates according to world space when the resolution changes.