at the risk of stepping on @Alan QuizTix’s toes, who has already given a good answer, I’d approach it as follows…
first, I’d create a parametric function to memoize your curve definition, fe:
local function makeOscillator(x,y,dx,dy,phz,frq,amp) return function(t) return x+t\*dx, y+t\*dy+amp\*math.cos(phz+t\*frq) end end
then i’d make an instance of it with desired values:
-- assuming no config.lua, ie just device pixels, for simplicity: local CW = display.contentWidth local CH = display.contentHeight local osc = makeOscillator(0,CH/8, CW,CH/2, 0,20,CH/10)
then i’d test and tune it by plotting some dots (a la @Alan QuizTix’s trail):
for t = 0,1,0.01 do local x,y = osc(t) local dot = display.newRect(x,y,4,4) end
then i’d create something to follow that curve:
local thing = display.newRect(0,0,10,10)
then, depending on whether you’re doing frame animation or transitions, either
local t = 0 local dt = 1/(5\*30) -- 5 seconds @ 30fps local function enterFrame() thing.x, thing.y = osc(t) t = t + dt end Runtime:addEventListener("enterFrame", enterFrame)
or via transition: (a bit more voodoo, but essentially equivalent)
local function makeProxy(object, f) local t = 0 return setmetatable({}, { \_\_index = function(\_,\_) return t end, \_\_newindex = function(\_,\_,v) t = v; object.x, object.y = f(t) end }) end local proxy = makeProxy(thing, osc) transition.to(proxy, { time=5000, t=1.0 })
hth