Rotating object to the user touch

I want to create a spinning wheel.

So far I managed to rotate the object, when user starts dragging the wheel. It is fixed at the center and always rotates to users touch. Now I have the problem - second time users starts dragging/spinning the wheel it jumps…

local spinner = display.newImage( "img/spin.png") spinner.x, spinner.y = display.contentCenterX, display.contentCenterY spinner:scale( .3, .3 ) local adjustment = 0 local PI = 3.14159265358 local function onTouch(e) if(e.phase == "began") then local dx = e.x - spinner.x local dy = e.y - spinner.y adjustment = math.atan2(dy,dx) \* 180 / PI end if(e.phase == "moved") then local dx = e.x - spinner.x local dy = e.y - spinner.y spinner.rotation = (math.atan2(dy,dx) \* 180 / PI) - adjustment end end Runtime:addEventListener('touch', onTouch)

Got it to work, here is the full working code:

local spinner = display.newImage( "img/spin.png") spinner.x, spinner.y = display.contentCenterX, display.contentCenterY spinner:scale( .3, .3 ) local adjustment = 0 local PI = 3.14159265358 local function onTouch(e) if(e.phase == "began") then local dx = e.x - spinner.x local dy = e.y - spinner.y adjustment =   math.atan2(dy,dx) \* 180 / PI - spinner.rotation end if(e.phase == "moved") then local dx = e.x - spinner.x local dy = e.y - spinner.y spinner.rotation = (math.atan2(dy,dx) \* 180 / PI)  - adjustment myText.text = spinner.rotation end end Runtime:addEventListener('touch', onTouch)

Got it to work, here is the full working code:

local spinner = display.newImage( "img/spin.png") spinner.x, spinner.y = display.contentCenterX, display.contentCenterY spinner:scale( .3, .3 ) local adjustment = 0 local PI = 3.14159265358 local function onTouch(e) if(e.phase == "began") then local dx = e.x - spinner.x local dy = e.y - spinner.y adjustment =   math.atan2(dy,dx) \* 180 / PI - spinner.rotation end if(e.phase == "moved") then local dx = e.x - spinner.x local dy = e.y - spinner.y spinner.rotation = (math.atan2(dy,dx) \* 180 / PI)  - adjustment myText.text = spinner.rotation end end Runtime:addEventListener('touch', onTouch)