Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local part = class()
- function part:init(x, y, l)
- --coords
- self.c = vec2(x, y)
- --original coords
- self.oc = vec2(x, y)
- --velocity
- self.v = vec2(0,0)
- self.l = l
- end
- local s, f, loss, el = 1, 0.99, 1.1, 0.01
- function part:draw(pull, isMove)
- ---[[
- if isMove < ENDED then
- local d = self.c:dist(pull) * s
- self.v.x = (self.v.x + (pull.x - self.c.x) / (d^loss)) * f
- self.v.y = (self.v.y + (pull.y - self.c.y) / (d^loss)) * f
- end
- --]]
- d = math.max(1, self.c:dist(self.oc)) * s
- ---[[
- self.v.x = (self.v.x + (self.oc.x - self.c.x) / (d^loss)) * f
- self.v.y = (self.v.y + (self.oc.y - self.c.y) / (d^loss)) * f
- --]]
- self.c = self.c + self.v
- --point(self.c.x, self.c.y)
- end
- function part:pull(pull, str)
- local ov = self.v
- str = str or 1
- local d = math.max(1, self.l / math.abs(self.l - self.c:dist(pull)))
- local dir = (self.l - self.c:dist(pull)) > 0 and -1 or 1
- --print(d)
- ---[[
- self.v.x = (self.v.x + (pull.x - self.c.x) / ((d)^loss)*str*dir)
- self.v.y = (self.v.y + (pull.y - self.c.y) / ((d)^loss)*str*dir)
- --]]
- self.c = self.c + (self.v - ov)
- --point(self.c.x, self.c.y)
- end
- local net = {}
- function makeNet(size)
- for y = 1, size do
- net[y] = {}
- for x = 1, size do
- net[y][x] = part(x * HEIGHT / size, y * HEIGHT / size, HEIGHT / size)
- end
- end
- end
- makeNet(10)
- local nn = {}
- local function pullToPart(py, px, x, nn, cx, cy)
- if nn[py-cy] and nn[py-cy][px-cx] then
- x:pull(vec2(nn[py-cy][px-cx].c.x, nn[py-cy][px-cx].c.y), el)
- end
- end
- function draw()
- background(255, 255, 255, 255)
- -- This sets the line thickness
- strokeWidth(5)
- stroke(0)
- --[[
- for i, v in pairs(net) do
- nn[i] = v
- end
- --]]
- nn = net
- local ct = vec2(CurrentTouch.x, CurrentTouch.y)
- for py, y in ipairs(net) do
- for px, x in ipairs(y) do
- x:draw(ct, CurrentTouch.state)
- ---[[
- pullToPart(py, px, x, nn, -1, 0)
- pullToPart(py, px, x, nn, 1, 0)
- pullToPart(py, px, x, nn, 0, -1)
- pullToPart(py, px, x, nn, 0, 1)
- --]]
- ---[[
- if px > 1 then
- line(x.c.x, x.c.y, net[py][px-1].c.x, net[py][px-1].c.y)
- end
- if py > 1 then
- line(x.c.x, x.c.y, net[py-1][px].c.x, net[py-1][px].c.y)
- end
- ---[[
- --]]
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment