Lock mouse on game screen

Like in topic. Currently I am using Robot class to lock mouse on game screen, but this is not something I want, because it can’t do it smoothly (in the way user can’t see mouse “jumps”).

This is my code (inside main game loop):

if (xAbs<=Display.getX()+10) {
            robot.mouseMove(Display.getX()+10, yAbs);
            xAbs = MouseInfo.getPointerInfo().getLocation().x;
            yAbs = MouseInfo.getPointerInfo().getLocation().y;
        }
        if (yAbs<=Display.getY()+30) {
            robot.mouseMove(xAbs, Display.getY()+30);
            xAbs = MouseInfo.getPointerInfo().getLocation().x;
            yAbs = MouseInfo.getPointerInfo().getLocation().y;
        }
        if (xAbs>=Display.getX()+Display.getWidth()-10) {
            robot.mouseMove(Display.getX()+Display.getWidth()-10, yAbs);
            xAbs = MouseInfo.getPointerInfo().getLocation().x;
            yAbs = MouseInfo.getPointerInfo().getLocation().y;
        }
        if (yAbs>=Display.getY()+Display.getHeight()) {
            robot.mouseMove(xAbs, Display.getY()+Display.getHeight());
            xAbs = MouseInfo.getPointerInfo().getLocation().x;
            yAbs = MouseInfo.getPointerInfo().getLocation().y;
        }

Is there any way to do it “smoothly”?

Well, at what rate is this code executing? And what do you mean by “jump”? Is the mouse cursor snapping back and forth between the desired position and the actual mouse position?

It is executing about 60 times per second, but FPS may vary. By “jumping” I mean that when you want to leave “box” and move mouse fast enough, you can see how mouse leave game screen and comes back to it one frame later.

Reposition the mouse cursor every frame, to the center of the viewport.

What’s wrong with [icode]Mouse.setGrabbed(true);[/icode]?

Now there is no mouse on screen.

If the mouse is snapping on the mouse all the time, why would you want to draw the mouse anyways?

I want to see cursor, but it must be “trapped” inside game screen.

Why not hide the mouse with Mouse.setGrabbed(true), and then create a cursor texture, render it and make it follow the position of the mouse with Mouse.getX() and Mouse.getY() . Hope that helped! :slight_smile:

As far as I know, this does only help a little bit, since the Mouse.getX() returns negative values very soon, if you move the mouse to the left. So if you’d draw a cursor directly at the Mouse.getX() position, it would be drawn outside the window.

What could possibly work is:
Set mouse to grabbed,
If the mouse is outside the window on the left, set the x position to the leftmost position,
If the mouse is outside the window on the top, set the y position to the topmost position,
If the mouse is outside the window on the right, set the x position to the rightmost position,
If the mouse is outside the window on the bottom, set the y position to the bottommost position,
Render mouse.

Just use Mouse.getDX() and Mouse.getDY() to see how much the mouse traveled in the last frame :point:

http://www.2shared.com/file/LaFt_tB_/CursorTestRUN.html
Download that. I hope that is what you mean. Made by me in 10 minuets! Source code: http://pastebin.java-gaming.org/e08aa0f4936 ;D

Very big thanks, this is exacly the effect that I want to obtain. :wink: