I am working on a mobile game in Corona SDK and I want to make a controller for moving the character similar to the one used in the Grand Theft Auto 3 mobile version.
Example in the left corner:
What happens now is, I create an object on the screen where the user touches the screen. It remembers the x and y position from where the player starts touching the screen. Now when the user moves his finger it will follow the users finger this works fine.
But now i want it to keep following the direction of the finger after the finger reaches a certain distance from the starting position.
Now I know how to calculate distance, but how would I make it too keep following the direction of the finger, but not follow it completely after for example a distance of 30 pixels from the starting position. So it stops at 30 pixels from the starting position, but if you would draw a line from the start position to the users finger position, the control object would be on the same line, but not further away then 30 pixels from the starting pos.
What i have now is this:
local moveControls = display.newCircle(0,0,10) local moveCntrStartX,moveCntrStartY = 0,0 moveControls.isVisible = false Runtime:addEventListener("touch", onTouch) function onTouch(event) if event.phase == "began" then placeMoveControls(event.xStart,event.yStart) return true elseif event.phase == "ended" then removeMoveControls() return true elseif event.phase == "moved" then moveMoveControls(event.x, event.y) return true end end function moveMoveControls(xPos,yPos) local dx = xPos - moveCntrStartX local dy = yPos - moveCntrStartY local distance = math.sqrt(math.pow(dx,2)+math.pow(dy,2)) if distance < 30 then moveControls.x, moveControls.y = xPos,yPos else --Here I do not know how to calculate that the moveControls are still "pointing" at the users finger, --but are not going further away then 30 from the start position. end end function removeMoveControls() moveControls.isVisible = false end function placeMoveControls(xPos,yPos) moveCntrStartX, moveCntrStartY = xPos,yPos moveControls.x, moveControls.y = xPos,yPos moveControls.isVisible = true end
Edited by SoulRyder, 31 October 2012 - 10:50 AM.