Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Virtual Joystick Module For Games
Started by Appvism Mar 20 2013 01:26 PM

17 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Appvism

[GLOBAL: userInfoPane.html]
Appvism
  • Enthusiast

  • 84 posts
  • Corona SDK

Hello,

 

At this moment in time, what would you say is the best external module to use for a virtual joystick (360 degree movement) for a game? This is for  player control in e.g. a shmup shooting game...

 

I've come across one or two from the code section previously, but can't remember where now or which was better. Can anyone recommend one based on their own experience - perhaps one you used yourself in your own game? I want to know which one you think works best - in terms of responsiveness, movement precision and how adaptable is it to skin and taylor the variables to adapt it for your own game.

 

Appreciate any feedback, cheers!



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,380 posts
  • Enterprise

Ive used this one from the Community Code exchange in a couple of projects.  It's from the creators of Particle Candy.

 

http://developer.coronalabs.com/code/simple-analog-stick-joystick-module



[TOPIC: post.html]
#3

Appvism

[GLOBAL: userInfoPane.html]
Appvism
  • Enthusiast

  • 84 posts
  • Corona SDK

Thanks for the reply, ley me take a look at this. From a user/gamer perspective, do you find this works well when using it on a real device? Does it feel precise and responsive?

Thanks,

[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,380 posts
  • Enterprise

The apps I've used them in it seems responsive enough. 



[TOPIC: post.html]
#5

bubblebobble

[GLOBAL: userInfoPane.html]
bubblebobble
  • Contributor

  • 309 posts
  • Corona SDK

I too have used the analog stick Rob mentioned within 2games, I currently have a game in development which is also utilising it. However although very good have switched too Roaming gamers SSK libraries which include a joystick and lots of other input devices. whichever you choose will work and both very good.



[TOPIC: post.html]
#6

Appvism

[GLOBAL: userInfoPane.html]
Appvism
  • Enthusiast

  • 84 posts
  • Corona SDK

Thanks for the alternative suggestion, i'll take a look at it later. Tried the analog joystick example above - currently only testing on the corona simulator and on that it feels ok ish, although because i'm using the trackpad on my laptop, i can't quite get a proper feel of this because i sometimes find the mouse "scrolls away from the joystick". I'll need to test on real device with my finger for sure for a proper review!

 

I did start a small test to try and build a game using a joystick and i will definitely need this control at some point in the future.

 

Anyone else have any other suggestions/recommendations?

 

Cheers,



[TOPIC: post.html]
#7

Matthew Pringle

[GLOBAL: userInfoPane.html]
Matthew Pringle
  • Contributor

  • 660 posts
  • Corona SDK

I made this one age ago for Corona http://developer.coronalabs.com/code/joystick

 

The code might need updating if the SDK has changed, ive been away from Corona Dev for a bit.

 

There are a few examples so you can test it out.



[TOPIC: post.html]
#8

Appvism

[GLOBAL: userInfoPane.html]
Appvism
  • Enthusiast

  • 84 posts
  • Corona SDK

[TOPIC: post.html]
#9

LairdGames

[GLOBAL: userInfoPane.html]
LairdGames
  • Contributor

  • 969 posts
  • Corona SDK

I too use the code mentioned by Rob. If you want to check how good it work on a device check out my app (free)

 

 

https://itunes.apple.com/app/id590540185?mt=8

 

(shameless plug ...LOL) It has been long time but I think I had to use "soft" filter to avoid jitter and have better control. Frankly it has been so long i am not sure anymore. A lot of people said to me that they feel that the joystick is very responsive during gameplay. I just hope the author would get some time to remove the:

 

"module(..., package.seeall)"

 

Still it is a great module (as the one from Matthew!)

 

Good luck!

 

Mo



[TOPIC: post.html]
#10

Appvism

[GLOBAL: userInfoPane.html]
Appvism
  • Enthusiast

  • 84 posts
  • Corona SDK

The plug worked, i check it out!

I've actually started playing around with another prototype that does not need to make use of a virtual joystick, as a result, this request has fallen by the wayside - for now. I imagine i will need this though in the future because i want to make a shmup! :)

Cheers

[TOPIC: post.html]
#11

Nemens

[GLOBAL: userInfoPane.html]
Nemens
  • Observer

  • 6 posts
  • Corona SDK

I've used Matt Pringle's joystick code before, but now after 2.0 it doesn't seem to work - still scratching my head, but I'm sure at some point I'll give up :)



[TOPIC: post.html]
#12

tonygod

[GLOBAL: userInfoPane.html]
tonygod
  • Contributor

  • 196 posts
  • Corona SDK

Ive used this one from the Community Code exchange in a couple of projects.  It's from the creators of Particle Candy.

 

http://developer.coronalabs.com/code/simple-analog-stick-joystick-module

 

Hi Rob.  This code seems to have gone missing.  Any idea where we can get it now?



[TOPIC: post.html]
#13

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,380 posts
  • Enterprise

Thats a link to our very old community code system.  Most of that code is old G1 code, or the developers are not maintaining it etc.  We had that up quite a while to give people time to move things.

 

I think this is the module.  I actually used it in one of my games.   This is the version in my downloads folder, so it should be the original file.  I know I had to make some changes in my game to implement it, and I wouldn't want to share the hacked version.

module(..., package.seeall);

function joystick:create(group, imgJoystick, joyWidth, joyHeight, imgBgJoystick, bgWidth, bgHeight)
    local stage = display.getCurrentStage();
    local mMin = math.min;
    local mCos = math.cos;
    local mSin = math.sin;
    local mAtan2 = math.atan2;
    local mSqrt = math.sqrt;
    local mFloor = math.floor;
    local mPi = math.pi;
    
    local joyGroup = display.newGroup();
    group:insert(joyGroup);
    local bgJoystick = display.newImageRect(joyGroup, imgBgJoystick, bgWidth, bgHeight );
    bgJoystick.x, bgJoystick.y = 0, 0;
    local radius = bgJoystick.contentWidth/4;
    joyGroup.radius = radius;
    local radToDeg = 180/mPi;
    local degToRad = mPi/180;
    local joystick = display.newImageRect(joyGroup, imgJoystick, joyWidth, joyHeight );
    
    function joystick:touch(event)
        local phase = event.phase;
        if phase == "moved" then
            if self.isFocus then
                local parent = self.parent;
                local posX, posY = parent:contentToLocal(event.x, event.y);
                local angle = (mAtan2( posX, posY )*radToDeg)-90;
                if angle < 0 then angle = 360 + angle end;
                local distance = mSqrt((posX*posX)+(posY*posY));
                if distance >= radius then
                    local radAngle = angle*degToRad;
                    distance = radius;
                    self.x, self.y = distance*mCos(radAngle), -distance*mSin(radAngle)
                else
                    self.x, self.y = posX, posY;
                end
                parent.angle = angle;
                parent.distance = distance;
            else
                stage:setFocus(event.target, event.id);
                self.isFocus = true;
                self.parent.state = true;
                self.alpha = 0.5;    
            end
        elseif phase == "began" then
            stage:setFocus(event.target, event.id);
            self.isFocus = true;
            self.parent.state = true;
            self.alpha = 0.5;
            local parent = self.parent;
            local posX, posY = parent:contentToLocal(event.x, event.y);
            self.x, self.y = posX, posY;
            local angle = (mAtan2( posX, posY )*radToDeg)-90;
            if angle < 0 then angle = 360 + angle end;
            local distance = mSqrt((posX*posX)+(posY*posY));
            if distance >= radius then
                local radAngle = angle*degToRad;
                distance = radius;
                self.x, self.y = distance*mCos(radAngle), -distance*mSin(radAngle)
            else
                self.x, self.y = posX, posY;
            end
            parent.angle = angle;
            parent.distance = distance;
        else
            self.isFocus = false;
            self.parent.state = false;
            self.x, self.y = 0, 0;
            self.parent.distance = 0;
            self.parent.angle = 0;
            stage:setFocus(nil, event.id);
            self.alpha = 1;
        end
        return true;
    end
    
    joyGroup.activate = function()
        joyGroup[1]:addEventListener("touch", joyGroup[2]);
        joyGroup.angle, joyGroup.distance = 0, 0;
    end
    joyGroup.deactivate = function()
        joyGroup[1]:removeEventListener("touch", joyGroup[2]);
        joyGroup.angle, joyGroup.distance = 0, 0;
    end
    joyGroup.x, joyGroup.y = joyGroup.contentWidth*0.5, display.contentHeight-joyGroup.contentHeight*0.5;
    return (joyGroup);
end
 

 

Rob



[TOPIC: post.html]
#14

tonygod

[GLOBAL: userInfoPane.html]
tonygod
  • Contributor

  • 196 posts
  • Corona SDK

Thats a link to our very old community code system.  Most of that code is old G1 code, or the developers are not maintaining it etc.  We had that up quite a while to give people time to move things.

 

I think this is the module.  I actually used it in one of my games.   This is the version in my downloads folder, so it should be the original file.  I know I had to make some changes in my game to implement it, and I wouldn't want to share the hacked version.

 

Thanks a lot, Rob.  Alex also found a copy in a different thread and sent a link.  It is slightly different from what you posted, but accomplishes the same thing.  I have it working in my code now and am tweaking it for my own purposes.  Really helpful code.



[TOPIC: post.html]
#15

mark.dallamore

[GLOBAL: userInfoPane.html]
mark.dallamore
  • Enthusiast

  • 31 posts
  • Corona SDK

Sorry cannot believe I am stuck on this but I cant seem to link the simplejoystick.lua (Rob's simple module) to the object that I want to move. I have activated the module and created the joystick js:activate() but how do I link the joystick to a function to move an object?

 

I tried using roaming gamers joystick in the ssk but I cannot seem to get it activated at all. Don't think I have the requires in the correct places.



[TOPIC: post.html]
#16

Michael W.

[GLOBAL: userInfoPane.html]
Michael W.
  • Contributor

  • 276 posts
  • Alumni

This would make a decent community-sourced Lua plugin that someone could distribute for free in our store. ;)



[TOPIC: post.html]
#17

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,380 posts
  • Enterprise

Well it's not *my* joystick.  I just made sure it survived getting to the new community code base.

 

Rob



[TOPIC: post.html]
#18

mark.dallamore

[GLOBAL: userInfoPane.html]
mark.dallamore
  • Enthusiast

  • 31 posts
  • Corona SDK

Could someone explain what LairdGames meant by using a "soft filter". I'm interested because I am creating a prototype just using a white box that responds to the virtual joystick and it seems to flicker alot when moving across the screen...Could this maybe be caused because I'm using a white box and not a regular png?




[topic_controls]
[/topic_controls]