Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. local points = {}
  2. local trails = {}
  3.  
  4. local N = 100
  5. local w,h = love.graphics.getDimensions()
  6. print(w,h)
  7.  
  8. local function color(x,y)
  9. local th = (x+y)/20
  10. return {(math.cos(th)+1)/2, (math.sin(th)+1)/2, (math.cos(th+math.pi/2+1))/2}
  11. end
  12.  
  13. local point_mt
  14.  
  15. local function newpoint(x,y)
  16. local t = {x=x,y=y}
  17. setmetatable(t, point_mt)
  18. return t
  19. end
  20. point_mt = {
  21. __add = function(a,b)
  22. return newpoint(a.x+b.x, a.y+b.y)
  23. end,
  24.  
  25. __sub = function(a,b)
  26. return newpoint(a.x-b[1], a.y-b.y)
  27. end,
  28.  
  29. __mul = function(p,k)
  30. return newpoint(p.x*k, p.y*k)
  31. end,
  32.  
  33. dist2 = function(a,b)
  34. return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)
  35. end,
  36.  
  37. dist = function(a,b)
  38. return math.sqrt(a:dist2(b))
  39. end,
  40.  
  41. abs = function(a)
  42. return math.sqrt(a.x*a.x+a.y*a.y)
  43. end
  44. }; point_mt.__index = point_mt
  45.  
  46. function love.load()
  47. for i=1,N do
  48. points[i] = {
  49. pos = newpoint(love.math.random(w), love.math.random(h)),
  50. vel = newpoint(0,0)
  51. }
  52. end
  53. end
  54.  
  55. local mindist = 10
  56.  
  57. time = 0
  58. maxtime = 0.5
  59.  
  60. function love.update(dt)
  61. time = time + dt
  62.  
  63. local mx, my = love.mouse.getPosition()
  64. for _,p in ipairs(points) do
  65. if p.pos:dist(newpoint(mx,my)) < mindist then
  66. p.vel = newpoint(p.pos.x-mx, p.pos.y-my)
  67. else
  68. p.vel = p.vel*0.99
  69. end
  70. for _,p2 in ipairs(points) do
  71. if p2 ~= p then
  72. if p.pos:dist(newpoint(p2.pos.x,p2.pos.y)) < 5 then
  73. p.vel = newpoint(p.pos.x-p2.pos.x, p.pos.y-p2.pos.y)
  74. p2.vel = p.vel*-1
  75. end
  76. end
  77. end
  78.  
  79. table.insert(trails, {birth = time, x = p.pos.x, y = p.pos.y})
  80.  
  81. p.pos = p.pos+p.vel+newpoint(math.sin(p.vel.x)*p.vel:abs(), math.sin(p.vel.y)*p.vel:abs())
  82. p.pos.x = p.pos.x%w
  83. p.pos.y = p.pos.y%h
  84. end
  85.  
  86. local remove = {}
  87. for i,t in ipairs(trails) do
  88. if time - t.birth >= maxtime then
  89. table.insert(remove, i)
  90. end
  91. end
  92. for k,v in ipairs(remove) do table.remove(trails, v) end
  93. end
  94.  
  95. function love.draw()
  96. for i,t in ipairs(trails) do
  97. local c = color(t.x, t.y)
  98. c[4] = 1-(time-t.birth)/maxtime
  99. love.graphics.setColor(c)
  100. love.graphics.circle("fill", t.x, t.y, 2)
  101. end
  102.  
  103. for _,p in ipairs(points) do
  104. love.graphics.setColor(color(p.pos.x, p.pos.y))
  105. love.graphics.circle("fill", p.pos.x, p.pos.y, 3)
  106. end
  107.  
  108. love.graphics.circle("line", love.mouse.getX(), love.mouse.getY(), mindist)
  109. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement