Rotate image

I am new user of Corona and i am developing test game. I need to rotate image, when i slide my finger on screen. So when i slide my finger somewhere on screen to right, picture rotates to right. And when i slide to left, it rotates to left. And finaly image must not rotate too much, so when it has rotated to right 30 degrees, rotation stops. And image has to rotate that many degrees, how far i slide my finger.
Might sound little bit complex, but please help me.

And here is my code:

[code]local lauta = display.newImage (“lauta.png”)
lauta.x = 240
lauta.y = 650
physics.addBody(lauta, “static”)[code] [import]uid: 18445 topic_id: 6562 reply_id: 306562[/import]

I have tried to do that myself, but i cant get it working. Now if i click my backgroung picture once, image (lauta.png) rotates 10 degrees. So it rotates everytime i click background 10 degrees.
Could somebody help me little bit. So i need to get touches x-coordinates and replace 10 degrees with that coordinates. So when i move my finger right, image (lauta.png) rotates that much, how far i move my finger.
Please help me!

Here is little bit code:

[code]local background = display.newImage (“tausta.png”)

local lauta = display.newImage (“lauta.png”)
lauta.x = 240
lauta.y = 650

local rotate = function( event )
lauta.rotation = lauta.rotation + 10
end

background:addEventListener( “touch”,rotate) [import]uid: 18445 topic_id: 6562 reply_id: 22937[/import]

Now i have figured rotation myself. :slight_smile: I just needed to edit code little bit, yes i am completely newbie yet.

local rotate = function( event ) lauta.rotation = event.x end

So now it works. But hardest part (for me) is, that image must not rotate too much. So when it has rotated 30 degrees to right or left rotation stops, even if i move my finger right or left. So how i can do that?

And there is also another little problem: now if i touch for example right side of screen, image rotates directly as much as my touch coordinate is. I want to, that rotation angle increase when i slide my finger on screen. How i can do that?

Help, please! [import]uid: 18445 topic_id: 6562 reply_id: 22970[/import]

Thanks very much! It worked! But i needed to edit your code little bit: there was one “=” too much in your code:

Wrong: local r = = event.x - centerX -- (shift values to between eg -240 to 240)

Correct: local r = event.x - centerX -- (shift values to between eg -240 to 240) [import]uid: 18445 topic_id: 6562 reply_id: 23216[/import]

try this… for your first question

[lua]local max = math.max
local min = math.min
local centerX = display.contentWidth/2

local function rotate(event)

local r = event.x - centerX – (shift values to between eg -240 to 240)

r = min(r, 30) – no higher than 30
r = max(r, -30) – no lower than -30

lauta.rotation = r

end [import]uid: 6645 topic_id: 6562 reply_id: 23172[/import]

There is one final problem :). Now if i move my finger very little, images rotates to 30 degrees almost immediately. I want to, that i can move my finger almost to right side of screen and then image is rotated 30 degrees. So rotation must happen slower. Please help me again. :slight_smile: [import]uid: 18445 topic_id: 6562 reply_id: 23237[/import]

Newer mind. I figured that problem myself. :slight_smile:
Solution:

[code]local function rotate(event)

local r = centerX - event.x – (shift values to between eg -240 to 240)

r = min(r, 150)
r = max(r, -150)
lauta.rotation = r/5

end
[import]uid: 18445 topic_id: 6562 reply_id: 23250[/import]