You see this in a lot of games, this kind of infinite mouse movement, where you can move your mouse around as much as you want, without ever getting stopped by the edges of your screen.
I’ve made something like this, and also made it so that the mouse movement, although getting reset each time the mouse is moved, can actually be extracted.
import java.awt.*;
Robot robot;
PVector mouse = new PVector();
boolean autoMove = false;
void setup() {
fullScreen(P3D);
mouse.set(width/2, height/2);
strokeWeight(5);
noCursor();
try {
robot = new Robot();
}
catch (Exception e) {
}
}
void draw() {
background(255);
if (!autoMove && (mouseX-pmouseX != 0 || mouseY-pmouseY != 0)) {
mouse.add(mouseX-width/2, mouseY-height/2);
robot.mouseMove(width/2, height/2);
autoMove = true;
} else {
autoMove = false;
}
point(mouse.x, mouse.y);
println(mouse.x, mouse.y);
}
As you can see, I’ve done it through the Robot class, which automatically resets the mouse whenever it is moved. However, I am disappointed by a couple of things:
(1) I was unable to make this work in a windowed mode. For that, I would need to somehow get the parameters for the window’s position on the screen in the code, but nothing helped, all the guides online were either for an older version of Processing, or too complicated for me to understand.
(2) I couldn’t use the mouseMoved() function, because the resetting of the mouse also triggers it, and that would instantly undo any saved mouse movement away from the point on screen to which the robot resets the mouse. Instead, I’ve had to create my own function, or rather, a conditional statement, which activates only if it didn’t activate the last time, and vice versa.
(3) The movement is quite laggy.
Is there any way I could improve this sketch? Mainly so that I could resolve these three points above?