Skip to content

Instantly share code, notes, and snippets.

@martin-braun
Created January 19, 2020 04:21
Show Gist options
  • Save martin-braun/bb356adfe096d6598c252bd39c6920a2 to your computer and use it in GitHub Desktop.
Save martin-braun/bb356adfe096d6598c252bd39c6920a2 to your computer and use it in GitHub Desktop.
Pulsing Shapes using the LÖVE framework
local lg, lt = love.graphics, love.timer
local pi, cos, sin = math.pi, math.cos, math.sin
local centerX, centerY = 400, 300
local wave = 0
-- rotate x,y around xRef,yRef by r
function rotateAround(x, y, xRef, yRef, r)
local c, s = cos(r), sin(r)
return c * (x - xRef) - s * (y - yRef) + xRef, s * (x - xRef) + c * (y - yRef) + yRef
end
-- draw an isotopy shape
function drawShape(corners, radius, x, y, r, color)
local cornerRot = 2 * pi / corners
lg.push()
lg.translate(x, y)
lg.rotate(math.rad(-45))
lg.setColor(color)
x, y = rotateAround(0, 0 - radius, 0, 0, r)
for i=1, corners do
local nx, ny = rotateAround(x, y, 0, 0, cornerRot)
lg.line(x, y, nx, ny)
x, y = nx, ny
end
lg.pop()
end
function love.update()
wave = sin(2 * math.pi * 1.041 * lt.getTime())
end
function love.draw()
local x, y, t = centerX, centerY, lt.getTime()
drawShape(3, 15 + 5 * wave, x, y, t, { 1, 0, 0, 1 })
drawShape(6, 15 + 5 * wave, x, y, t + pi, { 1, 0, 0, 1 })
local pulse = ((wave + 1) / 2) * 30
for i=1, 30 do
drawShape(4, 50 + (i * 5) + 20 * wave ^ 2, x, y, t * ((i - 10) * 0.025), { 0, 1, 0, 0.5 + (pulse - i) * 0.01 })
end
drawShape(6, 200 + 10 * -wave ^ 3 / wave, x, y, t / 5, { 1, 1, 0, 1 })
drawShape(6, 200 + 10 * -wave ^ 3 / wave, x, y, t / 5 - pi / 2, { 1, 1, 0, 1 })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment