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 :slight_smile: [import]uid: 66985 topic_id: 11708 reply_id: 311708[/import]

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.

[lua]-- 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[/lua]
[import]uid: 6084 topic_id: 11708 reply_id: 42567[/import]

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. [import]uid: 66985 topic_id: 11708 reply_id: 42781[/import]

Thanks BeyondtheTech,

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

Thank you.
[import]uid: 23689 topic_id: 11708 reply_id: 42782[/import]

Here’s the full code I tested out.

[lua]-- 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 )[/lua]

Create a circular image that you can check its orientation (put something in the circle) and call it circle.png and put it in the same folder as the code above. When you touch the circle, it will rotate based on how you move your finger around it. [import]uid: 6084 topic_id: 11708 reply_id: 42788[/import]

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

[lua]local circleHighlight = display.newImageRect( “circleOutline.png”, 300, 300 )
circleHighlight.x = circle.x
circleHighlight.y = circle.y
circleHighlight.alpha = 0[/lua]

Then, insert this before line 30:
[lua]circleHighlight.alpha = 1[/lua]

And this before line 50:
[lua]circleHighlight.alpha = 0[/lua]

Of course, make an image called circleOutline.png as necessary.

Good luck! [import]uid: 6084 topic_id: 11708 reply_id: 42789[/import]

THANK YOU SO MUCH! BEYONDTHETECH, I DON’T KNOW HOW TO THANK YOU. I WISH EVERY ONE WAS LIKE YOU :slight_smile: THANKS ALOT ! [import]uid: 66985 topic_id: 11708 reply_id: 42800[/import]

THANK YOU SO MUCH! BEYONDTHETECH, I DON’T KNOW HOW TO THANK YOU. I WISH EVERY ONE WAS LIKE YOU :slight_smile: THANKS ALOT ! [import]uid: 66985 topic_id: 11708 reply_id: 42802[/import]

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. [import]uid: 23689 topic_id: 11708 reply_id: 42803[/import]

Hey, I checked you out and found your game mercenary for hire. You got my money, and my 5 star ratings :slight_smile: sounds like a fun game. Thanks again. [import]uid: 23689 topic_id: 11708 reply_id: 42804[/import]

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. [import]uid: 6084 topic_id: 11708 reply_id: 42838[/import]

@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. [import]uid: 53149 topic_id: 11708 reply_id: 45570[/import]

Hey,

This fixes it :slight_smile:

It’s a more simplified way…

[code]

–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) [import]uid: 23689 topic_id: 11708 reply_id: 45579[/import]

@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 :wink:

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

@MichaelAssadi - Thanks a lot! That is better. Feels better and does not snap.
[import]uid: 53149 topic_id: 11708 reply_id: 45647[/import]

@spenggong

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

Thank ME not him, sheesh! [import]uid: 22392 topic_id: 11708 reply_id: 45651[/import]

Haha… Ya spenggong you should thank noahm26. He did the work. Nice job noahm26. [import]uid: 23689 topic_id: 11708 reply_id: 45659[/import]

@ChunkyApps - I am so sorry… Thank you! Thank you!!! [import]uid: 53149 topic_id: 11708 reply_id: 47816[/import]