Hello all, I just wanted to share the particle experiment I wrote last evening. The code is really short and simple (~50 lines) but you get some pretty interesting liquid like results with very little effort. Just touch the screen and watch the colors flow. If anybody is interested in what’s going on, i’d be happy to explain.
Would love to hear your thoughts and suggestions!
-- flowfield
function setup()
backingMode(RETAINED) -- retain screen pixels
background(0, 0, 0, 255) -- draw black background
col = color(math.random(255), math.random(255), math.random(255)) -- set random line color
parameter("swirls", .002, .02, .01) -- declare interactive swirlyness parameter
parameter("lineStrength", 1, 10, 2) -- declare interactive line alpha parameter
parameter("fadeScreen", 0, .2, 0) -- declare interactive screen fade parameter
watch("particles") -- watch particle count
touches = {} -- make container to track screen touches
parts = {} -- make container for particles
seed = math.random(1000) -- randomise noise
end
function touched(touch) -- track all screen touches
if touch.state == ENDED then
touches[touch.id] = nil
col = color(math.random(255), math.random(255), math.random(255), lineStrength*12.5)
else touches[touch.id] = touch
end
end
function draw()
fill(0, 0, 0, fadeScreen*100) -- set fill alpha acording to fade parameter
rect(0,0, WIDTH, HEIGHT) -- draw transparent rectangle to slowly fade screen
particles = #parts -- track nr of particles
col.alpha = lineStrength*12.5 -- set line alpha to transparancy parameter
fill(col)
for i, p in pairs(parts) do -- loop through particles
pixel = noise((seed+p.x)*swirls, (seed+p.y)*swirls) -- get location of noisemap pixel
brightness = (pixel+1) / 2 * p.mult -- define brightness between 0-1
speed = brightness * p.mult -- get particle speed value
size = 2 + speed/2 * p.mult -- get particle size value
angle = brightness * 360 * math.pi / 180 -- get angle based on pixel brightness
p.x = p.x + math.cos(angle) * speed -- move particle over X
p.y = p.y + math.sin(angle) * speed -- move particle over Y
ellipse(p.x, p.y, size) -- draw particle
if p.x < 0 or p.x > WIDTH or p.y < 0 or p.y > HEIGHT then
table.remove(parts, i) -- remove particle if outside of screen
end
end
for i, t in pairs(touches) do -- add particles at touch position
if t.state == MOVING or t.state == BEGAN then
table.insert( parts, {x=t.x, y=t.y, mult=(1.5 + math.random() * 1.5)} )
end
end
end