I'm starting with a screen resolution of 1280 x 960
and that is the default resolution where mouse aiming is calculated thus:
angleRad = (float) (Math.atan2(screenX - (screenWidth / 2), screenY - (screenHeight / 2)));
angle = (float) Math.toDegrees(angleRad - Math.PI / 2);
angle = Math.round(angle) <= 0 ? angle += 360 : angle;
if (Math.round(angle) == 360)
angle = 0;
Which all works fine. But when the game is resized to some resolution with a vastly different ratio to the default, like for example 2560 x 1440
at full screen in my case, the aiming is off. The resize method is like this:
@Override
public void resize(int width, int height) {
game.getViewport().update(width, height);
control.screenWidth = width;
control.screenHeight = height;
}
Where control.screenWidth
and control.screenHeight
are used in the above angle calculation and as the set resolution in other areas. The angle calculation when resized is roughly correct but tends to get further and further off course the further the aim is away from the player.
If the player aims straight up, down, left or right (N, S, W or E basically) then the aim is dead on, but veers off as aiming goes away from the player towards a screen edge - no doubt due to the resolution ratio difference.
We have these ratio differences which no doubt affect the angle calculation:
1280x960 1.33 ratio
2560x1440 1.77 ratio
But I'm not sure how to take into account these ratios to adjust the angle calculation accordingly in my code?
The crosshair and bullets are both off position. In the screenshot below the crosshair position in comparison with the mouse pointer can be seen. This is for resolution 2560x1440
. The code for the crosshair is this:
point = angleToVector((float) (angleRad + Math.PI)).scl(10000);
gs.getWorld().rayCast(callBack, pos, point);
angleRad
is the mouse angle in radians, pos
is the vector position of the player and point
is the vector target position. This is the raycast callback
private RayCastCallback callBack = new RayCastCallback() {
@Override
public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
collision.set(point);
return fraction;
};
pos
is defined. I would strongly recommend (again) that you edit your question to include a Minimal Complete Verifiable Example. That is: every step that we would need to reproduce this problem in a new project. Once we can reproduce the problem, we can test potential fixes to be sure they'll work for you. \$\endgroup\$ – DMGregory♦ Aug 25 '20 at 16:21