Object moving along a Bezier curve example

408
Vote up!

Hello, I am Brazilian, so any errors in the writing, I apologize.

First Understood the Beziel Curve Proprierties

local curve = bezier:curve({xInitial, xControlPoint1, xControlPoint2, xControlPoint3}, {yInitial, yControlPoint1, yControlPoint2, yControlPoint3})


THE CODE:

local bezier = require('bezier')

local carro = display.newImage("images/carro.png")
carro.x = _W/2
carro.y = _H - 250
carro.name = "carro"

local curve1 = bezier:curve({150, 1100, 150},{ 1080, 686, 500})
local x1, y1 = curve1(10.51)
local line3 = display.newLine(x1, y1, x1+1, y1+1)
line3:setColor(0, 0, 255, 255 )
line3.width = 3

for i=0.02, 1.01, 0.01 do
local x, y = curve1(i)
line3:append(x, y)
print(i, x, y)
end

-- move car along curve
local pointInc = 0.00

local function follow_curve (event )
if pointInc < 1.01 then
carro.x, carro.y = curve1(pointInc)
-- print(pointInc,ball.x, ball.y)
pointInc = pointInc + 0.01
else
Runtime:removeEventListener("enterFrame",follow_curve)
end
end

Runtime:addEventListener("enterFrame",follow_curve);

--------- COPIE THE CODE AND USE, SOON I UPLOAD NEW CODE OPTIMIZED AND MORE FUNCTIONS.

Works with Corona build #: 
2014.2393
Contributor: 
William