Skip to content

Instantly share code, notes, and snippets.

@jonbro
Created May 15, 2012 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbro/2703862 to your computer and use it in GitHub Desktop.
Save jonbro/2703862 to your computer and use it in GitHub Desktop.
import processing.opengl.*;
PImage bg = new PImage();
PImage car = new PImage();
PImage[] grid = new PImage[4096];
PVector[] vgrid = new PVector[4096];
color[] cgrid = new color[4096];
float[] dgrid = new float[4096];
boolean DEBUG;
int x_distance;
int y_distance;
void setup(){
DEBUG = false;
size(576, 320, P3D);
bg = loadImage("background.jpg");
car = loadImage("car.png");
x_distance = 576/64;
y_distance = 320/64;
// set textures and vectors and colors
for(int h=0;h<64;h++){
for(int w=0;w<64;w++){
grid[w+h*64] = createImage(x_distance, y_distance, ARGB);
grid[w+h*64] = car.get(x_distance*w, y_distance*h, x_distance, y_distance);
vgrid[w+h*64] = new PVector(x_distance*w, y_distance*h);
cgrid[w+h*64] = color(0, 0, 0);
dgrid[w+h*64] = 0.0;
}
}
}
void draw(){
textureMode(IMAGE);
image(bg, 0, 0);
drawCar();
}
void mousePressed(){
PVector mouse = new PVector(mouseX, mouseY);
for(int h=0;h<63;h++){
for(int w=0;w<63;w++){
PVector direction = PVector.sub(vgrid[w+h*64], mouse);
direction.normalize();
float distance = 1.0/(PVector.dist(vgrid[w+h*64], mouse)+1.0)*10.0;
direction.mult(distance);
cgrid[w+h*64] = color(0, 255.0*distance, 0);
vgrid[w+h*64].add(direction);
}
}
}
void mouseDragged(){
PVector mouse = new PVector(mouseX, mouseY);
PVector pmouse = new PVector(pmouseX, pmouseY);
for(int h=0;h<63;h++){
for(int w=0;w<63;w++){
PVector direction = PVector.sub(mouse, pmouse);
//direction.normalize();
float distance = 1.0/(max(PVector.dist(vgrid[w+h*64], mouse), 0)+1.0)*10.0;
dgrid[w+h*64] = distance = max(dgrid[w+h*64], distance);
direction.mult(distance);
cgrid[w+h*64] = color(0, 255.0*distance, 0);
vgrid[w+h*64].add(direction);
}
}
}
void mouseReleased(){
for(int h=0;h<63;h++){
for(int w=0;w<63;w++){
dgrid[w+h*64] = 0.0;
}
}
}
void drawCar(){
for(int h=0;h<63;h++){
for(int w=0;w<63;w++){
stroke(#FF00FF);
noStroke();
beginShape();
texture(grid[h*64+w]);
vertex(vgrid[w+h*64].x, vgrid[w+h*64].y, 0, 0);
vertex(vgrid[(w+1)+h*64].x, vgrid[(w+1)+h*64].y, x_distance, 0);
vertex(vgrid[(w+1)+(h+1)*64].x, vgrid[(w+1)+(h+1)*64].y, x_distance, y_distance);
vertex(vgrid[w+(h+1)*64].x, vgrid[w+(h+1)*64].y, 0, y_distance);
endShape();
if(DEBUG){
stroke(cgrid[h*64+w]);
point(vgrid[w+h*64].x, vgrid[w+h*64].y);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment