• Create Account

## Touch controller, how to calculate it?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1SoulRyder  Members

141
Like
0Likes
Like

Posted 31 October 2012 - 10:46 AM

Hello,

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:
http://2.bp.blogspot....320x480-75.jpg

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

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.

Alexander Sarton

Co - Founder / Producer @ Barrelman Games

### #2Olof Hedman  Members

5697
Like
0Likes
Like

Posted 31 October 2012 - 11:25 AM

If you see the difference between the original point to the thumb as a vector, just make sure its not longer then 30 pixels, if it is, shorten it to 30 pixels and draw the thumb pad at that position.

You shorten it by normalizing it and multiply it with 30

Edited by Olof Hedman, 31 October 2012 - 11:29 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.