Advertisement
nucular

[Löve2D] trippy stuff

Jul 23rd, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. -- left click: move towards
  2. -- right click: set center
  3. -- wheel: rotation speed
  4.  
  5. -- play around with these values:
  6. local pennum = 800 -- higher == more awesomeness but more lag
  7. local mousegrav = 0.04 -- higher == higher attraction towards mouse
  8. local rotation = 2 -- rotation speed
  9. local fadeout = 0.2 -- higher == less after-image
  10. local wobbly = 0 -- higher == more random target
  11.  
  12.  
  13. local time = 0
  14. local targetx = 401
  15. local targety = 301
  16.  
  17. function hsl2rgb(h, s, l)
  18.     local r, g, b = 0, 0, 0
  19.  
  20.     if s == 0 then
  21.         r = l
  22.         g = l
  23.         b = l
  24.     else
  25.         local function hue2rgb(p, q, t)
  26.             if t < 0 then t = t + 1 end
  27.             if t > 1 then t = t - 1 end
  28.             if t < 1/6 then return p + (q - p) * 6 * t end
  29.             if t < 1/2 then return q end
  30.             if t < 2/3 then return p + (q - p) * (2/3 - t) * 6 end
  31.             return p
  32.         end
  33.  
  34.         local q = (l < 0.5) and (l * (1 + s)) or (l + s - l * s)
  35.         local p = 2 * l - q
  36.         r = hue2rgb(p, q, h + 1/3)
  37.         g = hue2rgb(p, q, h)
  38.         b = hue2rgb(p, q, h - 1/3)
  39.     end
  40.  
  41.     return math.floor(r * 255), math.floor(g * 255), math.floor(b * 255)
  42. end
  43.  
  44. function love.load()
  45.     -- love.graphics.setLineStyle("rough") -- ugly
  46.     canvas = love.graphics.newCanvas()
  47.  
  48.     pens = {}
  49.     for i = 1, pennum do
  50.         pens[i] = {
  51.             x = 400,
  52.             y = 300,
  53.             lx = 400,
  54.             ly = 300
  55.         }
  56.     end
  57. end
  58.  
  59. function love.draw()
  60.     love.graphics.setCanvas(canvas)
  61.     love.graphics.setColor(0, 0, 0, 255 * fadeout)
  62.     love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
  63.  
  64.     for i, v in ipairs(pens) do
  65.         love.graphics.setColor(hsl2rgb(i / pennum, 1, math.min(time / 2, 0.5)))
  66.         love.graphics.line(v.lx, v.ly, v.x, v.y)
  67.     end
  68.  
  69.     love.graphics.setCanvas()
  70.     love.graphics.setColor(255, 255, 255)
  71.     love.graphics.draw(canvas, 0, 0)
  72.  
  73.     love.graphics.print("FPS: " .. love.timer.getFPS())
  74. end
  75.  
  76. function love.update(dt)
  77.     if wobbly ~= 0 then
  78.         targetx = (targetx + (math.random() * 800) * wobbly) / (1 + wobbly)
  79.         targety = (targety + (math.random() * 600) * wobbly) / (1 + wobbly)
  80.     end
  81.  
  82.     if love.mouse.isDown("r") then
  83.         targetx, targety = love.mouse.getPosition()
  84.     end
  85.  
  86.     for i, v in ipairs(pens) do
  87.         v.lx = v.x
  88.         v.ly = v.y
  89.         local x, y = v.x, v.y
  90.         local r = math.cos(time * (i / pennum)) * rotation * dt * ((targetx / 800) + (targety / 600))
  91.  
  92.         local nx = targetx + (x - targetx) * math.cos(r) - (y - targety) * math.sin(r)
  93.         local ny = targety + (x - targetx) * math.sin(r) + (y - targety) * math.cos(r)
  94.         x = (x + nx) / 2
  95.         y = (y + ny) / 2
  96.  
  97.         if love.mouse.isDown("l") then
  98.             local mx, my = love.mouse.getPosition()
  99.             --local nx = mx + (x - mx) * math.cos(r) - (y - my) * math.sin(r)
  100.             --local ny = my + (x - mx) * math.sin(r) + (y - my) * math.cos(r)
  101.             x = (x + mx * mousegrav) / (1 + mousegrav)
  102.             y = (y + my * mousegrav) / (1 + mousegrav)
  103.         end
  104.  
  105.         v.x, v.y = x, y
  106.     end
  107.  
  108.     time = time + dt
  109. end
  110.  
  111. function love.mousepressed(x, y, b)
  112.     if b == "wd" then
  113.         rotation = rotation - 1
  114.     elseif b == "wu" then
  115.         rotation = rotation + 1
  116.     end
  117. end
  118.  
  119. function love.keypressed(k)
  120.     if k == "escape" then
  121.         love.event.push("quit")
  122.     end
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement