Last active
January 30, 2020 21:32
-
-
Save EngineerSmith/9d2e59c2021eed00428a360586bf99df to your computer and use it in GitHub Desktop.
Love2d, drawing an animated line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local lg = love.graphics | |
local floor = math.floor | |
local insert = table.insert | |
local image = lg.newImage("Line.png") --I used a 64x64 image to test | |
image:setWrap("repeat","repeat") | |
local iw, ih = image:getDimensions() | |
--Options | |
local frames = 20 -- Frames per line section | |
-- | |
local points -- Predicted points | |
local quads -- Drawn quads | |
local quadsPos -- Quad's drawn position | |
local rotations -- Quad's rotation | |
local index = frames | |
local sqrt = math.sqrt | |
local pow2 = function(n) return n*n end | |
local function magSqrt(x, y) | |
return sqrt(pow2(x) + pow2(y)) | |
end | |
local atan2 = math.atan2 | |
local pi = math.pi | |
local function rotation(x, y) | |
return atan2(y, x) - pi | |
end | |
local function calculateQuads() | |
quads = {} | |
quadsPos = {} | |
rotations = {} | |
local lineOffset = 0 | |
for i = 3, #points, 2 do | |
local dx = points[i-2] - points[i] --Vector to next point | |
local dy = points[i-1] - points[i+1] | |
insert(rotations, rotation(dx, dy)) | |
local mag = magSqrt(dx, dy) --Length of line | |
local ux = dx / mag --Normalize vector | |
local uy = dy / mag | |
insert(quadsPos, points[i-2]-uy*ih/2) --Calculating centre offset | |
insert(quadsPos, points[i-1]+ux*iw/2) | |
for q = 0, frames-1 do | |
insert(quads, lg.newQuad(lineOffset+iw*q*(iw/frames),ih,mag,ih,iw,ih)) | |
end | |
lineOffset = lineOffset + mag --Offset to contine line | |
end | |
end | |
local function CalculatePoints(height) | |
local h = floor(height/2) | |
h = h + 50 -- Just to make things less cramped | |
points = { --Example set of points, more fine they are less corners there will be | |
50, h, | |
100,h - 30, | |
150,h - 60, | |
200,h - 80, | |
250,h - 50, | |
300,h + 10, | |
350,h + 50, | |
} | |
--points = {200,h,500,h,500,h-200} --debug | |
calculateQuads() | |
end | |
function love.load() | |
local _, h = lg.getDimensions() | |
CalculatePoints(h) | |
end | |
local delta = 0 | |
function love.update(dt) | |
delta = delta + dt | |
while delta > 0.05 do | |
delta = delta - 0.05 | |
index = index - 1 | |
if index < 1 then | |
index = frames | |
end | |
end | |
end | |
function love.draw() | |
lg.print(1*2-1) | |
lg.line(points) -- Debug to show the line | |
for i = 1, #quads / frames do -- Probably a better way to do the loop | |
local qx = quadsPos[i*2-1] | |
local qy = quadsPos[i*2] | |
local frame = index+i*frames-frames | |
lg.print(tostring(qx)..";"..tostring(qy)..";"..tostring(frame),10,i*20) -- "x;y;frame" | |
lg.draw(image, quads[frame],qx,qy,rotations[i]) | |
end | |
end | |
function love.resize(_, h) CalculatePoints(h) end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment