Sign in to follow this  

Touch controller, how to calculate it?

This topic is 1904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
[url="http://2.bp.blogspot.com/-s35Df5OsdtM/TunGULkOhVI/AAAAAAAAAu0/YodMUXWrE-U/s1600/mzl.xuivvfln.320x480-75.jpg"]http://2.bp.blogspot....320x480-75.jpg[/url]

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:

[CODE]
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
[/CODE] Edited by SoulRyder

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this