Skip to content

Commit

Permalink
Added bezier
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Ritzl committed Aug 19, 2017
1 parent c237826 commit 81450bd
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 0 deletions.
217 changes: 217 additions & 0 deletions examples/bezier/bezier.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
name: "default"
scale_along_z: 0
embedded_instances {
id: "go"
data: "components {\n"
" id: \"bezier\"\n"
" component: \"/examples/bezier/bezier.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/assets/examples.atlas\\\"\\n"
"default_animation: \\\"blue_circle\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
embedded_instances {
id: "cp1"
data: "embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/assets/examples.atlas\\\"\\n"
"default_animation: \\\"green_circle\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 102.845
y: 77.718
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
embedded_instances {
id: "cp2"
data: "embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/assets/examples.atlas\\\"\\n"
"default_animation: \\\"green_circle\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 303.463
y: 201.442
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
embedded_instances {
id: "cp3"
data: "embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/assets/examples.atlas\\\"\\n"
"default_animation: \\\"green_circle\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 349.744
y: 466.354
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
embedded_instances {
id: "cp4"
data: "embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/assets/examples.atlas\\\"\\n"
"default_animation: \\\"green_circle\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 797.818
y: 283.467
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
47 changes: 47 additions & 0 deletions examples/bezier/bezier.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local bezier = require "ludobits.m.bezier"


local function animate(self)
local p = table.remove(self.points, 1)
if not p then
return
end
go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, p, go.EASING_LINEAR, 0.05, 0, animate)
end

function init(self)
local cps = {
go.get_position("cp1"),
go.get_position("cp2"),
go.get_position("cp3"),
go.get_position("cp4"),
}
self.points = bezier.create(cps, 20)
go.set_position(cps[1])
animate(self)
end

function final(self)
-- Add finalization code here
-- Remove this function if not needed
end

function update(self, dt)
-- Add update code here
-- Remove this function if not needed
end

function on_message(self, message_id, message, sender)
-- Add message-handling code here
-- Remove this function if not needed
end

function on_input(self, action_id, action)
-- Add input-handling code here
-- Remove this function if not needed
end

function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end
43 changes: 43 additions & 0 deletions ludobits/m/bezier.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local M = {}


local function point_on_cubic_bezier(cp, t)
local ax, bx, cx
local ay, by, cy
local tSquared, tCubed

-- /* calculation of the polinomial coeficients */

cx = 3.0 * (cp[2].x - cp[1].x)
bx = 3.0 * (cp[1].x - cp[2].x) - cx
ax = cp[4].x - cp[1].x - cx - bx

cy = 3.0 * (cp[2].y - cp[1].y)
by = 3.0 * (cp[3].y - cp[2].y) - cy
ay = cp[4].y - cp[1].y - cy - by

-- /* calculate the curve point at parameter value t */

tSquared = t * t
tCubed = tSquared * t

local result = vmath.vector3()
result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[1].x
result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[1].y
return result
end


function M.create(cp, points)
local curve = {}
local dt = 1.0 / (points - 1)
local i

for i = 1, points do
curve[i] = point_on_cubic_bezier(cp, i * dt)
i = i + 1
end
return curve
end

return M

0 comments on commit 81450bd

Please sign in to comment.