Created
October 11, 2019 18:29
-
-
Save RemyPorter/18e41ad58e3d6d27d9ccad26276c36cb to your computer and use it in GitHub Desktop.
This file contains 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
float normGauss() { | |
return norm(randomGaussian(), -1, 1.); | |
} | |
class Particle { | |
PVector position; | |
PVector velocity; | |
Particle() { | |
position = new PVector(normGauss(), normGauss(), normGauss()); | |
velocity = PVector.random2D(); | |
} | |
} | |
void applyBounds(Particle p) { | |
if (p.position.x >= 1.) { | |
p.position.x = 1.; | |
p.velocity.x *= -1; | |
} else if (p.position.x < 0) { | |
p.position.x = 0; | |
p.velocity.x *= -1; | |
} | |
if (p.position.y >= 1.) { | |
p.position.y = 1.; | |
p.velocity.y *= -1; | |
} else if (p.position.y < 0) { | |
p.position.y = 0; | |
p.velocity.y *= -1; | |
} | |
} | |
void applyVelocity(Particle p) { | |
PVector scaled = PVector.div(p.velocity, width); | |
p.position.add(scaled); | |
} | |
void attraction(Particle p, PVector attr) { | |
float d = p.position.dist(attr); | |
PVector dir = PVector.sub(p.position, attr); | |
dir.mult(0.25 / (d * d)); | |
p.velocity.sub(dir); | |
} | |
void friction(Particle p) { | |
p.velocity.mult(0.99); | |
} | |
ArrayList<Particle> swarm = new ArrayList<Particle>(); | |
void setup() { | |
//size(640, 480, P3D); | |
fullScreen(P3D); | |
ellipseMode(CENTER); | |
background(0); | |
for (int i = 0; i < 1000; i++) { | |
swarm.add(new Particle()); | |
} | |
} | |
PGraphics lastFrame; | |
void drawParticle(Particle p, PGraphics frame) { | |
PVector pp = p.position; | |
float mag = p.velocity.mag(); | |
frame.fill(lerpColor(#FF00FF, #00FFFF, noise(pp.x*2, pp.y*2, frameCount * 0.01))); | |
frame.strokeWeight(mag / 6); | |
frame.ellipse(pp.x * width, pp.y * height, max(2, mag), max(2, mag)); | |
} | |
void draw() { | |
clear(); | |
if (frameCount > 600) { | |
return; | |
} | |
//translate(width/2, height/2); | |
boolean attract = false; | |
PVector attr = null; | |
PGraphics frame = createGraphics(width, height); | |
frame.beginDraw(); | |
if (lastFrame != null) { | |
frame.tint(255, 250); | |
frame.image(lastFrame, 0, 0); | |
frame.noTint(); | |
} | |
if (mousePressed) { | |
attract = true; | |
attr = new PVector(((float)mouseX)/width, ((float)mouseY)/height); | |
} | |
for (Particle p : swarm) { | |
if (attract) { | |
attraction(p, attr); | |
} | |
applyVelocity(p); | |
applyBounds(p); | |
friction(p); | |
drawParticle(p, frame); | |
} | |
frame.endDraw(); | |
image(frame, 0, 0); | |
lastFrame = frame; | |
saveFrame("frames/####.tif"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment