Mouse tracking that doesn't lag on mobile.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By yonni
:warning: Old Version Published before Godot 3 was released.

I have a node that tracks the mouse/touch position during a drag. This works basically fine, but the position tracking noticably lags behind the system’s touch position on Android.

This can be seen by having godot render a sprite on the node and turning on “visible touches” in android’s developer options.

The framerate is vsync locked at 60fps, so that shouldn’t be a problem.

I’m not sure why godot doesn’t expose a Input.get_mouse_pos() function (given that it already has Input.get_mouse_speed()) so that scripts can get the system’s mouse position directly.

Here’s my tracking code:

# Handle input events
func _input( event ):
	
	# We're interested in touch down, touch move, mouse down, mouse move
	if( event.type == InputEvent.SCREEN_TOUCH ):
		if( event.is_pressed() ):
			_touch_down()
			set_pos( event.pos )
		else:
			_touch_up()
	elif( event.type == InputEvent.SCREEN_DRAG ):
		# We only care if we're registering this touch
		if touch:
			set_pos( event.pos )
	elif( event.type == InputEvent.MOUSE_BUTTON and event.button_index == BUTTON_LEFT ):
		if( event.is_pressed() ):
			_touch_down()
			set_pos( event.pos )
		else:
			_touch_up()
	elif( event.type == InputEvent.MOUSE_MOTION ):
		# We only care if the mouse is down
		if( !Input.is_mouse_button_pressed( BUTTON_LEFT ) ):
			_touch_up() # The move event occured before the mouse-up event
		elif( touch ):
			set_pos( event.pos )


# Handle touch/mouse down
func _touch_down():
	touch = true


# Handle touch/mouse up
func _touch_up():
	if( touch ):
		touch = false
		set_pos( offscreen_position )
		emit_signal( "on_touch_release" )

Is there any way that it could be done better to avoid the lag?

Just tried to recreate laggy drag based on the code you provided but was unable to get the same result on mobile, however there is one thing you could try that comes to mind: Instead of setting your position exclusively in func _input, set a is_dragging boolean in func _input and then set your position from within func _process when is_dragging = true. Not sure if it will solve your problem, hence the reason this is a comment instead of a answer.

Sample project download: https://drive.google.com/file/d/0BwnfZQAEnciAUC1MN0pnd0kyWWs/view?usp=sharing

ericdl | 2016-08-27 04:24

Thanks for your help. Your project on my phone (LG G3 for reference) still lags noticably behind android’s native touch tracker when moving quickly. Switching the position setting to _process doesn’t help (and I’m not sure why it would). Maybe this is just some input delay inherent to the platform (perhaps android just has a delay in sending input events to applications?)

yonni | 2016-08-29 13:11

Did you solve the problem?

Skyfrit | 2016-09-07 21:48

Nope, I think it might be impossible to speed up without changes to the engine. You can check out the game I was talking about here: https://play.google.com/store/apps/details?id=com.qu_2.games.quiddity_free

yonni | 2016-09-08 20:51