360 degree controller thumb pad

I’m trying to create a thumb-pad controller - detecting movement of the pad is the next hurdle but I’m stuck at the point of limiting the pad’s movement to within an outer perimeter.

I have an outer circle that I want to act as boundary of movement for an inner filled circle.

I want to limit the touch the the outer circle (or possibly a third larger circle to catch any near-misses of the controller - not included in this code).

i’m using pythagorean theorem to position innerPad.x,y, according to event.x,y to try and limit the movement to the circle outerPad. The issue is that if the touch event ends outside the circle outerPad, the innerPad becomes stuck at the boundary rather than returning to the centre point.

I’ve tried to fix this using (math.ceil) and also with trigonometry but now the innerPad disappears after a few touches - I think it’s to do with whether I set the listener to innerPad or outerPad. I’ve also got getCurrentStange and setFocus in there too, which I’m a bit hazy about…

Any advice would be very greatly appreciated! Many thanks.

[lua]

local outerPad = display.newCircle( display.screenOriginX+45, display.screenOriginY+_H-45, 30 )

outerPad.strokeWidth = 1

–outerPad.alpha = 0

outerPad:setFillColor(0,0,0,0) 

outerPad:setStrokeColor(128,128,128) 

local innerPad = display.newCircle( display.screenOriginX+45, display.screenOriginY+_H-45, 25 )

innerPad.strokeWidth = 0

innerPad:setFillColor(200,200,200) 

local function onPadTouch( event )

    if event.phase == “began” or event.phase == “moved” then

        if (((display.screenOriginX+45-event.x) * (display.screenOriginX+45-event.x)) + ((display.screenOriginY+_H-45-event.y) * (display.screenOriginY+_H-45-event.y))) <= 250 then

        innerPad.x , innerPad.y = event.x, event.y;

        display.getCurrentStage():setFocus(event.target)

        event.target.isFocus = true

        end

    elseif (((display.screenOriginX+45-event.x) * (display.screenOriginX+45-event.x)) + ((display.screenOriginY+_H-45-event.y) * (display.screenOriginY+_H-45-event.y))) > 250 then

           --innerPad.x , innerPad.y = (math.sqrt (250 - innerPad.y)) , (math.sqrt (250 - innerPad.x));

           innerPad.x , innerPad.y = 15 *((math.asin ( math.atan2 (event.y , event.x )))) , 15 *((math.acos ( math.atan2 (event.y , event.x ))));

           display.getCurrentStage():setFocus(event.target)

        event.target.isFocus = true

    elseif 

        event.phase == “ended” or event.phase == “cancelled” then

        innerPad.x, innerPad.y = display.screenOriginX+45, display.screenOriginY+_H-45;

        display.getCurrentStage():setFocus(nil)

        event.target.isFocus = false

    --else innerPad.x, innerPad.y = display.screenOriginX+45, display.screenOriginY+_H-45;

    end

end

innerPad:addEventListener( “touch”, onPadTouch )

[/lua]

There are several preexisting joysticks available for you to plug in. Unless this is a learning exercise you’re better off with using them:

developer.coronalabs.com/code/joystick

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

Thank you, those plug-ins look great, just what I need. 

I’ve been trying to get this one [http://developer.coronalabs.com/code/joystick] to work and I have a couple of questions - I wonder if anyone can offer any advice?

(1)

In the documentation it’s mentioned that the following should be tested before attempting to work with values from the joystick:  if joyX and joyY ~= false then …

Am I correct in thinking that it’s OK to detect true/false and also a number value for the same variable (e.g. joyX)? Is that what’s happening here? I thought a variable should be either an integer or a boolean…?

(2)

​In the Advanced Setup demo the controller moves a plane which is defined in a plane.lua and ‘required’ in the main.lua. The plane.lua applies a single plane image to different x values of joystick movement. Would I be able to apply animations to different x (and y) values? Whats happening right now is that only the first frame of the animation is showing. I’m using

[lua]elseif xStrength >= 0.1 and xStrength < 0.2 then

        player:setSequence( “run” ) 

        player:play()[/lua]

I’m guessing my increments need widening to something like ‘elseif xStrength >= 0.1 and xStrength <= 1’ …?

I’m generally finding that, although I can see joystick movements returned in the terminal, if I try to use the data (joyX, joyY) in the main.lua, I’m getting nothing.

 

Many thanks for any help - always much appreciated.

There are several preexisting joysticks available for you to plug in. Unless this is a learning exercise you’re better off with using them:

developer.coronalabs.com/code/joystick

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

Thank you, those plug-ins look great, just what I need. 

I’ve been trying to get this one [http://developer.coronalabs.com/code/joystick] to work and I have a couple of questions - I wonder if anyone can offer any advice?

(1)

In the documentation it’s mentioned that the following should be tested before attempting to work with values from the joystick:  if joyX and joyY ~= false then …

Am I correct in thinking that it’s OK to detect true/false and also a number value for the same variable (e.g. joyX)? Is that what’s happening here? I thought a variable should be either an integer or a boolean…?

(2)

​In the Advanced Setup demo the controller moves a plane which is defined in a plane.lua and ‘required’ in the main.lua. The plane.lua applies a single plane image to different x values of joystick movement. Would I be able to apply animations to different x (and y) values? Whats happening right now is that only the first frame of the animation is showing. I’m using

[lua]elseif xStrength >= 0.1 and xStrength < 0.2 then

        player:setSequence( “run” ) 

        player:play()[/lua]

I’m guessing my increments need widening to something like ‘elseif xStrength >= 0.1 and xStrength <= 1’ …?

I’m generally finding that, although I can see joystick movements returned in the terminal, if I try to use the data (joyX, joyY) in the main.lua, I’m getting nothing.

 

Many thanks for any help - always much appreciated.

These code submissions are no longer showing up.  Anyone know if these can be found somewhere else?

I put up the analog joystick here. I’m not sure about the second as I have zero idea which one that was referring to. But the xpressive one is pretty good all the same.

Thank you very much, Alex!

These code submissions are no longer showing up.  Anyone know if these can be found somewhere else?

I put up the analog joystick here. I’m not sure about the second as I have zero idea which one that was referring to. But the xpressive one is pretty good all the same.

Thank you very much, Alex!