Skip to content

Instantly share code, notes, and snippets.

@dantes2023
Created August 25, 2014 22:01
Show Gist options
  • Select an option

  • Save dantes2023/bcedb9b0ffd80d3682ba to your computer and use it in GitHub Desktop.

Select an option

Save dantes2023/bcedb9b0ffd80d3682ba to your computer and use it in GitHub Desktop.
Object Moving Along A Bezier Curve Example
local bg = display.newImage("images/bg.png")
bg.x = _W/2; bg.y = _H/2;
local car = display.newImage("images/car.png")
car.x = _W/2
car.y = _H - 250
car.name = "car"
-- understood proprierties of bezier curve
--local curve = bezier:curve({xInitial, xControlPoint1, xControlPoint2, xControlPoint3}, {yInitial, yControlPoint1, yControlPoint2, yControlPoint3})
local curve = bezier:curve({150, 1100, 150},{ 1080, 686, 500})
local x1, y1 = curve(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 = curve(i)
line3:append(x, y)
print(i, x, y)
end
-- move ball along curve
local pointInc = 0.00
local function follow (event )
if pointInc < 1.01 then
car.x, car.y = curve(pointInc)
--print(pointInc,ball.x, ball.y)
pointInc = pointInc + 0.01
else
Runtime:removeEventListener("enterFrame",follow)
end
end
Runtime:addEventListener("enterFrame",follow);
@diogenesbecker
Copy link
Copy Markdown

Cool code. For a beginner in Corona SDK, don´t forget to put the line:
local bezier = require('bezier')
before the local curve variable.
and put the bezier.lua file in the same folder of your project. You can download at:
https://github.com/neostar20/Bezier-Curve-for-Corona-SDK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment