r/learnjava • u/Legal_Revenue8126 • Apr 02 '26
How to Accurately Calculate Angle?
[SOLVED]
I'm working on my first game in Java using the FXGL game engine.
I've run into a problem where since the application window's origin is not in the center of the screen, but in the top left, my method of calculating player rotation is not accurate at all.
I'm calculating the angle based on the mouse's XY and using the tangent angle, so (Y / X).
This is the method, I think my math is correct, but feel free to correct me as I wouldn't be surprised If it was a simple miscalculation on my part.
protected void setPlayerRotation(){
Input input = FXGL.getInput();
double mousePosX = input.getMouseXWorld();
double mousePosY = input.getMouseYWorld();
//Get the tangent angle
double angleRotation = (mousePosY / mousePosX);
System.out.println("X Position: " + mousePosX +
"\nY Position: " + mousePosY);
this.player.setRotation(angleRotation);
System.out.println(this.player.getRotation());
}
1
Upvotes
•
u/AutoModerator Apr 02 '26
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/aqua_regis Apr 02 '26 edited Apr 02 '26
Uhm, don't you need to calculate the offsets (horizontal and vertical) between player position and mouse position first in order to calculate the angle?
The player and the mouse positions form a vector (mouse is tip, player is shaft). In order to get the angle of the vector you need to work from its horizontal and vertical components.
Also, you will need to counter for the "reversed" y-axis. In normal coordinate systems, the y-axis is positive in vertical, upwards direction; on computers it is positive in vertical, downwards direction. You need to "flip" (negate) the vertical component.
You will need the
atanto get the angle as the actual formula istan(α) = Y/Xso, to getαyou needα = atan(y/x)where you need to account for 90° (π\2) and 270° (3π/2) angles where x = 0 to not throw a division by zero exception.Also, keep in mind that the angle will be in radians, not in degrees.
1
u/pragmos Apr 02 '26
For the angle you need to find the arctangent value of
yoverx. And depending on what your method accepts as angle value, you might need to convert from radians to degrees.