Skip to content

Instantly share code, notes, and snippets.

@sixhat
Created September 17, 2019 14:05
Show Gist options
  • Save sixhat/472d3a23e184d4da0756d19553eabc27 to your computer and use it in GitHub Desktop.
Save sixhat/472d3a23e184d4da0756d19553eabc27 to your computer and use it in GitHub Desktop.
Animating with particles in processing - https://www.youtube.com/watch?v=q0DH0BVg-yw&feature=youtu.be
// Particles - Move around
// When meeting other Particles
// Reproduce or Fight
ArrayList <Borg> borgs;
void setup() {
size(600, 600);
borgs = new ArrayList();
for (int i = 0; i<150; i++) {
borgs.add(new Borg(random(width), random(height)));
}
}
void checkCollisions() {
ArrayList <Borg> toDie = new ArrayList();
ArrayList <Borg> toCreate = new ArrayList();
for (int a = 0; a < borgs.size(); a++) {
Borg p = borgs.get(a);
for (int b = a+1; b < borgs.size(); b++) {
Borg q = borgs.get(b);
PVector pq = new PVector(q.x-p.x, q.y-p.y);
if (pq.mag()<50) {
line(p.x, p.y, q.x, q.y);
float sim = p.vx * q.vx + p.vy * q.vy;
if (sim>1) {
toCreate.add(new Borg((p.x+q.y)/2.0, (p.y+q.y)/2.0));
}
if (sim<-0.5) {
toDie.add(p);
}
}
}
}
borgs.removeAll(toDie);
borgs.addAll(toCreate);
}
void draw() {
fill(borgs.size(), 200, 200, 1);
rect(0, 0, width, height);
for (Borg b : borgs) {
b.update();
}
checkCollisions();
for (Borg b : borgs) {
b.draw();
}
println(borgs.size());
}
class Borg {
float x, y, vx, vy;
public Borg() {
}
public Borg(float x, float y) {
this.x = x;
this.y = y;
this.vx = random(-1, 1);
this.vy = random(-1, 1);
}
void update() {
this.vx += random(-0.01, 0.01);
this.vy += random(-0.01, 0.01);
this.x += this.vx;
this.y += this.vy;
this.x = (this.x + width ) % width;
this.y = (this.y + height) % height;
}
void draw() {
fill(255);
//ellipse(x, y, 10, 10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment