[Godot 3.0.2] Get Global Position from Touchscreen?

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

Hello! I was trying to get the global position based on where the finger has touched on a mobile touchscreen, somewhat similar to the function: get_global_mouse_position() where the mouse global position is captured. I thought this was done by using this code:

func _input(event):

	if event is InputEventScreenDrag:
		touch_position = event.position

The touch_position represents the Vector2() coordinates based on the event.position. Unfortunately though this only captures the position the finger is touching on the screen. Say that my resolution screen size is set at 400x224 pixels then I can only get the position in that range such as 345x120 or 20x200 pixel position. is there a better way to get the global position capture based on the position of the touchscreen resolution limit?

Unfortunately though this only captures the position the finger is touching on the screen.

I’m not certain 100% what you’re asking for.

  1. Do you want a more precise location of the finger touch?
  2. Is the location you get not accurate enough?
  3. Do you want to require that the user make more accurate finger placement?

jitterbeetle | 2018-05-16 17:21

:bust_in_silhouette: Reply From: parradoxSVK

For anyone wondering, you have to use transformation matrix to get world coordinates from touch position.

world_position = get_canvas_transform().xform_inv(event.position)

It took me quit a while to figure it out. So i hope it will help you save some time.
Basically this solution is is the same as get_global_mouse_position(). But that function cannot be used when you are working with multitouch inputs.

Is this supposed to work when using a camera2D? I’m getting completely different coordinates with to with get_canvas_transform().xform_inv(event.position) than I am with get_global_mouse_position()

Ben Nicholl | 2020-03-19 03:32

For anyone who has this problem, this works in the cases where event.position is returning a vector relative to the viewport and you need the global position of it:

target = get_canvas_transform().affine_inverse().xform(event.position)

I found it here: https://github.com/godotengine/godot/blob/master/scene/main/canvas_item.cpp

rseonp | 2020-05-28 01:59

:bust_in_silhouette: Reply From: WhiteWolf_Sim

well its been 4 years and i think u got answer
but if anyone had this question u can use this
to_global(event.position)

Thank you! This helped me simplify my method which involved:

 onready var dim = $cam.get_viewport_rect().size 

multiplied by the event.position on mobile which where 0 - 1.

I had to use:

 $cam.to_global(event.position) 

using the camera since I’m ‘drawing’ in a Node.

poetaster | 2022-11-07 11:25

:bust_in_silhouette: Reply From: WardensDev

Godot 4 has just been released, and I wanted to update the answer to this question since it’s something I’m working with at the moment.

Nowadays you can get the world position from the event by doing this:

if event is InputEventScreenTouch:
    var world_position = get_canvas_transform().affine_inverse().translated(event.position).origin
2 Likes

I still can’t find a solution to extract values where event.position == get_global_mouse_position().
I’ve added the event to the player and called the player on a map, but I can’t get the same value. I’ve tried all your combinations… but nothing