+8 votes

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

in Engine by (1,462 points)

5 Answers

+3 votes

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.

by (370 points)
+14 votes

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 )
  3. 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) :)

Plugin/Scene: https://www.dropbox.com/s/4tqbbi262fy8xf2/analog_stick.7z?dl=0

Fast dirty sample project: https://www.dropbox.com/s/pkplt82u1wb4y8q/analog_stick_sample.7z?dl=0

And here is short and poor footage on how they work:
https://www.youtube.com/watch?v=FSItRjcNpv4

Hope this will help!

by (1,279 points)
edited by
Great! It was just what I was looking for...
Thanks a lot  kubecz3k!

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.

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

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

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

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

You are great!
Thank you very much!!!!!!!

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!!!!!

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)

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!

0 votes

@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 !

by (1,462 points)
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! :)
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!
+5 votes

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 https://github.com/gswashburn/Analog-Stick-Demo
I will also post this in the Godot Developers Forum.
Thanks Jacub for the nice piece of code.

by (204 points)
edited by

Nice work, thank you for your effort! :)

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

0 votes

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.

by (18 points)
edited by

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

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 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 webmaster@godotengine.org with your username.