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]