简体   繁体   English

Java中的鼠标位置

[英]Mouse location in java

I am developing a first person shooter in Java and I want to implement controls in which movement of the mouse rotates the player. 我正在用Java开发第一人称射击游戏,我想实现鼠标移动旋转播放器的控件。 However in Java, I can only get mouse coordinates by using MouseListener events, so the coordinates will stop changing once the mouse cursor leaves the monitor edge and I will be unable to turn the player's view. 但是,在Java中,我只能通过使用MouseListener事件获取鼠标坐标,因此一旦鼠标光标离开监视器边缘,坐标将停止更改,并且我将无法打开播放器的视图。

Any tips/suggestions on how to do that? 有关如何操作的任何提示/建议? Thanks. 谢谢。

In some games, on every mouse movement event the cursor is moved back to the middle of the screen, and the view moves with the corresponding magnitude and direction of the mouse event. 在某些游戏中,在每次鼠标移动事件中,光标都移回到屏幕中间,并且视图以鼠标事件的相应大小和方向移动。 You can get that vector by calculating the offset of the cursor position to the center of the screen prior to centering the cursor. 您可以通过在居中光标之前计算光标位置到屏幕中心的偏移量来获得该矢量。 To move the cursor back to the center of the screen you can try using the java.awt.Robot class. 要将光标移回屏幕中心,可以尝试使用java.awt.Robot类。

Since you're building a first person shooter, you'll probably want to hide the center locked cursor, and draw your own crosshair where the player is intending to aim. 由于您正在构建第一人称射击游戏,因此您可能需要隐藏中心锁定的光标,并在玩家打算瞄准的位置绘制自己的十字准线。 That will also involve keeping track of where the cursor should be based on the running total of previous mouse movement events. 这还将涉及基于先前鼠标移动事件的运行总数来跟踪光标位于的位置。

If you want to achieve behaviour where the view will continue moving relative to the starting position of the mouse (even once the mouse has stopped moving), you could keep a moving sum of all the previous mouse movement vectors, and move the view correspondingly once every frame. 如果要实现视图将相对于鼠标的起始位置继续移动的行为(即使鼠标停止移动,即使您停止移动),也可以保留所有先前鼠标移动矢量的移动总和,并相应地移动视图一次每帧。 However, this probably applies more to something like a flight simulator than a first person shooter. 但是,这可能比第一人称射击游戏更适用于飞行模拟器之类的东西。

I tried using a java.awt.Robot as AerandiR suggests, but there were a couple of problems I ran into, and it's possible other people will run into them as well, so I will elaborate. 我按照AerandiR的建议尝试使用java.awt.Robot ,但是遇到了一些问题,其他人也可能会遇到这些问题,因此我将详细说明。

If your goal is to keep the cursor in one position (preferably the center of the screen), then you will want to call something like robot.mouseMove(width/2, height/2); 如果您的目标是将光标保持在一个位置(最好是屏幕的中心),那么您将需要调用类似robot.mouseMove(width/2, height/2); at the end of your mouseMoved() method. mouseMoved()方法的末尾。 With this implementation, every time the mouse is moved off center, the Robot will move it back to the center. 通过此实现,每次将鼠标移离中心时, Robot都会将其移回中心。

However, when the Robot re-centers the mouse, the player will turn back to where it was. 但是,当Robot手将鼠标重新居中时,播放器将返回到原来的位置。 In effect, the player will stutter between the original position and a turned position. 实际上,玩家将在原始位置和转身位置之间口吃。

To fix this, instead of defining how far your player turns on the difference between where the mouse is now and where it was, define it as the distance from the center. 要解决此问题,无需定义播放器开启鼠标现在与当前位置之间的距离的距离,而是将其定义为距中心的距离。

Like so: turnAmountX += e.getX() - width/2; 像这样: turnAmountX += e.getX() - width/2;

Now, if the Robot re-centers the mouse, e.getX() - width/2 will always yield zero. 现在,如果Robot手将鼠标重新居中,则e.getX() - width/2将始终产生零。

Recap: 概括:

    void mouseMoved(MouseEvent e) {
        turnAmountX += e.getX() - width/2;
        turnAmountY += e.getY() - height/2;
        robot.mouseMove(this.getLocationOnScreen().x + width/2, 
            this.getLocationOnScreen().y + height/2;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM