Skip to content

Instantly share code, notes, and snippets.

@johnniehard
Created October 19, 2014 09:55
Show Gist options
  • Save johnniehard/286199e1fe4a74cb9b5f to your computer and use it in GitHub Desktop.
Save johnniehard/286199e1fe4a74cb9b5f to your computer and use it in GitHub Desktop.
Moving points that slow down on darker areas of an image.
PImage src;
Mover[] movers = new Mover[10000];
void setup(){
src = loadImage("YOUR_IMAGE.JPG");
src.resize(src.width/4, src.height/4);
size(src.width, src.height);
for(int i = 0; i < movers.length; i++){
movers[i] = new Mover(src, new PVector(random(width), random(height)));
}
background(255);
}
void draw(){
for(int i = 0; i < movers.length; i++){
movers[i].run();
}
}
class Mover {
PImage src;
PVector acceleration;
PVector velocity;
PVector location;
color c;
Mover(PImage src_, PVector loc_) {
src = src_;
acceleration = new PVector();
velocity = new PVector(int(1)*(random(-1,1)), int(1)*(random(-1, 1)));
location = loc_;
}
void update() {
float b;
c = color(0, 5);
b = map(brightness(readColor()), 0, 255, 0.1, 2);
velocity.normalize();
velocity.mult(b);
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
noStroke();
fill(c);
pushMatrix();
translate(location.x, location.y);
ellipse(0, 0, 1, 1);
popMatrix();
}
void run() {
update();
checkEdges();
display();
}
color readColor() {
src.loadPixels();
int pixelLoc = int(int(location.y)*src.width + int(location.x));
if (pixelLoc < src.pixels.length && pixelLoc >= 0) {
int r = (int) red(src.pixels[pixelLoc]);
int g = (int) green(src.pixels[pixelLoc]);
int b = (int) blue(src.pixels[pixelLoc]);
return color(r, g, b);
}else{
return int(0);
}
}
void checkEdges(){
if(location.x > src.width || location.x < 0){
velocity.x *= -1;
}
if(location.y > src.height || location.y < 0){
velocity.y *= -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment