How to make objects stick to mouse cursor

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

I set a mouse cursor and object following the cursor. The problem is, objects that follow the cursor is slow.

Codes I used is bellow

Input.set_custom_mouse_cursor("preloaded image blabla", 0, cursor_position)

and

func _process(delta: float) -> void:
    $Follower.position = get_global_mouse_position()

I know using _process is slower than Input
how to make objects directly stick to the mouse? Do I need to code in GDNative for more speed? Or make object accelerate on mouse movement?

++ changed hosting website… cus of lag
gif for detail

Your code looks fine, e.g. here’s one from a crafting game I have:

  public override void _Process(float delta)
  {
        base._Process(delta);
        Position = GetViewport().GetMousePosition();
  }

The gif looks interesting and displays some ‘lag’. So what else is going on in the scene to cause slow-down? Have you profiled the game?

spaceyjase | 2021-11-17 09:37

It just follows the mouse position frame-by-frame. Ignore that lag word pls. It was about img hosting server lag.

dewcked | 2021-11-17 10:31

Now I’, trying to interpolate the object location with Derivative

# variable for saving location, speed, acceleration and result
# past and current location of mouse
var PastMouseLocation: Vector2
var CurrentMouseLocation: Vector2

# Derivated beforePast~Past and Past~Current location
var DerivatedPastMouseSpeed: Vector2
var DerivatedCurrentMouseSpeed: Vector2

# Derivated beforePast~Past speed
var DerivatedMouseAcceleration: Vector2

# result
var InterpolatedMouseLocation: Vector2

and I set initial value in _ready function

func _ready() -> void:
    PastMouseLocation = get_global_mouse_position()
    CurrentMouseLocation = get_global_mouse_position()
    DerivatedPastMouseSpeed = Vector2(0, 0)
    DerivatedCurrentMouseSpeed = Vector2(0, 0)
    DerivatedMouseAcceleration = Vector2(0, 0)

At last, _process

func _process(_delta: float) -> void:
    # calculate position
    PastMouseLocation = CurrentMouseLocation
    CurrentMouseLocation = get_global_mouse_position()

    # calculate speed
    DerivatedPastMouseSpeed = DerivatedCurrentMouseSpeed
    DerivatedCurrentMouseSpeed = CurrentMouseLocation - PastMouseLocation

    # calculate acceleration
    DerivatedMouseAcceleration = DerivatedCurrentMouseSpeed \
     - DerivatedPastMouseSpeed

    InterpolatedMouseLocation = ???
    # Do something with CurrentMouseLocation,
    # DerivatedCurrentMouseSpeed and DerivatedMouseAcceleration

This gives me some headaches. But worth trying.

dewcked | 2021-11-17 11:42

I just tried dragging file in Windows OS folder and it definitely works like the first code I questioned about. Maybe this question is meaningless as Windows OS filesystem itself doesn’t handle about it. The more I try to interpolate, the more the object attatched to mouse pointer trembles.

dewcked | 2021-11-17 12:32

I think a ‘best of’ approach is the correct one, likely the simplest too! Good luck!

spaceyjase | 2021-11-17 13:03

:bust_in_silhouette: Reply From: Drawsi

Go to settings and under Display choose Mouse Cursor. There you can change it’s image without needing to code it yourself

Actually there’s no way to make tree of nodes stick to mouse pointer smoothly. So this is the answer. v.v

dewcked | 2021-11-18 07:20