Created
February 11, 2017 21:12
-
-
Save daneden/910b7cea8cdfdf030df60410c2ae88a8 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
ArrayList<Particle> particles = new ArrayList<Particle>(); | |
ArrayList<Particle> attractors = new ArrayList<Particle>(); | |
int noOfParticles = 4000; | |
int noOfAttractors = 2000; | |
float fg = 10; | |
float bg = 255 - fg; | |
void settings() { | |
size(800, 800); | |
pixelDensity(displayDensity()); | |
} | |
void setup() { | |
for(int i = 0; i < noOfParticles; i++) { | |
float a = (TWO_PI/noOfParticles)*i; | |
float x = (cos(a) * width/2) + width/2; | |
float y = (sin(a) * width/2) + width/2; | |
//float x = random(width); | |
//float y = random(height); | |
particles.add(new Particle(x, y)); | |
} | |
for(int i = 0; i < noOfAttractors; i++) { | |
attractors.add(new Particle(random(width), random(height), true)); | |
//attractors.add(new Particle(width/2, height/2, true)); | |
} | |
background(bg); | |
} | |
void draw() { | |
//background(bg); | |
for(int i = 0; i < particles.size(); i++) { | |
Particle p = particles.get(i); | |
p.show(); | |
for(int j = 0; j < attractors.size(); j++) { | |
p.attracted(attractors.get(j).pos); | |
} | |
p.update(); | |
} | |
} | |
void mousePressed() { | |
String name = "export-" + random(1000) + ".png"; | |
saveFrame(name); | |
} |
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
class Particle { | |
PVector pos; | |
PVector prev; | |
PVector vel = new PVector(); | |
PVector acc = new PVector(); | |
boolean isAttractor = false; | |
Particle(float nx, float ny) { | |
this.pos = new PVector(nx, ny); | |
this.prev = new PVector(nx, ny); | |
} | |
Particle(float nx, float ny, boolean attracts) { | |
this.pos = new PVector(nx, ny); | |
this.prev = new PVector(nx, ny); | |
this.isAttractor = attracts; | |
} | |
void update() { | |
this.vel.add(this.acc); | |
this.vel.limit(7); | |
this.pos.add(this.vel); | |
this.acc.mult(0); | |
} | |
void show() { | |
if(!this.isAttractor) { | |
strokeWeight(1); | |
stroke(fg, 20); | |
line(this.pos.x, this.pos.y, this.prev.x, this.prev.y); | |
} else { | |
strokeWeight(4); | |
stroke(255, 0, 0); | |
point(this.pos.x, this.pos.y); | |
} | |
this.prev.x = this.pos.x; | |
this.prev.y = this.pos.y; | |
} | |
void attracted(PVector target) { | |
PVector force = PVector.sub(target, this.pos); | |
float d = force.mag(); | |
d = constrain(d, 1, 25); | |
float G = 50; | |
float strength = G / (d * d); | |
force.setMag(strength); | |
if(d < 20) { | |
force.mult(-10); | |
} | |
this.acc.add(force); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment