Java Swing - 计算鼠标位置与屏幕中心的角度
我正在使用 Java Swing 制作 2D 自顶向下视图射击游戏。我想计算鼠标指针与屏幕中心相比的角度,以便我的一些精灵可以看向指针,这样我就可以创建由角度和速度描述的射弹。另外,如果指针位于屏幕中间的正上方,我希望角度为 0°,如果直接向右,则为 90°,如果直接低于 180°,则直接向左为 270°。
我做了一个函数来计算这个:
public static float calculateMouseToPlayerAngle(float x, float y){
float mouseX = (float) MouseInfo.getPointerInfo().getLocation().getX();
float mouseY = (float)MouseInfo.getPointerInfo().getLocation().getY();
float hypotenuse = (float) Point2D.distance(mouseX, mouseY, x, y);
return (float)(Math.acos(Math.abs(mouseY-y)/hypotenuse)*(180/Math.PI));
}
它背后的想法是我计算斜边的长度,然后计算与所讨论的角度相反的边的长度。 2 的分数应该是我的角度的 cos
,因此将结果的 arc cos
乘以 180/Pi 应该会得到以度为单位的角度。这确实适用于上方和右侧,但直接下方返回 0,直接左侧返回 90。这意味着我目前有 2 个问题,其中我的输出域仅为 [0,90] 而不是 [0,360) 并且它是通过 y(高度)轴镜像。我哪里搞砸了?
I'm making a 2D topdown view shooter game with Java Swing. I want to calculate what angle the mouse pointer is compared to the center of the screen so some of my Sprites can look toward the pointer and so that I can create projectiles described by an angle and a speed. Additionally If the pointer is straight above the middle of the screen, I want my angle to be 0°, if straight to its right, 90°, if straight below 180°, and straight left 270°.
I have made a function to calculate this:
public static float calculateMouseToPlayerAngle(float x, float y){
float mouseX = (float) MouseInfo.getPointerInfo().getLocation().getX();
float mouseY = (float)MouseInfo.getPointerInfo().getLocation().getY();
float hypotenuse = (float) Point2D.distance(mouseX, mouseY, x, y);
return (float)(Math.acos(Math.abs(mouseY-y)/hypotenuse)*(180/Math.PI));
}
The idea behind it is that I calculate the length of the hypotenuse then the length of the side opposite of the angle in question. The fraction of the 2 should be a cos
of my angle, so taking that result's arc cos
then multiplying that by 180/Pi should give me the angle in degrees. This does work for above and to the right, but straight below returns 0 and straight left returns 90. That means that I currently have 2 problems where the domain of my output is only [0,90] instead of [0,360) and that it's mirrored through the y (height) axis. Where did I screw up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以这样做。
500x500
的窗口大小,左上角位于点0,0
处,右下角位于500,500
处。Y
变化相对于X
变化的变化。也称为斜率,它是特定角度的sin
与cos
之比。要找到该角度,可以使用arctan
(Math.atan
或Math.atan2
)。第二种方法有两个参数,如下所示。我将
Math.atan2 的弧度输出转换为度数。对于您的应用程序,将它们保留为弧度可能会更方便。
这是一个类似的
Function
,用于使用Math.hypot
查找距离You can do it like this.
500x500
, top left being at point0,0
and bottom right being at500,500
.Y
over the change inX
of two points. Also known as the slope it is the ratio of thesin
tocos
of a specific angle. To find that angle, thearctan
(Math.atan
orMath.atan2
) can be used. The second method takes two arguments and is used below.prints
I converted the radian output from
Math.atan2
to degrees. For your application it may be more convenient to leave them in radians.Here is a similar
Function
to find the distance usingMath.hypot