I have read the excellent tutorial for moving display objects along curved paths using Bezier curves (https://coronalabs.com/blog/2014/09/09/tutorial-working-with-curved-paths/). The problem is that the example code only uses two anchor points. I have been trying to make the code more generic so that any number of anchor points can be used but I simply cannot figure out how to do it…
The biggest problem is the function where the curve itself is rendered and I am completely lost at the “Bezier formula” stuff. Anyone done this before?
local function renderCurve( segNum, r, g, b ) local inc = ( 1.0 / segNum ) for i = 1,#anchorPoints,4 do local t = 0 local t1 = 0 local i = 1 for j = 1,segNum do t1 = 1.0 - t local t1\_3 = t1 \* t1 \* t1 local t1\_3a = (3\*t) \* (t1\*t1) local t1\_3b = (3\*(t\*t)) \* t1 local t1\_3c = t \* t \* t local p1 = anchorPoints[i] local p2 = anchorPoints[i+1] local p3 = anchorPoints[i+2] local p4 = anchorPoints[i+3] local x = t1\_3 \* p1.x x = x + t1\_3a \* p2.x x = x + t1\_3b \* p3.x x = x + t1\_3c \* p4.x local y = t1\_3 \* p1.y y = y + t1\_3a \* p2.y y = y + t1\_3b \* p3.y y = y + t1\_3c \* p4.y pathPoints[j].x = x pathPoints[j].y = y t = t + inc end end --reset/clear core if curve then display.remove( curve ) end curve = display.newLine( pathPoints[1].x, pathPoints[1].y, pathPoints[2].x, pathPoints[2].y ) for i = 3,#pathPoints do curve:append( pathPoints[i].x, pathPoints[i].y ) end curve:append( anchorPoints[4].x, anchorPoints[4].y ) curve:setStrokeColor( r, g, b ) curve.strokeWidth = 4 curve:toBack() end