[Library] Andralog - Analog control for Android

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
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

Post by HugoBDesigner »

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
--'r' is the analog's radius
--'b' is the button/stick radius
--'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 :P ).

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!
(2.37 KiB) Downloaded 233 times
[attachment=1]andralog.lua - For LÖVE 0.10.x![/attachment]

LICENSE: https://creativecommons.org/licenses/by-sa/3.0/
Attachments
Andralog Example_10.love
For LÖVE 0.10.x!
(3.59 KiB) Downloaded 221 times
andralog.lua
For LÖVE 0.10.x!
(5.9 KiB) Downloaded 206 times
Last edited by HugoBDesigner on Fri Jul 01, 2016 1:16 pm, edited 5 times in total.
HugoBDesigner - my Twitter
HugoBDesigner - my Blog
User avatar
Jeeper
Party member
Posts: 588
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: [Library] Andralog - Analog control for Android

Post by Jeeper »

Wow, this is actually really cool, well done!

Now I want to make a platformer game for android :D
User avatar
farvardin
Party member
Posts: 164
Joined: Sat Jun 28, 2008 6:46 pm

Re: [Library] Andralog - Analog control for Android

Post by farvardin »

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
(2.42 KiB) Downloaded 159 times
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
User avatar
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

Post by HugoBDesigner »

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

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 :ultraglee:
HugoBDesigner - my Twitter
HugoBDesigner - my Blog
User avatar
alberto_lara
Party member
Posts: 352
Joined: Wed Oct 30, 2013 8:59 pm

Re: [Library] Andralog - Analog control for Android

Post by alberto_lara »

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.
User avatar
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

Post by HugoBDesigner »

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 Twitter
HugoBDesigner - my Blog
User avatar
alberto_lara
Party member
Posts: 352
Joined: Wed Oct 30, 2013 8:59 pm

Re: [Library] Andralog - Analog control for Android

Post by alberto_lara »

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

Post by HugoBDesigner »

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

EDIT: New video: https://www.youtube.com/watch?v=yTvBMLHlutQ
HugoBDesigner - my Twitter
HugoBDesigner - my Blog
User avatar
alberto_lara
Party member
Posts: 352
Joined: Wed Oct 30, 2013 8:59 pm

Re: [Library] Andralog - Analog control for Android

Post by alberto_lara »

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 :megagrin: I think I'm going to use one or two of this ideas for GÖÖi, thanks!
User avatar
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

Post by HugoBDesigner »

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 :megagrin: 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).
HugoBDesigner - my Twitter
HugoBDesigner - my Blog
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests