FeynmanTech

Codea Net Sim

Mar 4th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.68 KB | None | 0 0
  1. local part = class()
  2.  
  3. function part:init(x, y, l)
  4.     --coords
  5.     self.c = vec2(x, y)
  6.    
  7.     --original coords
  8.     self.oc = vec2(x, y)
  9.    
  10.     --velocity
  11.     self.v = vec2(0,0)
  12.    
  13.     self.l = l
  14. end
  15.  
  16. local s, f, loss, el = 1, 0.99, 1.1, 0.01
  17.  
  18. function part:draw(pull, isMove)
  19.    
  20.     ---[[
  21.     if isMove < ENDED then
  22.         local d = self.c:dist(pull) * s
  23.         self.v.x = (self.v.x + (pull.x - self.c.x) / (d^loss)) * f
  24.         self.v.y = (self.v.y + (pull.y - self.c.y) / (d^loss)) * f
  25.     end
  26.     --]]
  27.    
  28.     d = math.max(1, self.c:dist(self.oc)) * s
  29.    
  30.     ---[[
  31.     self.v.x = (self.v.x + (self.oc.x - self.c.x) / (d^loss)) * f
  32.     self.v.y = (self.v.y + (self.oc.y - self.c.y) / (d^loss)) * f
  33.    
  34.     --]]
  35.    
  36.     self.c = self.c + self.v
  37.    
  38.     --point(self.c.x, self.c.y)
  39. end
  40.  
  41. function part:pull(pull, str)
  42.    
  43.     local ov = self.v
  44.    
  45.     str = str or 1
  46.    
  47.     local d = math.max(1, self.l / math.abs(self.l - self.c:dist(pull)))
  48.    
  49.     local dir = (self.l - self.c:dist(pull)) > 0 and -1 or 1
  50.    
  51.     --print(d)
  52.    
  53.     ---[[
  54.     self.v.x = (self.v.x + (pull.x - self.c.x) / ((d)^loss)*str*dir)
  55.     self.v.y = (self.v.y + (pull.y - self.c.y) / ((d)^loss)*str*dir)
  56.     --]]
  57.    
  58.     self.c = self.c + (self.v - ov)
  59.    
  60.     --point(self.c.x, self.c.y)
  61. end
  62.  
  63. local net = {}
  64.  
  65. function makeNet(size)
  66.     for y = 1, size do
  67.         net[y] = {}
  68.         for x = 1, size do
  69.             net[y][x] = part(x * HEIGHT / size, y * HEIGHT / size, HEIGHT / size)
  70.         end
  71.     end
  72. end
  73.  
  74. makeNet(10)
  75.  
  76. local nn = {}
  77.  
  78. local function pullToPart(py, px, x, nn, cx, cy)
  79.     if nn[py-cy] and nn[py-cy][px-cx] then
  80.         x:pull(vec2(nn[py-cy][px-cx].c.x, nn[py-cy][px-cx].c.y), el)
  81.     end
  82. end
  83.  
  84. function draw()
  85.    
  86.     background(255, 255, 255, 255)
  87.  
  88.     -- This sets the line thickness
  89.     strokeWidth(5)
  90.     stroke(0)
  91.     --[[
  92.     for i, v in pairs(net) do
  93.         nn[i] = v
  94.     end
  95.     --]]
  96.     nn = net
  97.     local ct = vec2(CurrentTouch.x, CurrentTouch.y)
  98.     for py, y in ipairs(net) do
  99.         for px, x in ipairs(y) do
  100.             x:draw(ct, CurrentTouch.state)
  101.             ---[[
  102.             pullToPart(py, px, x, nn, -1, 0)
  103.             pullToPart(py, px, x, nn, 1, 0)
  104.             pullToPart(py, px, x, nn, 0, -1)
  105.             pullToPart(py, px, x, nn, 0, 1)
  106.             --]]
  107.             ---[[
  108.             if px > 1 then
  109.                 line(x.c.x, x.c.y, net[py][px-1].c.x, net[py][px-1].c.y)
  110.             end
  111.             if py > 1 then
  112.                 line(x.c.x, x.c.y, net[py-1][px].c.x, net[py-1][px].c.y)
  113.             end
  114.             ---[[
  115.            
  116.             --]]
  117.         end
  118.     end
  119. end
Advertisement
Add Comment
Please, Sign In to add comment