Using swipes to move a character

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

I’ve managed to detect the swipe (start position and end position points).
I’ve managed to calculate the distance between the two points (so the more distance the more speed).
My problem comes when I want to give the direction of the swipe to the player’s movement. I have no clue.

Anyone can throw some light on it?

Thanks.

Since you have acquired the end position, you could have your character point and move towards it by using the look_at() method. More info here:
2D movement overview — Godot Engine (3.1) documentation in English

Is your game 2D or 3D? Do you have a top-down approach or something else?

johnygames | 2019-08-07 17:50

Thanks for your answer.
The game is 2d.
Yes, I’ve tried the look_at() method, and the player moves towards the end point and it works as expected. The problem is that it is not exactly what I’m trying to achieve, because if I swipe to the left, for example, and the player is placed more on the left of my finger, the player will move right and not left. What I want is the player moving in the same direction of my swipe, no matter where the swipe occurs on the screen. I don’t know if I explain myself well enough…

Danicano | 2019-08-07 18:15

:bust_in_silhouette: Reply From: johnygames

I think I found a way to do this. Now, I am no mathematician, but I figured that what you want is to calculate the normalized distance between two vectors and move the player using that result.

So, here’s how I implemented it:

First, I got the normalized distance between the start position and end position.

PP = (p1 - p2).normalized()# p1 is a Vector2 holding the start position, p2 is the end position

Then I updated the position of the player.

player.position = player.position-(PP*3)

Notice how I subtracted PP from the player’s current position instead of adding it.

This should work as a basis for swiping events. You could also do more advanced stuff, like calculating the angle between the swipe and the x axis and then use that to make your player face the same direction. You can read all about that here: Vector math — Godot Engine (3.1) documentation in English

Finally, there is a video I found which explains a bit more about swiping events: https://www.youtube.com/watch?v=7XlMqjikI9A

Awesome.It works like a charm, the player moves the right way! I was stucked but now I can move along.
Thanks a lot for the links to learn more too.

Danicano | 2019-08-08 16:37

Hi, I am a beginner and I wanted to try to do something like this and after a whole day of research, I couldn’t even get the detecting swipes part right. So I was wondering if you could share how did you do it.
I’m aware it might be too much to ask and don’t expect much but man I would really appreciate the help.

mauu21 | 2019-10-27 22:50

First you need a controller node, one where you can place the swipe logic. Once you have that, you can write the following in the Node’s script:

extends Node2D
var velocity = Vector2(100,-100)
var PP = Vector2(0,0)
var s1
var s2
onready var p1 = get_node('/root/Node2D/Player') # object that acts as a guide for swiping

onready var p3 = get_node('/root/Node2D/Player3') #object affected by the swipe


# Called when the node enters the scene tree for the first time.
func _ready():
       pass

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _input(event):
	if event.is_action_pressed("mouse_click"):
		s1 = get_global_mouse_position() # get initial position
	if event.is_action_released("mouse_click"):
		s2 = get_global_mouse_position() # get final position on release of mouse button
		PP = (s1 - s2).normalized()  # obtain the movement vector 

func _process(delta):

	#print(PP)
	p3.position = p3.position-(PP*16) # move player by mutiplying the vector we got by a factor of your choice (I chose a speed of 16)
	
	

johnygames | 2019-10-29 03:54