moving an object in a circular and along a curved path

Hi,
I have been trying to find a way to move objects along curved and circular paths.
I have read about bezier curves and splines but I am un-sure if this is the way to do such things.

I use flash lots and have tried to utilise a way to get all the x,y co-ords from a script in the code exchange here:
http://developer.coronalabs.com/code/animation-along-path-flash
but cannot get it work…

Would anyone be so kind as to point me in the correct direction for doing this properly?

thank you [import]uid: 127675 topic_id: 31897 reply_id: 331897[/import]

Hey there,

Carlos Icaza (ex Corona CEO, one of the co founders) wrote a really great blog post on this with an example, take a look; http://www.carlosicaza.com/2011/04/23/moving-an-object-along-a-bezier-curve-part-i/

Peach :slight_smile: [import]uid: 52491 topic_id: 31897 reply_id: 127217[/import]

@peach,
I appreciate the link and it obviously has some excellent information
Can I ask, how would you move an object or sprite in a circular path?
Not sure if you know the game r- type , but there are lots of enemy spawns that move in this way, think classic space shooters etc.
Oh yes, the new image looks fab, you look even cuter You’re as.much a credit to corona as JB.
Now where is my free subscription? Lol

Rock on!

[import]uid: 127675 topic_id: 31897 reply_id: 127296[/import]

Double post

[import]uid: 127675 topic_id: 31897 reply_id: 127258[/import]

If your after bezier curve movement you could check out my “Space Shooter Template” that i have for sale on our site: http://www.tandgapps.co.uk/downloads/space-shooter-template/

If you literaly only want to create a circular path for an enemy to follow you could use a simple function like below to make a set of waypoints that could be used for movement.
The function basically returns an array of x, y points which you can loop through to move an object, which is shown with the enterFrame gameloop function.

