Java Swing - 计算鼠标位置与屏幕中心的角度

发布于 2025-01-13 18:29:28 字数 859 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你可以这样做。

  • 对于 500x500 的窗口大小,左上角位于点 0,0 处,右下角位于 500,500 处。
  • 正切是两点的 Y 变化相对于 X 变化的变化。也称为斜率,它是特定角度的 sincos 之比。要找到该角度,可以使用arctanMath.atanMath.atan2)。第二种方法有两个参数,如下所示。
BiFunction<Point2D, Point2D, Double> angle = (c,
        m) -> (Math.toDegrees(Math.atan2(c.getY() - m.getY(),
                c.getX() - m.getX())) + 270)%360;
        
BiFunction<Point2D, Point2D, Double> distance = (c,
                m) -> Math.hypot(c.getY() - m.getY(),
                        c.getX() - m.getX());
                
int screenWidth = 500;
int screenHeight = 500;
int ctrY = screenHeight/2;
int ctrX = screenWidth/2;

Point2D center = new Point2D.Double(ctrX,ctrY );

Point2D mouse = new Point2D.Double(ctrX, ctrY-100);
double straightAbove = angle.apply(center, mouse);
System.out.println("StraightAbove: " + straightAbove);

mouse = new Point2D.Double(ctrX+100, ctrY);
double straightRight = angle.apply(center, mouse);
System.out.println("StraightRight: " + straightRight);

mouse = new Point2D.Double(ctrX, ctrY+100);
double straightBelow = angle.apply(center, mouse);
System.out.println("StraightBelow: " + straightBelow);

mouse = new Point2D.Double(ctrX-100, ctrY);
double straightLeft = angle.apply(center, mouse);
System.out.println("Straightleft: " + straightLeft);
      

我将

StraightAbove: 0.0
StraightRight: 90.0
StraightBelow: 180.0
Straightleft: 270.0

Math.atan2 的弧度输出转换为度数。对于您的应用程序,将它们保留为弧度可能会更方便。

这是一个类似的Function,用于使用Math.hypot查找距离

BiFunction<Point2D, Point2D, Double> distance = (c,m) ->
             Math.hypot(c.getY() - m.getY(),
                        c.getX() - m.getX());

You can do it like this.

  • For a window size of 500x500, top left being at point 0,0 and bottom right being at 500,500.
  • The tangent is the change in Y over the change in X of two points. Also known as the slope it is the ratio of the sin to cos of a specific angle. To find that angle, the arctan (Math.atan or Math.atan2) can be used. The second method takes two arguments and is used below.
BiFunction<Point2D, Point2D, Double> angle = (c,
        m) -> (Math.toDegrees(Math.atan2(c.getY() - m.getY(),
                c.getX() - m.getX())) + 270)%360;
        
BiFunction<Point2D, Point2D, Double> distance = (c,
                m) -> Math.hypot(c.getY() - m.getY(),
                        c.getX() - m.getX());
                
int screenWidth = 500;
int screenHeight = 500;
int ctrY = screenHeight/2;
int ctrX = screenWidth/2;

Point2D center = new Point2D.Double(ctrX,ctrY );

Point2D mouse = new Point2D.Double(ctrX, ctrY-100);
double straightAbove = angle.apply(center, mouse);
System.out.println("StraightAbove: " + straightAbove);

mouse = new Point2D.Double(ctrX+100, ctrY);
double straightRight = angle.apply(center, mouse);
System.out.println("StraightRight: " + straightRight);

mouse = new Point2D.Double(ctrX, ctrY+100);
double straightBelow = angle.apply(center, mouse);
System.out.println("StraightBelow: " + straightBelow);

mouse = new Point2D.Double(ctrX-100, ctrY);
double straightLeft = angle.apply(center, mouse);
System.out.println("Straightleft: " + straightLeft);
      

prints

StraightAbove: 0.0
StraightRight: 90.0
StraightBelow: 180.0
Straightleft: 270.0

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 using Math.hypot

BiFunction<Point2D, Point2D, Double> distance = (c,m) ->
             Math.hypot(c.getY() - m.getY(),
                        c.getX() - m.getX());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文