在 Java 中检测 3D 第一人称世界的鼠标移动

发布于 2024-12-21 11:47:42 字数 337 浏览 1 评论 0原文

我正在用 Java 开发第一人称游戏,我正在尝试让 3D 动作正常工作。

我的问题是我想捕获鼠标移动,但将鼠标保留在窗口内。捕获鼠标移动后,我认为将鼠标保留在窗口中的最佳方法是在移动后使用 Robot.moveMouse(x,y) 将鼠标置于窗口中心。这工作正常,但是机器人的移动会触发我的窗口中的一个事件,然后该事件被解释为正常事件,从而在世界中移动我的角色。

我尝试了各种保持状态并忽略运动的方案,直到我位于中心,但它们看起来都很挑剔,并且不能完全检测到哪些事件是用户控制的,哪些是机器人控制的。

有没有一种简单的方法可以检测到鼠标移动来自机器人?

是否有一种更简单的方法来解决我忽略的问题?

I am working on a first person game in Java, and I am trying to get the 3D movement working.

My problem is I would like to capture mouse movement, yet keep the mouse inside the window. After I capture the mouse movement, I figure the best way to keep the mouse in my window is to center the mouse in the window after moving, using Robot.moveMouse(x,y). This works fine, however the movement from the Robot triggers an event in my window which then gets interpreted as a normal event, and thus moves my character in the world.

I've tried various schemes of keeping state and ignoring movements until I am in the center, but they all seem finicky and don't quite detect which events are user vs Robot controlled.

Is there an easy way to detect that a mouse movement came from the Robot?

Is there perhaps a simpler way to solve my problem that I am overlooking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夏末的微笑 2024-12-28 11:47:42

我通过使用 JOGL 2.0 RC4 切换到 NEWT 解决了这个问题。特别是,我使用 GLWindow 和 warpPointer 而不是带有 Robot.mouseMove 的 AWT 框架。有了开关,我立刻就得到了流畅的动作。一些示例代码类似于我正在做的事情(里程可能会有所不同):

public class MyClass implements MouseListener {
    private GLWindow window;
    private int centeredX = -1;
    private int centeredY = -1;

    // ...

    public void mouseMoved(MouseEvent e) {
        if (centeredX == -1 || centeredY == -1) {
            center();
            return;
        }

        int deltaX = e.getX() - centeredX;
        int deltaY = e.getY() - centeredY;

        // ... Do something with the deltas

        centeredX = window.getWidth() / 2;
        centeredY = window.getHeight() / 2;
        window.warpPointer(centeredX, centeredY);
    }
}

I solved this by switching to NEWT with JOGL 2.0 RC4. In particular, I use GLWindow and warpPointer instead of an AWT Frame with the Robot.mouseMove. With the switch, I instantly got smooth movements. Some sample code similar to what I'm doing (mileage may vary):

public class MyClass implements MouseListener {
    private GLWindow window;
    private int centeredX = -1;
    private int centeredY = -1;

    // ...

    public void mouseMoved(MouseEvent e) {
        if (centeredX == -1 || centeredY == -1) {
            center();
            return;
        }

        int deltaX = e.getX() - centeredX;
        int deltaY = e.getY() - centeredY;

        // ... Do something with the deltas

        centeredX = window.getWidth() / 2;
        centeredY = window.getHeight() / 2;
        window.warpPointer(centeredX, centeredY);
    }
}
Saygoodbye 2024-12-28 11:47:42

好吧,我并不是 100% 相信这一点,但是您是否在鼠标事件上使用了 getsource()getComponent() 函数?他们可能会将机器人作为其来源归还。除此之外,我会有一个像布尔机器人控制这样的类变量,并且每当它控制鼠标时,将其设置为 true。然后,在 mouseListener 中执行 if(!robotControlling){...}。希望这有帮助。

编辑:如果您的应用程序中有未使用的鼠标按钮(Java 有按钮 1、按钮 2 和按钮 3),您可以让机器人按下该按钮,并在鼠标侦听器中忽略按下该代码的任何事件。 (为此使用 evt.getButton() )当然,这不完全是最干净的解决方案:P

Well, I'm not 100% about this, but have you used the getsource() or getComponent() functions on your mouse event? They may return the robot as the source of it. Barring that, I would have a class variable like boolean robotControlling and anytime it takes control of the mouse, set that to true. Then, in you mouseListener, do a if(!robotControlling){...}. Hope this helps.

EDIT: if you have unused mouse buttons in your application (Java has Button 1, Button 2 and Button 3), you could make the robot press that, and in your mouse listener ignore any events with that code pressed. (use evt.getButton() for this) Of course, thats not exactly the cleanest solution :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文