Omni-directional touch pad

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

Hi,
in many Godot platform game examples, to move a character using different touch buttons; one to go left, one to go right, one to go up, etc. Is there any example with an omni-directional touch pad, like a ball joystick, so go in all directions ?
Thanks in advance.
-jospic

:bust_in_silhouette: Reply From: umfk

I don’t have an example to hand but if you have used analog inputs (from a controller) for movement before it should be relatively easy to transform the touch data to the same format. You can get the continuous location of a touch event on the screen in the _input(event) function and the event should be of type InputEvent.SCREEN_DRAG. The position of the event will be stored in event.pos.

You now have to convert this pos to a direction vector that should have a maximum length of 1 (but it can be shorter if you want people to be able to walk slower than max speed). The way to do this conversion depends on how you get the input event.

The straight forward way would be to get the global position data, subtract the origin of your omni-directional touchpad and then to scale the resulting vector to the correct relative size. Another way (and I’m not sure if this would work but it would be very elegant I think) is to grab the input position data in the local coordinates of your correctly scaled touchpad so that they automatically have a max length of 1.

In both ways you must be careful to check what happens in different resolutions!

InputEvent.SCREEN_DRAG only updates on movement so make sure to keep the last known position and only reset it if you get a InputEvent.SCREEN_TOUCH where is_pressed() == false. You also have to monitor InputEvent.SCREEN_TOUCH for the initial touch but in that case only for is_pressed() == true.

If you have your direction vector ready you simply scale it with some speed variable and the delta of the process function and then apply it to your character, either with move() if you use a KinematicBody2D or by applying a force.

:bust_in_silhouette: Reply From: kubecz3k

Quite a long time ago I wrote ready to use scene for touch screen analog sticks. I don’t have instructions on how to use them (they got lost on one of the forum migration).

  1. Generally you need to add it as a child to ‘Control’ node. Width and Height of this control node will define what is the active area of given Analog.
  2. You also need to fill “Listener Node Path” property to target your node that will listen to the changes. In this node you need to have function:

analog_force_change(Vector2, Analog )

  1. Generally you will get normalized Vector2 with percentage joystick shift (range from 0-1 for x and y). And second parameter (Analog) will be handy if you will have more than one analog on the screen (you will be able to check which one is firing this function).
    There is also param that will allow you to dynamically hide analog stick when it’s not touched by the user.
    It was written quite long time ago, I will update and polish it when we will have asset store (or however it will be called) :slight_smile:

Plugin/Scene: Dropbox - analog_stick.7z - Simplify your life

Fast dirty sample project: Dropbox - analog_stick_sample.7z - Simplify your life

And here is short and poor footage on how they work:

Hope this will help!

Great! It was just what I was looking for…
Thanks a lot kubecz3k!

jospic | 2016-02-28 12:38

I used your code for the touch controls of my game Procedural Chaos

I give you credits by linking your account from this forum.
If you prefer something different or to remove your code please inform me.

kakoeimon | 2016-06-24 10:06

Hello, no problem with using my code, I’m glad you find it usefull. If you really want to give credits for me you might use my full name (Jakub Grzesik) or my site (www.kivano.net) or both, but basically you don’t need to if you don’t want :slight_smile:

kubecz3k | 2016-06-24 11:48

I don’t understand exactly how it works, but with minimal adaptation it does! Thanks dude!

garred | 2016-11-04 23:22

Thanks for sharing. Too bad the links are dead, but I’ll try to go with the explanation you gave.
Thanks!

Danicano | 2017-04-04 06:32

Links are updated now. Don’t know why, but dropbox decided to make old urls not valid anymore.

kubecz3k | 2017-04-04 07:57

You are great!
Thank you very much!!!

Danicano | 2017-04-04 10:01

Hey, you analog pad works like a charm for me! I will put references if I publish something with that!
Thanks a lot for sharing!!!

Danicano | 2017-04-22 09:48

I recently converted this code to Godot V3 and to work with 2D. I’d be happy to send it your way if you like. It worked pretty good once I figured out all the V2 to v3 changes I needed to make. I used it in my game Batty Bat.
http://geraldwashburn.com/batty-bat/ - Check it out. (Windows Version)

gswashburn | 2018-01-30 06:44

I was in dire need of this, so thank you very much kubecz3k for the original code.

I went ahead and converted it to work in Godot V3, cleaned up the code, documented it, and got it setup for the Godot Asset Store, under a MIT license.

I ensured that on the description I put both your name and your website.

The tool is currently pending aproval on the Asset Store, but will hopefully be up by the time you read this.

Search for Virtual Analog (Catagorized as a tool) to download the tool and an example project.

Once more, thanks so much!

AfterMath24 | 2019-05-23 14:17

:bust_in_silhouette: Reply From: jospic

@kubecz3k
Thanks for your precious resouces.
I tried to implement your touchpad in my project . Even if I specify the listener node it seems that it can not see it. Could you send me a small sample project ? Thank you so much !

Hello, sorry for delay, had no idea you was asking me about that (haven’t got any notification).
Anyway here is fast and dirty sample for you: https://dl.dropboxusercontent.com/u/168251/godot/analog_stick_sample.7z
Hope it will be helpful! :slight_smile:

kubecz3k | 2016-03-01 14:40

Sorry again. It 's all resolved.
Previously, I had extract the 7zip archive on my Mac and probably your project was incomplete.
Now it’s all clear. Thanks again!

jospic | 2016-03-01 15:23

:bust_in_silhouette: Reply From: gswashburn

As stated in previous reply, I converted Jacub Grzesik’s code over to Godot 3.x and 2D. I finally had chance to create a demo project for people to get up & running on this code. The demo project can be found on GitHub GitHub - gswashburn/Analog-Stick-Demo: Godot 3 - Analog Stick Demo
I will also post this in the Godot Developers Forum.
Thanks Jacub for the nice piece of code.

Nice work, thank you for your effort! :slight_smile:

kubecz3k | 2018-03-15 19:49

do you have time to help me figure out how to make those joysticks work with multi touch?

Tova | 2018-06-20 10:45

:bust_in_silhouette: Reply From: Shinneider Libanio d

I created a lib for this case, and you can select between d-pad and analog.

https://godotengine.org/asset-library/asset/714

I know that topic is kind of old, but it is one of the ones that better indexed for the subject on google.

this is really awesome. but this does not work if you have other touch buttons for example a touch button which makes player fire bullets. i am controlling the player to go left and i have my button on right side of screen so when i press the fire button while moving left it makes the player move right and fire right

RockyXDeadman | 2020-11-13 13:19