Rotate an object by circling around the object

Hello,

I want to rotate the object by dragging your mouse around the circumference of the object. Your basically circling around the object to rotate it. How can we do this ?

Help is very much appreciated :)

You'll need to get the angle between the finger touch and the center of the object you're trying to rotate. Then, simply set the rotation to that angle.

Use the below code to compute the actual angle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- compute angle between two sets of points
function angleBetweenPoints ( srcObjx, srcObjy, dstObjx, dstObjy )
        -- make sure we never return -1.#IND if we try to find angle between identical points
        if ( srcObjx == dstObjx and srcObjy == srcObjy ) then return 0 end
 
        -- Original angleBetween
        local xDist = dstObjx - srcObjx ; local yDist = dstObjy - srcObjy
        local angleBetween = math.deg( math.atan( yDist / xDist ) )
        if ( srcObjx < dstObjx ) then angleBetween = angleBetween + 90 else angleBetween = angleBetween - 90 end
 
        -- These tend to get flipped around, this is a quick fix
        if ( angleBetween == 0 ) then angleBetween = -180
        elseif ( angleBetween == -180 ) then angleBetween = 0
        end
        return angleBetween
end

Thank you.

When you tap my object it is going to have a highlighting around it. Therefore, having the ability to rotate it by circling your finger around the object to change the rotation. You would first tap the object, but after that im in suspicion. Im not sure how to do touch circling around the object, any help ? Then after that i suppose you use the code you gave me for the angles ?

Thank you very much!
I am fairly new to corona. Thanks for your support.

Thanks BeyondtheTech,

I am also wondering about this topic. I am also rotating my object the same way. Any help ?:)
I am interested in rotating the object, by circling around it and changing the rotation. I am in the same position.

Thank you.

Here's the full code I tested out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- compute angle between two sets of points
local function angleBetweenPoints ( srcObjx, srcObjy, dstObjx, dstObjy )
        -- make sure we never return -1.#IND if we try to find angle between identical points
        if ( srcObjx == dstObjx and srcObjy == srcObjy ) then return 0 end
 
        -- Original angleBetween
        local xDist = dstObjx - srcObjx ; local yDist = dstObjy - srcObjy
        local angleBetween = math.deg( math.atan( yDist / xDist ) )
        if ( srcObjx < dstObjx ) then angleBetween = angleBetween + 90 else angleBetween = angleBetween - 90 end
 
        -- These tend to get flipped around, this is a quick fix
        if ( angleBetween == 0 ) then angleBetween = -180
        elseif ( angleBetween == -180 ) then angleBetween = 0
        end
        return angleBetween
end
 
-- display the circle
local circle = display.newImageRect( "circle.png", 256, 256 )
circle.x = display.contentWidth / 2
circle.y = display.contentHeight / 2
 
-- what to do when circle is touched
local function accessCircle( event )
        local t = event.target
 
        -- is the circle being touched?
        if event.phase == "began" then
 
                -- keep touch focus on the circle
                local parent = t.parent
                parent:insert( t )
                display.getCurrentStage():setFocus( t )
                t.isFocus = true
 
                -- store initial angle
                t.angle0 = t.rotation
 
        elseif t.isFocus then
                -- is finger moving around circle?
                if event.phase == "moved" then
                        t.x1 = event.x
                        t.y1 = event.y
                        local newAngle = math.ceil( angleBetweenPoints ( t.x, t.y, t.x1, t.y1 ))
                        -- rotate circle based on original angle (use t.rotation = newAngle for direct angle)
                        t.rotation = t.angle0 + newAngle
 
                -- did finger let go of circle?
                elseif event.phase == "ended" or event.phase == "cancelled" then
                        -- release focus for other objects
                        display.getCurrentStage():setFocus( nil )
                        t.isFocus = false
                end
        end
end
 
-- let circle object listen for touch events
circle:addEventListener( "touch", accessCircle )

If you want to create a highlight effect when the circle is touched, you can always add this after the circle object is created:

1
2
3
4
local circleHighlight = display.newImageRect( "circleOutline.png", 300, 300 )
circleHighlight.x = circle.x
circleHighlight.y = circle.y
circleHighlight.alpha = 0

THANK YOU SO MUCH! BEYONDTHETECH, I DON'T KNOW HOW TO THANK YOU. I WISH EVERY ONE WAS LIKE YOU :) THANKS ALOT !

THANK YOU SO MUCH! BEYONDTHETECH, I DON'T KNOW HOW TO THANK YOU. I WISH EVERY ONE WAS LIKE YOU :) THANKS ALOT !

Really? Your really awesome. THANKS SO MUCH. I can't repay you. I've spent so much time on this, and here it is. Im astonished, THANKS AGAIN.

Hey, I checked you out and found your game mercenary for hire. You got my money, and my 5 star ratings :) sounds like a fun game. Thanks again.

Glad I could help out. Hope the code is understandable. Lua, and Corona SDK for that matter, is easy to pick up and fun to master when you keep at it.

Thanks for checking out my game. The concept of getting the angle is used in that game when trying to get the angle of trajectory, so it just took a little thinking to get what you asked for.

@BeyondtheTech

Thank you for the post. This works great, however I noticed it snaps sometimes to one end depending on how close you are to the center of the circle or if you drag diagonally across the circle. In other words, not so smooth of a rotation sometimes. Anyway to fix that? Adding a delay to the rotation? I just don't know how to go about doing that. Any help is very much appreciated.

Hey,

This fixes it :)

It's a more simplified way...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
--Add an object called yourObject.--
 
--local yourObject = display.newImage ""
 
local function rotateObj(event)
        local t = event.target
        local phase = event.phase
        
        if (phase == "began") then
                display.getCurrentStage():setFocus( t )
                t.isFocus = true
                
                -- Store initial position of finger
                t.x1 = event.x
                t.y1 = event.y
                
        elseif t.isFocus then
                if (phase == "moved") then
                        t.x2 = event.x
                        t.y2 = event.y
                        
                        angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
                        angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
                        print("angle1 = "..angle1)
                        rotationAmt = angle1 - angle2
 
                        --rotate it
                        t.rotation = t.rotation - rotationAmt
                        print ("t.rotation = "..t.rotation)
                        
                        t.x1 = t.x2
                        t.y1 = t.y2
                        
                elseif (phase == "ended") then
                        
                        display.getCurrentStage():setFocus( nil )
                        t.isFocus = false
                end
        end
        
        -- Stop further propagation of touch event
        return true
end
yourObject:addEventListener("touch", rotateObj)

@MichaelAssadi

How about a little shout out at least for throwing my code up there? It took me half a day to come up with that you know ;)

http://developer.anscamobile.com/code/rotate-object-finger

@MichaelAssadi - Thanks a lot! That is better. Feels better and does not snap.

@spenggong

Are you kidding me? That's my code not his! Arrrrrrrgh!!!

Thank ME not him, sheesh!

Haha... Ya spenggong you should thank noahm26. He did the work. Nice job noahm26.

@ChunkyApps - I am so sorry.. Thank you! Thank you!!!

views:3119 update:2011/9/20 13:12:00
corona forums © 2003-2011