Make an object move around a circle when dragging your object?

Hey there!

I have a code bit that allows me to drag an object along the screen with my finger but i want my object to only move in a circle instead of all over the screen. How do i do this?

Image for clarification in attachement

My code:

 -- Define a circle object need to be move local circleObj = display.newCircle( 150, 150, 40 ) -- 150,150 =\> set the position 40 =\> radius of the circle circleObj:setFillColor(121,121,121) -- fill the circle with color circleObj.strokeWidth = 4 -- Sets the width of the border of circle circleObj:setStrokeColor(128,0,0) -- Sets the border color --Function that move the circle function moveCircle( event ) -- Set the cordinates of the circle circleObj .x, circleObj .y = event.x + 20, event.y return true end --Touch event that will fired when we will touch the screen circleObj:addEventListener("touch", moveCircle)

I feel like i am really close but i cant seem to figure it out.
Help me please!

Regards,

The bad news is that no, you weren’t close at all :slight_smile:

The good news is that I thought it was an interesting puzzle do do in a break, so I did it for you. Here you go.

local toPolarCoordinates = function(x, y) x = x or 0 y = y or 0 local radius = math.sqrt(x ^ 2 + y ^ 2) local angle = math.atan2(x, y) \* 180 / math.pi return radius, angle end local toCartesianCoordinates = function(radius, angle) radius = radius or 0 angle = angle or 0 local angleInRadians = angle \* math.pi / 180 local x = radius \* math.cos(angleInRadians) local y = radius \* math.sin(angleInRadians) return x, y end local circleObj = display.newCircle(200, 100, 30 ) circleObj:setFillColor(121,121,121) circleObj.strokeWidth = 4 circleObj:setStrokeColor(128,0,0) local planet = display.newCircle(200, 200, 50) local function moveCircle( event ) local t = event.target local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus(t) t.isFocus = true t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then local newX, newY = event.x - t.x0, event.y - t.y0 local radius, angle = toPolarCoordinates(planet.x - newX, planet.y - newY) local adjustedX, adjustedY = toCartesianCoordinates(100, -angle - 90) newX, newY = planet.x + adjustedX, planet.y + adjustedY t.x, t.y = newX, newY elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end return true end circleObj:addEventListener("touch", moveCircle)

Hey thanks a lot! It feels so embarrasing to not even be close haha… at least i get a circle now thanks to you!
I might try to replace the circle with a image to make it more beautifull!
Ill play around with it for a bit!

Thank you for your help!

Regards,

The bad news is that no, you weren’t close at all :slight_smile:

The good news is that I thought it was an interesting puzzle do do in a break, so I did it for you. Here you go.

local toPolarCoordinates = function(x, y) x = x or 0 y = y or 0 local radius = math.sqrt(x ^ 2 + y ^ 2) local angle = math.atan2(x, y) \* 180 / math.pi return radius, angle end local toCartesianCoordinates = function(radius, angle) radius = radius or 0 angle = angle or 0 local angleInRadians = angle \* math.pi / 180 local x = radius \* math.cos(angleInRadians) local y = radius \* math.sin(angleInRadians) return x, y end local circleObj = display.newCircle(200, 100, 30 ) circleObj:setFillColor(121,121,121) circleObj.strokeWidth = 4 circleObj:setStrokeColor(128,0,0) local planet = display.newCircle(200, 200, 50) local function moveCircle( event ) local t = event.target local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus(t) t.isFocus = true t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then local newX, newY = event.x - t.x0, event.y - t.y0 local radius, angle = toPolarCoordinates(planet.x - newX, planet.y - newY) local adjustedX, adjustedY = toCartesianCoordinates(100, -angle - 90) newX, newY = planet.x + adjustedX, planet.y + adjustedY t.x, t.y = newX, newY elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end return true end circleObj:addEventListener("touch", moveCircle)

Hey thanks a lot! It feels so embarrasing to not even be close haha… at least i get a circle now thanks to you!
I might try to replace the circle with a image to make it more beautifull!
Ill play around with it for a bit!

Thank you for your help!

Regards,