Rotate finger clockwise and counter-clockwise to...

I am trying to do this simple thing. At least I think it is simple.

  1. Help me figure out how I can “Touch” anywhere on the screen and when I move my finger in a circle it rotates an object I have on screen.

Let’s say it’s a ball. If I touch and rotate my finger clockwise the ball rotates clockwise and if I do the opposite the ball rotates in the opposite direction. Pleas Help and thanks [import]uid: 53149 topic_id: 12452 reply_id: 312452[/import]

funny, I asked this question earlier today…

http://developer.anscamobile.com/forum/2011/07/14/feed-me-oil-style-rotation [import]uid: 22392 topic_id: 12452 reply_id: 45473[/import]

Not so easy, also not so hard. You need to know some math.

First onTouch “began” you take the coordinates of the finger. lets say you save that coordinates into table as point1.x and point1.y.

Then onTouch “move” you take the coordinates again and put them into point2.x and point2.y.

After that, you need to calculate the slope of an imaginary line between, ballCenter.x, ballCenter.y and point1.x and point2.y. Slope formula is easy and could be find here: http://en.wikipedia.org/wiki/Slope, the slope which is shown as “m” means tan(t) which t is angle. To find the angle from slope, you use arctan(t). After all those calculations you will have the angle of the line [point1, ballCenter]

Do the same calculations for the point2.

Now you will have two angles, the difference between angles is how much you should rotate the ball. ball:rotate(angle1 - angle2)

if you want to “rotate” method as above, after rotation you should overwrite point2 onto point1, I mean, point1 = point2. OR you do not use the rotate method, instead use “rotation” property as ball.rotation = angle1 - angle2.

I didn’t try it, but the simulator in my mind thinks it works. :slight_smile:
[lua]function onTouch (event)
if event.phase == “began” then
– get point1
elseif event.phase ==“move” then
– get point2
– do calculations
end[/lua] [import]uid: 46529 topic_id: 12452 reply_id: 45478[/import]

@ culutas

I attempted this but no-go [import]uid: 22392 topic_id: 12452 reply_id: 45493[/import]

I got it… I have added the code to the code exchange

http://developer.anscamobile.com/code/rotate-object-finger [import]uid: 22392 topic_id: 12452 reply_id: 45498[/import]