How to detect swipe and multitouch

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

I managed to detect multitouch using two Area2D._input_event (one area covers half screen), but now I can’t detect a swipe properly.
Using Input.is_action_pressed() and Input.is_action_released() is great for detecting swipe, although apparently it’s not capable of detecting more than one touch at a time.
So I tried detecting an InputEventScreenDrag, like this

func _on_GunArea_input_event( viewport, event, shape_idx ):
	if event is InputEventScreenTouch and not swiped:
	if event is InputEventScreenDrag:
		gun_area_swipe += event.relative
	if abs(gun_area_swipe.x) > minimum_drag or abs(gun_area_swipe.y) > minimum_drag:
		gun_area_swipe = Vector2(0, 0)

Multitouch works fine with this approach, but swipe detection is not accurate (sometimes it calls swap_guns twice at the same time, sometimes it shoots after/before a swipe).
The main problem with this approach is that with InputEventScreenDrag I can’t know how to detect when the swipe is over.
Is there a way to detect both multitouch and swipe in Godot 3.0?

:bust_in_silhouette: Reply From: DodoIta

After some struggling I managed to have both multitouch and swipe detection, here’s what I found and how I did it:

  • the InputEventScreenTouch and the InputEventScreenDrag classes are good for detecting multiple touches, as you can see from the index property. You can also use TouchScreenButton as long as you don’t need to detect a swipe gesture;
  • you can detect multitouch events using Area2D._input_event, just connect the corresponding signal to a function and write your code in there.

So I used two Area2Ds, connected the input_event signal and I wrote:

func _on_GunArea_input_event( viewport, event, shape_idx ):
	if event is InputEventScreenTouch and event.pressed:
		swipe_start = event.position
	if event is InputEventScreenTouch and not event.pressed:

Basically all you need is check if the event is an InputEventScreenTouch event type and check the status of the pressed property to see when the user starts touching and when he releases that touch. Then the calculate_swipe function checks if the input is a swipe and eventually emits a signal, as suggested in this post by another user.

I know this sounds pretty simple, but I’m sure that trying to check the pressed property did not work at all. Maybe it was because I was using an alpha build, maybe I was just burned out I missed something, but using this approach again worked with the stable. And it’s so simple I can’t believe how much time I wasted trying a workaround on my own!

Anyway, I hope this will help future newbies like me.

Thank you it helped a lot. Especially I didn’t know about the TouchScreenButton.

mustapha | 2019-03-30 07:27