Skip to content

Instantly share code, notes, and snippets.

@diogenesbecker
Created January 26, 2015 17:03
Show Gist options
  • Select an option

  • Save diogenesbecker/9b80e56d2a13d4fb97b7 to your computer and use it in GitHub Desktop.

Select an option

Save diogenesbecker/9b80e56d2a13d4fb97b7 to your computer and use it in GitHub Desktop.
Corona SDK - bezier curve exemple
-------------------------------------------------
-- bezier.lua
-- Version: 1.0
-- Author: Rajendra Pondel
-- Email: neostar20@gmail.com
-- Copyright: 2012 Rajendra Pondel <neostar20@gmail.com>
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in the
-- Software without restriction, including without limitation the rights to use, copy,
-- modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
-- and to permit persons to whom the Software is furnished to do so, subject to the
-- following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies
-- or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
local bezier = {}
function bezier:curve(xv, yv)
local reductor = {__index = function(self, ind)
return setmetatable({tree = self, level = ind}, {__index = function(curves, ind)
return function(t)
local x1, y1 = curves.tree[curves.level-1][ind](t)
local x2, y2 = curves.tree[curves.level-1][ind+1](t)
return x1 + (x2 - x1) * t, y1 + (y2 - y1) * t
end
end})
end
}
local points = {}
for i = 1, #xv do
points[i] = function(t) return xv[i], yv[i] end
end
return setmetatable({points}, reductor)[#points][1]
end
return bezier
local bg = display.newImage("bg_earth.jpg")
bg.x = 964/2
bg.y = 507
local ship = display.newImage("ship03.png")
ship.x = 500
ship.y = 200
ship.name = "ship"
-- understood proprierties of bezier curve
--local curve = bezier:curve({xInitial, xControlPoint1, xControlPoint2, xControlPoint3}, {yInitial, yControlPoint1, yControlPoint2, yControlPoint3})
local bezier = require('bezier')
local curve = bezier:curve({350, 1100, 350},{ 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 )
-- imput 0 (zero) value in line below for don´t see the line
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 ship along curve
local pointInc = 0.00
local function follow (event )
if pointInc < 1.01 then
ship.x, ship.y = curve(pointInc)
--print(pointInc,ship.x, ship.y)
pointInc = pointInc + 0.01
else
Runtime:removeEventListener("enterFrame",follow)
end
end
Runtime:addEventListener("enterFrame",follow);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment