## [Library] Andralog - Analog control for Android

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

### [Library] Andralog - Analog control for Android

Hello again! This time, I decided to share a quick library I worked on for a friend's game port to Android. Basically, it adds a simple analog input to control a character or whatever you're playing at by simply dragging the analog button/stick and moving it around. Unlike button inputs, you don't have to tap it or keep your fingers in it all the time. With this library, you can move your fingers far from the analog's center. Since I didn't make it with a library in mind, I just made a quick conversion of it into a library, which may not have been the best, to be honest.
Anyways, here's a video demonstrating it:

To load and use it, simply require the "andralog" file as you'd do with any other. Next, assemble a variable to it:

Code: Select all

myAnalog = newAnalog(x, y, r, b, d)
--'x' is the analog's center x
--'y' is the analog's center y
--'d' is the analog's deadzone. Varies from 0 to 1, 0 being no deadzone and 1 being whole analog as deadzone
These are the callbacks required for this to work:
myAnalog.update(dt) on love.update
myAnalog.draw() on love.draw
myAnalog.touchPressed(id, x, y, dx, dy, pressure) on love.touchPressed
myAnalog.touchReleased(id, x, y, dx, dy, pressure) on love.touchReleased
myAnalog.touchMoved(id, x, y, dx, dy, pressure) on love.touchMoved

And these are the variables that you can change after you've created your analog:
myAnalog.spring - If true, the analog stick returns to the center after being released. If false, stays in place and retains movement output. Defaults to true.
myAnalog.reclick - If true, you can catch the stick after being released before it reaches the center (with spring on). If false, the stick can only be grabbed again once it reaches the center. Defaults to true.
myAnalog.limitedRange - If true, doesn't change the stick's angle if you move it further than its effect radius. If false, moves it around as if it was being moved within its effect radius. Defaults to false.
myAnalog.rangeRelease - If true, releases the stick once you move it further than its effect radius (with spring on). If false, continues taking actions based on the previous setting. Defaults to false.
myAnalog.pressure - Defines the minimum pressure based on the sensitivity of the touchscreen required to move the stick. Some touchscreens don't have pressure sensitivity, so its value is set to 1. Defaults to 0.5.
myAnalog.releaseSpeed - Defines the time (in seconds) it takes for the stick to reach the center once released (with spring on). Defaults to 0.2.

You can always replace the draw function to whatever you prefer. The one I did includes 3 functions I wrote to make it fancy, so you're free to use those as well, which I'm publishing on the "Small Useful Functions" thread (which SURPRISE, it's mine too ).

Last, but not least, the functions you can use to get info about the controls:
myAnalog.getX() - returns a number from -1 to 1 regarding the stick's horizontal direction
myAnalog.getY() - returns a number from -1 to 1 regarding the stick's vertical direction
myAnalog.isHeld() - returns true or false about whether or not the stick is being held

I'm including a download to the video's example as well, so you can experiment a bit. It was made specifically for LÖVE Android, but I made a quick converter from mouse input to touch controls. Also, this library should support having two or more analogs working individually of each other, even though people will rarely even use 2 analogs.

[attachment=0]Andralog Example_10.love - For LÖVE 0.10.x![/attachment]
Andralog Example.love
For LÖVE 0.9.x!
[attachment=1]andralog.lua - For LÖVE 0.10.x![/attachment]

Attachments
Andralog Example_10.love
For LÖVE 0.10.x!
andralog.lua
For LÖVE 0.10.x!
Last edited by HugoBDesigner on Fri Jul 01, 2016 1:16 pm, edited 5 times in total.
HugoBDesigner - my Blog

Jeeper
Party member
Posts: 587
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

### Re: [Library] Andralog - Analog control for Android

Wow, this is actually really cool, well done!

Now I want to make a platformer game for android

farvardin
Party member
Posts: 164
Joined: Sat Jun 28, 2008 6:46 pm

### Re: [Library] Andralog - Analog control for Android

thanks for this. It didn't work with Löve 0.10, so I've tried to convert the deprecated function to the new ones (or the replacements). I'm not an expert in lua, so it might not be all correct, but it seems to work. Is there a way to simulate a button press (like a firebutton)?
andralog_0-10.love
Why the Löve team doesn't keep old function in later versions until people take their time to upgrade their code?

We can see this kind of things often:
Available since LÖVE 0.9.0 and removed in LÖVE 0.10.0
it's strange to add new syntax then remove them completely in next version.

Sometimes there are links to equivalents, sometime there's nothing, like some variants of https://love2d.org/wiki/love.graphics.newMesh

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

### Re: [Library] Andralog - Analog control for Android

farvardin wrote:thanks for this. It didn't work with Löve 0.10, so I've tried to convert the deprecated function to the new ones (or the replacements). I'm not an expert in lua, so it might not be all correct, but it seems to work.
Hey farvardin, thanks for trying it out! After your comment, I decided to update the code as well, just so I could republish it for 0.10.x.
The main post was updated with the proper 0.10.x library, plus the updated example

farvardin wrote:Is there a way to simulate a button press (like a firebutton)?
I didn't include that because there are plenty of button libraries out there, but if you want, I can include something like that in the example, so you can use the code

farvardin wrote:Why the Löve team doesn't keep old function in later versions until people take their time to upgrade their code?

We can see this kind of things often:
Available since LÖVE 0.9.0 and removed in LÖVE 0.10.0
it's strange to add new syntax then remove them completely in next version.

Sometimes there are links to equivalents, sometime there's nothing, like some variants of https://love2d.org/wiki/love.graphics.newMesh
Most of the time they don't actually delete it, but rather rename it to make it easier for users. For example, love.graphics.setStencil becomes love.graphics.stencil and love.graphics.setStencilTest, or love.graphics.setDefaultImageFilter becomes love.graphics.setDefaultFilter. I will say, however, that I don't really know why they removed functions such as love.window.getWidth or love.window.getHeight, but we can still get around that with love.window.getMode (even though that adds an extra step to the code).

Anyways, I'm glad you liked my small library
HugoBDesigner - my Blog

alberto_lara
Party member
Posts: 349
Joined: Wed Oct 30, 2013 8:59 pm

### Re: [Library] Andralog - Analog control for Android

Cool! Question: can I press a button (e.g. to fire something) while I'm moving the analog control? or move two "sticks" at the same time? (in other words, multitouch support) That would be nice

As a tip: it would nice as well to have a parameter to disable the "spring" mechanism so the stick would stay in the direction you leave it.

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

### Re: [Library] Andralog - Analog control for Android

As far as I'm concerned, it should be possible. This library has touch id detection, so if you're holding something, then press the analog, it should still function. The same applies to pressing buttons while holding the analog (as long as your button library has touch id detection as well).

As for the spring mechanism, there are ways to get around it currently, but if you want, I can get a new update with a few more customization settings
HugoBDesigner - my Blog

alberto_lara
Party member
Posts: 349
Joined: Wed Oct 30, 2013 8:59 pm

### Re: [Library] Andralog - Analog control for Android

HugoBDesigner wrote:As far as I'm concerned, it should be possible. This library has touch id detection, so if you're holding something, then press the analog, it should still function. The same applies to pressing buttons while holding the analog (as long as your button library has touch id detection as well).

As for the spring mechanism, there are ways to get around it currently, but if you want, I can get a new update with a few more customization settings
Sure I'll show you a library I've been making: https://github.com/tavuntu/gooi

Since it's mainly oriented to Android, I added a joystick as the one you created, please feel free to take a look, I hope this helps you!

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

### Re: [Library] Andralog - Analog control for Android

Hey, I updated the library once again, as well as the example file. I added four different new variables that control the analog's behavior. Everything is detailed and explained in the first post, which was updated with new info and new downloads. I'm uploading an updated video, will edit as soon as it finishes uploading

HugoBDesigner - my Blog

alberto_lara
Party member
Posts: 349
Joined: Wed Oct 30, 2013 8:59 pm

### Re: [Library] Andralog - Analog control for Android

myAnalog.releaseSpeed - Defines the time (in seconds) it takes for the stick to reach the center once released (with spring on). Defaults to 0.2.
I really liked that one because we can use it to simulate inertia on the object we're controlling I think I'm going to use one or two of this ideas for GÖÖi, thanks!

HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

### Re: [Library] Andralog - Analog control for Android

alberto_lara wrote:
myAnalog.releaseSpeed - Defines the time (in seconds) it takes for the stick to reach the center once released (with spring on). Defaults to 0.2.
I really liked that one because we can use it to simulate inertia on the object we're controlling I think I'm going to use one or two of this ideas for GÖÖi, thanks!
No problem, just keep in mind that it is only an aesthetic thing. I could add a "persistence required" variable to make it so that the speed slowly decreases, but I don't think that'd be such an issue, since people could just work around it manually (detect when the stick is released, then slowly decrease the speed of the object).