How to make sprite be perfectly next to the mouse at all times?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jujumumu

In process(delta) I get the cursor position the make it the sprites position but with a little offset. When I move the cursor really fast the sprite lags behind. I want it to stay perfectly next to the cursor no matter how quick the cursor is moving. How can I accomplish that?

:bust_in_silhouette: Reply From: Zylann

This is a surprisingly hard thing to do.
There are multiple possible reasons for this lag:

One is that your mouse might be updated by the OS before Godot even gets to process the event that it moved. You can even notice the problem on sliders or windows of the OS itself, they never really are perfectly lined up with the mouse when you drag.

Second, graphics card are client-server accelerated graphics, which makes them a tiiiny bit longer to return a result than a direct (but slower) update of the screen data. I suspect the mouse does not even use accelerated graphics.

Third, Godot uses OpenGL double-buffering (triple?) to avoid flickering, which inevitably introduces one frame of lag. Desktop compositing might also play a role in this.

Fourth, Godot runs at 60 frames per second, so the time at which the result happens on screen is further delayed. You can reduce that time a little by not capping framerate and disabling v-sync, but there will still be a small delay.
Of course if your game runs too slow, that gap will increase but should then be fixed by optimizing the game.

If you want this for a custom mouse cursor, I think there are limited ways to get this working, but otherwise eliminating lag on a graphic following the mouse can be really hard, to my knowledge.

You can define a custom mouse cursor in the Project Settings (“hardware cursor”) or set it using the Input.set_custom_mouse_cursor() method), which won’t suffer from an one-frame lag compared to a “software” cursor. This is true even if V-Sync is disabled.

Calinou | 2019-08-02 16:11

So is there no way to do this?

jujumumu | 2019-08-02 16:28

:bust_in_silhouette: Reply From: looming

I has the same trouble.
At last I find a trick.
1)Hide the system cursor
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
2)create a cursor node, with two child node:
a) a cursor image like the system cursor
b) a sprite which is changeful

Without the reference substance(system cursor), the cursor and sprite moves at the same time. And players almost can not perceive the delay.