--Localise the maths vars.  
local mPi = math.pi  
local mS = math.sin  
local mC = math.cos  
local function makeCircle( startPointXY, endPointXY, degreesStart, degreesEnd, yScale, xScale, value)  
 local xy = {} --Holds the points we make.  
 local distance = endPointXY[1] - startPointXY[1] --The radius  
  
 --Sets the scales if needed.  
 if yScale == nil then yScale = 1 end  
 if xScale == nil then xScale = 1 end  
 if value == nil then value = 2 end  
  
 --Changes the direction of the path if needed.  
 if degreesEnd \< degreesStart then   
 value = -value   
 end  
  
 --Now create the circlular path!  
 local i   
 for i=degreesStart, degreesEnd, value do  
 local radians = i\*(mPi/180)  
 local x = (endPointXY[1] + ( distance \* mS( radians ) )\*xScale)   
 local y = (endPointXY[2] + ( distance \* mC( radians ) )\*yScale)   
 xy[#xy+1] = {x, y}  
  
 --Display a circle to show you the path your making.  
 local circle = display.newCircle(0,0,1)  
 circle.x = x; circle.y = y; circle:setFillColor(255,60,60)   
 end  
  
 --Return the array.  
 return xy  
end  
  
--Plug in the needed variables.   
--You could also do the yScale, xScale and value but that doesnt need to be put in.  
local waypoints = makeCircle( {20,400}, {320,400}, 0, 360)  
  
--Now we are going to move a circle around that path.....  
--FIrst create the circle  
local circle = display.newCircle(0,0,5)  
  
--Then create the gameLoop function which will run every frame.  
local int = 1  
local function gameLoop()  
 if int \<= #waypoints then  
 circle.x = waypoints[int][1]  
 circle.y = waypoints[int][2]  
 int = int + 1  
 else   
 int = 1  
 end  
end  
Runtime:addEventListener("enterFrame", gameLoop)  

Copy and paste that into a blank main.lua file and you can see what it does. Its plug and play!
I’ve done something horribly wrong in there with the y values… Buuuuut it isn’t too bad for 10 minutes work :smiley: [import]uid: 69826 topic_id: 31897 reply_id: 127299[/import]

ps. changing the yScale for example to 0.6 would make the circle a lot more elliptical which can come in quite handy :smiley: [import]uid: 69826 topic_id: 31897 reply_id: 127300[/import]

Thanks T+G,

yes, the movement in your game,i.e. the wave like motion along the curves is what I am after for starters.
I figure if I can hash up a routine I can obviously alter the parameters to meet my needs.
The code you posted above is also of interest.
I may have to purchase the framework to see how the beziers are made.
thanks [import]uid: 127675 topic_id: 31897 reply_id: 127304[/import]

Hey there,

Carlos Icaza (ex Corona CEO, one of the co founders) wrote a really great blog post on this with an example, take a look; http://www.carlosicaza.com/2011/04/23/moving-an-object-along-a-bezier-curve-part-i/

Peach :slight_smile: [import]uid: 52491 topic_id: 31897 reply_id: 127217[/import]

@peach,
I appreciate the link and it obviously has some excellent information
Can I ask, how would you move an object or sprite in a circular path?
Not sure if you know the game r- type , but there are lots of enemy spawns that move in this way, think classic space shooters etc.
Oh yes, the new image looks fab, you look even cuter You’re as.much a credit to corona as JB.
Now where is my free subscription? Lol

Rock on!

[import]uid: 127675 topic_id: 31897 reply_id: 127296[/import]

Double post

[import]uid: 127675 topic_id: 31897 reply_id: 127258[/import]

If your after bezier curve movement you could check out my “Space Shooter Template” that i have for sale on our site: http://www.tandgapps.co.uk/downloads/space-shooter-template/

If you literaly only want to create a circular path for an enemy to follow you could use a simple function like below to make a set of waypoints that could be used for movement.
The function basically returns an array of x, y points which you can loop through to move an object, which is shown with the enterFrame gameloop function.

--Localise the maths vars.  
local mPi = math.pi  
local mS = math.sin  
local mC = math.cos  
local function makeCircle( startPointXY, endPointXY, degreesStart, degreesEnd, yScale, xScale, value)  
 local xy = {} --Holds the points we make.  
 local distance = endPointXY[1] - startPointXY[1] --The radius  
  
 --Sets the scales if needed.  
 if yScale == nil then yScale = 1 end  
 if xScale == nil then xScale = 1 end  
 if value == nil then value = 2 end  
  
 --Changes the direction of the path if needed.  
 if degreesEnd \< degreesStart then   
 value = -value   
 end  
  
 --Now create the circlular path!  
 local i   
 for i=degreesStart, degreesEnd, value do  
 local radians = i\*(mPi/180)  
 local x = (endPointXY[1] + ( distance \* mS( radians ) )\*xScale)   
 local y = (endPointXY[2] + ( distance \* mC( radians ) )\*yScale)   
 xy[#xy+1] = {x, y}  
  
 --Display a circle to show you the path your making.  
 local circle = display.newCircle(0,0,1)  
 circle.x = x; circle.y = y; circle:setFillColor(255,60,60)   
 end  
  
 --Return the array.  
 return xy  
end  
  
--Plug in the needed variables.   
--You could also do the yScale, xScale and value but that doesnt need to be put in.  
local waypoints = makeCircle( {20,400}, {320,400}, 0, 360)  
  
--Now we are going to move a circle around that path.....  
--FIrst create the circle  
local circle = display.newCircle(0,0,5)  
  
--Then create the gameLoop function which will run every frame.  
local int = 1  
local function gameLoop()  
 if int \<= #waypoints then  
 circle.x = waypoints[int][1]  
 circle.y = waypoints[int][2]  
 int = int + 1  
 else   
 int = 1  
 end  
end  
Runtime:addEventListener("enterFrame", gameLoop)  

Copy and paste that into a blank main.lua file and you can see what it does. Its plug and play!
I’ve done something horribly wrong in there with the y values… Buuuuut it isn’t too bad for 10 minutes work :smiley: [import]uid: 69826 topic_id: 31897 reply_id: 127299[/import]

ps. changing the yScale for example to 0.6 would make the circle a lot more elliptical which can come in quite handy :smiley: [import]uid: 69826 topic_id: 31897 reply_id: 127300[/import]

Thanks T+G,

yes, the movement in your game,i.e. the wave like motion along the curves is what I am after for starters.
I figure if I can hash up a routine I can obviously alter the parameters to meet my needs.
The code you posted above is also of interest.
I may have to purchase the framework to see how the beziers are made.
thanks [import]uid: 127675 topic_id: 31897 reply_id: 127304[/import]

Thank you very much TandG!!!

:D  :D  :smiley:

Thank you very much TandG!!!

:D  :D  :smiley: