// PARTICLE WALKER // By Patrick J. Hebron // January 24, 2010 // USER-CONTROLLED CONSTANTS & VARIABLES int WALKER_DIMENSION = 300; // Contributes to output characteristics int PARTICLE_RADIUS = 15; // Contributes to output characteristics int VELOCITY_LIMIT = 20; // Contributes to output characteristics int JUST_COLLIDED_FRAMECOUNT = 10; int SCREEN_WIDTH = 1000; int SCREEN_HEIGHT = 1000; boolean constrainWalkerToScreen = true; boolean drawWalker = true; boolean drawCenterPath = true; boolean drawParticles = true; // MOUSE-HANDLER VARS (particle instantiation) boolean mPressed = false; int mInX = -1; int mInY = -1; int mOutX = -1; int mOutY = -1; // WALKER Walker w; void setup() { // Establish window size(SCREEN_WIDTH,SCREEN_HEIGHT); // Instantiate new walker w = new Walker(WALKER_DIMENSION); // Display key legend println("Keys:"); println(" r: Reset walker."); println(" c: Constrain walker to screen."); println(" 1: Toggle walker visibility."); println(" 2: Toggle center path visibility."); println(" 3: Toggle particle visibility."); println(" +: Increase particle radius."); println(" -: Decrease particle radius."); } void keyReleased() { // Reset walker if(key == 'r') { w = new Walker(WALKER_DIMENSION); println(">> RESET WALKER"); } // Toggle walker constaint else if(key == 'c') { constrainWalkerToScreen = !constrainWalkerToScreen; if(constrainWalkerToScreen) println(">> WALKER CONSTRAINED"); else println(">> WALKER UNCONSTRAINED"); } // Toggle walker visibility else if(key == '1') { drawWalker = !drawWalker; if(drawWalker) println(">> DRAW WALKER"); else println(">> HIDE WALKER"); } // Toggle center path visibility else if(key == '2') { drawCenterPath = !drawCenterPath; if(drawCenterPath) println(">> DRAW CENTER PATH"); else println(">> HIDE CENTER PATH"); } // Toggle particle visibility else if(key == '3') { drawParticles = !drawParticles; if(drawParticles) println(">> DRAW PARTICLES"); else println(">> HIDE PARTICLES"); } else if(key == '+') { PARTICLE_RADIUS++; if(PARTICLE_RADIUS > WALKER_DIMENSION/2) PARTICLE_RADIUS = WALKER_DIMENSION/2; // Set radius for all pre-existing particles w.setParticleRadius(PARTICLE_RADIUS); println(">> PARTICLE RADIUS: " + PARTICLE_RADIUS); } else if(key == '-') { PARTICLE_RADIUS--; if(PARTICLE_RADIUS < 1) PARTICLE_RADIUS = 1; // Set radius for all pre-existing particles w.setParticleRadius(PARTICLE_RADIUS); println(">> PARTICLE RADIUS: " + PARTICLE_RADIUS); } } void draw() { // Clear screen background(255); // Handle mouse if(mousePressed) { // Record in-point if(!mPressed) { mInX = mouseX; mInY = mouseY; mPressed = true; } // Record (potential) out-point else { mOutX = mouseX; mOutY = mouseY; } } else { // If a new particle is on deck if(mOutX != -1) { // Create new particle using mouse vector PVector v = new PVector(mOutX-mInX,mOutY-mInY); v.limit(VELOCITY_LIMIT); PVector l = new PVector(mInX,mInY); // Add new particle to walker Particle newParticle = new Particle(l,v,PARTICLE_RADIUS); w.addParticle(newParticle); // Reset mouse vars for next particle instantiation mInX = -1; mInY = -1; mOutX = -1; mOutY = -1; mPressed = false; } } // Run walker w.go(); }