This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+4 votes

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?

in Engine by (208 points)

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?

3 Answers

+12 votes

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.

by (36 points)

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()

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:

0 votes

well its been 4 years and i think u got answer
but if anyone had this question u can use this

by (22 points)
reshown by

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:


using the camera since I'm 'drawing' in a Node.

0 votes

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
by (14 points)
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 Frequently asked questions and 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 [email protected] with your username.