+1 vote

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

Godot version 3.4
in Engine by (236 points)
edited by

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?

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

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.

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.

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

1 Answer

0 votes
Best answer

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

by (110 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.