Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//from youtube video https://www.youtube.com/watch?v=le6t-kpXFZ4
float inc = 0.8;
float scl = 5;
int xcols, yrows;
Particle[] parts = new Particle[3000];
PVector[] flowField;
PVector prePos = new PVector(random(width), random(height));
int h = 1;
void setup()
{
size(1000, 1000);
xcols = int(width/scl);
yrows = int(height/scl);
colorMode(HSB);
background(0);
for(int i=0; i<parts.length;i++){
parts[i] = new Particle();
}
flowField = new PVector[xcols * yrows];
System.out.print("xcols="+xcols);
System.out.print("yrows="+yrows);
background(255);
}
void draw()
{
background(255);
float yoff = 0;
for (int y = 0; y < yrows;y++){
float xoff = 0;
for (int x = 0; x < xcols; x++){
int index = x + y*xcols;
float angle = noise(xoff, yoff)*TWO_PI;
PVector vector = new PVector(0,0).fromAngle(angle).setMag(0.1);
flowField[index] = vector;
xoff += inc;
// stroke(1);
// pushMatrix();
// translate (x*scl, y*scl);
// rotate(vector.heading());
// line(0,0,scl,0);
// popMatrix();
// fill(random(255));
// rect(x*scl, y*scl, scl, scl);
}
yoff += inc;
}
for(int i = 0; i<parts.length; i++){
parts[i].follow(flowField);
parts[i].update();
parts[i].show();
parts[i].edges();
}
}
class Particle {
PVector position = new PVector(random(width), random(height));
PVector velocity = new PVector(0,0);
PVector acc = new PVector(0,0);
PVector prePos;
// prePos = new PVector(position.x,position.y);
int maxSpeed = 4;
void update(){
prePos = new PVector(position.x, position.y);
position.add(velocity);
velocity.add(acc);
velocity.limit(maxSpeed);
acc.mult(0);
}
void follow(PVector[] v){
int x = floor(position.x /scl);
int y = floor(position.y /scl);
int index = x + y*xcols;
PVector force = v[index];
applyForce(force);
}
void applyForce(PVector force){
acc.add(force);
}
void show(){
stroke(h, 120, 120, 56);
h = h+1;
if(h >255){
h = 0;
}
strokeWeight(2);
fill(random(255));
line(position.x,position.y, prePos.x, prePos.y);
point(position.x, position.y);
updatePrev();
}
void updatePrev() {
prePos.x = position.x;
prePos.y = position.y;
}
void edges(){
if(position.x > width-0.1) position.x = 0.1;
if(position.x < 0.1) position.x = width-0.1;
if(position.y > height-0.1) position.y = 0.1;
if(position.y < 0.1) position.y = height-0.1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.