Skip to content

Instantly share code, notes, and snippets.

@whichlight
Created January 4, 2012 06:51
Show Gist options
  • Save whichlight/1558861 to your computer and use it in GitHub Desktop.
Save whichlight/1558861 to your computer and use it in GitHub Desktop.
Basic processing sketch to simulate a 3D environment, simple interactivity with mouse
//grid of arrows directed by mouse
//Kawandeep Virdee
//interesting things happen when you change the direction of the
// vector that is the difference between the mouse and the point.
PVector[][] grid;
int width=400;
int height=500;
int resolution = 20;
int rows = height/resolution;
int cols = width/resolution;
void setup() {
colorMode(HSB, 255);
size(width,height);
smooth();
grid = new PVector[cols][rows];
for (int i = 0; i < cols; i ++ ) {
for (int j = 0; j < rows; j ++ ) {
// Initialize each object
grid[i][j] = new PVector(i*(resolution),j*(resolution));
}
}
}
void draw() {
background(255);
PVector mouseLoc = new PVector(mouseX,mouseY);
for (int i = 0; i < cols; i ++ ) {
for (int j = 0; j < rows; j ++ ) {
// Oscillate and display each object
// make it grid - mouse to see like 3D
//make it mouse - grid to have all vectors point to mouse
PVector v = PVector.sub(grid[i][j], mouseLoc);
color arrowColor = color((random(255)), 255, 0, 100);
drawVector(v,grid[i][j],0.1f, arrowColor);
}
}
}
void drawVector(PVector v, PVector loc, float scayl, color arrowcolor) {
pushMatrix();
float arrowsize = 1;
translate(loc.x,loc.y);
stroke(arrowcolor);
strokeWeight(2);
rotate(v.heading2D());
float len = v.mag()*scayl;
line(0,0,len,0);
line(len,0,len-arrowsize,+arrowsize/2);
line(len,0,len-arrowsize,-arrowsize/2);
popMatrix();
}
void mousePressed(){
saveFrame();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment