Using robot JavaFX keyevents is delayed when movemouse()
I'm trying to write some kind of first person view 3D game in JavaFX, and the motion effect is good so far
I'm using JavaFX's built - in shape and perspective camera classes to render images into the scene
However, the input delay of all key events is about 1-2 seconds
Lag only:
>Once the program switches to full screen (even if you switch back to the window, they still exist, but not when the program starts) > rotate the camera with the mouse shortly before pressing / releasing the button
For example, I press w to move forward and move the mouse at the same time If I release w now, the player can still move forward for 1-2 seconds It will not happen without moving the mouse
This is how input is tracked:
scene.setOnKeyPressed(e -> keyPressed(e)); scene.setOnKeyReleased(e -> keyReleased(e));
And (for keyreleased (E), it is just the same as "false")
private void keyPressed(KeyEvent e) { if (e.getCode().toString() == "W") w = true; if (e.getCode().toString() == "A") a = true; if (e.getCode().toString() == "S") s = true; if (e.getCode().toString() == "D") d = true; }
For camera movement, I use this feature:
private void mouseMovement(){ if(primaryStage.isFocused()){ mxdelta = MouseInfo.getPointerInfo().getLocation().getX() - centerx; mydelta = MouseInfo.getPointerInfo().getLocation().getY() - centery; // Rotate Camera cry += mxdelta * sens; // cry = camera rotation around y-axis crx -= mydelta * sens; // crx = camera rotation around x-axis // move curser back to the center of the screen robot.mouseMove(centerx,centery); } }
If I delete "robot. MouseMove (centerx, center);" business as usual. In addition, if I control the camera motion through the arrow keys, there is no lag
The whole game loop is included in JavaFX's animation timer, which provides a stable frame rate
Another small observation: as the program has just been launched, there are many small stutters Once I switch to the full screen program, the program runs smoothly (although the input lag is of course. But the mouse has no input delay at all)
If necessary, I can release the complete code. If the problem is not clear enough, I can upload the video
Solution
So I found that running the whole main / game loop in another thread can obviously solve the problem Without input lag, stuttering disappeared
If someone is interested in my solution, it is:
First, I packed all the contents of the function "mainloop()" in the game loop, which looks like the following:
public void mainloop(){ mouseMovement(); // <- full function is in the question above updatePosition(); collisionDetection(); }
Then I created a new thread and just put mainloop () in it:
Thread t = new Thread() { public void run() { Platform.runLater(new Runnable() { public void run() { mainloop();} }); } }; t.setDaemon(true);
Finally, use animationtimer to call the thread:
new AnimationTimer() { @Override public void handle(long Now) { t.run(); } }.start();
I'm not sure if this is an effective way to solve this problem, but it seems to be working