Created
          April 13, 2019 20:28 
        
      - 
      
- 
        Save lemon32767/f46a37c8e44c0e97c6e2ef454d5d3932 to your computer and use it in GitHub Desktop. 
    trails
  
        
  
    
      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 points = {} | |
| local trails = {} | |
| local N = 100 | |
| local w,h = love.graphics.getDimensions() | |
| print(w,h) | |
| local function color(x,y) | |
| local th = (x+y)/20 | |
| return {(math.cos(th)+1)/2, (math.sin(th)+1)/2, (math.cos(th+math.pi/2+1))/2} | |
| end | |
| local point_mt | |
| local function newpoint(x,y) | |
| local t = {x=x,y=y} | |
| setmetatable(t, point_mt) | |
| return t | |
| end | |
| point_mt = { | |
| __add = function(a,b) | |
| return newpoint(a.x+b.x, a.y+b.y) | |
| end, | |
| __sub = function(a,b) | |
| return newpoint(a.x-b[1], a.y-b.y) | |
| end, | |
| __mul = function(p,k) | |
| return newpoint(p.x*k, p.y*k) | |
| end, | |
| dist2 = function(a,b) | |
| return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) | |
| end, | |
| dist = function(a,b) | |
| return math.sqrt(a:dist2(b)) | |
| end, | |
| abs = function(a) | |
| return math.sqrt(a.x*a.x+a.y*a.y) | |
| end | |
| }; point_mt.__index = point_mt | |
| function love.load() | |
| for i=1,N do | |
| points[i] = { | |
| pos = newpoint(love.math.random(w), love.math.random(h)), | |
| vel = newpoint(0,0) | |
| } | |
| end | |
| end | |
| local mindist = 10 | |
| time = 0 | |
| maxtime = 0.5 | |
| function love.update(dt) | |
| time = time + dt | |
| local mx, my = love.mouse.getPosition() | |
| for _,p in ipairs(points) do | |
| if p.pos:dist(newpoint(mx,my)) < mindist then | |
| p.vel = newpoint(p.pos.x-mx, p.pos.y-my) | |
| else | |
| p.vel = p.vel*0.99 | |
| end | |
| for _,p2 in ipairs(points) do | |
| if p2 ~= p then | |
| if p.pos:dist(newpoint(p2.pos.x,p2.pos.y)) < 5 then | |
| p.vel = newpoint(p.pos.x-p2.pos.x, p.pos.y-p2.pos.y) | |
| p2.vel = p.vel*-1 | |
| end | |
| end | |
| end | |
| table.insert(trails, {birth = time, x = p.pos.x, y = p.pos.y}) | |
| p.pos = p.pos+p.vel+newpoint(math.sin(p.vel.x)*p.vel:abs(), math.sin(p.vel.y)*p.vel:abs()) | |
| p.pos.x = p.pos.x%w | |
| p.pos.y = p.pos.y%h | |
| end | |
| local remove = {} | |
| for i,t in ipairs(trails) do | |
| if time - t.birth >= maxtime then | |
| table.insert(remove, i) | |
| end | |
| end | |
| for k,v in ipairs(remove) do table.remove(trails, v) end | |
| end | |
| function love.draw() | |
| for i,t in ipairs(trails) do | |
| local c = color(t.x, t.y) | |
| c[4] = 1-(time-t.birth)/maxtime | |
| love.graphics.setColor(c) | |
| love.graphics.circle("fill", t.x, t.y, 2) | |
| end | |
| for _,p in ipairs(points) do | |
| love.graphics.setColor(color(p.pos.x, p.pos.y)) | |
| love.graphics.circle("fill", p.pos.x, p.pos.y, 3) | |
| end | |
| love.graphics.circle("line", love.mouse.getX(), love.mouse.getY(), mindist) | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment